calculateTHD function

Requirements.ChecksInFixedWindow_withFFT.Internal.calculateTHD  · source

Icon of calculateTHD

Compute Total Harmonic Distortion from amplitude of FFT

Extends Modelica.Icons.Function

Computes the total harmonic distortion of the amplitude spectrum A_buf with base frequency at index iBase:

thd = sqrt(A_2^2 + A_3^2 + ... + A_n^2) / A_buf[iBase],   n = n_harmonics

A_i is the largest amplitude in a small window around the frequency index of the i-th harmonic, 1 + i*(iBase-1), of half width min(div(i,2), max(1,div(iBase,4))), which tolerates a base frequency that is not exactly on the frequency grid. The window is cut at both ends of A_buf; a harmonic whose window lies completely beyond the end of A_buf is ignored (it is outside the computed frequency band). If A_buf[iBase] = 0, thd is Modelica.Constants.inf if a harmonic is present and 0 otherwise. Used by checkTHD for block MaxTotalHarmonicDistortion.

Revisions

DateDescription
Nov. 29, 2015Initial version implemented by Martin R. Kuhn and Martin Otter (DLR Institute of System Dynamics and Control)
The research leading to these results has received funding from the European Union’s Seventh Framework Programme (FP7/2007-2016) for the Clean Sky Joint Technology Initiative under grant agreement no. CSJU-GAM-SGO-2008-001.
Dec. 3, 2026Search window of a harmonic limited to 1 .. size(A_buf,1) and a harmonic beyond the end of A_buf ignored (before, max was taken over an empty array); guarded against A_buf[iBase] = 0 (H. Tummescheit, Model Based Innovation LLC)

Inputs

NameTypeDescription
iBase inputIntegerA_buf[iBase] is computed base frequency
n_harmonics inputIntegerNumber of harmonics to take into account
A_buf[…] inputRealAmplitudes (A_buf[i] at frequency (i-1)*f_resolution)

Outputs

NameTypeDescription
thd outputRealTotal harmonic distortion