I need to calculate the harmonics in the power supply of $50\textrm{ Hz}$. If suppose I need to calculate up to 20 harmonics.

Then my sampling frequency by Nyquist criteria should be :

$$f_s \ge 2 f_m$$

where $f_m$ will be of the highest harmonic frequency, i.e $f_m = 20 \times 50\textrm{ Hz} = 1000\textrm{ Hz}$. So $f_s = 2000 \textrm{ samples/sec}$.

If the signal is $50\textrm{ Hz}$ then in this case, $T_{\rm period} = 0.02 \textrm{ sec} = 20\textrm{ msec}$. So in each $20\textrm{ msec}$ period I am taking $0.02 \times 2000 = 40$ samples per period of $50\textrm{ Hz}$.

  1. So in this case do I need to do $40$ point FFT ?

  2. Also if input signal is split into $40$ points then the output of the FFT is also $40$ different values. So this means the output is produced for $40$ harmonics not for $20$ harmonics is it right ?

  3. How this has to be done at software level?

    I will take a timer which interrupt at every $1/2000\textrm{ sec} = 0.5\textrm{ msec}$. On every interrupt take the ADC sample. So after $40$ interrupts ($20\textrm{ msec}$ is passed) I will be having buffer of $40$ samples then I will perform the $40$ point FFT algorithm to get the harmonics value, right ?

    Then I will clear the buffer & again fill the buffer with new $40$ samples after $40$ interrupts ($20\textrm{ msec}$ is passed) then again I will perform the $40$ point FFT algorithm to get the harmonics value, right ?

    As my FFT window is $40$ samples, do I have to clear the buffer every time after the FFT is performed or I will shift the signal in the buffer to the right by $1$ (i.e. use array of $40$ as ring buffer or FIFO ) ?

Please suggest, is my calculation is right ?


2 Answers 2


I am not sure I can fully answer your question but you have a few things you need to be aware of:

As for the Nyquist criteria, as Ben said, you have to sample at a rate greater than 2 times (5, 10 times or more) the highest frequency you have in your signal. Imagine you have a 1 Hz (one period per second) sinusoid you want to sample. If you do so at the Nyquist frequency of 2 Hz (2*1 Hz) you would get two points per second/period (your sinusoid will no longer be represented as a sinusoid but instead as two points!)

For your 1st question, you shouldn't do a 40 points FFT for the reasons I mentioned above and which will affect the precision of your estimated frequencies.

For your 2nd question, you are wrong! Having 40 points FFT doesn't mean you will have 40 harmonics. Having a 40 points FFT means that the frequency range $[0, f_s]$ (where $f_s$ is the sampling frequency) will be represented using 40 points. If $f_s=2000$ Hz and you use a 40 points FFT then your frequency points will be separated by $2000/40=50$ Hz and this is a very bad precision since as you might already know the power line frequency $f_0$ is not exactly 50 Hz but varies a little around this value and the other frequencies are odd-order harmonics of this power line frequency. If $f_0=50.05$ Hz then your 10th harmonic $f_{10}=10*50.05=500.5$ Hz is already $0.5$ Hz off of $500$ Hz and with the precision you have you will necessarily get wrong estimations.

To alleviate your precision problem you can take not only one period to estimate the harmonics but instead a time-period where you estimate that the harmonics characteristics (amplitude, frequency value) did not change much, let's say something around a 1-second time period. In that case, you will have 40*50=2000 points to do FFT on (that is if you keep your $f_s$ as is!), this way you improve your frequency precision by a factor of 50.

For the anti-aliasing filter, as the name suggests, it is a counter-measure against aliasing. Basically, you need to be sure that your (original) signal does not contain frequencies that are greater than $f_s/2$ otherwise they might appear (taking an alias frequency value :-)) inside the frequency-range of interest when they actually belong outside this range.

I am not sure this is practically a problem for you if you measure voltage since the voltage signal is supposed to contain a single $50$ Hz and other harmonics if they exist they should have very small amplitudes, unless your have some disturbances (electromagnetic for e.g.) from outside, but I guess this is what you want to check in the first place. Moreover, any sensor you use will a specific bandwidth which would limit the frequency content of your measured signal. So, what you need to do is to first check the bandwidth of your voltage sensor and if it is greater than $f_s/2$ then you should use an anti-aliasing filter.

  • $\begingroup$ "In that case, you will have 40*50=2000 points to do FFT on (that is if you keep your as is!),". .. and so you mean to say do not do 40 point fft .collect 2000 points during 1 second period & then perform 2000 pt FFT ..." this way you improve your frequency precision by a factor of 50."... How precision improve here as instead of doing FFT every 20msec I am now doing every 1 sec I.e more time delay between two FFT calculation. $\endgroup$
    – user6363
    Commented May 26, 2017 at 1:11
  • $\begingroup$ Also if I do 2000 points FFT.. then if suppose output of this FFT is X[0] to X[1999] , so X[1] will be information about 50 Hz fundamental frequency & X[2] will be the 2nd harmonic & X[3] will be the 3rd harmonic & so on is it right..? then I will get information about 1000 harmonics & there complex conjugate .. but in my case I am interested in only first 20 harmonics of 50 Hz fundamental frequency is it really beneficial to do calculation for 1000 harmonics.. ? $\endgroup$
    – user6363
    Commented May 26, 2017 at 3:09
  • 1
    $\begingroup$ Taking $N=40$ or $2000$ points won't change the frequency range you're looking at as long as you have the same $f_s$! For e.g, suppose $N=40$, then in the FFT output $X[0]$ corresponds to $f=0$ Hz, $X[1]$ corresponds to $f=50$ Hz, $X[2]$ corresponds to $f=100$ Hz, ..., $X[39]$ corresponds to $f=1950$ Hz. If now $N=2000$, then in the FFT output $X[0]$ corresponds to $f=0$ Hz, $X[1]$ corresponds to $f=1$ Hz, $X[2]$ corresponds to $f=2$ Hz, ..., $X[1999]$ corresponds to $f=1999$ Hz. What you are doing is changing the frequency step (precision) $\Delta f=f_s/N$ you get in the Fourier domain. $\endgroup$ Commented May 26, 2017 at 5:29
  • $\begingroup$ Ok got it.. but one thing i will like to correct in your last post ... X[0] is the constant term (also refereed to as DC bias) X[1] is the fundamental frequency X[2] is the first harmonic X[1000] is the 999th harmonic ... then ... X[1001] to X[1999] are the complex conjugates of X[999] to X[1] respectively.... do you agree with this. $\endgroup$
    – user6363
    Commented May 26, 2017 at 6:16
  • 1
    $\begingroup$ This is related to the "symmetry of FFT for real signals." $\endgroup$ Commented May 30, 2017 at 7:45

First of all, you should sample at rate greater than 2 times the highest digitized frequency, not equal to.

Second of all, do you have an anti-aliasing filter?

  • $\begingroup$ Thanks fir replying.. Already my fs = 2*20*50 that met Nyquist criyeria.. What exactly will anti aliasing filter do ? Will it use to filter the adc voltage samples ? $\endgroup$
    – user6363
    Commented May 25, 2017 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.