# User:ParaTechNoid/sandbox

The Goertzel algorithm is a digital signal processing (DSP) technique that provides a means for efficient evaluation of individual terms of the Discrete Fourier Transform (DFT), thus making it useful in certain practical applications, such as recognition of DTMF tones produced by the buttons pushed on a telephone keypad. The algorithm was first described by Gerald Goertzel in 1958[1].

Like the DFT, the Goertzel Algorithm analyses one selectable frequency component from a Discrete signal [2][3][4]. 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, 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, though not limited to these.

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.[5][6]

## The algorithm

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 has a cascade of two stages. The first stage calculates an intermediate sequence, $s(n)$, given an input sequence, $x(n)$:

(1) $s(n) = x(n) + 2 \cos(2 \pi \omega) s(n-1) - s(n-2)$

The output sequence $s(n)$ from the first filter, is then applied to the following filter to produce output sequence $y(n)$.

(2) $y(n) = s(n) - e^{-2 \pi i \omega} s(n-1)$

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)$ for $n<0$ are presumed all equal to 0. To establish the initial filter state so that evaluation can begin at sample $x(0)$, the filter states are assigned initial values $s(-2) = s(-1) = 0$. The $\omega$ parameter specifies the normalised frequency to be analysed, given in cycles per sample. To avoid aliasing hazards, frequency $\omega$ is often restricted to the range 0 to 1/2, but the results are mathematically valid outside of this range.

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) is:

(3) $\frac{S(z)}{X(z)} = \frac{1}{1 - 2 \cos(2 \pi \omega) z^{-1} + z^{-2}} = \frac{1}{(1 - e^{+2 \pi i \omega} z^{-1})(1 - e^{-2 \pi i \omega} z^{-1})}.$

The Z transform of the second filter stage given in equation (2) is:

(4) $\frac{Y(z)}{S(z)} = 1 - e^{-2 \pi i \omega}z^{-1}$

The combined transfer function of the cascade of the two filter stages is then

(5) $\frac{S(z)}{X(z)} \frac{Y(z)}{S(z)} = \frac{Y(z)}{X(z)} = \frac{(1 - e^{-2 \pi i \omega}z^{-1})}{(1 - e^{+2 \pi i \omega} z^{-1})(1 - e^{-2 \pi i \omega} z^{-1})} = \frac{1}{1 - e^{+2 \pi i \omega} z^{-1}}.$

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$.

(6) \begin{align} y(n) & = x(n) + e^{+2 \pi i \omega} y(n-1) \\ & = \sum_{k=-\infty}^{n}x(k) e^{+2 \pi i \omega (n-k)} \\ & = e^{+2 \pi i \omega n} \sum_{k=0}^{n} x(k) e^{-2 \pi i \omega k} \end{align}

It can be observed that the poles of the filter's Z transform are located at $e^{+2 \pi i \omega}$ and $e^{-2 \pi i \omega}$, on a circle of unit radius centered on the origin of the complex Z transform plane. This property indicates that the filter process is marginally stable and potentially vulnerable to numerical error accumulation when computed using low-precision arithmetic and long input sequences.

## DFT computations

For the important case of computing a DFT term, the following special restrictions are applied.

• the filtering terminates at index $n=N$ where $N$ is the number of terms in the input sequence of the DFT
• the frequencies chosen for the Goertzel analysis are restricted to the special form
(7) $\omega = \frac{K}{N}$
• the index number $K$ indicating the "frequency bin" of the DFT is selected from the set of index numbers
(8) $K \epsilon \{ 0, 1, 2, ..., N-1 \}$

Making these substitutions into equation (6), and observing that the term $e^{-2 \pi i K} = 1.0$ , equation (6) then takes the following form.

(9) $y(N) = \sum_{k=0}^{N} x(k)e^{-2 \pi i \frac{k K}{N}}$

We can observe that the right side of equation (9) is extremely similar to the defining formula for DFT term $X(K)$, the DFT term for index number K, but not exactly the same. The summation shown in equation (9) requires N+1 input terms, but only N input terms are available when evaluating a DFT. A simple but inelegant expedient is to extend the input sequence $x$ with one more artificial value $x(N)=0$ [7]. We can see from equation (9), the mathematical effect on the final result is the same as removing term $x(N)$ from the summation, thus delivering the intended DFT value.

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$ is used in the final step,

(10) $s(N) = 2 \cos(2 \pi \omega) S(N-1) - s(N-2)$

Thus, the algorithm can be completed as follows:

• terminate the IIR filter after processing input term $x(N-1)$
• apply equation (10) to construct $s(N)$ from the prior outputs $s(N-1)$ and $s(N-2)$
• apply equation (2) with the calculated $s(N)$ value, and with $s(N-1)$ produced by the final direct calculation of the filter.

The last two mathematical operations are simplified by combining them algebraically.

(11) \begin{align} y(N)\quad & = s(N) - e^{-2 \pi i \frac{K}{N}} s(N-1)\quad \\ & = (2 \cos(2 \pi \omega) s(N-1) - s(N-2)) - e^{-2 \pi i \frac{K}{N}} s(N-1) \\ & = e^{2 \pi i \frac{K}{N}} s(N-1) - s(N-2) \end{align}

Note that stopping the filter updates at term $N-1$ and immediately applying equation (2) rather than equation (11) misses the final filter state updates, yielding a result with incorrect phase [8].

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)$ is used for calculating the DFT, so calculations for all the other output terms are omitted. Since the FIR filter is not calculated, the IIR stage calculations $s(0), s(1),$ etc. can be discarded immediately after updating the first stage's internal state.

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.

## Applications

### Power spectrum terms

Examining equation (6), a final IIR filter pass to calculate term $s(N)$ using a supplemental input value $x(N)=0$ applies a complex multiplier of magnitude 1.0 to the previous term $s(N-1)$. Consequently, $s(N)$ and $s(N-1)$ represent equivalent signal power. It is equally valid to apply equation equation (11) and calculate the signal power from term $y(N)$, or to apply equation (2) and calculate the signal power from term $y(N-1)$. Both cases lead to the following expression for the signal power represented by DFT term X(K).

(12) \begin{align} X(K) X'(K) & = y(N)\ y'(N) = y(N-1)\ y'(N-1) \\ & = s(N-1)^2 + s(N-2)^2 - 2 cos(2 \pi \frac{K}{N})\ s(N-1)\ s(N-2) \end{align}

In the implementation illustrated below, the variables $sprev$ and $sprev2$ are used for the IIR filter state values that preserve output history.

Nterms defined here
Kterm selected here
omega = 2 * PI * Kterm / Nterms;
wr = cos(omega);
wi = sin(omega);
coeff = 2 * wr;

sprev = 0;
sprev2 = 0;
for each index k in range 0 to Nterms-1
s = x[k] + coeff * sprev - sprev2;
sprev2 = sprev;
sprev = s;
end

power = sprev2*sprev2 + sprev*sprev - coeff*sprev*sprev2 ;


This implementation presumes that the complete input data sequence $x$ is provided as an array. It is also possible to organise the computations so that incoming samples are delivered singly, in real-time, to a software object that maintains the filter state between updates, with the final power result accessed after the other processing is done.

### Single DFT term with real-valued arithmetic

The case of real-valued input data arises frequently, especially in embedded systems where the input streams result from direct measurements of physical processes. Comparing to the implementation shown in the previous section, 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)$, and filter iterations are terminated, equation (11) must be applied to evaluate the DFT term. The final calculation uses complex-valued arithmetic, but this can be converted into real-valued arithmetic by separating real and imaginary terms.

(13) \begin{align} Wr & = cos(2 \pi \frac{K}{N}) \\ Wi & = sin(2 \pi \frac{K}{N}) \\ y(N) & = (Wr\ s(N-1) - s(N-2)) + i (Wi\ s(N-1)) \end{align}

Comparing to the power spectrum application, the only difference is the calculation used to finish.

(Same IIR filter calculations as in the signal power implementation)
XKreal = sprev * wr - sprev2;
XKimag = sprev * wi;


### Phase detection

This application requires the same evaluation of DFT term $X(K)$, as discussed in the previous section, using a real-valued or complex-valued input stream. Then the signal phase can be evaluated as:

(14) $phase = tan^{-1}(imaginary(X(K)) / (real(X(K))$

taking appropriate precautions for singularities, quadrant, and so forth when computing the inverse tangent function.

### Complex signals in real arithmetic

Since complex signals decompose linearly into real and imaginary parts, the Goertzel algorithm in real-valued arithmetic can be applied separately to the sequence of real parts, yielding $yr(n)$, and to the sequence of imaginary parts, yielding $yi(n)$, both computed in real arithmetic. After that, the two complex-valued partial results can be recombined:

(15) $y(n) = yr(n) + i\ yi(n)$

## Computational complexity

According to Computational complexity theory, computing a set of $M$ DFT terms using $M$ applications of the Goertzel algorithm on a data set with $N$ values with a "cost per operation" of $K$ has a complexity order

$K\ N\ M$
• To compute a single DFT bin $X(\omega)$ for a complex input sequence of length $N$, the Goertzel algorithm requires $2 N$ multiplications and $4\ N$ additions/subtractions within the loop, as well as 4 multiplications and 4 final additions/subtractions, for a total of $2 N\ + 4$ multiplications and $4 N\ + 4$ additions/subtractions. This is repeated for each of the $M$ frequencies.

In contrast, using an FFT on a data set with $N$ values has a complexity order

$K\ N\ log(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 $2 log_2(N)$ multiplications and $3 log_2(N)$ additions/subtractions per DFT bin, for each of the $N$ bins.

In the complexity order expressions, when the number of calculated terms $M$ is smaller than $log(N)$, the advantage of the Goertzel algorithm is clear. But because FFT code is comparatively very complex, the "cost per unit of work" factor $K$ is often much larger for an FFT, and the practical advantage favours the Goertzel algorithm more than the complexity expressions indicate.

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 in the data set upward to the nearest exact power of 2, calling this $N_2$, and the Goertzel algorithm is likely to be faster if

$M <= \frac{5 N_2}{6 N} \log_2 N_2$

FFT implementations and processing platforms have a significant impact on the relative performance. Some FFT implementations[9] 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.

## References

1. ^ Goertzel, G. (January 1958), "An Algorithm for the Evaluation of Finite Trigonometric Series", American Mathematical Monthly 65 (1): 34–35, doi:10.2307/2310304
2. ^
3. ^
4. ^
5. ^ http://www.dattalo.com/technical/theory/sinewave.html
9. ^ Press; Flannery (2007), "Chapter 12", Numerical Recipes, The Art of Scientific Computing, Cambridge University Press Unknown parameter |Last3= ignored (|last3= suggested) (help); Unknown parameter |Last4= ignored (|last4= suggested) (help)