How to Compare the lengths of vectors
조회 수: 1 (최근 30일)
이전 댓글 표시
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
답변 (1개)
Wayne King
2012년 6월 7일
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');
참고 항목
카테고리
Help Center 및 File Exchange에서 Spectral Measurements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!