Recent from talks
Nothing was collected or created yet.
Goertzel algorithm
View on WikipediaThe 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
[edit]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 in a cascade of two stages with a parameter , giving the frequency to be analysed, normalised to radians per sample.
The first stage calculates an intermediate sequence, :
| 1 |
The second stage applies the following filter to , producing output sequence :
| 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 for are presumed all equal to 0. To establish the initial filter state so that evaluation can begin at sample , the filter states are assigned initial values . To avoid aliasing hazards, frequency 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 .
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 |
The Z transform of the second filter stage given in equation (2) is
| 4 |
The combined transfer function of the cascade of the two filter stages is then
| 5 |
This can be transformed back to an equivalent time-domain sequence, and the terms unrolled back to the first input term at index :[citation needed]
| 6 |
Numerical stability
[edit]It can be observed that the poles of the filter's Z transform are located at and , 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
[edit]For the important case of computing a DFT term, the following special restrictions are applied.
- The filtering terminates at index , where 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 |
- The index number indicating the "frequency bin" of the DFT is selected from the set of index numbers
| 8 |
Making these substitutions into equation (6) and observing that the term , equation (6) then takes the following form:
| 9 |
We can observe that the right side of equation (9) is extremely similar to the defining formula for DFT term , the DFT term for index number , but not exactly the same. The summation shown in equation (9) requires input terms, but only input terms are available when evaluating a DFT. A simple but inelegant expedient is to extend the input sequence with one more artificial value .[8] We can see from equation (9) that the mathematical effect on the final result is the same as removing term 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 is used in the final step,
| 10 |
Thus, the algorithm can be completed as follows:
- terminate the IIR filter after processing input term ,
- apply equation (10) to construct from the prior outputs and ,
- apply equation (2) with the calculated value and with produced by the final direct calculation of the filter.
The last two mathematical operations are simplified by combining them algebraically:
| 11 |
Note that stopping the filter updates at term 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 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 , 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
[edit]Power-spectrum terms
[edit]Examining equation (6), a final IIR filter pass to calculate term using a supplemental input value applies a complex multiplier of magnitude 1 to the previous term . Consequently, and represent equivalent signal power. It is equally valid to apply equation (11) and calculate the signal power from term or to apply equation (2) and calculate the signal power from term . Both cases lead to the following expression for the signal power represented by DFT term :
| 12 |
In the pseudocode below, the complex-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
[edit]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 , 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 |
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
[edit]This application requires the same evaluation of DFT term , as discussed in the previous section, using a real-valued or complex-valued input stream. Then the signal phase can be evaluated as
| 14 |
taking appropriate precautions for singularities, quadrant, and so forth when computing the inverse tangent function.
Complex signals in real arithmetic
[edit]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 , and over the sequence of imaginary parts, yielding . After that, the two complex-valued partial results can be recombined:
| 15 |
Computational complexity
[edit]This section needs additional citations for verification. (February 2014) |
- According to computational complexity theory, computing a set of DFT terms using applications of the Goertzel algorithm on a data set with values with a "cost per operation" of has complexity .
- To compute a single DFT bin for a complex input sequence of length , the Goertzel algorithm requires multiplications and additions/subtractions within the loop, as well as 4 multiplications and 4 final additions/subtractions, for a total of multiplications and additions/subtractions. This is repeated for each of the frequencies.
- In contrast, using an FFT on a data set with values has complexity .
- 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 multiplications and additions/subtractions per DFT bin, for each of the bins.
In the complexity order expressions, when the number of calculated terms is smaller than , the advantage of the Goertzel algorithm is clear. But because FFT code is comparatively complex, the "cost per unit of work" factor is often larger for an FFT, and the practical advantage favours the Goertzel algorithm even for several times larger than .
As a rule-of-thumb for determining whether a radix-2 FFT or a Goertzel algorithm is more efficient, adjust the number of terms in the data set upward to the nearest exact power of 2, calling this , and the Goertzel algorithm is likely to be faster if
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
[edit]- Bluestein's FFT algorithm (chirp-Z)
- Frequency-shift keying (FSK)
- Phase-shift keying (PSK)
References
[edit]- ^ Goertzel, G. (January 1958), "An Algorithm for the Evaluation of Finite Trigonometric Series", American Mathematical Monthly, 65 (1): 34–35, doi:10.2307/2310304, JSTOR 2310304
- ^ Mock, P. (March 21, 1985), "Add DTMF Generation and Decoding to DSP-μP Designs" (PDF), EDN, ISSN 0012-7515; 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 (PDF), Application Report, Texas Instruments, SPRA066
- ^ Schmer, Gunter (May 2000), DTMF Tone Generation and Detection: An Implementation Using the TMS320C54x (PDF), Application Report, Texas Instruments, SPRA096a
- ^ Cheng, Eric; Hudak, Paul (January 2009), Audio Processing and Sound Synthesis in Haskell (PDF), archived from the original (PDF) on 2017-03-28
- ^ 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. Retrieved 2014-02-03.
- ^ "Electronic Engineering Times | Connecting the Global Electronics Community". EE Times. Retrieved 2014-02-03.
- ^ Elmenreich, Wilfried (August 25, 2011). "Efficiently detecting a frequency using a Goertzel filter". Retrieved 16 September 2014.
- ^ Press; Flannery; Teukolsky; Vetterling (2007), "Chapter 12", Numerical Recipes, The Art of Scientific Computing, Cambridge University Press
Further reading
[edit]- 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
[edit]- Goertzel Algorithm at the Wayback Machine (archived 2018-06-28)
- A DSP algorithm for frequency analysis
- The Goertzel Algorithm by Kevin Banks
- Analysis of the Goertzel Algorithm by Uwe Beis in which he compares it to analog 2nd order Chebyshev low pass filter
Goertzel algorithm
View on GrokipediaIntroduction
Overview and Purpose
The Goertzel algorithm is a computational technique in digital signal processing that enables the efficient evaluation of individual coefficients in the discrete Fourier transform (DFT) of a discrete-time signal. It operates as a second-order infinite impulse response (IIR) filter tuned to a specific frequency, allowing the isolation of a single frequency bin from the input sequence without performing a full spectral analysis. Originally formulated for the evaluation of finite trigonometric series, the algorithm has become a standard method for targeted frequency detection in signal processing contexts.[2][3] The primary purpose of the Goertzel algorithm is to compute one or a small number of DFT coefficients selectively, avoiding the overhead of transforming the entire signal spectrum. This makes it particularly valuable in resource-constrained or real-time environments where only specific frequency components, such as tones in audio signals, need to be analyzed. By leveraging a recursive structure, the algorithm minimizes redundant calculations inherent in broader transforms like the DFT.[3] In operation, the algorithm takes as input a finite-length time-domain sequence for , along with a target frequency index , and outputs the corresponding complex DFT coefficient , which represents the signal's amplitude and phase at the normalized frequency . A key benefit is its linear time complexity of operations per frequency bin, significantly lower than the required by fast Fourier transform (FFT) algorithms for complete spectra, thus offering substantial savings when full computation is unnecessary.[3]Historical Background
The Goertzel algorithm was invented by Gerald Goertzel, an American theoretical physicist, in 1958 while affiliated with the Nuclear Development Corporation of America. Originally developed in the context of nuclear physics computations, the algorithm provided an efficient method for evaluating finite trigonometric series, which are fundamental in analyzing periodic phenomena. Goertzel detailed the algorithm in his seminal paper, "An Algorithm for the Evaluation of Finite Trigonometric Series," published in The American Mathematical Monthly. This two-page article outlined a recursive technique requiring approximately N multiplications and 2N additions for an N-term series, offering significant computational savings over direct summation methods prevalent at the time. The work built on the mathematical framework of the discrete Fourier transform (DFT) but optimized it for single-frequency evaluation, reflecting the practical needs of mid-20th-century scientific computing in physics. During the 1970s and 1980s, as digital signal processing (DSP) emerged with the advent of affordable digital hardware, the Goertzel algorithm saw widespread adoption in telephony and audio applications. Its efficiency for detecting specific frequencies made it ideal for dual-tone multi-frequency (DTMF) signaling in touch-tone telephones, where it enabled robust tone detection under noisy conditions. Key milestones included its integration into implementations compliant with International Telecommunication Union (ITU) recommendations, such as ITU-T Q.24 for multifrequency signaling systems, which specified performance criteria for DTMF detection that the algorithm effectively met.[5] As of 2025, the Goertzel algorithm remains relevant in resource-constrained environments like embedded systems and Internet of Things (IoT) devices, valued for its minimal memory footprint and low computational overhead compared to full DFT computations. Its core recurrence relation continues to support targeted frequency analysis in applications ranging from sensor data processing to real-time audio monitoring.[6]Mathematical Foundation
Relation to Discrete Fourier Transform
The Discrete Fourier Transform (DFT) of a finite-length sequence , where , is defined as for frequency indices .[7] This formulation requires complex multiplications to compute all coefficients, representing a significant computational burden when only a subset of the spectrum is needed.[8] The Goertzel algorithm computes an individual by recasting the DFT summation as the response of a tuned digital filter to the input sequence, leveraging the periodic nature of the complex exponential .[7] This approach exploits the trigonometric identity underlying the DFT, where can be separated into real cosine and imaginary sine components, avoiding the full matrix-vector multiplication of the standard DFT.[9] It is particularly suited for partial spectrum analysis, such as isolating a single frequency bin without evaluating the entire transform.[8] The equivalence between the Goertzel algorithm and the DFT bin is established by deriving a recursive form of the summation that preserves the exact value for finite .[7] Specifically, the algorithm evaluates the polynomial at using nested multiplication (Horner's method), which unfolds to match the DFT sum precisely.[8] This holds under the assumption of basic complex exponential properties and finite summation, ensuring the output aligns with the standard DFT for the selected .[9]Derivation of the Recurrence Relation
The derivation of the recurrence relation for the Goertzel algorithm begins with the expression for a single bin of the discrete Fourier transform (DFT), given by where is the input sequence of length , and is the frequency index of interest.[10] Using Euler's formula, with , the DFT bin can be separated into its real and imaginary parts: This form highlights the trigonometric series that the algorithm efficiently evaluates. The original algorithm by Goertzel targeted such finite trigonometric sums, and its adaptation to the complex DFT follows similarly by deriving a recursive structure.[11] To obtain the recurrence, consider the DFT as the output of a linear time-invariant filter whose impulse response is the complex exponential geometric series for . The z-transform of this finite geometric series is . Since , this simplifies, but for efficient computation, the Goertzel approach uses an infinite impulse response (IIR) approximation matched to the finite case via boundary adjustments. The denominator corresponds to a first-order recurrence, but to enable real-valued arithmetic, the poles at (conjugates on the unit circle) lead to a second-order characteristic equation .[10] The corresponding second-order linear difference equation for an auxiliary sequence is thus which arises directly from the real coefficient filter , where the numerator ensures the finite-length response matches the DFT sum. This equation is obtained by multiplying the first-order complex recurrence by the conjugate factor and combining, leveraging the identity . The trigonometric recurrence for the cosine and sine components follows from this structure, reducing complex multiplications.[8][10] The initial conditions are set as and , ensuring the recursion starts with s_0 = x{{grok:render&&&type=render_inline_citation&&&citation_id=0&&&citation_type=wikipedia}}. These zero-state initial conditions align the IIR filter response with the finite DFT sum via the numerator and final adjustment, accounting for the boundary effects in the finite series.[10] After computing the sequence up to , the DFT bin is obtained via the adjustment which incorporates the remaining phase correction from the filter's numerator to yield the exact DFT value. This step resolves the IIR approximation into the precise finite sum, confirming the recurrence's validity for single-bin computation.[10]Algorithm Implementation
Step-by-Step Procedure
To implement the Goertzel algorithm for computing a single bin of the discrete Fourier transform (DFT), begin with precomputation of the frequency-specific parameters based on the DFT length and the target bin index . Calculate the angular frequency and the recurrence coefficient .[11][4] Initialize the state variables to zero: set and . This ensures the recursion starts from a neutral state.[4] Process the input sequence iteratively for to . For each sample, compute the next state using the recurrence relation , then update the previous states by shifting: and . This loop requires careful indexing to avoid off-by-one errors, typically implemented with temporary variables for the current and prior states. After the loop, compute the final state .[11][4] After computing , extract the complex DFT coefficient from the final states and . The real part is given by , and the imaginary part by . An alternative non-complex form computes the magnitude squared directly as for applications like tone detection.[4] The following pseudocode illustrates a simple iterative implementation in a code-like structure:precompute θ = 2 * π * k / N
precompute c = 2 * cos(θ)
precompute cos_θ = cos(θ)
precompute sin_θ = sin(θ)
initialize s_prev2 = 0
initialize s_prev1 = 0
for n = 0 to N-1:
s_current = c * s_prev1 - s_prev2 + x[n]
s_prev2 = s_prev1
s_prev1 = s_current
s_N = c * s_prev1 - s_prev2
real_part = s_N - s_prev1 * cos_θ
imag_part = s_prev1 * sin_θ
precompute θ = 2 * π * k / N
precompute c = 2 * cos(θ)
precompute cos_θ = cos(θ)
precompute sin_θ = sin(θ)
initialize s_prev2 = 0
initialize s_prev1 = 0
for n = 0 to N-1:
s_current = c * s_prev1 - s_prev2 + x[n]
s_prev2 = s_prev1
s_prev1 = s_current
s_N = c * s_prev1 - s_prev2
real_part = s_N - s_prev1 * cos_θ
imag_part = s_prev1 * sin_θ
