I want to calculate THD. SO how do I isolate the fundamental frequency from a signal with harmonics? I have a signal V which is a sum of V1, V2 and V3. V1=A1sin(w1t+phi1), V2=A2sin(w2t+phi2) and V3=A3sin(w3t+phi3). I want to find out the magnitude/amplitude of a known frequency w1 in the signal. How do I proceed? Should I use the FFT library? If yes then how?

  • Though this is not really Arduino specific: Yes, I think you need to do an FFT on the signal. When googling something like "Arduino FFT" you get for example this library. It also has examples. You can use it, or program the FFT yourself. That would be a pure C/C++ programming question.
    – chrisl
    Nov 27, 2020 at 10:17
  • What are the values of the frequencies ω₁/2π...? To what precision are they known? Are they constant? How long does the signal last? Is it steady enough to allow you to measure V₁, V₂ and V₃ sequentially, rather than simultaneously? To what precision do you need the THD measurement? Nov 27, 2020 at 14:12
  • @chrisl: He may not need an FFT. The FFT computes the amplitude of many frequencies. If the OP only needs three frequencies, the FFT is most likely overkill. Nov 27, 2020 at 14:15

1 Answer 1


What exactly are you trying to do? If you want to measure THD from an external signal, an Arduino is probably not the best choice. It only has 10-bit ADCs and they are DC-coupled. You can build a simple circuit (https://www.daqarta.com/dw_rrll.htm) to shift an AC-coupled input to DC for the Arduino, but you will still have only 10-bit resolution. I haven't looked deep enough into the library that chrisl mentions to see how big an FFT it can perform in the limited RAM of an Arduino, but I suspect the spectral resolution would be so poor as to be unhelpful for real-world THD measurements.

The way you describe the signal makes me think that this is an academic exercise, not a real-world problem. If so, you don't need an Arduino or any other hardware, just run the FFT on your main computer. If you do need to make real-world measurements, an ordinary sound card on your computer is a better way to go. See https://www.daqarta.com/dw_0d0t.htm for an example. The sound card approach has the added advantage that it can generate a low-distortion test signal to drive the device whose distortion you are trying to measure. That allows a "trick" (called "Spectral Line Lock" in the example) to get even better resolution, by making the test signal synchronous with the sample rate so you don't need window functions.

If you really want to measure THD with an Arduino, consider an approach used in what used to be called a "wave analyzer". The block diagram is shown here: https://www.daqarta.com/dw_0h0p.htm. This is essentially a one-spectral-line analyzer, which actually can be more selective than an FFT. You basically multiply the input signal by the sine and cosine of the reference frequency (the fundamental) and then rectify and filter the results, and combine RMS-wise. That gives you the fundamental value (or any individual harmonic you tune the reference to), so to get THD you divide by the RMS of the overall input signal.


Your Answer

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

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