The Goertzel algorithm is a technique in digital signal processing (DSP) for efficient evaluation of the individual terms of the discrete Fourier transform (DFT). It is useful in certain practical applications, such as recognition of dual-tone multi-frequency signaling (DTMF) tones produced by the push buttons of the keypad of a traditional analog telephone. The algorithm was first described by Gerald Goertzel in 1958.
Like the DFT, the Goertzel algorithm analyses one selectable frequency component from a discrete signal.[1] Unlike direct DFT calculations, the Goertzel algorithm applies a single real-valued coefficient at each iteration, using real-valued arithmetic for real-valued input sequences. For covering a full spectrum (except when using for continuous stream of data where coefficients are reused for subsequent calculations, which has computational complexity equivalent of sliding DFT), the Goertzel algorithm has a higher order of complexity than fast Fourier transform (FFT) algorithms, but for computing a small number of selected frequency components, it is more numerically efficient. The simple structure of the Goertzel algorithm makes it well suited to small processors and embedded applications.
The Goertzel algorithm can also be used "in reverse" as a sinusoid synthesis function, which requires only 1 multiplication and 1 subtraction per generated sample.
The main calculation in the Goertzel algorithm has the form of a digital filter, and for this reason the algorithm is often called a Goertzel filter. The filter operates on an input sequence
x[n]
\omega0
The first stage calculates an intermediate sequence,
s[n]
s[n]
y[n]
The first filter stage can be observed to be a second-order IIR filter with a direct-form structure. This particular structure has the property that its internal state variables equal the past output values from that stage. Input values
x[n]
n<0
x[0]
s[-2]=s[-1]=0
\omega0
\omega0
The second-stage filter can be observed to be a FIR filter, since its calculations do not use any of its past outputs.
Z-transform methods can be applied to study the properties of the filter cascade. The Z transform of the first filter stage given in equation (1) isThe Z transform of the second filter stage given in equation (2) isThe combined transfer function of the cascade of the two filter stages is then This can be transformed back to an equivalent time-domain sequence, and the terms unrolled back to the first input term at index
n=0
It can be observed that the poles of the filter's Z transform are located at
+j\omega0 | |
e |
-j\omega0 | |
e |
For the important case of computing a DFT term, the following special restrictions are applied.
n=N
N
k
Making these substitutions into equation (6) and observing that the term
e+j=1
We can observe that the right side of equation (9) is extremely similar to the defining formula for DFT term
X[k]
k
N+1
N
x[n]
x[N]=0
x[N]
However, there is a more elegant approach that avoids the extra filter pass. From equation (1), we can note that when the extended input term
x[N]=0
x[N-1]
s[N]
s[N-1]
s[N-2]
s[N]
s[N-1]
The last two mathematical operations are simplified by combining them algebraically:
Note that stopping the filter updates at term
N-1
The particular filtering structure chosen for the Goertzel algorithm is the key to its efficient DFT calculations. We can observe that only one output value
y[N]
s[0],s[1]
This seems to leave a paradox: to complete the algorithm, the FIR filter stage must be evaluated once using the final two outputs from the IIR filter stage, while for computational efficiency the IIR filter iteration discards its output values. This is where the properties of the direct-form filter structure are applied. The two internal state variables of the IIR filter provide the last two values of the IIR filter output, which are the terms required to evaluate the FIR filter stage.
Examining equation (6), a final IIR filter pass to calculate term
y[N]
x[N]=0
y[N-1]
y[N]
y[N-1]
y[N]
y[N-1]
X[k]
In the pseudocode below, the real-valued input data is stored in the array x
and the variables sprev
and sprev2
temporarily store output history from the IIR filter. Nterms
is the number of samples in the array, and Kterm
corresponds to the frequency of interest, multiplied by the sampling period.
Nterms defined here Kterm selected here ω = 2 × π × Kterm / Nterms; coeff := 2 × cos(ω) sprev := 0 sprev2 := 0 for each index n in range 0 to Nterms-1 do s := x[n] + coeff × sprev - sprev2 sprev2 := sprev sprev := s end power := sprev2 + sprev22 - (coeff × sprev × sprev2)
It is possible[5] to organise the computations so that incoming samples are delivered singly to a software object that maintains the filter state between updates, with the final power result accessed after the other processing is done.
The case of real-valued input data arises frequently, especially in embedded systems where the input streams result from direct measurements of physical processes. When the input data are real-valued, the filter internal state variables sprev
and sprev2
can be observed also to be real-valued, consequently, no complex arithmetic is required in the first IIR stage. Optimizing for real-valued arithmetic typically is as simple as applying appropriate real-valued data types for the variables.
After the calculations using input term
x[N-1]
Comparing to the power-spectrum application, the only difference are the calculation used to finish:
(Same IIR filter calculations as in the signal power implementation) XKreal = sprev * cr - sprev2; XKimag = sprev * ci;
This application requires the same evaluation of DFT term
X[k]
Since complex signals decompose linearly into real and imaginary parts, the Goertzel algorithm can be computed in real arithmetic separately over the sequence of real parts, yielding
yr[n]
yi[n]
M
M
N
K
O(KNM)
To compute a single DFT bin
X(f)
N
2N
4 N
2N+4
4N+4
M
N
O(KNlog2(N))
This is harder to apply directly because it depends on the FFT algorithm used, but a typical example is a radix-2 FFT, which requires
2log2(N)
3log2(N)
N
In the complexity order expressions, when the number of calculated terms
M
logN
K
M
log2(N)
As a rule-of-thumb for determining whether a radix-2 FFT or a Goertzel algorithm is more efficient, adjust the number of terms
N
N2
M\le
5N2 | |
6N |
log2(N2)
FFT implementations and processing platforms have a significant impact on the relative performance. Some FFT implementations perform internal complex-number calculations to generate coefficients on-the-fly, significantly increasing their "cost K per unit of work." FFT and DFT algorithms can use tables of pre-computed coefficient values for better numerical efficiency, but this requires more accesses to coefficient values buffered in external memory, which can lead to increased cache contention that counters some of the numerical advantage.
Both algorithms gain approximately a factor of 2 efficiency when using real-valued rather than complex-valued input data. However, these gains are natural for the Goertzel algorithm but will not be achieved for the FFT without using certain algorithm variants specialised for transforming real-valued data.