Goertzel

This library implements the goertzel algorithm to calculate phase and power in an efficient way for a single frequency of interest

goertzel_coefficients takes an 1-dimensional array of data and returns a complex number for the given target frequency. Use calc_power and calc_phase to transform this complex number into angle in radians or amplitude. You can get the data from the ringbuffer struct by direct access.

Because the target phase virtually never can be met exactly, use phase_to_trigger, giving it the current phase and the phase from the last time you calculated it.

Defines

PI
TAUPI

Functions

double double_modulo(double n, double m)

takes the modulo of a value.

Parameters
  • n – the starting value

  • m – the divisor

Returns

b the remainder

double complex goertzel_coefficients (double *ringbuffer_data, int ringbuffer_len, int target_frequency, int sampling_rate)

calculates the Goertzel transformation

Parameters
  • ringbuffer_data – a pointer to the data array in the ringbuffer

  • ringbuffer_len – how many past samples to use for the algorithm

  • target_frequency – for which frequency to calculate the transformation

  • sampling_rate – the sampling rate of the data in the ringbuffer

Returns

a complex value calculated for the target_frequency

double calc_power (double complex z)

calculates the power from a precomputed complex number

double calc_phase (double complex z)

calculates the phase from a precomputed complex number

int phase_to_trigger(double current_phase, double last_phase, double target_phase)

decides if the target phase is met This function will decide whether the phase is met by a) calculation whether the target_phase has passed since the last calculation (i.e. lies between last_phase and current_phase) b) predict whether you will pass it with the next calculation, which is sensible only if you have a stable time between iterative calculations, as e.g. running this function in the main loop of LuckyApp.

Parameters
  • current_phase – the current phase in radians

  • last_phase – the phase from the last calculation in radians

  • target_phase – the target phase in radians

Returns

:

  • 0 for false (will not reach target phase)

  • 1 for true (target phase will be reached in next step)