Goertzel DFT
Goertzel DFT
Goertzel DFT
Purpose
The purpose of this project is to demonstrate how to use a PIC32 processor to decode the DTMF
tones generated by a touch tone telephone. The method used in this project uses Discrete Fourier
Transforms (DFT) to determine the energy if a signal in selected frequency bands. The DFT computation
is based on the Goertzel Algorithm.
page 1 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
Equipment List
1. chipKITTM Pro MX7 processor board with USB cable
2.
Software Resources
1. MPLAB X Integrated Development Environment (IDE)\
2. MPLAB XC32 C/C++ Compiler
3. XC32 C/C++ Compiler Users Guide
4. Wav to Code converter
References
1. chipKITTM Pro MX7 Board Reference Manual
2. C Programming Reference
3. PIC32 Family Reference Manual, Section 14:
http://ww1.microchip.com/downloads/en/DeviceDoc/61105E.pdf
4. A Discrete Fourier Transform Based Digital DTMF Detection Algorithm,
http://www.rootsecure.net/content/downloads/pdf/paper_dtmf.pdf
5. Intelligent Detection of DTMF Tones using a Hybrid Signal Processing Technique with Support
Vector Machines, http://www.idsia.ch/~nagi/conferences/itsim_dtmf_detection.pdf
page 2 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
DTMF
Touch tone dialing was introduced by AT&T for telephones in 1963 as a replacement for rotary dialsi.
The primary advantage of touch tone dialing over rotary dialing is that the dialing information can be
carried in the audio band where as the dialing information for rotary dialing is DC. The rotary phones
(circa 1891-present day) created pulses in the voltage supplied by the telephone exchange to power the
telephone. Rotary dials are mechanical devices that use a cam to actuate a mechanical switch. Over time
the mechanics of the rotary dial would break or begin to operate so slowly that the pulses could no
longer be accurately decoded. Conventional touch tone operated telephones have 12 keys: 0 through 9
as well as the * and # keys. The full implementation of the touch tone key pad as shown in Figure 1
contains 16 keys. The additional four keys are designated A through D. Some phone systems use the
additional keys for phone system management and control.
page 3 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
the signal is generated that consists of the two frequencies specified by the keys row and column.
DTMF standards specify that the frequencies of the eight DTMF tones be accurate to within 1.8%. This
tolerance results in a tone band width of 25Hz based on the row one frequency to 59 Hz for the column
four. This has ramifications when designing a digital filter to identify the tone pairs when a key is
pressed.
770 Hz
852 Hz
941 Hz
frequency rate and N is the truncated sequence length. It can be shown that the non recursive
expression shown in Eq. 1 can converted to the recursive first order digital filter expressed by Eq. 2 and
Eq. 3.
Eq. 2
1
Goertzel, G. (January 1958), "An Algorithm for the Evaluation of Finite Trigonometric Series", American Mathematical
Monthly 65 (1): 3435, doi:10.2307/2310304
page 4 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
Eq. 3
However Eq. 2 and Eq. 3 use the factor
which is a complex variable that we would like to avoid
when implementing in microprocessor code to gain the advantage of speed of computation.
Enduring additional mathematical gymnastics, the difference equation expressed by Eq. 2 can be
expressed by Eq. 4.
Eq. 4
This recursive equation can be graphically represented as shown in Figure 2. The intermediate results
expressed by Vk(n) and Vk(n-1) are used to compute the output. Eq. 3 tells that only the terms for Vk(N)
and Vk(N-1) are needed to compute the final result for
.
vk(n)
x(n)
b0
+
-a1
1/z
yk(n)
b1
vk(n-1)
+
-a2
1/z
vk(n-2)
Figure 2. A Direct Form II flow diagram that describes the recursive form for computing the Goertzel
Algorithm
The expression for determining the intermediate values Vk(n), Vk(n-1) and Vk(n-2) is shown by Eq. 5.
Eq. 5
If the samples of the signal are real-valued (normally the case) then all of the results for Vk(n), Vk(n-1)
and Vk(n-2) are real values because the coefficients a1 and a2 are real. The expression shown for Eq. 5
must be iterated until n = N before the values of Vk(N) and Vk(N-1) can be used in the output expression
shown in Eq. 6.
Eq. 6
Since the coefficient b2 is complex, the resulting computation for
is also complex. Squaring the
results by multiplying
by its complex conjugate will allow the elimination of all complex values.
page 5 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
Again, through mathematical effort, the result can be expressed using only the values for Vk(N) and
Vk(N-1) as shown in Eq. 7.
Eq. 7
Note that the term used in Eq. 7 is the same as that used in Eq. 5. Hence, only one constant is
required to compute the energy component of a signal at a specified frequency.
The blue lines in Figure 3 shows the results of taking the DFT of a signal that contains at all eight DTFM
tones using a window that is sampled at 8KHz consisting of 205 samples. The red lines are the results of
using the Goertzel algorithm specifically tuned for the eight DTMF frequencies. Even while using the
same size of data set, improved tuning is achieved by not restricting the bin number used to generate
the argument of the cosine function, , to be an integer. Rather compute using the expression shown
in Eq. 8.
Eq. 8
The improvement is evident by observing the graph shown in Figure 3 that shows three different
approaches to computing energy-frequency spectrum of a signal containing all eight DTMF tones with
equal amplitude. The DFT plot shows the results obtained by applying the conventional DFT
transformation expressed by Eq. 1. The plot labeled G2 uses the Goertzel algorithm where the term is
computed as shown in Eq. 7. Lastly, the plot labeled G1 uses the Goertzel algorithm where the term is
computed as shown in Eq. 8. For all instances except on, the Goertzel algorithm that uses the term as
defined by Eq. 8 shows a greater sensitivity as indicated by the magnitude of the energy.
Energy
60
40
20
G1
G2
DFT
0
0
500
110
1.510
210
Frequency - Hz
page 6 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
Figure 3. Comparison of DFT output and Goertzel Algorithm output of a signal containing all 8 DTMF
tones
Using the recommend 205 for the block size and a sampling rate of 8 KHz results in a frequency
resolution of 39 Hz. Consider the separation of frequency of the tones generated for row one and row 2.
The difference of these two frequencies is 73 Hz. Using a sample block size of 205 Hz, Figure 3 clearly
demonstrates that the energies of the row tones are separated by less than two DFT bins. The minimum
threshold level can also be determined from Figure 3 by observing that any energy that exists due to a
full amplitude adjacent tone will be below 20 as opposed to the Goertzel output of greater than 40 for
tones at the frequency specified for the Goertzel algorithm.
Figure 4 show a plot of a signal containing DTMF tones that automatically generated at the specified
maximum rate. The minimum periods of tone burst and inter-tone gap is 50 ms. It the DTMF tones are
generated by someone pressing a touch tone key pad then the duration of the tone bursts and the
period between the tone bursts will be much longer and we would not expect the intervals to be
uniform. Hence the algorithm implemented in the processor must be adaptive to all possible tone burst
patterns.
Amplitude
0.5
0.5
1
Time - seconds
Figure 4. Plot of data for a sequence of DTMF tones spaced at 50 ms intervals.
page 7 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
The second argument is the constant alpha that is associated with the frequency bin number as
described above. The third parameter, N, is the length of the data array. An integer representing the
scaled energy value associated with the frequency bin is returned by this function. This function uses
only fixed point mathematics.
Listing 1. Goertzel Function
/* goertzel FUNCTION DESCRIPTION *****************************************
SYNTAX:
int goertzel(signed char *blk, int alpha, int N);
KEYWORDS:
DFT, Goertzel, algorithm
DESCRIPTION:
This function computes the energy of the signal at a single
frequency using the Goertzel algorithm.
PARAMETER 1:
char pointer to the array of the block of sampled analog
data
PARAMETER 2:
The constant associated with the bin number for the
frequency of interest.
PARAMETER 3:
The number of elements (data points) to process
RETURN VALUE:
Integer value (32 bit signed) of the energy of the signal
NOTES:
None.
END DESCRIPTION **********************************************************/
int goertzel(signed char *blk, int alpha, int N)
{
int v0, v1, v2, y;
int i;
v2 = blk[0];
/* Initialize filter */
v1 = blk[1] + alpha*v2/AS;
for(i=2; i<N; i++)
/* Digital filter algorithm */
{
V0 = blk[i] + (alpha*v1)/AS v2; /* Use fixed point math */
V2 = v1;
/* Propagate past results */
v1 = v0;
}
y = v1*v1 - (alpha*v1*v0)/AS + v0*v0; /* Square to compute energy */
return y/(N/2); /* Scale based upon block size */
} /* End of goertzel */
page 8 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
band for the eight DTMF tones. The eight energy levels are stored in an eight element array. This array is
passed to the tone decoder that compares the energy levels for the frequencies that correlate with the
signals generated the row and column of the key that is pressed. This function determines which two
tones (if any) have energy above a preset value. The Tone Decoder function then returns an ASCII
character value assigned to the key. This value is displayed on the LCD.
Hardware
Initialization
TIMER 1
ISR
Initialize
System
Process Control
LCD
Goertzel
Algorithm
Tone
Decoder
Start
Reading
Next Data
NO
Data File
Block
Read?
YES
Goertzel
Process
Data Block
Decode
Tones to
Keys
All Data
Read?
NO
YES
Wait 1
second
The control process implement for the demonstration program is shown in Figure 6 above. After
initialization, the program waits for a flag to be set in the Timer interrupt service routing that indicates
that a buffer of data is ready to be processed. Data collection is suspended until the background task
resets this flag indicating that it is okay to fill the buffer with new data. The background task decodes
page 9 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
each buffer of data to determine the key that was pressed as indicated by the two tones present or a
special code that indicates that no tones were detected that were above a preset threshold value. An
algorithm is used that rejects multiple decoding of the same key unless there is an intervening detection
of no key pressed. The entire process is repeated after a delay of one second.
page 10 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
{
if(tones[i] > col) /* If largest and greater than threshold */
{
/* save the element number - range 4 - 7 */
col = tones[i];
c = 1<<i;
}
}
if(r==0 || c == 0)
return 0;
switch (r|c)
/* Decode the row-column combination */
{
case 0x11:
key = '1';
break;
case 0x12:
key = '4';
break;
case 0x14:
key = '7';
break;
case 0x18:
key = '*';
break;
case 0x21:
key = '2';
break;
case 0x22:
key = '5';
break;
case 0x24:
key = '8';
break;
case 0x28:
key = '0';
break;
case 0x41:
key = '3';
break;
case 0x42:
key = '6';
break;
case 0x44:
key = '9';
break;
case 0x48:
key = '#';
break;
case 0x81:
key = 'A';
break;
case 0x82:
key = 'B';
break;
case 0x84:
key = 'C';
break;
case 0x88:
key = 'D';
page 11 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
break;
default:
key = 0;
}
return key;
} /* End of key_code */
Code Performance
Table II provides a performance measure for this implementation of the Goertzel algorithm. This data
was determined from measurements illustrated in Error! Reference source not found. and Error!
Reference source not found.. 25.65 ms is required to collect a 205 sample window of the input signal.
The Goertzel algorithm and the LCD display update require 0.9744 ms which means that sampling is
halted for approximately 6 samples. Since the minimum tone burst is 50ms, the maximum processing
window size is 400 samples. Using a sample window of 205, there is always at least one window will be
entirely within one tone burst or entirely within one inter-tone gap. Worst case energy level detection
would occur then two buffers of data straddle either a tone burst or an inter-tone gap.
Table II. Task code execution timing
Task
Sample Block
Decode Tones
Update Display
DAC Output
Execution Time
25.65 ms
926 s
48.4 s
10.8 s
Figure 7. Processing timing: 3-DAC, 2-Read Analog, 1-Decode Tones, 0-Display Results
page 12 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
Final Remarks
Since the sampling is interrupt driven and represents a foreground task, it is possible to modify the C
code to continue sampling the data while computing the tone decoding algorithm and displaying the
results in the background. Background tasks are the functions that the processor executes when there
no higher priority operations waiting for execution. Operating the DTMF decoder system in this manner
requires dual data buffers. One data buffers would be getting loaded with the sampled data while
processing the data out of the other. As in the batch mode method first presented, it is necessary that
the time required for tone decoding be less than the time required for filling a buffer with data.
The threshold value depends on the audio level and the number of bits of resolution provided by the
ADC. Figure 8 shows the decoding results for threshold levels set for 30,000, 40,000, and 50,000. Only
when the threshold is set to 50,000 is the tone sequence correctly decoded. This level was determined
experimentally.
Figure 8. Decoding with various threshold levels: 30000, 40000, and 50000
page 13 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
Figure 9. Processing timing: 3-DAC, 2-Read Analog, 1-Decode Tones, 0-Display Results
Figure 11 and Figure 12 show the analog output for a DTMF tone burst. An interesting experiment will
be to feed the analog output from the analog outputs to a PIC processor ADC channel and actually
process the analog data. The question as to whether it will be better to continuously fill the DTMF block
or arbitrary begin sampling when all previous processing is complete is yet to be answered.
Another interesting experiment is to process the PWM output directly with the Goertzel algorithm whit
no band pass filtering to remove the carrier frequency and sidebands.
page 14 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
page 15 of 16
DTMF Decoding
TM
with the ChipKIT Pro MX7
i
ii
http://en.wikipedia.org/wiki/Rotary_dial
http://en.wikipedia.org/wiki/Dual-tone_multi-frequency_signaling#.23.2C_.2A.2C_A.2C_B.2C_C.2C_and_D
page 16 of 16