Goertzel algorithm
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.[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 (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.[5]
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 operates on an input sequence [math]\displaystyle{ x[n] }[/math] in a cascade of two stages with a parameter [math]\displaystyle{ \omega_0 }[/math], giving the frequency to be analysed, normalised to radians per sample.
The first stage calculates an intermediate sequence, [math]\displaystyle{ s[n] }[/math]:
-
[math]\displaystyle{ s[n] = x[n] + 2 \cos(\omega_0) s[n-1] - s[n-2]. }[/math]
(1)
The second stage applies the following filter to [math]\displaystyle{ s[n] }[/math], producing output sequence [math]\displaystyle{ y[n] }[/math]:
-
[math]\displaystyle{ y[n] = s[n] - e^{-j \omega_0} s[n-1]. }[/math]
(2)
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 [math]\displaystyle{ x[n] }[/math] for [math]\displaystyle{ n \lt 0 }[/math] are presumed all equal to 0. To establish the initial filter state so that evaluation can begin at sample [math]\displaystyle{ x[0] }[/math], the filter states are assigned initial values [math]\displaystyle{ s[-2] = s[-1] = 0 }[/math]. To avoid aliasing hazards, frequency [math]\displaystyle{ \omega_0 }[/math] is often restricted to the range 0 to π (see Nyquist–Shannon sampling theorem); using a value outside this range is not meaningless, but is equivalent to using an aliased frequency inside this range, since the exponential function is periodic with a period of 2π in [math]\displaystyle{ \omega_0 }[/math].
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
-
[math]\displaystyle{ \begin{align} \frac{S(z)}{X(z)} &= \frac{1}{1 - 2 \cos(\omega_0) z^{-1} + z^{-2}} \\ & = \frac{1}{(1 - e^{+j \omega_0} z^{-1})(1 - e^{-j \omega_0} z^{-1})}. \end{align} }[/math]
(3)
The Z transform of the second filter stage given in equation (2) is
-
[math]\displaystyle{ \frac{Y(z)}{S(z)} = 1 - e^{-j \omega_0}z^{-1}. }[/math]
(4)
The combined transfer function of the cascade of the two filter stages is then
-
[math]\displaystyle{ \begin{align} \frac{S(z)}{X(z)} \frac{Y(z)}{S(z)} = \frac{Y(z)}{X(z)} &= \frac{(1 - e^{-j \omega_0}z^{-1})}{(1 - e^{+j \omega_0} z^{-1})(1 - e^{-j \omega_0} z^{-1})} \\ &= \frac{1}{1 - e^{+j \omega_0} z^{-1}}. \end{align} }[/math]
(5)
This can be transformed back to an equivalent time-domain sequence, and the terms unrolled back to the first input term at index [math]\displaystyle{ n = 0 }[/math]:[citation needed]
-
[math]\displaystyle{ \begin{align} y[n] &= x[n] + e^{+j \omega_0} y[n-1] \\ &= \sum_{k=-\infty}^{n}x[k] e^{+j \omega_0 (n-k)} \\ &= e^{j \omega_0 n} \sum_{k=0}^{n} x[k] e^{-j \omega_0 k} \qquad \text{since }\forall k \lt 0, x[k] = 0. \end{align} }[/math]
(6)
Numerical stability
It can be observed that the poles of the filter's Z transform are located at [math]\displaystyle{ e^{+j \omega_0} }[/math] and [math]\displaystyle{ e^{-j \omega_0} }[/math], 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 vulnerable to numerical-error accumulation when computed using low-precision arithmetic and long input sequences.[6] A numerically stable version was proposed by Christian Reinsch.[7]
DFT computations
For the important case of computing a DFT term, the following special restrictions are applied.
- The filtering terminates at index [math]\displaystyle{ n = N }[/math], where [math]\displaystyle{ N }[/math] 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
-
[math]\displaystyle{ \omega_0 = 2 \pi \frac{k}{N}. }[/math]
(7)
-
- The index number [math]\displaystyle{ k }[/math] indicating the "frequency bin" of the DFT is selected from the set of index numbers
-
[math]\displaystyle{ k \in \{ 0, 1, 2, ..., N-1 \}. }[/math]
(8)
-
Making these substitutions into equation (6) and observing that the term [math]\displaystyle{ e^{+j 2 \pi k} = 1 }[/math], equation (6) then takes the following form:
-
[math]\displaystyle{ y[N] = \sum_{n=0}^{N} x[n]e^{-j 2 \pi \frac{n k}{N}}. }[/math]
(9)
We can observe that the right side of equation (9) is extremely similar to the defining formula for DFT term [math]\displaystyle{ X[k] }[/math], the DFT term for index number [math]\displaystyle{ k }[/math], but not exactly the same. The summation shown in equation (9) requires [math]\displaystyle{ N+1 }[/math] input terms, but only [math]\displaystyle{ N }[/math] input terms are available when evaluating a DFT. A simple but inelegant expedient is to extend the input sequence [math]\displaystyle{ x[n] }[/math] with one more artificial value [math]\displaystyle{ x[N] = 0 }[/math].[8] We can see from equation (9) that the mathematical effect on the final result is the same as removing term [math]\displaystyle{ x[N] }[/math] 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 [math]\displaystyle{ x[N] = 0 }[/math] is used in the final step,
-
[math]\displaystyle{ s[N] = 2 \cos(\omega_0) s[N-1] - s[N-2]. }[/math]
(10)
Thus, the algorithm can be completed as follows:
- terminate the IIR filter after processing input term [math]\displaystyle{ x[N-1] }[/math],
- apply equation (10) to construct [math]\displaystyle{ s[N] }[/math] from the prior outputs [math]\displaystyle{ s[N-1] }[/math] and [math]\displaystyle{ s[N-2] }[/math],
- apply equation (2) with the calculated [math]\displaystyle{ s[N] }[/math] value and with [math]\displaystyle{ s[N-1] }[/math] produced by the final direct calculation of the filter.
The last two mathematical operations are simplified by combining them algebraically:
-
[math]\displaystyle{ \begin{align} y[N] & = s[N] - e^{-j 2 \pi \frac{k}{N}} s[N-1] \\ & = (2 \cos(\omega_0) s[N-1] - s[N-2]) - e^{-j 2 \pi \frac{k}{N}} s[N-1] \\ & = e^{j 2 \pi \frac{k}{N}} s[N-1] - s[N-2]. \end{align} }[/math]
(11)
Note that stopping the filter updates at term [math]\displaystyle{ N-1 }[/math] and immediately applying equation (2) rather than equation (11) misses the final filter state updates, yielding a result with incorrect phase.[9]
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 [math]\displaystyle{ y[N] }[/math] 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 [math]\displaystyle{ s[0], s[1] }[/math], 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 [math]\displaystyle{ y[N] }[/math] using a supplemental input value [math]\displaystyle{ x[N]=0 }[/math] applies a complex multiplier of magnitude 1 to the previous term [math]\displaystyle{ y[N-1] }[/math]. Consequently, [math]\displaystyle{ y[N] }[/math] and [math]\displaystyle{ y[N-1] }[/math] represent equivalent signal power. It is equally valid to apply equation (11) and calculate the signal power from term [math]\displaystyle{ y[N] }[/math] or to apply equation (2) and calculate the signal power from term [math]\displaystyle{ y[N-1] }[/math]. Both cases lead to the following expression for the signal power represented by DFT term [math]\displaystyle{ X[k] }[/math]:
-
[math]\displaystyle{ \begin{align} X[k] \, X'[k] & = y[N] \, y'[N] = y[N-1] \, y'[N-1] \\ & = s^2[N-1] + s^2[N-2] - 2 \cos \left(2 \pi \frac{k}{N} \right) \, s[N-1] \, s[N-2]. \end{align} }[/math]
(12)
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[10] 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.
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. 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 [math]\displaystyle{ x[N-1] }[/math], 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:
-
[math]\displaystyle{ \begin{align} c_r &= \cos(2 \pi \tfrac{k}{N}), \\ c_i &= \sin(2 \pi \tfrac{k}{N}), \\ y[N] &= c_r s[N-1] - s[N-2] + j c_i s[N-1]. \end{align} }[/math]
(13)
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;
Phase detection
This application requires the same evaluation of DFT term [math]\displaystyle{ X[k] }[/math], as discussed in the previous section, using a real-valued or complex-valued input stream. Then the signal phase can be evaluated as
-
[math]\displaystyle{ \phi = \tan^{-1}\frac{\Im(X[k])}{ \Re(X[k])}, }[/math]
(14)
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 can be computed in real arithmetic separately over the sequence of real parts, yielding [math]\displaystyle{ y_\text{r}[n] }[/math], and over the sequence of imaginary parts, yielding [math]\displaystyle{ y_\text{i}[n] }[/math]. After that, the two complex-valued partial results can be recombined:
-
[math]\displaystyle{ y[n] = y_\text{r}[n] + j y_\text{i}[n]. }[/math]
(15)
Computational complexity
- According to computational complexity theory, computing a set of [math]\displaystyle{ M }[/math] DFT terms using [math]\displaystyle{ M }[/math] applications of the Goertzel algorithm on a data set with [math]\displaystyle{ N }[/math] values with a "cost per operation" of [math]\displaystyle{ K }[/math] has complexity [math]\displaystyle{ O(KNM) }[/math].
- To compute a single DFT bin [math]\displaystyle{ X(f) }[/math] for a complex input sequence of length [math]\displaystyle{ N }[/math], the Goertzel algorithm requires [math]\displaystyle{ 2 N }[/math] multiplications and [math]\displaystyle{ 4\ N }[/math] additions/subtractions within the loop, as well as 4 multiplications and 4 final additions/subtractions, for a total of [math]\displaystyle{ 2 N + 4 }[/math] multiplications and [math]\displaystyle{ 4 N + 4 }[/math] additions/subtractions. This is repeated for each of the [math]\displaystyle{ M }[/math] frequencies.
- In contrast, using an FFT on a data set with [math]\displaystyle{ N }[/math] values has complexity [math]\displaystyle{ O(KN\log_2(N)) }[/math].
- 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 [math]\displaystyle{ 2 \log_2(N) }[/math] multiplications and [math]\displaystyle{ 3 \log_2(N) }[/math] additions/subtractions per DFT bin, for each of the [math]\displaystyle{ N }[/math] bins.
In the complexity order expressions, when the number of calculated terms [math]\displaystyle{ M }[/math] is smaller than [math]\displaystyle{ \log N }[/math], the advantage of the Goertzel algorithm is clear. But because FFT code is comparatively complex, the "cost per unit of work" factor [math]\displaystyle{ K }[/math] is often larger for an FFT, and the practical advantage favours the Goertzel algorithm even for [math]\displaystyle{ M }[/math] several times larger than [math]\displaystyle{ \log_2(N) }[/math].
As a rule-of-thumb for determining whether a radix-2 FFT or a Goertzel algorithm is more efficient, adjust the number of terms [math]\displaystyle{ N }[/math] in the data set upward to the nearest exact power of 2, calling this [math]\displaystyle{ N_2 }[/math], and the Goertzel algorithm is likely to be faster if
- [math]\displaystyle{ M \le \frac{5 N_2}{6 N} \log_2(N_2) }[/math]
FFT implementations and processing platforms have a significant impact on the relative performance. Some FFT implementations[11] 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[which?] specialised for transforming real-valued data.
See also
- Bluestein's FFT algorithm (chirp-Z)
- Frequency-shift keying (FSK)
- Phase-shift keying (PSK)
References
- ↑ Goertzel, G. (January 1958), "An Algorithm for the Evaluation of Finite Trigonometric Series", American Mathematical Monthly 65 (1): 34–35, doi:10.2307/2310304
- ↑ Mock, P. (March 21, 1985), "Add DTMF Generation and Decoding to DSP-μP Designs", EDN, ISSN 0012-7515, http://focus.ti.com/lit/an/spra168/spra168.pdf; also found in DSP Applications with the TMS320 Family, Vol. 1, Texas Instruments, 1989.
- ↑ Chen, Chiouguey J. (June 1996), Modified Goertzel Algorithm in DTMF Detection Using the TMS320C80 DSP, Application Report, Texas Instruments, SPRA066, http://focus.ti.com/lit/an/spra066/spra066.pdf
- ↑ Schmer, Gunter (May 2000), DTMF Tone Generation and Detection: An Implementation Using the TMS320C54x, Application Report, Texas Instruments, SPRA096a, http://focus.ti.com/lit/an/spra096a/spra096a.pdf
- ↑ Cheng, Eric; Hudak, Paul (January 2009), Audio Processing and Sound Synthesis in Haskell, http://haskell.cs.yale.edu/wp-content/uploads/2011/01/AudioProc-TR.pdf
- ↑ Gentleman, W. M. (1 February 1969). "An error analysis of Goertzel's (Watt's) method for computing Fourier coefficients". The Computer Journal 12 (2): 160–164. doi:10.1093/comjnl/12.2.160.
- ↑ Stoer, J.; Bulirsch, R. (2002), Introduction to Numerical Analysis, Springer, ISBN 9780387954523
- ↑ "Goertzel's Algorithm". Cnx.org. 2006-09-12. http://cnx.org/content/m12024/latest/.
- ↑ "Electronic Engineering Times | Connecting the Global Electronics Community". EE Times. http://www.eetimes.com/design/signal-processing-dsp/4024443/The-Goertzel-Algorithm.
- ↑ Elmenreich, Wilfried (August 25, 2011). "Efficiently detecting a frequency using a Goertzel filter". http://netwerkt.wordpress.com/2011/08/25/goertzel-filter/.
- ↑ Press; Flannery; Teukolsky; Vetterling (2007), "Chapter 12", Numerical Recipes, The Art of Scientific Computing, Cambridge University Press
Further reading
- Proakis, J. G.; Manolakis, D. G. (1996), Digital Signal Processing: Principles, Algorithms, and Applications, Upper Saddle River, NJ: Prentice Hall, pp. 480–481, Bibcode: 1996dspp.book.....P
External links
Original source: https://en.wikipedia.org/wiki/Goertzel algorithm.
Read more |