필터 지우기
필터 지우기

How to Compare the lengths of vectors

조회 수: 1 (최근 30일)
engineer bsc
engineer bsc 2012년 6월 7일
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);

답변 (1개)

Wayne King
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');
  댓글 수: 1
engineer bsc
engineer bsc 2012년 6월 7일
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 ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by