Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

FFT implementation for harmonics

Status
Not open for further replies.

akaballa

Computer
May 29, 2011
7
Hi I am trying to run the fft command on my matlab waveform that I generated.

I created a superimposed waveform of to generate harmonics. The fundamental frequency is 60 Hz and the high frequency is 900 Hz. I want to be able to show each of the harmonics in the FFT spectrum

Code:
a = 1; % alitude
fHz = 60;%*i; % Hz
fRads = 2*pi*fHz; % freq rad/sec
 t = 0:0.0005:1 ; % sampling time
y = (a * sin(fRads * t))...
       + (0.65*a * sin(3*fRads * t)) ...
       + (0.55*a * sin(5*fRads * t))...
        + (0.45*a * sin(7*fRads * t))...
        + (0.35*a * sin(9*fRads * t))...
        + (0.25*a * sin(11*fRads * t))...
       + (0.15*a * sin(13*fRads * t))...
        + (0.05*a * sin(15*fRads * t));

The sampling frequency used here is 2000 Hz since high frequency is 15*60 Hz = 900 Hz. Ideally the amplitude of the spectrum should decrease as the frequency increases after doing fft.

This is the FFT code that i created:

Code:
 Y = abs(fft(y,2001));
    %Y = fft(y,NFFT)/L;
    %f = 128/2*linspace(0,1,NFFT/2+1);
      f = 0:2000;
      plot(f,y,'-x'),title('N =2000'),axis([0 900 0 20]);
   
    % Plot single-sided amplitude spectrum.
    %plot(f,2*abs(Y(1:NFFT/2+1))) ;
    
   title('Amplitude Spectrum of y(t)');
    xlabel('Frequency (Hz)');
ylabel('|Y(f)|');

I am not getting the correct amplitude spectrum. I am pretty sure I am doing something wrong with the fft code. Plz help!
 
Replies continue below

Recommended for you

But, how far off is it?

> Your sampling rate is too low to guarantee spectral purity. It's barely a factor of 2 higher than the highest frequency. You should sample at something like 50x the max frequency to ensure no pathological sampling artifacts.


> Every FFT implementation is slight different. You need to verify and account for the scaling factor.

TTFN

FAQ731-376
Chinese prisoner wins Nobel Peace Prize
 
Perhaps the expected amplitude is out by a factor of 2, since you're dividing the spectrum by L instead of (L/2)?

- Steve

LinkedIn
 
Also there will be some leakage because there are not a whole number of cycles in your time range.

--
Dr Michael F Platten
 
I divided by L/2 and worked fine..the harmonic amplitude lowers as frequency increases. However, the harmonics are not displaying on the correct frequencies. I tried increasing the sampling to more than 2000 ( 10000) but increasing doesnt seem to improve it. Any suggestions?
 
I always work from first principles for the frequency axis. The first point is zero. The second is 1/T, where T is the sample length. And then all subsequennt frequencies are multiples of 1/T. I forget "magic" formulae for things like this that involve Fs and nfft, etc.

Beware that the DC and Nyquist frequency bins need only be divided by L, not L/2. (If they are important to you).

- Steve

LinkedIn
 
Code:
f = 0:2000;
This needs to be tied to your bin width as stated by SomptingGuy.

=====================================
(2B)+(2B)' ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor