Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Goertzel algorithm

From Wikipedia, the free encyclopedia
Technique in digital signal processing

TheGoertzel algorithm is a technique indigital signal processing (DSP) for efficient evaluation of the individual terms of thediscrete Fourier transform (DFT). It is useful in certain practical applications, such as recognition ofdual-tone multi-frequency signaling (DTMF) tones produced by the push buttons of the keypad of a traditional analogtelephone. The algorithm was first described byGerald Goertzel in 1958.[1]

Like the DFT, the Goertzel algorithm analyses one selectable frequency component from adiscrete signal.[2][3][4] Unlike direct DFT calculations, the Goertzel algorithm applies a singlereal-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 ofsliding DFT), the Goertzel algorithm has ahigher order of complexity thanfast 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 adigital filter, and for this reason the algorithm is often called aGoertzel filter. The filter operates on an input sequencex[n]{\displaystyle x[n]} in a cascade of two stages with a parameterω0{\displaystyle \omega _{0}}, giving the frequency to be analysed, normalised toradians per sample.

The first stage calculates an intermediate sequence,s[n]{\displaystyle s[n]}:

s[n]=x[n]+2cos(ω0)s[n1]s[n2].{\displaystyle s[n]=x[n]+2\cos(\omega _{0})s[n-1]-s[n-2].}1

The second stage applies the following filter tos[n]{\displaystyle s[n]}, producing output sequencey[n]{\displaystyle y[n]}:

y[n]=s[n]ejω0s[n1].{\displaystyle y[n]=s[n]-e^{-j\omega _{0}}s[n-1].}2

The first filter stage can be observed to be a second-orderIIR filter with adirect-form structure. This particular structure has the property that its internal state variables equal the past output values from that stage. Input valuesx[n]{\displaystyle x[n]} forn<0{\displaystyle n<0} are presumed all equal to 0. To establish the initial filter state so that evaluation can begin at samplex[0]{\displaystyle x[0]}, the filter states are assigned initial valuess[2]=s[1]=0{\displaystyle s[-2]=s[-1]=0}. To avoidaliasing hazards, frequencyω0{\displaystyle \omega _{0}} is often restricted to the range 0 to π (seeNyquist–Shannon sampling theorem); using a value outside this range is not meaningless, but is equivalent to using an aliased frequency inside this range, since theexponential function is periodic with a period of 2π inω0{\displaystyle \omega _{0}}.

The second-stage filter can be observed to be aFIR 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

S(z)X(z)=112cos(ω0)z1+z2=1(1e+jω0z1)(1ejω0z1).{\displaystyle {\begin{aligned}{\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{aligned}}}3

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

Y(z)S(z)=1ejω0z1.{\displaystyle {\frac {Y(z)}{S(z)}}=1-e^{-j\omega _{0}}z^{-1}.}4

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

S(z)X(z)Y(z)S(z)=Y(z)X(z)=(1ejω0z1)(1e+jω0z1)(1ejω0z1)=11e+jω0z1.{\displaystyle {\begin{aligned}{\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{aligned}}}5

This can be transformed back to an equivalent time-domain sequence, and the terms unrolled back to the first input term at indexn=0{\displaystyle n=0}:[citation needed]

y[n]=x[n]+e+jω0y[n1]=k=nx[k]e+jω0(nk)=ejω0nk=0nx[k]ejω0ksince k<0,x[k]=0.{\displaystyle {\begin{aligned}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<0,x[k]=0.\end{aligned}}}6

Numerical stability

[edit]

It can be observed that thepoles of the filter'sZ transform are located ate+jω0{\displaystyle e^{+j\omega _{0}}} andejω0{\displaystyle e^{-j\omega _{0}}}, on a circle of unit radius centered on the origin of the complex Z-transform plane. This property indicates that the filter process ismarginally stable and vulnerable tonumerical-error accumulation when computed using low-precision arithmetic and long input sequences.[6] A numerically stable version was proposed byChristian Reinsch.[7]

DFT computations

[edit]

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

  • The filtering terminates at indexn=N{\displaystyle n=N}, whereN{\displaystyle 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
ω0=2πkN.{\displaystyle \omega _{0}=2\pi {\frac {k}{N}}.}7
  • The index numberk{\displaystyle k} indicating the "frequency bin" of the DFT is selected from the set of index numbers
k{0,1,2,...,N1}.{\displaystyle k\in \{0,1,2,...,N-1\}.}8

Making these substitutions into equation (6) and observing that the terme+j2πk=1{\displaystyle e^{+j2\pi k}=1}, equation (6) then takes the following form:

y[N]=n=0Nx[n]ej2πnkN.{\displaystyle y[N]=\sum _{n=0}^{N}x[n]e^{-j2\pi {\frac {nk}{N}}}.}9

We can observe that the right side of equation (9) is extremely similar to the defining formula for DFT termX[k]{\displaystyle X[k]}, the DFT term for index numberk{\displaystyle k}, but not exactly the same. The summation shown in equation (9) requiresN+1{\displaystyle N+1} input terms, but onlyN{\displaystyle N} input terms are available when evaluating a DFT. A simple but inelegant expedient is to extend the input sequencex[n]{\displaystyle x[n]} with one more artificial valuex[N]=0{\displaystyle x[N]=0}.[8] We can see from equation (9) that the mathematical effect on the final result is the same as removing termx[N]{\displaystyle 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 termx[N]=0{\displaystyle x[N]=0} is used in the final step,

s[N]=2cos(ω0)s[N1]s[N2].{\displaystyle s[N]=2\cos(\omega _{0})s[N-1]-s[N-2].}10

Thus, the algorithm can be completed as follows:

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

y[N]=s[N]ej2πkNs[N1]=(2cos(ω0)s[N1]s[N2])ej2πkNs[N1]=ej2πkNs[N1]s[N2].{\displaystyle {\begin{aligned}y[N]&=s[N]-e^{-j2\pi {\frac {k}{N}}}s[N-1]\\&=(2\cos(\omega _{0})s[N-1]-s[N-2])-e^{-j2\pi {\frac {k}{N}}}s[N-1]\\&=e^{j2\pi {\frac {k}{N}}}s[N-1]-s[N-2].\end{aligned}}}11

Note that stopping the filter updates at termN1{\displaystyle N-1} 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 valuey[N]{\displaystyle 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 calculationss[0],s[1]{\displaystyle 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

[edit]

Power-spectrum terms

[edit]

Examining equation (6), a final IIR filter pass to calculate termy[N]{\displaystyle y[N]} using a supplemental input valuex[N]=0{\displaystyle x[N]=0} applies a complex multiplier of magnitude 1 to the previous termy[N1]{\displaystyle y[N-1]}. Consequently,y[N]{\displaystyle y[N]} andy[N1]{\displaystyle y[N-1]} represent equivalent signal power. It is equally valid to apply equation (11) and calculate the signal power from termy[N]{\displaystyle y[N]} or to apply equation (2) and calculate the signal power from termy[N1]{\displaystyle y[N-1]}. Both cases lead to the following expression for the signal power represented by DFT termX[k]{\displaystyle X[k]}:

X[k]X[k]=y[N]y[N]=y[N1]y[N1]=s2[N1]+s2[N2]2cos(2πkN)s[N1]s[N2].{\displaystyle {\begin{aligned}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{aligned}}}12

In thepseudocode below, the complex-valued input data is stored in thearrayx and the variablessprev andsprev2 temporarily store output history from the IIR filter.Nterms is the number of samples in the array, andKterm corresponds to the frequency of interest, multiplied by the sampling period.

Nterms defined hereKterm selected hereω = 2 × π × Kterm / Nterms;coeff := 2 × cos(ω)sprev := 0sprev2 := 0for each indexn in range 0 to Nterms-1do    s := x[n] + coeff × sprev - sprev2    sprev2 := sprev    sprev := sendpower := sprev2 + sprev22 - (coeff × sprev × sprev2)

It is possible[10] to organise the computations so that incoming samples are delivered singly to asoftware 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 variablessprev andsprev2 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 termx[N1]{\displaystyle 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:

cr=cos(2πkN),ci=sin(2πkN),y[N]=crs[N1]s[N2]+jcis[N1].{\displaystyle {\begin{aligned}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]+jc_{i}s[N-1].\end{aligned}}}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 termX[k]{\displaystyle 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

ϕ=tan1(X[k])(X[k]),{\displaystyle \phi =\tan ^{-1}{\frac {\Im (X[k])}{\Re (X[k])}},}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, yieldingyr[n]{\displaystyle y_{\text{r}}[n]}, and over the sequence of imaginary parts, yieldingyi[n]{\displaystyle y_{\text{i}}[n]}. After that, the two complex-valued partial results can be recombined:

y[n]=yr[n]+jyi[n].{\displaystyle y[n]=y_{\text{r}}[n]+jy_{\text{i}}[n].}15

Computational complexity

[edit]
icon
This sectionneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources in this section. Unsourced material may be challenged and removed.(February 2014) (Learn how and when to remove this message)
To compute a singleDFT binX(f){\displaystyle X(f)} for a complex input sequence of lengthN{\displaystyle N}, the Goertzel algorithm requires2N{\displaystyle 2N} multiplications and4 N{\displaystyle 4\ N} additions/subtractions within the loop, as well as 4 multiplications and 4 final additions/subtractions, for a total of2N+4{\displaystyle 2N+4} multiplications and4N+4{\displaystyle 4N+4} additions/subtractions. This is repeated for each of theM{\displaystyle M} frequencies.
This is harder to apply directly because it depends on the FFT algorithm used, but a typical example is a radix-2 FFT, which requires2log2(N){\displaystyle 2\log _{2}(N)} multiplications and3log2(N){\displaystyle 3\log _{2}(N)} additions/subtractions perDFT bin, for each of theN{\displaystyle N} bins.

In the complexity order expressions, when the number of calculated termsM{\displaystyle M} is smaller thanlogN{\displaystyle \log N}, the advantage of the Goertzel algorithm is clear. But because FFT code is comparatively complex,the "cost per unit of work" factorK{\displaystyle K} is often larger for an FFT, and the practical advantage favours the Goertzel algorithm even forM{\displaystyle M} several times larger thanlog2(N){\displaystyle \log _{2}(N)}.

As a rule-of-thumb for determining whether a radix-2 FFT or a Goertzel algorithm is more efficient, adjust the number of termsN{\displaystyle N} in the data set upward to the nearest exact power of 2, calling thisN2{\displaystyle N_{2}}, and the Goertzel algorithm is likely to be faster if

M5N26Nlog2(N2){\displaystyle M\leq {\frac {5N_{2}}{6N}}\log _{2}(N_{2})}

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 fortransforming real-valued data.

See also

[edit]

References

[edit]
  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,JSTOR 2310304
  2. ^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.
  3. ^Chen, Chiouguey J. (June 1996),Modified Goertzel Algorithm in DTMF Detection Using the TMS320C80 DSP(PDF), Application Report, Texas Instruments, SPRA066
  4. ^Schmer, Gunter (May 2000),DTMF Tone Generation and Detection: An Implementation Using the TMS320C54x(PDF), Application Report, Texas Instruments, SPRA096a
  5. ^Cheng, Eric; Hudak, Paul (January 2009),Audio Processing and Sound Synthesis in Haskell(PDF), archived fromthe original(PDF) on 2017-03-28
  6. ^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.
  7. ^Stoer, J.; Bulirsch, R. (2002),Introduction to Numerical Analysis, Springer,ISBN 9780387954523
  8. ^"Goertzel's Algorithm". Cnx.org. 2006-09-12. Retrieved2014-02-03.
  9. ^"Electronic Engineering Times | Connecting the Global Electronics Community". EE Times. Retrieved2014-02-03.
  10. ^Elmenreich, Wilfried (August 25, 2011)."Efficiently detecting a frequency using a Goertzel filter". Retrieved16 September 2014.
  11. ^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]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Goertzel_algorithm&oldid=1320745086"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp