Audio Total Harmonic Distortion Analyzer for LTSPICE, making THD vs. Amplitude and Frequency sweeps in LTSPICE

FFT capabilities integrated into LTSPICE simulator are nice and flexible. However, if you would like to evaluate frequency or amplitude dependence of distortions, you have to take these measurements point by point and then manually plot them.

I created an LTSPICE add-on to automate THD measurements and plot result in the form of THD vs. Amplitude and THD vs. Frequency graphs.

How my LTSPICE Audio THD Analyzer works:

It outputs sinusoidal signal with amplitude or frequency stepping sweep into device under test (DUT).  Output signal from DUT is feed into analyzer input. After waiting some time for signal to become steady state, analyzer restores fundamental and subtracts it from input signal. This subtraction allows to increase resolution or reduce measurement time for the same resolution. You can monitor residual components at “Notch output”.

Residual signal is then fed into synchronous filters and detector. Each harmonic is filtered and measured separately. Maximum of 10 harmonics are analyzed. Amount of harmonics could be easily increased by adding corresponding filters and possessing.

This is how waveforms look after analysis is completed:

Analysis Waveforms after Amplitude Sweep

Analysis Waveforms after Amplitude Sweep

Total Harmonic Distortions vs. Output Amplitude plot looks like this:

Total Harmonic Distortions vs. Output Amplitude

Total Harmonic Distortions vs. Output Amplitude

Total Harmonic Distortions vs. Frequency plot looks like this:

Total Harmonic Distortions vs. Frequency

Total Harmonic Distortions vs. Frequency

How to use LTSPICE Audio THD Analyzer:

Place THD_Analyzer.asy symbol and Analyser_Controls.txt files in the same directory, where you are saving schematic (DUT schematic),
that you would like to analyze.

Put SPICE directives “.inc Analyzer_Controls.txt” and “.tran 0 {AnalysisTime} {SettlingTime} {MaxTimestep}”  in DUT schematic .

Edit “Analyzer_Controls.txt“  to enable (uncomment) appropriate sweep (amplitude  or frequency ) and save this file.

Setup  “.param   Ag=xxx”  as amplitude for  frequency sweep or “.param   Fg=xxx” as frequency  for  amplitude  sweep.

Run the simulation.

After simulation is complete, go to View menu and open SPICE Error Log or use Ctrl+L command.

Click with right mouse button on opened Log file.

Execute “Plot .step’ed .meas data” command. Right mouse button click on opened plot and use Add Trace or Ctrl+A and select the data that you want to plot.

You may want to double click on axis to change axis limits or switch to logarithmic scale.

Notch output shows residual components, after fundamental removal.

Please note that fundamental may not be removed completely. This is not necessarily affecting resolution of measurements as soon as additional synchronous filtering is used to measure amplitude of harmonics.

Increasing SettlingTime and StrobeLength,  or (and) decreasing MaxTimestep would likely improve fundamental rejection.

Generator output is DC coupled and has 0 Ohm output impedance. Use external AC coupling and appropriate series resistor if required, to ensure proper operation of simulated circuit.

THD_Analyzer contains all necessary files and example. Unzip all files in the same directory, open “Example_BJT_THD_TEST.asc” and run simulation.
You can monitor analysis progress in the left lower corner of LTSPICE window.  After simulation and analysis is complete (including completion of .MEASURE), follow the instructions to display
results.

Comments

3 Responses to Audio Total Harmonic Distortion Analyzer for LTSPICE, making THD vs. Amplitude and Frequency sweeps in LTSPICE

  1. Luca Girotti says:

    Really huge and great job!!!! Thanks for sharing this! I have one questions for an upgrade: is it possible to add THD+N plotting (THD including noise) as an option to the pure THD? This would be helpful to conduct THD+N vs Frequency and THD+N vs Voltage plots.
    Also how to output THD vs Power and not voltage level?

    • Eugene says:

      Hello Luca. I’m not sure about THD+N, because noise requires different type of analysis. And for most of real analyzers it is like additional error that you have to leave with, but would like to get rid of. Because noise decrease resolution of THD measurements.
      Calculating power is really easy. Open script file with any text editor and find place where .MEASURE commands are located (somewhere close to the end).
      Find the line :
      .meas Fundamental_Out_V_RMS param sqrt(2)*hypot(aH1S,aH1C)
      Somewhere after this line add line:
      .meas Output_Power param (Fundamental_Out_V_RMS*Fundamental_Out_V_RMS)/Rload
      Use value of your load instead of Rload. Like in this example:
      .meas Output_Power param (Fundamental_Out_V_RMS*Fundamental_Out_V_RMS)/8
      Save changed script file.
      When you will be plotting analysis results, double click on horizontal axis (the same as if you wanted to change horizontal scale) and instead of ag or fundamental put Output_Power.

      • Luca says:

        Dear Eugene,

        noted your thinking about Noise contribution to Distorsion measurement. Thinking back about this, as from my experience, there is so big difference between “simulated” noise and “real world” noise that you are right….there is no sense to add it.

        Thanks about the hint on how to plot THD vs. Power, I will test it shortly!!!

        Thanks again for the great job you have done and donated to the public community!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>