Calculate the harmonics in the power supply of $50\textrm{ Hz}$ using FFT 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}$.


So in this case do I need to do $40$ point FFT ?  
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 ?  
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 &amp; 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 ?
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?