Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Scroll Documents: Update page title prefix

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 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 Block
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

...

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 Block
 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.
 Copyright 2018.  DSP Concepts, Inc.  All Rights Reserved.

...