Filter löschen
Filter löschen

How to Compare the lengths of vectors

1 Ansicht (letzte 30 Tage)
engineer bsc
engineer bsc am 7 Jun. 2012
HI, In the code below, i want to plot : "plot (f,AmpTab);" and i get this warning :
"Error using ==> plot Vectors must be the same lengths."
i want to compare the length of AmpTab to the length of f, but i don't want to loss any value or date of AmpTab.
i know that there are operations of "zeros" or " padarray " but i don't know how to use it correctly.
can you help me please ?
clear all;
close all;
Fs = 200
t= 0:1/Fs:1
y = 3*sin(2*pi*10*t) + 7*sin(2*pi*20*t) + 11*sin(2*pi*30*t); % input in time domain
L=length (y);
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(y,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
figure(1)
plot(f,2*abs(Y(1:NFFT/2+1))) ;
figure(2)
[B] = sort(2*abs(Y(1:NFFT/2+1))); %order the amplitudes
A1=B(end); %amplitude of the first peak
A2=B(end-1); %amplitude of second peak
AmpTab=[A1 A2];
plot (f,AmpTab);

Antworten (1)

Wayne King
Wayne King am 7 Jun. 2012
Not sure why you want to pad the DFT in this case, you end up not getting accurate frequency estimates by doing that.
Fs = 200;
t= 0:1/Fs:1-1/200;
y = 3*sin(2*pi*10*t) + 7*sin(2*pi*20*t) + 11*sin(2*pi*30*t);
L = length(y);
Y = fft(y)/L;
f = 0:Fs/length(y):100;
figure(1), plot(f,2*abs(Y(1:length(y)/2+1))) ; xlabel('Hz');
[B,I] = sort(2*abs(Y(1:length(y)/2+1)),'descend'); %order the amplitudes
figure(2)
stem(f(I(1:2)),B(1:2),'color',[0 0 1],'markerfacecolor',[0 0 1]);
set(gca,'xlim',[0 100]); xlabel('Hz');
  1 Kommentar
engineer bsc
engineer bsc am 7 Jun. 2012
HI Wayne King,
Thank you for you answer, my main aim is to calculate the amplitude and the frequency of the signal after the FFT.
i don't know how to do that, i saw that there is an operation "sort" so i tried to calculate the amplitude with that, that is the reason i asked about the padding and comparing the vectors length
can you guide me, how can i do this correctly please ?

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by