Goertzel Algorithm DTMF Detection - Hobbydebraj

18
hobbydebraj About me 2-line interface for graphic LCD Android OBD scanner hack CAN-BUS based OBD reader Closed loop speed control of DC motor using back emf sening dsPIC based Spectrum Analyzer dsPIC WAV player Dummy electrical Load Exploding Duracell 9V Goertzel algorithm DTMF Detection Graphic LCD Projects Home Power Meter Internet Power Meter PC Based Signal Generator Programable Power Supply Rigol DS1052 Oscilloscope Switched mode programable power supply Web Server Project Inside Floppy/ CD Drive Test Sitemap About me > Goertzel algorithm DTMF Detection Goertzel algorithm is well known in the DSP domain. It is used to detect the presence of a frequency or a number of frequencies. This algorithm is popular as the calculation required for implementation is lesser as compared to other techniques such as DFT (FFT), when a smaller number of frequencies are to be detected. Some projects that I have seen implementing Goertzel algorithm are: - 1. DTMF detection. 2. Tuning of guitar string 3. Detection of specific frequencies in audio signals. My project uses PIC18F4520 and Goertzel algorithm to detect DTMF frequencies. The code is written entirely in "C". Even though, the implementation of my project is good, I do not guarantee that it can be used in a commercial application. Understanding the Goertzel algorithm: - The understanding of Goertzel algorithm can be made using the unit-circle diagram on z-plane (as shown below). Traduzir Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d... 1 de 18 3/11/2013 10:14

Transcript of Goertzel Algorithm DTMF Detection - Hobbydebraj

Page 1: Goertzel Algorithm DTMF Detection - Hobbydebraj

hobbydebrajAbout me

2-line interface forgraphic LCDAndroid OBDscanner hackCAN-BUS basedOBD reader

Closed loop speedcontrol of DC motorusing back emfsening

dsPIC basedSpectrum Analyzer

dsPIC WAV playerDummy electricalLoadExploding Duracell9V

Goertzel algorithmDTMF DetectionGraphic LCDProjectsHome Power MeterInternet Power Meter

PC Based SignalGeneratorProgramable PowerSupplyRigol DS1052Oscilloscope

Switched modeprogramable powersupply

Web Server Project

Inside Floppy/ CDDriveTestSitemap

About me >

Goertzel algorithm DTMF Detection

Goertzel algorithm is well known in the DSP domain. It is usedto detect the presence of a frequency or a number offrequencies. This algorithm is popular as the calculationrequired for implementation is lesser as compared to othertechniques such as DFT (FFT), when a smaller number offrequencies are to be detected.

Some projects that I have seen implementing Goertzel algorithmare: -1. DTMF detection.2. Tuning of guitar string3. Detection of specific frequencies in audio signals.

My project uses PIC18F4520 and Goertzel algorithm to detectDTMF frequencies. The code is written entirely in "C". Eventhough, the implementation of my project is good, I do notguarantee that it can be used in a commercial application.

Understanding the Goertzel algorithm: -

The understanding of Goertzel algorithm can be made using theunit-circle diagram on z-plane (as shown below).

Traduzir

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

1 de 18 3/11/2013 10:14

Page 2: Goertzel Algorithm DTMF Detection - Hobbydebraj

Usually for DTMF detection, the sample frequency is chosen tobe 8000Hz. But I choose to use the sampling frequency to be5908Hz. The reason for choosing this particular value will beexplained later.

If we assume a system that has 2 conjugate poles (conjugatepoles because, that effectively removes the complex term andmake implementation possible using real world micro controller)on the unit circle, the output of such a system has a sharp peakat that frequency.

Sample frequency of 5908Hz falls on 2π radian on unit circle ofz-plane.Also, the frequencies beyond π is more than 1/2 of samplingfrequency and hence falls beyond then Nyquist rate and hencewe will not consider them.

Now, we have the following frequencies on DTMF: -697Hz770Hz852Hz941Hz

1209Hz1336Hz1477Hz

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

2 de 18 3/11/2013 10:14

Page 3: Goertzel Algorithm DTMF Detection - Hobbydebraj

In order to place 697Hz on z-plane, we will normalize this withthe sampling frequency: -5908Hz ---> 2πso, 697Hz = (2π/5908) * 697 = 0.74126 radians

Following the similar conversion, the values for eachfrequencies are: -697Hz = 0.74126 radians770Hz = 0.81889 radians852Hz = 0.9061 radians941Hz = 1.00713 radians

1209Hz = 1.28577 radians1336Hz = 1.4208 radians1477Hz = 1.57079 radians

If we place 2 conjugate poles for a system at frequency of697Hz, and 2 zeros on origin, the z-plane diagram will look likethis: -

And, the transfer functions of the system will be: -

(z – 0)(z – 0)

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

3 de 18 3/11/2013 10:14

Page 4: Goertzel Algorithm DTMF Detection - Hobbydebraj

----------------------

(z – ej0.74126)(z – e-j0.74126)

In order to generalize our frequency, we will replace the term

0.74126 with "θ": -

(z – 0)(z – 0)

----------------------

(z – ejθ)(z – e-jθ)

z2

----------------------

z2 – ze-jθ - zejθ + 1

Using Eulers formula and replacing

ejθ = Cosθ + jSinθ

and

e-jθ = Cosθ - jSinθ

the equation becomes as below: -

z2

----------------------

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

4 de 18 3/11/2013 10:14

Page 5: Goertzel Algorithm DTMF Detection - Hobbydebraj

z2 – z(Cosθ – jSinθ) – z(Cosθ + jSinθ) + 1

z2

----------------------

z2 – zCosθ – zjSinθ – zCosθ + zjSinθ + 1

z2 Output

---------------------- = -------

z2 – 2zCosθ + 1 Input

Multiplying numerator and denominator with "z-2": -

z2 * z-2 Output

---------------------- = -------

(z2 – 2zCosθ + 1) * z-2 Input

1 Output

---------------------- = -------

1 – 2z-1Cosθ + z-2 Input

Replacing "z-1" as one delay and "z-2" as two delay: -

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

5 de 18 3/11/2013 10:14

Page 6: Goertzel Algorithm DTMF Detection - Hobbydebraj

Input = Output - 2 * Cosθ * Prev_Output +Prev_Prev_Output

Output = Input + 2 * Cosθ * Prev_Output -Prev_Prev_Output

Above equation is the implementation of Goertzel algorithm. The

term (2 * Cosθ) is called "coefficient" is pre-calculated for each

frequencies: -

697Hz = 0.74126 radians = 1.4752770Hz = 0.81889 radians = 1.3660852Hz = 0.9061 radians = 1.2336941Hz = 1.00713 radians = 1.0685

1209Hz = 1.28577 radians = 0.56231336Hz = 1.4208 radians = 0.29881477Hz = 1.57079 radians = 0

As you can see that the coefficient for "1477Hz" is zero and thatbasically save lot of calculation during detection of 1477Hz. Thisthe reason why I choose a sampling frequency of 5908Hz.

These coefficients are placed as "#define" constants in"DTMF.h" file of my code. In order to speed up the calculationon PIC, I avoided any float calculation. Hence, the coefficientsare multiplied by 256 to convert them into a integer. Later, Idivide the result by 256 to get the actual value. This way, mycalculations are way faster than float implementation.

The calculation for Goertzel is performed on a number ofacquired ADC sample. I have chosen a sample number of 100to perform the calculations. This is a balance between detectionresolution of frequencies and processing time.

Once the calculations are performed on all the 100 samples, the

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

6 de 18 3/11/2013 10:14

Page 7: Goertzel Algorithm DTMF Detection - Hobbydebraj

magnitude of detection peak is calculated as: -

Magnitude = Prev_Prev_Output 2 +

Prev_Output2 – (Coefficient * Prev_Prev_Output* Prev_Output)

If this magnitude is > than a threshold, then the frequency ispresent. Otherwise, the frequency is absent.

if(magnitude > THRESHOLD)

{

// Frequency is present

}

Understanding the Goertzel algorithm through excel sheet: -

For a more practical simulation of the algorithm, I have createdan excel sheet. You can download the excel sheet and trydifferent frequencies. Even though, the algorithm is used forDTMF detection, it can be used to detect any frequencyprovided the coefficients and sampling rates are correct.

Basic tests: -

I started my project by sampling a sine wave using PIC18F4520ADC (channel - 0) triggered by Timer-3 (to make a precise timesampling rate). The Timer triggering ADC is absolutely essentialas any jitter in sampling rate will degrade the performance offrequency detection. For testing purpose, I used a sample rateof 8KHz.

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

7 de 18 3/11/2013 10:14

Page 8: Goertzel Algorithm DTMF Detection - Hobbydebraj

Screen shot for sample of 1KHz signal (@8KHz): -

Screen shot for sample of 400Hz signal (@8KHz): -

Screen shot for sample of 200Hz signal (@8KHz): -

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

8 de 18 3/11/2013 10:14

Page 9: Goertzel Algorithm DTMF Detection - Hobbydebraj

In each of the above screen shot, count the number of points for1 cycle should give the relation between (8000Hz/signal freq).

Design of hardware: -

Telephone line usually contains high voltages, specially duringringing, which can give some nasty shock. So isolating thetelephone line from my circuit was essential. Hence, I hadordered the audio transformer (TY146P) from TRIAD. It is aaudio transformer 1:1 with 600ohms impedance, which is perfectfor telephone line coupling.

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

9 de 18 3/11/2013 10:14

Page 10: Goertzel Algorithm DTMF Detection - Hobbydebraj

Circuit schematic: -

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

10 de 18 3/11/2013 10:14

Page 11: Goertzel Algorithm DTMF Detection - Hobbydebraj

Explanation of hardware: -1. 2 X 0.47uF X 450V capacitors are connected in parallel sothat they block all DC voltage going into transformer. But theyallow AC voltage like audio and DTMF signals to flow into thetransformer.

2. 2K resistor is placed to reduce loading of telephone line.

3. TY-146P is a 600-600 ohms isolation transformer used foraudio signal coupling.

4. 2 X 1N4007 diodes connected in antiparallel to limit thevoltage that goes into the opamp.

5. LM358 -- one opamp used as voltage follower to generate2.5V. This 2.5V is exactly the half of ADC reference used byPIC18F4520. Any AC signal to be measured by PIC18F4520 hasto be DC offset by known voltage so that the negative sides ofAC does not go below the Vss voltage (ground). In the software,1/2 of ADC full range (in my case 129) is subtracted to removethe DC offset addition.

6. LM358 -- second opamp used as summer to add the 2.5Vand input signal.

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

11 de 18 3/11/2013 10:14

Page 12: Goertzel Algorithm DTMF Detection - Hobbydebraj

7. PIC18F4520 -- uses a 10MHz XTAL (internally X4PLL togenerate 40MHz). Also, a LCD is interfaced to display thedetected frequencies.

Once I rigged up the circuit, I did some measurement byconnecting the circuit to my telephone line: -

FFT for signal while pressing "key - 1" (Freq = 697Hz,1209Hz)

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

12 de 18 3/11/2013 10:14

Page 13: Goertzel Algorithm DTMF Detection - Hobbydebraj

FFT for signal while pressing "key - 2" (Freq = 697Hz,1336Hz)

FFT for signal while pressing "key - 3" (Freq = 697Hz,1477Hz)

FFT for signal while pressing "key - 7" (Freq = 852Hz,1209Hz)

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

13 de 18 3/11/2013 10:14

Page 14: Goertzel Algorithm DTMF Detection - Hobbydebraj

Waveform when phone rings (without the protection diodesto limit volt level)

Please notice that when the phone rings, the voltage level goesvery high. The above signal is captured after theisolation transformer, which had the capacitor and 2K resistorconnected in primary side. The measured values is about+35V/-35V. If we do not use protection diode to clip the signallevel, this high value of signal will certainly damage the analoginput of microcontroller or the opamp.

Waveform when phone rings (with the antiparallel diodes1N4007)

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

14 de 18 3/11/2013 10:14

Page 15: Goertzel Algorithm DTMF Detection - Hobbydebraj

As we can see that the voltage level is safely limited to diodeforward voltage of about +/-0.7V. The usage of diode doesintroduce some harmonics during the measurement of DTMFsignal, but if the signal is kept low enough (within +/-0.7V), byincreasing the value of input resistor R1 (from 2K to highervalue), then the harmonics can be eliminated. Later, the signalcan be amplified by amplifier to get sufficient level (if required).But if we use Goertzel algorithm, it will look for frequenciesin specific range. Even if harmonics exists within the DTMF band,it will be ignored.

Advantages of using Goertzel/ software implementation ofDTMF detection vs. dedicated chips: -

1. No extra hardware needed like encoder and decoder chips(like MT8870 and TP5089) and crystals. Using softwareimplementation saves all that.

2. The equation for Goertzel can also be run from a timer(reverse Goertzel) to generate sinusoidal frequency that actslike DTMF generator. Hence, the encoder can also beimplemented in software.

3. If a hardware design needs isolation, then the isolationtransformer is required both with hardware implementation orGoertzel implementation. Same is for high voltage capacitors.

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

15 de 18 3/11/2013 10:14

Page 16: Goertzel Algorithm DTMF Detection - Hobbydebraj

4. The concept for Goertzel can be used to detect anyfrequency which is not possible with hardware chips, as theyare built to detect DTMF frequencies only.

5. Software implementation gives a fair idea about DSPconcepts -- this is the main reason for me to like the softwareimplementation.

Limitations of my project: -

1. Because of processing power limits, I am not detecting thesecond harmonics for DTMF. This is usually done in commercialimplementation so as to eliminate any chance of false detectiondue to music or speech. Music or speech cannot generate puresinusoids and hence contains second harmonics. But DTMFsignals are pure sinusoids and hence does not contain secondharmonics.

2. The peak detection might detect a false peak as there existsome ripples near the real peak. These ripples can beeliminated in a commercial implementation by using windowfunction. I did not use window function due to processing powerlimits.

MIPS requirement: -PIC18F4520 runs at a max frequency of 40MHz and internally,the clock is divided by 4 to get 10MIPS.The implementation of Goertzel acquires a buffer of 100sample,sampled at a rate of 5908Hz.Time to acquire 100 samples = 100 * (1/5908) = 16.92millisec

Time to perform Goertzel decoding (measured by toggling a

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

16 de 18 3/11/2013 10:14

Page 17: Goertzel Algorithm DTMF Detection - Hobbydebraj

GPIO on oscilloscope) = 9.40millisec

MIPS required = (9.40/16.92) * 10 = 5.55MIPS.

This means that my implementation of Goertzel for DTMFdecoding takes > 50% of processing. More optimization can bedone in the code (if I spend more time, which I cannot) or byconverting C code into assembly code (which I do not likeunless forced upon).

Final application: -

I wanted to create a home automation system and initiallythought of using xigbee modules. But they are expensive. Later,it came to my mind that my house has a telephone networkwhich goes to all the rooms. So I decided to build low costDTMF boards to do home automation. The information for homeautomation will be sent as DTMF codes and this is the end useof my project.

Demonstration: -

DTMF Decoder using Goertzel algorithm on PIC18F4520

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

17 de 18 3/11/2013 10:14

Page 18: Goertzel Algorithm DTMF Detection - Hobbydebraj

Fazer login | Denunciar abuso | Imprimir página | Remover acesso | Tecnologia Google Sites

18F4520_Goertzel.zi Debraj Deb, ďv.1… …

Goertzel_Algorithm_ Debraj Deb, Ĉ ďv.1… …

Goertzel algorithm DTMF Detection - hobbydebraj https://sites.google.com/site/hobbydebraj/home/goertzel-algorithm-dtmf-d...

18 de 18 3/11/2013 10:14