×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# FFT implementation for harmonics

## FFT implementation for harmonics

(OP)
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
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!

### RE: FFT implementation for harmonics

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.

### RE: FFT implementation for harmonics

Perhaps the expected amplitude is out by a factor of 2, since you're dividing the spectrum by L instead of (L/2)?

- Steve

### RE: FFT implementation for harmonics

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

--
Dr Michael F Platten

### RE: FFT implementation for harmonics

(OP)
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?

### RE: FFT implementation for harmonics

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

### RE: FFT implementation for harmonics

#### CODE

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

=====================================
(2B)+(2B)'  ?

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

#### Resources

Low-Volume Rapid Injection Molding With 3D Printed Molds
Learn methods and guidelines for using stereolithography (SLA) 3D printed molds in the injection molding process to lower costs and lead time. Discover how this hybrid manufacturing process enables on-demand mold fabrication to quickly produce small batches of thermoplastic parts. Download Now
Examine how the principles of DfAM upend many of the long-standing rules around manufacturability - allowing engineers and designers to place a partâ€™s function at the center of their design considerations. Download Now
Taking Control of Engineering Documents
This ebook covers tips for creating and managing workflows, security best practices and protection of intellectual property, Cloud vs. on-premise software solutions, CAD file management, compliance, and more. Download Now

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!