Distortion Analyzer

I have an HP distortion analyzer I drag out every so often when I’m building audio widgets. I also have a General Radio audio oscillator and a couple of other nice pieces of test equipment.

I wonder how much of the distortion analyzer can be done with common tools inside audacity.

Apply a pure, quiet tone to the equipment under test. Inspect the output by completely deleting the original sine wave and whatever is left is Distortion plus Noise. The number is generally given in percentage of the total output signal. 30% harmonic distortion means one third of the output signal is garbage of one form or another.

The anylzer has “auto nulling” which means within reason, it will find the original tone and take it out. If you don’t use auto nulling, you have to manually jocky frequency and phase controls back and forth for several minutes to get rid of the original tone.

I know you can do complicated frequency analysis with waveforms and printouts with “circles and arrows and a paragraph on the back explaining what each one does.” [“Alice’s Restaurant,” Arlo Guthrie, 1967]

I want one number. Under these conditions, this amplifier has 0.2% Harmonic Distortion.


Quite a lot I think - I had a play with this idea today while waiting for some DVDs to burn.

30% garbage, yes I can see that, but not necessarily “harmonic distortion”. Which is the more important measurement for practical test purposes - “% harmonic distortion” or “% garbage”? I think “% garbage” would be easier to determine, and probably more useful for the home enthusiast.

A bit tricky that one, though it looks possible using Nyquist.
Search for positive going zero crossing point on both the original test tone and the “processed” tone, then invert one and iteratively amplify to find the minimum signal level (maximum nulling).
High sample rates as well as bit depths for increased accuracy.
Would that work?

100 x (difference of pre and post tones)/(amplitude of original test tone) and output the number… would that do the job?

I understand all this is a complete slave to the sound card, but yes. That should work.

The old instruments needed auto nulling because they really had no idea what the incoming tone pitch was since the analysis is done with two different machines. And it wouldn’t work if the analyzer main frequency dial was too far off.

The nulling business was complicated because the instrument was working with a physical dual “T” filter, a very high Q notch filter with several branches and variables to take out the original tone and nothing else.



If the test signal is always a sine wave, then that simplifies matters considerably. Nyquist has a notch filter function

(notch2 signal frequency Q)

Attached is a plug-in that uses this. Simply unzip it into the Audacity plug-ins folder and it will appear in the Analyse menu when Audacity is next started (Requires Audacity 1.3.x or later).

The method that I have in mind is:
Use the “Generate” function in Audacity to create a test signal (must be a mono sine wave)
Connect the output from your sound card to the input of the equipment under test, and the output from the equipment to the input of your sound card.
Play the test signal and record it (playing through the equipment under test).
Select the new track (at least 50,000 samples are required) and apply the “Distortion Analyser” plug-in.
It give a read-out as a % to two decimal places.

I’ve no idea how accurate this thing can be expected to be - of course it depends to a great extent on the sound card. If you are able to compare it to your old analogue equipment I’d be very interested to hear how it compares.

The output could easily be extended to more decimal places, but I doubt that it is worth the trouble.

[Edit] This file is no longer available - the forum software has eaten it - see later post

I’ve tested the plug-in by recording “Stereo Mix” (in effect, testing the sound card) and it gives a reading of around 0.06%
Testing it by playing the sound through PC speakers and recording with a cheap PC microphone gives a reading of around 6%

The results vary depending on the test frequency, as one would expect.

Just noticed a “feature” in the Audacity / Nyquist interface.

The slider control of Nyquist plug-ins is not accurate.
Adjusting the slider to 1000Hz (in this case 1000Hz should be the default) does NOT pass the value of 1000.000 to Nyquist. It passes the value of 1000.414 Hz !!!

The only way to set the frequency to an exact number is to type the number in the box. Typing “1000” (without quotes) in the box will set the value to exactly 1000.0000


The file ./…/files/4182_01d150e806eff8a9418ca0f270a77c64 does not exist.


I guess the forum must delete them after a certain time period.

Here it is again:

[Update: plug-in now updated due to changes in Audacity. Use this newer version instead: distortion-analyser.ny

Got it. Thank yew.

Is it necessary to run playback and record at the same time? Is there something you use that needs to “see” the outgoing for it to work?

I picture my General Radio audio oscillator plugged into the circuit under test and the circuit output plugged into my PC/Mac. The tool has an “Expected Frequency” setting and does everything else from there. Yes, the test signal is always a sine wave.

I will change my mind just a bit. That Hewlett Packard analyzer has a “Scope Output” connection and that would essentially display an Audacity timeline without the original pure sine wave in it. This tells you 1. whether or not the “distortion” is noise or signal malformation, and 2. what the distortion is. None of us have any trouble picturing what flat-topping would look like.

I’m interested in doing analysis on amplifiers that have to share the signal between two, say, transistors. There is always that stupid crossover point where the signal becomes an SEP (See: Hitchhiker).

There is one trick. The HP instrument only displays about ten or fifteen dB worth of range, so to read .02%, the range switch is turned. The “scope output” is amplified along with the distortion.

Sorry about being so fuzzy about this. It’s Friday.

To use the HP: Wire everything up, apply a sine wave, and adjust the HP until the needle reads “1” (100% signal). Then switch to the distortion mode and turn that big dial until the frequency is close to the test tone. Then press AUTO. The frequency knob and nulling controls recognize the tone (we assume it’s overwhelming larger than the junk. I don’t know what happens if it’s not) and watch the needle dive towards zero.

Then start increasing the sensitivity of the HP until the needle is as diven as it’s going to get. Read the Distortion + Noise number.

If you’re simply rolling in shekels/wampum, you own a scope and connect it to the HP and actually watch the characteristic of the error. That’s why it didn’t occur to me right away. Not enough wampum.


This is just a simple (very simple) tool.

  • You use Audacity to generate a sine tone (“Generate” menu).
  • You connect the computer sound card output to the input of the device under test, and the output of the device to the recording input of your computer sound card.
  • For testing amplifiers it will probably be necessary to use some sort of “potential divider” to attenuate the signal so as to avoid overloading the input of your computer sound card, but as long as it is reasonably well shielded from interference, a simple 2 resistor divider (or a low noise potentiometer) is unlikely to cause significant noise or distortion (assuming that we are not talking about high power amplifiers that make the resistors get hot).
  • You set Audacity to “play back other tracks while recording new one” (default) so that your generated tone is played through the device, and recorded onto a new track.

The new track that you have recorded, assuming that your sound card and the device under test are perfect, will now have an exact copy of the original track (we are not testing for differences in amplitude or any propagation delays).

However, we are aware that nothing is ever perfect, so we are testing to see how much the recorded signal differs from the idealised sine wave.

The assumption is that any distortion or noise that has occurs through the device will introduce additional frequencies (in addition to the test tone). The exact level of the recording is not important as the plug-in simply measures the distortion as a proportion of the recorded signal.

If the system was perfect, then the recorded signal would be a pure sine wave at the same frequency as the generated tone. Let’s say that our recorded signal has an amplitude of 0.764
By using a tight notch filter (in this case it is a digital filter, but it is essentially the same as the twin-tee filter in the hardware analyser) we can remove the sine wave at out test frequency.

A quick note here - the sine wave that is generated in Audacity is extremely accurate, and just limited by the sample rate, bit depth, and precision of the sound card.

So after we have removed the sine wave, any signal that remains is the crud that we want to measure.
“crud”/“recorded signal level” x 100 = % divergence from a perfect (zero distortion) system.
So if the “crud” measures at 0.021 amplitude, then the distortion will be reported as (0.021/0.764)*100 = 2.75%

Of course, before drawing any inferences from the results, it would be necessary to test the sound card itself (a simple loop-back connector from the output socket to the input socket). And not forgetting to type the test frequency value and not rely on the sliders (which are hopelessly inaccurate for this sort of thing).

I’d expect not, but did you realize that “flat-topping”, after the sine tone has been removed looks like this:
Which is harmonically identical to this (only difference is the phase relationships of the harmonics):
and it has a spectrum that looks like this:
window-Frequency Analysis-000.png
(Well I guess that you would expect that koz, but I think it is quite surprising the first time that you see it).

I could modify the plug-in so that there is an option to produce a “notch filtered and normalised” wave output if that would be useful. (It is not possible to produce both an audio track output and a text output at the same time, although a text output could still be produced in the “debug” output.

One other thing here, is that the first bit of audio through the notch filter is rubbish because of “ringing” caused by the high Q of the notch filter (the first 20,000 samples of the selection are ignored by this plug-in and only the following 20,000 samples are measured).

Anyway, after all of that, I’d be interested to hear if the distortion figures from this plug-in bare any relation to the figures from your hardware test equipment.

Due to changes in Audacity/Nyquist, the Distortion Analyzer plug-in posted previously in this topic does not work with the current (1.3.12) version of Audacity.
Here is an updated version that supports recent versions of Audacity 1.3.x

There are a few other minor changes, such as adding a 30 second tone generator for use with the analyser, and stripping out a lot of superfluous code.
For tips on how this plug-in is designed to be used, see previous posts in this topic.
distortion-analyser.ny (1.39 KB)

Hi Steve,

I wanted to use your distortion analyzer for some measurements. but first I checked the THD output values with a professional Neutrik NTI Minilyzer, I never trust a tool before checking myself that it works correct. comparing the two results, your plugin mostly showed more distortion than my Minilyzer.

When looking at the nyquist code I quickly realized why. You are measuring peak amplitudes instead of RMS (root mean square, also known as effective value). But all audio measurement tools measure RMS or at least average and not peak. Imagine you have just 2nd and 3rd harmonic, depending on the phase to each other this could lead to different peak values whereas the RMS value will remain the same. and so does THD.

I made the necessary changes to your analyze plug-in and it shows correct values now. it uses the rms function to generate a new low frequency array (20Hz) and then reading the peak from that. theoretically the ‘orig-amp’ should be calculated via rms as well, but this will rarely have a big effect unless you have 20% or more distortion. to simplfy things I just scaled it down by 3dB.

Because it’s your plug-in, I hope it’s ok if I publish the changed version here. delete it otherwise. I updated the version number and history. greets
distortion-analyser.ny (1.55 KB)

Yes, that’s absolutely fine, and thanks for your interest and comments.

As described in the preceding text, my original Distortion Analyzer was “just a simple (very simple) tool”. There was no attempt to calibrate or provide any kind of standardised output from it. You’re quite correct that RMS measurements would be more meaningful than peak measurements. Really it should also be bandwidth limited. Perhaps it would be best to offer a choice of weightings, for example an A-weighted measurement an ITU-R 468 weighted. However, I don’t have a distortion analyzer to test the accuracy of the results. As you have access to a Neutrik NTI Minilyzer, perhaps we could work together on this?

Would this be more precise?

(/ (sqrt 2.0))

Hi Steve,

at the moment the analyzer has enough precision for me and is very usable in the current implementation.
if i have more time I’ll come back to implement some of the filter functions.

about the ‘1/(sqrt(2)’, you’re right, this is the correct value. but the difference is really small, just 0.01dB or 0.0008 in absolute value. this will have no effect in the THD percentage output at the current resolution of 0.001%. and more is probably senseless, most AD converters will have more distortion than that.

You’re quite right of course, but since you’ve drawn attention to this plug-in I’ve had a look and noticed a couple of other details that it would be nice to fix. For example, the distortion is currently being measured as: THD+N / (signal + THD+N)
As long as THD+N is pretty small then again it makes little difference to the result, but I’ll correct that in the next version.
I’m currently looking into measuring the test frequency, which if I can get it working accurately enough (without taking too long to get there) will allow the plug-in to “auto-tune” to the test frequency.

I’m revisiting this again because one of the production editors brought his beloved microphone amplifier in for repairs.


Unfortunately, the irreplaceable output transformer is leaking, hissing and crackling, but in the middle of all that, I did a frequency run and found some very surprising results (see pix).

That’s “20 to 20” being done with an iPod Touch as the signal source and the MacBook Pro as the analyzer (top trace). The far right of the trace is the 17KHz Nyquist ambiguity that people keep insisting isn’t there, and the pulses at the beginning are the countdown since you can’t hear 20 Hz.

I also wanted to do a distortion run, but less willing to drag the HP analyzer into work. Wait, didn’t I start a thread about a hundred years ago…?

Picture 3.png

So the top track is straight out of the iPod Touch into the line input of the MacBook Pro?
The lower track is out of the iPod Touch, through the pre-amp and then into the line input of the MacBook Pro?

the top track is straight out of the iPod Touch

That’s correct. I used double audio attenuators to get to -50Db without resistor leakage problems. You can do 0dB to -50dB in one shot, but you can’t do it with conventional resistors and you can’t do it flat across the spectrum.



Doesn’t look like an General Radio signal generator, does it? I like that it fits in my pocket. My breast pocket.

Beep, beep, beep, beep, Booooooooooooooooooooooooo…

We’re putting most of the noise down to an intra-layer leak in the output transformer. Because it’s a reactive component, the symptoms are very seriously magic. You find your mouth forming the words, “it can’t do that” multiple times. It’s so magic (how magic is it?) it’s so magic that several very good techies have been flummoxed (technical term).

I went the brute force route and removed the transformer from the circuit, and simulated the surrounding electronics with fixed, well-behaved components. I applied 250VDC and the transformer started frying and crackling. So there’s zero doubt what’s wrong.

Chances of actually getting useful information from a distortion test are zero, too. The THD+N reading will be all N.

We’d love to get a replacement transformer, but chances of that are zero, too. I found a very depressing web site with multiple listings for [Wanted: Gates SA-134 amplifier].

I need to start making these things.


A note. I traced it back and I think I made that sweep signal (and others) with Cool Edit 2000. Koz


I am running 2.3.3 under Win 7.

I am looking at this distortion analyzer Nyquist script (v.1.2). This is quite an old thread and I am not sure if there is another script available somewhere else.
Anyway, my LISP knowledges are pretty thin. I remember LISP from AutoCAD but I never used that language.

I have done some tests to verify how the script behaves under different conditions. You can look at the image attached.
Basically, I have used the Expression and the tone Generator to generate the different frequencies and then measure the distortion. The value in the E column comes from Wikipedia (https://en.wikipedia.org/wiki/Total_harmonic_distortion).
This is the mathematical value for the distortion of a square signal and it’s only for reference.

All in one, the results are pretty good and I have a few questions:

  • Why are the tones from the Expression generator distorted?
  • In Audacity, in the bottom left, there is a list of sample rates. Are these coming from my audio card or are these
    the ones Audacity can deal with?
  • I guess I could use this script to measure vacuum tube audio amps. Is that correct?
    Did anybody use this script for that?

Thanks for your help and thanks for this interesting script.