Hi - Please why I don't get the excat amplitude in the spectrum of two added sine waves xt= 0.7*sin(2*pi*50*t)+sin(2*pi*120*t) , I mean i don't get excatly 0.7 for 50 Hz - nor excatly 1 for 120 Hz
조회 수: 1 (최근 30일)
이전 댓글 표시
% generate sin wave
% FFT on N=2^nextpow2(L)
clear;
clc;
Fs=1000
dt=1/Fs
t=0:dt:1.5-dt;
xt= 0.7*sin(2*pi*50*t)+sin(2*pi*120*t);
L=length(xt)
N=2^nextpow2(L)
% xt=1.7*sin(2*pi*50*t) + 2.3*sin(2*pi*1200*t)+randn(size(xt));
subplot(2,1,1)
plot(t,xt)
grid on
grid minor
X=fft(xt,N)/L; % Always divide by L
subplot(2,1,2)
a=2*abs(X(1:N/2+1)); % Take N/2+1 points
grid on
grid minor
f=linspace(0,Fs/2,N/2+1); % make N/2+1 points from 0 to Fs/2
plot(f,a)
grid on
grid minor
ylim([0 1.2])
title('Single-Sided Amplitude Spectrum of x(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
댓글 수: 0
채택된 답변
Ameer Hamza
2018년 6월 16일
It is happening because your signal is 1500 samples whereas you are trying to calculate 2048 point fft. MATLAB pad extra zeros to the end of your signal, it will introduce extra frequency components and the FFT changes. Try replacing the following line in your code
N = L; % instead of N = 2^nextpow2(L)
댓글 수: 0
추가 답변 (1개)
dpb
2018년 6월 16일
편집: dpb
2018년 6월 16일
To amplify on Ameer's answer...
>> [p, fr]=findpeaks(a,f,'minpeakheight',0.25)
p =
0.6097 0.9503
fr =
49.8047 120.1172
>>
NB: With the frequency space of Fmax/2048 the frequency binning isn't precisely that of the input frequencies and so there's discretization/binning roundoff/noise introduced. That "drains off" energy into neighboring bins so that the all the energy isn't in the single-frequency bin as it is in the example case when the FFT is done over the L bins and the frequency bins do precisely match the signal frequencies.
To see this,
>> hAx=subplot(2,1,2);
>> hAx.YScale='log';
>> ylim([1E-5 1])
NB: the spread in the two peaks; to get closer approximation the total energy you would need to integrate over the peak regions.
Contrast this image to that of the one-sided noiseless PSD shown in the documentation example for FFT of the same signal. It would be good for the doc to illustrate this effect as well.
NB3: In the one-sided energy normalization you used
a=2*abs(X(1:N/2+1)); % Take N/2+1 points
This is slightly in error as there is only a single DC and Fmax element in the returned FFT; the above will double those, too. If there's a significant DC component in particular, this may make a noticeable distortion in the one-sided PSD.
Use the form shown in the example of
P1=P2(1:L/2+1);
P1(2:end-1)=2*P1(2:end-1);
instead to get the positive frequency components first, then scale all except DC and Fmax. The reason for the above should be documented as well, granted, instead of leaving it for the user to try to discern the magic.
ADDENDUM
I went ahead and added the L-length result to the plot and then scaled to show the lower range of the peaks more clearly...you can see how when the bins align with the clean input signal frequencies all the power is in one bin, the energy on either side is of the magnitude of the floating-point roundoff.
>> [pk,loc]=findpeaks(P1,f1,'NPeaks',2,'MinPeakHeight',0.25)
pk =
0.7000 1.0000
loc =
50 120
>>
댓글 수: 4
dpb
2018년 6월 17일
Glad to help -- a few more votes or acceptances never hurts... :)
Altho that isn't real motivation but appreciate knowing is of some value.
Q? Did you try the suggested example of using the base length FFT on the sample waveform with a slight change in frequency?
참고 항목
카테고리
Help Center 및 File Exchange에서 Parametric Spectral Estimation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!