# Why does FFT show harmonics at different amplitude?

108 views (last 30 days)
Mehmet Fide on 21 Aug 2011
Hi,
I have a signal contains fundamental + second harmonic + third + .. up to eighth harmonics. But all harmonics have same amplitude. After fft transformation, matlab shows each harmonics in different amplitudes. I was expecting them to have same amplitude. What is the reason behind it?
I want to calculate THD of the signal. But first I have to get correct amplitudes after FFT. How can I compensate the difference?
Code like that:
N = 1024; %%we need 1024 point fft
fs = 976; %%sampling frequency of the system
fb = 50; %%fundamental frequency 50 Hz
t = linspace(0,N/fs,N);
x = 0.3*cos(2*pi*fb*t)+ ...
0.3*cos(2*pi*2*fb*t)+ ...
0.3*cos(2*pi*3*fb*t)+ ...
0.3*cos(2*pi*4*fb*t)+ ...
0.3*cos(2*pi*5*fb*t)+ ...
0.3*cos(2*pi*6*fb*t)+ ...
0.3*cos(2*pi*7*fb*t)+ ...
0.3*cos(2*pi*8*fb*t);
fft_x = fft(x);
plot((0:N-1)*fs/N, abs(fft_x));
axis([0 N/2 min(abs(fft_x)) max(abs(fft_x))]);
Thanks.

Wayne King on 21 Aug 2011
Did you change the t vector?
Fs = 976;
N = 976;
fb = 50;
t = 0:1/Fs:1-(1/Fs);
x = cos(2*pi*50*t)+cos(4*pi*50*t)+cos(6*pi*50*t);
xdft = fft(x,N);
plot(abs(xdft));

Mehmet Fide on 21 Aug 2011
Hi,
I've checked your code in other thread. You don't have ratio problem there, you simply did scaling to solve issue which is fine.
But for my case, all harmonics seem having different amplitude relative to each other. I mean after FFT, their exact values are not important for me. But their ratios are important. For example If first harmonics appears as a value 1000, second harmonics should be also 1000. If first one is 2000, second one should be 2000 too. Because all harmonics have same amplitude in the signal before fft.
Bu what I get is all of them has different amplitude.

Wayne King on 21 Aug 2011
Hi, Your problem is that your frequencies are not falling directly on DFT bins. The DFT bins are multiples of Fs/N.
Also, your original time vector did not reflect your sampling rate of 976 Hz. You do not incorporate zero-padding in the time vector. The way you have done it, you changed the sampling rate of the signal.
N = 976; fs = 976; %% sampling frequency of the system fb = 50; %% fundamental frequency 50 Hz t = 0:1/fs:1-(1/fs); x = 0.3*cos(2*pi*fb*t)+ ... 0.3*cos(2*pi*2*fb*t)+ ... 0.3*cos(2*pi*3*fb*t)+ ... 0.3*cos(2*pi*4*fb*t)+ ... 0.3*cos(2*pi*5*fb*t)+ ... 0.3*cos(2*pi*6*fb*t)+ ... 0.3*cos(2*pi*7*fb*t)+ ... 0.3*cos(2*pi*8*fb*t); fft_x = fft(x,N); plot((0:N-1)*fs/N, abs(fft_x));
Wayne

Mehmet Fide on 21 Aug 2011
Hi Wayne,
Thanks for your reply. I've tried again by changing N to 976. But still amplitude of harmonics are not same.

zohar on 21 Aug 2011
Hi
When you are using
t = linspace(0,N/fs ,N);
your fs is not as you assumed it's
1/(t(2)-t(1))
just change the lines
fs = N; %%sampling frequency of the system
t = linspace(0,N/fs - 1/fs,N);
and it's ok
And that is because of you are using linspace, for next time use
t = [0:N-1]/fs;
Have fun

Mehmet Fide on 21 Aug 2011
Hi zohar,
I have to do same calculation in my embedded system has MSP430 16 bit MCU from Texas Instruments. I allocated acquisition memory for fft purpose. Because of hardware limitation I can not change sampling rate. It has to be 976Hz. I can reduce acquisition memory size to be same with amount of sampling frequency. My current problem is now that I don't know how to do FFT with 976 points which is not power of 2. I will try to use matlab coder to see how it will implement it in C. I'll study on it now.
Thank you again..

Mehmet Fide on 21 Aug 2011
Unfortunately coder gave error: Length of transform dimension must be a power of 2. Any suggestion for that also?