thd

typhoon.test.harmonic.thd(signal, fundamental, max_harmonic, during=None, mode='multiple_harmonics_only', fundamental_tol=0.01)

Calculates signal THD

Parameters:
  • signal (Series) – A Pandas Series with the signal the THD value is to be calculated.

  • fundamental (int) – Fundamental frequency of the signal

  • max_harmonic (int) – Maximum harmonic (with respect to fundamental) to be taken into consideration in the calculation.

  • during (2-element tuple of float/int or Timedelta) – Period of signal to consider for the calculation.

  • mode (string - default is 'multiple_harmonics_only') – Selects one of three possible modes of calculation: - ‘multiple_harmonics_only’ - only harmonics which are divisible with fundamental are taken into consideration - ‘with_interharmonics’ - all harmonics bigger than fundamental are taken into consideration - ‘all_frequencies’ - all frequencies, including subharmonics are included in the calculation

  • fundamental_tol (float - default 0.01) – Tolerance to replace provided ‘fundamental’ value by the one obtained from ‘frequency_content’ (FFT) of the signal.

Returns:

thd – Calculated THD value.

Return type:

float

Note

All three calculation modes are based on fast fourier transform; this means that length of the signal which is taken into calculation should be chosen carefully, because this is the parameter which defines step between detected frequencies. consequently, if needed frequency step is 10Hz, signal should be captured(or sliced with during argument) to have length of 0.1s(1/10).

Examples

>>> from typhoon.test.signals import pandas_sine
>>> from typhoon.test.harmonic import thd
>>> # create 50 Hz sinusoidal signal - fundamental and add third harmonic
>>> serie = signals.pandas_sine(1, 50, 1, 1e-6)
>>> serie += signals.pandas_sine(0.5, 150, 1, 1e-6)
>>> # add interharmonic
>>> serie += signals.pandas_sine(1, 125, 1, 1e-6)
>>> # add two subharmonics
>>> serie += signals.pandas_sine(1, 10, 1, 1e-6)
>>> serie += signals.pandas_sine(1, 30, 1, 1e-6)
>>> # calculate thd with multiple harmonics only
>>> result = thd(serie, fundamental=50, max_harmonic=10)
>>> print(result) # prints 0.5
>>> # calculate thd with interharmonics included
>>> result = thd(serie, fundamental=50, max_harmonic=10, mode="with_interharmonics")
>>> print(result) # 1 over sqrt(2)
>>> # calculate thd with all frequencies included
>>> result = thd(serie, fundamental=50, max_harmonic=10, mode="all_frequencies")
>>> print(result) # prints 1