Measuring THD of a Sine Wave


I recently wanted to build a sine wave oscillator from scratch. Although many oscillator circuits are quite simple to build, these tend to be of a relaxation type and generate non-sinusoidal outputs. The easiest way to generate a sine wave oscillator is to use an op-amp with some passive components. There exist many versions of sine wave op-amp circuits, such as the Wien bridge oscillator, quadrature oscillator, and RC phase shift oscillators, to name a few. Op-amp-based designs are also limited to lower frequencies, mainly due to the bandwidth limitations of op-amps, but they are an excellent choice if the application requires low-frequency outputs, such as audio.

Setup1 Quad opamp phase shift sine wave oscillator built on a proto board (opamp : LM324)

The RC phase shift oscillator seems quite simple to build, has good stability, and is easy to understand—that’s why I chose to build it. This blog is not about design but about quantifying the output sine wave. My design uses a quad op-amp package with three op-amps to prevent loading of the RC phase shift network, and the last one buffers the output from the final stage of the oscillator. For more details on how to build these circuits, I recommend reading the article from TI’s Analog Applications Journal: “Design of Op-Amp Sine Wave Oscillators” by Ron Mancini.

Circuit Circuit Diagram with Initial gain of 22.

Coming back to the topic of THD, it’s better to start by understanding why we need such measurements. After building the aforementioned oscillator, I noticed that the output signal was quite distorted. This wasn’t a big issue since I could fix it by setting the bias voltage exactly to mid-supply. This got me thinking: “How can I measure distortion beyond just eyeballing?”

Waveform Low Distortion Sine wave output.

The foolproof method of measuring such distortion is to look at the signal in the frequency domain instead of the time domain. An ideal pure sine wave tends to show maximum spectral power density at the fundamental frequency and none at the harmonics. In other words, an ideal pure sine wave doesn’t have any harmonics.

In reality, every generator has some degree of distortion resulting in harmonics. A 0% THD would indicate an ideal sine wave with no harmonics. Minimizing the spectral power at these harmonics is also key to minimizing distortion at the fundamental frequency.

Due to system and component limitations, this harmonic spectral power density can’t be tuned to zero but can be attenuated to some degree. In order to quantify the distortion, we need to calculate the THD based on the output fundamental frequency and its harmonic components.

THD can be defined as the ratio of the sum of the RMS voltages of the harmonics of the signal to the RMS voltage of the fundamental.

The formula for THD is shown below:

THD=V22+V32+V42+VnV1×100%THD = \frac{\sqrt{V_2^2 + V_3^2 + V_4^2 + \dots{V_{n}}}}{V_1} \times 100\%

where:

  • V2,V3,V4,,VnV_2, V_3, V_4, \dots, V_n are the RMS voltages of the harmonics
  • V1V_1 is the RMS voltage of the fundamental

Voltages shown in the equation are absolute values obtained from the FFT plot. The SDS800x DSO I have is capable of displaying the voltages and power in dBV and dBm. The dBV voltages can be converted to decimal values and plugged directly into the equation to get the harmonic distortion.

To simplify the calculations further, we can use relative power measurements and substitute the RMS voltage ratios with power ratios:

THD=V22V12+V32V12+V42V12+×100%THD = \sqrt{\frac{V_2^2}{V_1^2} + \frac{V_3^2}{V_1^2} + \frac{V_4^2}{V_1^2} + \dots} \times 100\%

The power ratio can be written as:

dBc=10log10(PhPc)\text{dBc} = 10 \log_{10} \left( \frac{P_{h}}{P_{c}} \right)

where:

  • PhP_{h} is the power of the relative harmonic
  • PcP_{c} is the power of the carrier signal (the fundamental signal in this case)

Power can then be expressed as the square of the voltage divided by the impedance:

Ph=Vh2ZP_h = \frac{V_h^2}{Z} Pc=Vc2ZP_c = \frac{V_c^2}{Z}

Since the system impedance (ZZ) is identical for both the carrier and harmonic components, the ZZ terms cancel out when calculating the ratio. This results in the following equation for dBc:

dBc=10log10(Vh2Vc2)\text{dBc} = 10 \log_{10} \left( \frac{V_h^2}{V_c^2} \right)

The square of the voltage ratio used here is mathematically identical to the term found in the THD equation. Therefore, we can isolate the squared voltage ratio as:

Vh2Vc2=10dBc10 \frac{V_h^2}{V_c^2} = 10^{\frac{dBc}{10}}

where:

  • dBc is the power ratio of the harmonic relative to the carrier (fundamental)
  • VhV_h and VcV_c are the RMS voltages of the harmonic and carrier, respectively

The THD equation can now be rewritten as:

THD=10dBc210+10dBc310+10dBc410+×100%THD = \sqrt{10^{\frac{\text{dBc}_2}{10}} + 10^{\frac{\text{dBc}_3}{10}} + 10^{\frac{\text{dBc}_4}{10}} + \dots} \times 100\%

or in general form:

THD=n=210dBcn10×100%THD = \sqrt{\sum_{n=2}^{\infty} 10^{\frac{\text{dBc}_n}{10}}} \times 100\%

The latter method, involving the power ratio, is significantly faster to calculate because we only need to sum the power ratios and take the square root.

Below are the actual measurements for my phase shift oscillator, which we will use to calculate the THD:

FFT_Plot FFT Plot depicting amplitude of Fundamental Signal and its Harmonics

Spectral ComponentΔMeasurementFormulaLinear Value
1st Harmonic (V2V_2)32.8 dBc-32.8 \text{ dBc}1032.81010^{\frac{-32.8}{10}}524.8×106524.8 \times 10^{-6}
2nd Harmonic (V3V_3)32.7 dBc-32.7 \text{ dBc}1032.71010^{\frac{-32.7}{10}}537×106537 \times 10^{-6}
3rd Harmonic (V4V_4)55.6 dBc-55.6 \text{ dBc}1055.61010^{\frac{-55.6}{10}}2.75×1062.75 \times 10^{-6}
4th Harmonic (V5V_5)59.55 dBc-59.55 \text{ dBc}1059.551010^{\frac{-59.55}{10}}1.1×1061.1 \times 10^{-6}

THD=1.06565×103×100%THD = \sqrt{1.06565 \times 10^{-3}} \times 100\%

THD=3.26%THD = 3.26\%

As we can see, the spectral power is severely attenuated beyond the 2nd harmonic, and the contribution to the overall THD becomes negligible. A THD of 3.26% is reasonable for something built with a cheap off-brand LM324 and few passive components.

Upon closer inspection, it seems the first stage op-amp was clipping. This can be fixed by adjusting the gain. When I was first building the circuit, I didn’t give much thought to the appropriate gain and chose values that I could find readily.

Theoretically, the gain should be 8.33 for the 60-degree phase shift configuration. However, I had it set to 22, which resulted in clipping. The output was still sinusoidal due to the low-pass nature of the circuit, but the frequency was way off from the calculated frequency of 8.36 kHz.

Circuit_modified

Modified First stage with gain of 8.5



Setup2 Setup with Variable Resistors for tuning the gain

By replacing resistor R1 with a 100 kΩ resistor and a 100 kΩ potentiometer, I was able to tweak the output to avoid clipping, which brought the gain to 8.5. This attenuated the harmonics even more and improved the overall THD. The capture below shows the attenuated spectral content:

Waveform2 Split View showing the corrected low distortion sine wave and it’s FFT plot with attenuated harmonics

The frequency still seems off. For RC values of 3.3 kΩ and 10 nF, the calculated frequency is around 8.336 kHz, whereas I observed 6.6 kHz. This is mainly due to the poor tolerance of the ceramic disc capacitors I used in the circuit. When I measured the capacitors, the tolerance was nearly 30% (one capacitor was 13 nF instead of 10 nF). This seems to be the cause of the frequency discrepancy.

Coming back to the THD measurements, fixing the distortion at first stage seem to have yielded better results. The -42.4dB at first Harmonic, -53.8dB at the 2nd Harmonic and -48dB at 3rd Harmonic results in a THD of only 0.9%.

THD=10-42.410+10-53.810+10-48.310×100%THD = \sqrt{10^{\frac{\text{-42.4}}{10}} + 10^{\frac{\text{-53.8}}{10} }+ 10^{\frac{\text{-48.3}}{10} } } \times 100\%

THD=76.5×106×100%THD = \sqrt{76.5\times10^{-6}} \times 100\%

THD=0.87%THD=0.87\%

0.9% THD is quite an improvement from the previous 3.2%. Although, I can still notice some cross-over distortion at the first-stage, which might be the major source of harmonics.

Cross_Over_Distortion Cross-over distortion at first op-amp stage and its influence on odd harmonics

This can also be eliminated with further investigation, but 0.9% seems good enough for the current setup. Using a better op-amp might yield even better results. Since the goal of this test was to demonstrate THD measurements rather than chase the lowest possible numbers, I’ll consider this sufficient.

Calculations are based on video from the ham enthusiast Alan Wolke(W2AEW). Check out his video “#305: Measuring Total Harmonic Distortion THD using an FFT on an oscilloscope”.

Check out my web tool, if you hate punching all those formulas into the calculator Simple THD Calculator