HELP! Error using plot Vectors must be the same length. Error in Doc (line 94) plot(t, y);

46 ビュー (過去 30 日間)
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;

採用された回答

Paul
Paul 2024 年 6 月 8 日
編集済み: Paul 2024 年 6 月 8 日
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)]
ans = 1x2
5000 5000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
[X, f, ~] = fftm(x, Fs, NFFT);
X is derived from an 8192 point DFT
NFFT
NFFT = 8192
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)
ans = 4097
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);
Error using plot
Specify the coordinates as vectors or matrices of the same size, or as a vector and a matrix that share the same length in at least one dimension.
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 件のコメント
Matheus Victor
Matheus Victor 2024 年 6 月 8 日
Hi Paul! Many thanks for the reply!! So do you think that I should use fft function instead of fftm? Because this fftm function was given by my professor, but there's no problem if I use a different function, I just need this works 😅.
So, many thanks!
Paul
Paul 2024 年 6 月 8 日
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.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFourier Analysis and Filtering についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by