top of page
  • Writer's pictureDinesh Thogulua

THDN analysis tool for DSP systems

Updated: Oct 12, 2021

Total Harmonic Distortion + Noise, i.e., THDN, is a quality measure for both analog and digital audio systems. Most analog systems such as filters, amplifiers have a non-linear response because they are made up of components such as transistors, resistor, capacitors that themselves aren't perfectly linear. Furthermore, they are affected by thermal noise and other noise sources such as EMI, power noise. THDN tells us just how far away these systems are from ideal systems. For digital systems, THDN comes from finite register length effects, quantization operations (ex. rounding) and when the system has some non-linearity by design (some interpolation algorithm, speaker protection algorithm, compression algorithm etc.). In hardware implementations larger resolution multipliers, for example, mean larger synthesis area, and hence more static power. So finite register length (and hence, THDN) is a bigger problem in hardware implementations than software implementations

Measuring THDN

For analog systems, typically one would use a high quality sine wave generator, input the sine wave to the DUT, use a notch filter at the output to measure the power at input frequency vs. power everywhere else in the spectrum. High end audio amplifier, headphone, cable manufacturers typically chase after lower THDN or better THDN (psycho-acoustically better) as a way to make their products stand out from the competition.

For digital systems, THDN isn't as bit a concern as it is in the analog world, because digital systems don't have a lot of inherent non-linearities as analog systems. But it is a concern nevertheless. Usually a digital sinusoid is generated and fed to the DUT, and, at the output, FFT is used to separate out the signal power from the distortion and noise power to measure the THDN.

Python HTML App

I wanted to create an graphical app where the user can play with the values of different parameters associated with his/her system and look at the resulting THDN with associated graphs. I generally prefer UI apps over CLI as it makes it inviting to the user to goof around with the parameter values more than they would if it were a CLI. My personal experience is that you learn more by goofing around than learning something theoretically: You discover nuances that you never learn from theory.

The app is written in python, and it uses dash utility from the plotly library. In my code I have used the silliest of systems to demonstrate the app: A window-based FIR filter. I have provided controls for changing a bunch of parameters such as the multiplier resolution for MAC operation, number of filter taps, input sinusoid frequency and more. You can change the radio button, slider values and then hit 'Execute' button to make the changes take effect. Here is the link to my github page for the code. The app provides a skeleton: You may substitute your own (more complex) system in place of the FIR filter and also change the parameter controls as you like. Also, you can use this as a great way to learn plotly's dash utility for creating html apps using python. The code has enough comments for you to easily understand, and hence, modify. The comments along with this blog should be more than sufficient.

Some FYI points

I have restricted input resolution to typical resolutions such as 16 bits, 32 bits etc., although there is no inherent restriction: The code can handle values such as 17 bits. I just didn't see the point of including them because your inputs are often from audio sources that will not have weird resolutions like 17 bits.

I haven't restricted the output resolution (accumulator resolution), but typically you will have some restriction in the real world either due to buffers coming after the accumulator or just size restrictions (mm^2) in hardware implementations.

THD nuances

  • There is no consensus among audio professionals on how to measure THD: What frequencies to use for input sinusoids, how many harmonics to consider etc.

  • If you see spikes in the output spectrum that aren't at multiples of the input frequency, they may be frequency components higher than the sampling frequency/2 aliasing into lower frequency components

  • THDN can be measured in percentage or dB or some other relative measure. I have used dB. But this is not exactly related to (although there is a connection) to audio loudness measure of decibels.

  • Not all THDN are born equal: I will write in a different post about psycho-acoustics, but here is the gist: Human ear itself is a non-linear system with a non-monotonical frequency response. This is probably because of evolutionary reasons. So where the THDN is located in the spectrum is just as important as its magnitude.

Known issues in the app

  • Whenever numpy log function comes across zero, it triggers a warning. Just ignore them. It return -inf in those cases which cannot be displayed in the graph. You can pick a very low dB value as a substitute (as you can see in the code).

  • Depending upon the browser (since this is an html app), some controls may need scrolling to access.

  • You cannot use 'exec' method to run this code inside python command line. You have to use "python3" . It will show on the CLI the local host address with port number to use in the browser.

60 views0 comments

Recent Posts

See All



bottom of page