Chapter 17 Chapter 17 Goertzel Algorithm Goertzel Algorithm
Dec 10, 2015
Chapter 17Chapter 17
Goertzel AlgorithmGoertzel Algorithm
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 2
Learning ObjectivesLearning Objectives
Introduction to DTMF signaling and Introduction to DTMF signaling and tone generation.tone generation.
DTMF tone detection techniques and DTMF tone detection techniques and the Goertzel algorithm.the Goertzel algorithm.
Implementation of the Goertzel Implementation of the Goertzel algorithm for tone detection in both algorithm for tone detection in both fixed and floating point.fixed and floating point.
Hand optimisation of assembly code.Hand optimisation of assembly code.
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 3
IntroductionIntroduction
The Goertzel algorithm is mainly used to detect tones for Dual Tone Multi-Frequency The Goertzel algorithm is mainly used to detect tones for Dual Tone Multi-Frequency (DTMF) applications.(DTMF) applications.
DTMF is predominately used for push-button digital telephone sets which are an DTMF is predominately used for push-button digital telephone sets which are an alternative to rotary telephone sets.alternative to rotary telephone sets.
DTMF has now been extended to electronic mail and telephone banking systems in which DTMF has now been extended to electronic mail and telephone banking systems in which users select options from a menu by sending DTMF signals from a telephone.users select options from a menu by sending DTMF signals from a telephone.
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 4
In a DTMF signaling system a combination of two frequency tones represents a specific digit, character (A, In a DTMF signaling system a combination of two frequency tones represents a specific digit, character (A, B, C or D) or symbol (* or #).B, C or D) or symbol (* or #).
Two types of signal processing are involved:Two types of signal processing are involved: Coding or generation.Coding or generation. Decoding or detection.Decoding or detection.
For coding, two sinusoidal sequences of finite length are added in order to represent a digit, character or For coding, two sinusoidal sequences of finite length are added in order to represent a digit, character or symbol as shown in the following example.symbol as shown in the following example.
DTMF SignalingDTMF Signaling
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 5
DTMF Tone GenerationDTMF Tone Generation
Example: Button 5 results in a 770Hz and a Example: Button 5 results in a 770Hz and a 1336Hz tone being generated simultaneously.1336Hz tone being generated simultaneously.
11 22 33
665544
77 88 99
##00**
AA
BB
CC
DD
1209Hz1209Hz 1336Hz1336Hz 1477Hz1477Hz 1633Hz1633Hz
697Hz697Hz
770Hz770Hz
852Hz852Hz
941Hz941Hz
11 22 33
665544
77 88 99
##00**
AA
BB
CC
DD
13361336770770770770770770770770 133613361336133613361336
Freq (Hz)Freq (Hz)
Ou
tput
Ou
tput
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 6
DTMF Tone GenerationDTMF Tone Generation
Click on keypad to generate the sound.Click on keypad to generate the sound.
11 22 33
665544
77 88 99
##00**
AA
BB
CC
DD
1209Hz1209Hz 1336Hz1336Hz 1477Hz1477Hz 1633Hz1633Hz
697Hz697Hz
770Hz770Hz
852Hz852Hz
941Hz941Hz
11 22 33
665544
77 88 99
##00**
AA
BB
CC
DD
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 7
Detection of tones can be achieved by using a bank of filters or using the Discrete Fourier Transform Detection of tones can be achieved by using a bank of filters or using the Discrete Fourier Transform (DFT or FFT).(DFT or FFT).
However, the Goertzel algorithm is more efficient for this application.However, the Goertzel algorithm is more efficient for this application. The Goertzel algorithm is derived from the DFT and exploits the periodicity of the phase factor, exp(-The Goertzel algorithm is derived from the DFT and exploits the periodicity of the phase factor, exp(-
j*2j*2k/N) to reduce the computational complexity associated with the DFT, as the FFT does.k/N) to reduce the computational complexity associated with the DFT, as the FFT does. With the Goertzel algorithm only 16 samples of the DFT are required for the 16 tones (With the Goertzel algorithm only 16 samples of the DFT are required for the 16 tones (\Links\\Links\GoertzelGoertzel
Theory.pdfTheory.pdf).).
DTMF Tone DetectionDTMF Tone Detection
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 8
To implement the Goertzel algorithm the To implement the Goertzel algorithm the following equations are required: following equations are required:
Goertzel Algorithm ImplementationGoertzel Algorithm Implementation
21
2cos2
nnn QQN
knxQ
12
cos2122
NQNQN
kNQNQNyk
These equations lead to the following These equations lead to the following structure: structure:
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 9
Goertzel Algorithm ImplementationGoertzel Algorithm Implementation
++
+
-1
1z
1z
)( ny)( nx
kN
je
2
kN
2cos2
2nQ
nQ
1nQ
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 10
Finally we need to calculate the constant, k.Finally we need to calculate the constant, k. The value of this constant determines the The value of this constant determines the
tone we are trying to detect and is given by:tone we are trying to detect and is given by:
Goertzel Algorithm ImplementationGoertzel Algorithm Implementation
s
tone
f
fNk
Where:Where: fftonetone == frequency of the tone.frequency of the tone.
ffss == sampling frequency.sampling frequency.
N is set to 205.N is set to 205.
Now we can calculate the value of the Now we can calculate the value of the coefficient 2cos(2*coefficient 2cos(2**k/N).*k/N).
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 11
Goertzel Algorithm ImplementationGoertzel Algorithm Implementation
FrequencyFrequency kk CoefficientCoefficient(decimal)(decimal)
CoefficientCoefficient(Q15)(Q15)
16331633 4242 0.5594540.559454 0x479C0x479C14771477 3838 0.7900740.790074 0x65210x652113361336 3434 1.0088351.008835 0x4090*0x4090*12091209 3131 1.1631381.163138 0x4A70*0x4A70*941941 2424 1.4828671.482867 0x5EE7*0x5EE7*852852 2222 1.5622971.562297 0x63FC*0x63FC*770770 2020 1.6355851.635585 0x68B1*0x68B1*697697 1818 1.7032751.703275 0x6D02*0x6D02*
* The decimal values are divided by 2 to be * The decimal values are divided by 2 to be represented in Q15 format. This has to be represented in Q15 format. This has to be taken into account during implementation.taken into account during implementation.
N = 205N = 205
fs = 8kHzfs = 8kHz
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 12
QQnn = x(n) - Q= x(n) - Qn-2 n-2 + + coeff*Qcoeff*Qn-1n-1; 0; 0n<Nn<N
= sum1= sum1 ++ prod1 prod1
Goertzel Algorithm ImplementationGoertzel Algorithm Implementation
Where: coeff = 2cos(2Where: coeff = 2cos(2k/N)k/N)
The feedback section has to be repeated N The feedback section has to be repeated N times (N=205).times (N=205).
++
+
-1
1z
1z
)( ny)( nx
coeff
2nQ
nQ
1nQ
d e lay
d e lay1
d e lay2
p ro d 1
FeedbackFeedback FeedforwardFeedforward
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 13
|Y|Ykk(N) |(N) |2 2 = Q= Q22(N) + Q(N) + Q22(N-1) - coeff*Q(N)*Q(N-1)(N-1) - coeff*Q(N)*Q(N-1)
Goertzel Algorithm ImplementationGoertzel Algorithm Implementation
Where: coeff = 2*cos(2*Where: coeff = 2*cos(2**k/N)*k/N)
Since we are only interested in detecting the Since we are only interested in detecting the presence of a tone and not the phase we can presence of a tone and not the phase we can detect the square of the magnitude:detect the square of the magnitude:
++
+
-1
1z
1z
)( ny)( nx
coeff
2nQ
nQ
1nQ
d e lay
d e lay1
d e lay2
p ro d 1
FeedbackFeedback FeedforwardFeedforward
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 14
Goertzel Algorithm ImplementationGoertzel Algorithm Implementationvoid Goertzel (void){
static short delay;static short delay_1 = 0;static short delay_2 = 0;static int N = 0;static int Goertzel_Value = 0;int I, prod1, prod2, prod3, sum, R_in, output;short input;short coef_1 = 0x4A70; // For detecting 1209 Hz
R_in = mcbsp0_read(); // Read the signal in
input = (short) R_in;input = input >> 4; // Scale down input to prevent overflow
prod1 = (delay_1*coef_1)>>14;delay = input + (short)prod1 - delay_2;delay_2 = delay_1;delay_1 = delay;N++;
if (N==206){
prod1 = (delay_1 * delay_1);prod2 = (delay_2 * delay_2);prod3 = (delay_1 * coef_1)>>14;prod3 = prod3 * delay_2;Goertzel_Value = (prod1 + prod2 - prod3) >> 15;Goertzel_Value <<= 4; // Scale up value for sensitivityN = 0;delay_1 = delay_2 = 0;
}
output = (((short) R_in) * ((short)Goertzel_Value)) >> 15;
mcbsp0_write(output& 0xfffffffe); // Send the signal out return; }
‘‘C’ codeC’ code
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 15
Goertzel Algorithm ImplementationGoertzel Algorithm Implementation
.def _gz
.sect "mycode"
_gz .cproc input, coeff, count, mask2
.reg delay1, delay2, x, gzv
.reg prod1, prod2, prod3, sum1, sum2
zero delay1zero delay2
loop: ldh *input++, xmpy delay1, coeff, prod1shr prod1, 14, prod1sub x, delay2, sum1mv delay1, delay2add sum1, prod1, delay1
[count] sub count,1,count [count] b loop
mpy delay1, delay1, prod1mpy delay2, delay2, prod2add prod1, prod2, sum1
mpy delay1, coeff, prod3shr prod3, 14, prod3mpy prod3, delay2, prod3
sub sum1,prod3, sum1shr sum1, 15, gzv
.return gzv
.endproc
Linear assembly Linear assembly (fixed-point)(fixed-point)
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 16
Goertzel Algorithm ImplementationGoertzel Algorithm Implementation
.def _gz
.sect "mycode"
_gz .cproc input1, coeff, count, mask2
.reg delay1, delay2, x, gzv,test,y
.reg prod1, prod2, prod3, sum1, sum2
zero delay1zero delay2
loop: ldw *input1++, xmpysp delay1, coeff, prod1subsp x, delay2, sum1mv delay1, delay2addsp sum1, prod1, delay1
[count] sub count,1,count [count] b loop
mpysp delay1, delay1, prod1mpysp delay2, delay2, prod2addsp prod1, prod2, sum1
mpysp delay1, coeff, prod3mpysp prod3, delay2, prod3
subsp sum1,prod3, sum1
.return sum1
.endproc
Linear assembly Linear assembly (floating-point)(floating-point)
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 17
Hand OptimisationHand Optimisation
Implementation of:Implementation of:
QQnn = [(coeff*Q = [(coeff*Qn-1n-1)>> 14 + x(n)] - Q)>> 14 + x(n)] - Qn-2n-2
11 22 33 44 55 66 77 88 99 1010 1111CycleCycle
LDHLDH
MPYMPY SHRSHR
ADDADD
SUBSUB
MVMV
MVMV
QQn-2n-2=Q=Qn-1n-1
QQn-1n-1=Q=Qnn
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 18
Hand OptimisationHand Optimisation
Implementation of:Implementation of:
QQnn = [(coeff*Q = [(coeff*Qn-1n-1)>> 14] + [x(n) - Q)>> 14] + [x(n) - Qn-2n-2]]
11 22 33 44 55 66 77 88 99 1010 1111CycleCycle
LDHLDH
MPYMPY
SHRSHR
ADDADD
SUBSUB
MVMV QQn-2n-2=Q=Qn-1n-1
QQn-1n-1=Q=Qnn
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 19
11 22 33 44 55 66 77 88 99 1010 1111LDHLDH
MPYMPY
SHRSHR
ADDADD
SUBSUB
MVMV
Hand OptimisationHand Optimisation
Now let us consider adding a second iteration.Now let us consider adding a second iteration. When can we start the “MPY” of the second iteration? When can we start the “MPY” of the second iteration?
QQnn = [(coeff*Q = [(coeff*Qn-1n-1)>> 14] + [x(n) - Q)>> 14] + [x(n) - Qn-2n-2]]
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 20
11 22 33 44 55 66 77 88 99 1010 1111LDHLDH
MPYMPY
SHRSHR
ADDADD
SUBSUB
MVMV
Hand OptimisationHand Optimisation
We have to wait until the add has finished as the result of iteration We have to wait until the add has finished as the result of iteration 1 is one of the inputs to the multiply performed in iteration 2.1 is one of the inputs to the multiply performed in iteration 2.
QQnn = [(coeff*Q = [(coeff*Qn-1n-1)>> 14] + [x(n) - Q)>> 14] + [x(n) - Qn-2n-2]]
MPYMPY
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 21
11 22 33 44 55 66 77 88 99 1010 1111LDHLDH
MPYMPY
SHRSHR
ADDADD
SUBSUB
MVMV
Hand OptimisationHand Optimisation
The other instructions then follow in the same order.The other instructions then follow in the same order.
MPYMPY
SHRSHR
ADDADD
SUBSUB
MVMV
Finally the load of x[1] must have occurred before the sub, Finally the load of x[1] must have occurred before the sub, therefore the load must take place in cycle 5.therefore the load must take place in cycle 5.
LDHLDH
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 22
Goertzel Algorithm ImplementationGoertzel Algorithm Implementation
Hand optimised assembly (fixed-point):Hand optimised assembly (fixed-point):
; PIPED LOOP PROLOG
LDH .D1T1 *A0++(4),A3 || [ A1] SUB .L1 A1,0x1,A1
[ A1] B .S1 loop NOP 1
; PIPED LOOP KERNEL
loop: MPY .M2 B4,B5,B6
[ A1] SUB .L1 A1,0x1,A1 || LDH .D1T1 *A0++(4),A3
MV .L1X B4,A4 || SUB .D1 A3,A4,A3 || SHR .S2 B6,0xe,B4 || [ A1] B .S1 loop
ADD .L2X A3,B4,B4
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 23
Testing the ImplementationTesting the Implementation
The input signal is modulated with the square magnitude and sent to the codec.The input signal is modulated with the square magnitude and sent to the codec. Therefore when the frequency of the input signal corresponds to the detection frequency, the input tone appears at Therefore when the frequency of the input signal corresponds to the detection frequency, the input tone appears at
the output.the output.
PCPCDSKDSK
Signal Signal GenGen
Osc/Spec Osc/Spec AnalyserAnalyser
Dr. Naim Dahnoun, Bristol University, (c) Texas Instruments 2004
Chapter 17, Slide 24
Goertzel CodeGoertzel Code
Code location:Code location: Code\Chapter 17 - Goertzel AlgorithmCode\Chapter 17 - Goertzel Algorithm
Projects:Projects: Fixed Point in C:Fixed Point in C: \Goertzel_C_Fixed\\Goertzel_C_Fixed\ Fixed Point in C with EDMA: Fixed Point in C with EDMA: \\
Goertzel_C_Fixed_EDMA\Goertzel_C_Fixed_EDMA\ Fixed Point in Linear Asm:Fixed Point in Linear Asm: \Goertzel_Sa_Fixed\\Goertzel_Sa_Fixed\ Floating Point in Linear Asm:Floating Point in Linear Asm: \Goertzel_Sa_Float\\Goertzel_Sa_Float\
Chapter 17Chapter 17
Goertzel AlgorithmGoertzel Algorithm
- End -- End -