Vectors must be the same length
Afficher commentaires plus anciens
I am receiving a warning ' Vectors must be the same length' for 'plot(n,X,'LineWidth', 2)' . I have tried linspace but it didn't work. How can I solve it?
%Defining variables and limits
n = -40:40
w=[-50,50]
nlower=n(1)
nupper=n(end)
% Writing the x signal function x(n) = n(0.9)^n [u(n)-u(n-21)]
subplot(3,1,1)
x = n .*(0.9) .^ n.* (stepseq(0,nlower,nupper)-stepseq(21,nlower,nupper))
% Applying DTFT
if -50<= n <=50,
X = dtft(x,n,w);
plot(n,X,'LineWidth', 2)
grid on
grid minor
set(gca,'Fontsize', 8);
xlabel('\omega / \pi');
ylabel('|X(n)|');
Réponses (2)
Walter Roberson
le 19 Fév 2023
if -50<= n <=50,
MATLAB interprets that as
if ((-50<= n) <=50)
The first part compares every element in n to -50. You defined n as -40:40 so it is true that -50<=n for every entry in n. The result of that part of the expression is an array of logical true values the same size as n. That array of logical true values is then compared <= 50. logical true in mathematical contexts converts to 1 (false converts to 0) and for each of those true values from the first part of the expression, true <= 50 is true, so the result of the <= tests is going to be an array of true values the same size as n. Then when if is asked to test a non-scalar, the result is considered true only of all of the values being tested are non-zero. Which is the case because they are all true. So the if will pass, and might as well not have been there.
4 commentaires
stepseq is not part of MATLAB, but possibly you copied it from a source such as https://www.mathworks.com/matlabcentral/answers/819500-how-do-i-multiply-sin-and-stepseq-functions#answer_689890
And perhaps you got dtft from a source such as https://www.mathworks.com/matlabcentral/answers/469374-dtft-possible-on-matlab#answer_426731
%Defining variables and limits
n = -40:40
w=[-50,50]
nlower=n(1)
nupper=n(end)
% Writing the x signal function x(n) = n(0.9)^n [u(n)-u(n-21)]
subplot(3,1,1)
x = n .*(0.9) .^ n.* (stepseq(0,nlower,nupper)-stepseq(21,nlower,nupper))
% Applying DTFT
if -50<= n <=50,
X = dtft(x,n,w);
whos n x X
plot(n,X,'LineWidth', 2)
grid on
grid minor
set(gca,'Fontsize', 8);
xlabel('\omega / \pi');
ylabel('|X(n)|');
end
function [x,n] = stepseq(n0,n1,n2) % Copied From: <https://www.mathworks.com/matlabcentral/answers/12834-unsolved-function?s_tid=srchtitle#>
% Generates x(n) = u(n-n0); n1 <= n,n0 <= n2
% ------------------------------------------
% [x,n] = stepseq(n0,n1,n2)
%
if ((n0 < n1) | (n0 > n2) | (n1 > n2))
error('arguments must satisfy n1 <= n0 <= n2')
end
n = [n1:n2];
%x = [zeros(1,(n0-n1)), ones(1,(n2-n0+1))];
x = [(n-n0) >= 0];
end
function [X] = dtft(x,n,w)
% Computes Discrete-time Fourier Transform
% [X] = dtft(x,n,w)
% X = DTFT values computed at w frequencies
% x = finite duration sequence over n
% n = sample position vector
% w = frequency location vector
X = exp(-1i*w'*n) * x.';
% X = x*exp(-j*n'*w);
end
What went wrong? Well, you passed in only two values for the frequency location vector, so the output is going to be length 2.
The output of dtft is not the same size as the finite x inputs or the sample positions: it is the same size as the frequency vector. You should be plotting w against X, not n against X. And you will probably want more frequencies in your list.
Gizem Karslioglu
le 19 Fév 2023
Walter Roberson
le 19 Fév 2023
w = linspace(-50, 50, length(n));
However if you do this, you are likely to get confused between positions (n values) and frequencies (w values).
When you have M = length(n) input positions, you cannot justify more than M/2 frequencies as being fully resolved; any more than that you run into Nyquist limit problems. Even M/2 is probably an over-estimate.
Gizem Karslioglu
le 19 Fév 2023
Chawa Mozy
le 19 Fév 2023
0 votes
% Define variables and limits n = -40:40; w = [-50, 50]; nlower = n(1); nupper = n(end);
% Define the signal x(n) x = n .* (0.9) .^ n .* (stepseq(0, nlower, nupper) - stepseq(21, nlower, nupper));
% Compute and plot the magnitude and phase spectra using the DTFT X = dtft(x, n, w);
subplot(3, 1, 1) plot(n, x, 'LineWidth', 2) grid on grid minor set(gca, 'Fontsize', 8); xlabel('n'); ylabel('x(n)'); title('Signal x(n)');
subplot(3, 1, 2) plot(w, abs(X), 'LineWidth', 2); grid on grid minor set(gca, 'Fontsize', 8); xlabel('\omega / \pi'); ylabel('|X(\omega)|'); title('Magnitude Spectrum');
subplot(3, 1, 3) plot(w, angle(X), 'LineWidth', 2); grid on grid minor set(gca, 'Fontsize', 8); xlabel('\omega / \pi'); ylabel('\angle X(\omega)'); title('Phase Spectrum');
Catégories
En savoir plus sur Signal Generation dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

