yujun61hugh
Joined: 05 Mar 2009 Posts: 1
|
05 Mar 2009 13:35 how to calculate spectrum analysis in matlab |
|
|
|
|
I need to get the precise spectrum component of the signal I generated in Matlab Simulink. The code i use now is shown as
Fs = 2e6; % Sampling frequency
T = 1/Fs; % Sample time
L = 2000000; % Length of signal
y = output(2001:(L+2001),2)';
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);
plot(f,2*abs(Y(1:NFFT/2)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
Basicly, I simulate the system for 1.001 sec. and take the last 1s data to do FFT.
However, when I test this code using an ideal sin wave with frequency 1K Hz and amplitude equal to 1. the FFT result shows that at the 1K Hz, the signal component is only 0.75. who could tell me the reason and how to improve the code. Thanks a lot.
Added after 7 minutes:
I upload the spectrum figure here.
|
|
rramya
Joined: 14 Dec 2008 Posts: 76 Helped: 19
|
07 Mar 2009 7:58 linspace fft |
|
|
|
|
Why are you lusing this line ???
y = output(2001:(L+2001),2)'; esp output???? (where is the sinuisoidal signal instead)
also, simulate the system for 1.001 sec.??? Y not simulate exactly for 1 sec.
because,the exact number of cycles of the "x"Hz sine wave that occur in the no. of
samples i/p signal should be an integer. for getting the proper spectrum.
another point : in the code you used
f = Fs/2*linspace(0,1,NFFT/2);
just use f = Fs/2*linspace(0,1,NFFT/2+1);
plot(f,2*abs(Y(1:NFFT/2)+1)) to plot the single sided spectrum.
if U have further doubt hen vsist the folowing site:
www.mathworks.com
in the search engine type fft, see documentation details.
also visit
www.mathworks.com/support/tech-notes/1700/1702.html
www.mathworks.com/support/tech-notes/1700/1703.html
www.mathworks.com/support/tech-notes/1700/1704.html
Happy learning
|
|