Vectors must be the same length
3 views (last 30 days)
Show older comments
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)|');
0 Comments
Answers (2)
Walter Roberson
on 19 Feb 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 Comments
Walter Roberson
on 19 Feb 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.
Chawa Mozy
on 19 Feb 2023
% 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');
0 Comments
See Also
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!