HELP! Error using plot Vectors must be the same length. Error in Doc (line 94) plot(t, y);
12 views (last 30 days)
Show older comments
Hello everyone!! I'm trying to plot: x,y,X,Y. But when my matlab try to plot y, the following error appears: Error using plot Vectors must be the same length. Error in doc (lne 94) plot(t, y);
Please fell free to check the code below:
Fs = 5000;
T = 1/Fs;
t = 0:T:1-T;
NFFT = 2^nextpow2(length(t));
x = cos(2*pi*100*t) + cos(2*pi*500*t) + cos(2*pi*1000*t);
[X, f, ~] = fftm(x, Fs, NFFT);
H = double(f <= 600);
Y = X .* H;
y = ifft(Y);
figure;
subplot(2,1,1);
plot(t, x);
title('x(t)');
xlabel('Time(s)');
ylabel('Amp');
subplot(2,1,2);
plot(t, y);
title('y(t)');
xlabel('Time(s)');
ylabel('Amp');
figure;
subplot(2,1,1);
plot(f, abs(X));
title('X(\omega)');
xlabel('Freq(Hz)');
ylabel('Mag');
subplot(2,1,2);
plot(f, abs(Y));
title('Y(\omega)');
xlabel('Freq(Hz)');
ylabel('Mag');
Oh, the following function fftm in line 6, is a function downloaded by me, it's a fft function modified, and you can see it below:
% Modified fft algorithm
%
% This function estimates the fft of a signal x using the Fs sampling
% frequency, NFFT bins, and returns the frequencies values along with the spectrum X.
%
% [X,f] = fftm(x,Fs,NFFT)
%
% Example:
% Fs = 1e3;
% t = 0:0.001:1-0.001;
% x = cos(2*pi*100*t)+sin(2*pi*202.5*t);
% [X,f]=fftm(x,Fs,2000);
% plot(f,abs(X));
% http://www.mathworks.com/help/signal/ug/amplitude-estimation-and-zero-padding.html
% http://www.mathworks.com/help/signal/ug/psd-estimate-using-fft.html
function [xdft,f, psdx] = fftm(x,Fs,NFFT)
L = length(x);
if nargin < 3
NFFT = 2^nextpow2(L);
end
xdft = fft(x,NFFT);
xdft = xdft(1:length(xdft)/2+1);
psdx = (1/(Fs*L)).*abs(xdft).^2;
psdx(2:end-1) = 2*psdx(2:end-1);
psdx = 10*log10(psdx);
xdft = 1/L.*xdft;
xdft(2:end-1) = 2*xdft(2:end-1);
f = 0:Fs/NFFT:Fs/2;
0 Comments
Accepted Answer
Paul
on 8 Jun 2024
Edited: Paul
on 8 Jun 2024
Hi Matheus,
The explanation for the error is that the fftm function is doing a "one-sided" FFT, hence it returns an output with a different number of elements than in the input signal, x. Based on what it appears you're trying to do, you should reconsider using fftm, because the so-called "one-sided" FFT is probably not what should be used for this problem.
Fs = 5000;
T = 1/Fs;
t = 0:T:1-T;
NFFT = 2^nextpow2(length(t));
x = cos(2*pi*100*t) + cos(2*pi*500*t) + cos(2*pi*1000*t);
x has 5000 elements, as does t
[numel(x) numel(t)]
[X, f, ~] = fftm(x, Fs, NFFT);
X is derived from an 8192 point DFT
NFFT
But it looks like fftm is trying to do some sort of one-sided DFT (I'm not even sure it's doing it correctly),
numel(X)
Looks like this is trying to remove frequencies greater than 600. But this approach won't really work becasue half of the DFT is now missing
H = double(f <= 600);
Y = X .* H;
y = ifft(Y);
y will have as many elements as X, which is not the same number of elements as in t, hence the error below on the plot command.
figure;
subplot(2,1,1);
plot(t, x);
title('x(t)');
xlabel('Time(s)');
ylabel('Amp');
subplot(2,1,2);
plot(t, y);
title('y(t)');
xlabel('Time(s)');
ylabel('Amp');
figure;
subplot(2,1,1);
plot(f, abs(X));
title('X(\omega)');
xlabel('Freq(Hz)');
ylabel('Mag');
subplot(2,1,2);
plot(f, abs(Y));
title('Y(\omega)');
xlabel('Freq(Hz)');
ylabel('Mag');
% Modified fft algorithm
%
% This function estimates the fft of a signal x using the Fs sampling
% frequency, NFFT bins, and returns the frequencies values along with the spectrum X.
%
% [X,f] = fftm(x,Fs,NFFT)
%
% Example:
% Fs = 1e3;
% t = 0:0.001:1-0.001;
% x = cos(2*pi*100*t)+sin(2*pi*202.5*t);
% [X,f]=fftm(x,Fs,2000);
% plot(f,abs(X));
% http://www.mathworks.com/help/signal/ug/amplitude-estimation-and-zero-padding.html
% http://www.mathworks.com/help/signal/ug/psd-estimate-using-fft.html
function [xdft,f, psdx] = fftm(x,Fs,NFFT)
L = length(x);
if nargin < 3
NFFT = 2^nextpow2(L);
end
xdft = fft(x,NFFT);
xdft = xdft(1:length(xdft)/2+1);
psdx = (1/(Fs*L)).*abs(xdft).^2;
psdx(2:end-1) = 2*psdx(2:end-1);
psdx = 10*log10(psdx);
xdft = 1/L.*xdft;
xdft(2:end-1) = 2*xdft(2:end-1);
f = 0:Fs/NFFT:Fs/2;
end
2 Comments
Paul
on 8 Jun 2024
Yes, I suspect that the fft function is probably all that's needed if the goal is to compute the FFT of a signal, set to zero those elements of the FFT corresponding to frequences abs(f) > 600, and then computing the IFFT of the result (the intentional use of abs() is a hint).
If you try it and it still doesn't work, feel free to post back with the updated code showing where you're having a problem.
More Answers (0)
See Also
Categories
Find more on Spectral Analysis 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!