THD

Overview

Computes total harmonic distortion + noise

Discussion

This modules computes the THD+N (total harmonic distortion + noise) of a signal chain. It is typically used to compute THD+N through a loudspeaker to a microphones. The module has one audio input pin and one audio output pin. The audio pins are signal channel and the module supports any block size and any sample rate. The module works by generating a pure sine wave on the output pin and then analyzing the signal on the input pin. At instantiation time you specify the length of the DFT via the argument L. At run-time, the module computes a single point DFT of length L at precisely the frequency of the sine wave. This corresponds to the signal energy. The module also computes all of the residual energy that is not at the sine wave. This corresponds to the distortion + noise. With these two quantities, the module is able to compute the THD+N.

The module computes THD+N as a percentage. 2.0 corresponds to 2% THD+N. The module quantizes the sine wave frequency to the closest DFT bin. Allowable frequencies are at multiples of (sample rate) / L. For example, if you are processing at a sample rate of 48 kHz, then a reasonable choice is L=4800. Then the allowable sine waves fall on multiples of 48000/4800 = 10 Hz.

The module has an optional output pin which can be enabled as a second module argument. When enabled, this output pin will hold the computed THD.

The module allows you to specify the level of the output sine wave, in dB. The module also computes the level of the input signal, in dB. And lastly, the enable variable allows you to turn the sine wave on or off. The module takes a short time (maybe a second) to settle down after you change the frequency or sine wave level.

Type Definition

CODE
``````typedef struct _ModuleTHD
{
ModuleInstanceDescriptor instance;            // Common Audio Weaver module instance structure
INT32 L;                                      // Integration period in samples
FLOAT32 freq;                                 // Frequency at which to compute THD
FLOAT32 gain;                                 // Sine wave amplitude in dB
INT32 enable;                                 // Boolean that controls whether the module is active
FLOAT32 thd;                                  // Measured THD
FLOAT32 signalLevel;                          // Measured signal level
FLOAT32 gainLin;                              // Sine wave amplitude in linear units
INT32 count;                                  // Internal counter. Counts from 0 to L-1
FLOAT32 sumReal;                              // Accumulates the real part of the DFT calculation
FLOAT32 sumImag;                              // Accumulates the imaginary part of the DFT calculation
FLOAT32 freqInc;                              // Sample to sample frequency increment
FLOAT32 energySin;                            // Energy at the indicated sine wave fundamental frequency
FLOAT32 energyTotal;                          // Total energy in the signal
FLOAT32 energyPartial;                        // Partial energy in the signal
} ModuleTHDClass;``````

Properties

 Name Type Usage isHidden Default value Range Units L int const 0 1024 Unrestricted freq float parameter 0 984.4 46.875:46.875:23953.125 gain float parameter 0 -20 -60:1:0 dB enable int parameter 0 1 0:1 thd float state 0 0 0:10 percent signalLevel float state 0 0 -100:0 dB gainLin float derived 1 0.1 Unrestricted count int state 1 0 Unrestricted sumReal float state 1 0 Unrestricted sumImag float state 1 0 Unrestricted freqInc float derived 1 0.1289 Unrestricted radians/sample energySin float state 1 0 Unrestricted energyTotal float state 1 0 Unrestricted energyPartial float state 1 0 Unrestricted

Pins

Input Pins

Name: in

Description: Input signal

Data type: float

Channel range: 1

Block size range: Unrestricted

Sample rate range: Unrestricted

Complex support: Real

Output Pins

Name: out

Description: Output signal

Data type: float

MATLAB Usage

File Name: thd_module.m

CODE
`````` M = thd_module(NAME, L, OUTPUTVALUE)
This module measures the total harmonic distortion plus noise.  Arguments:
Arguments:
NAME - name of the module.
L - integration period (samples) over which the measurement is done
OUTPUTVALUE - optional argument which causes the measured THD to be
output on a second output pin.