How to Compare the lengths of vectors
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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);
0 Kommentare
Antworten (1)
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');
Siehe auch
Kategorien
Mehr zu Spectral Measurements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!