text stringlengths 1 330k |
|---|
share|improve this answer |
Try to use drawCircle instead |
circlePaint.setColor(0xFFFF0000); //opaque red |
float radius = 10f; //10 pixels |
canvas.drawCircle(p.x, p,y, radius, circlePaint); |
share|improve this answer |
It draws correctly if you draw to a canvas backed by a bitmap. It doesn't seem to draw correctly if you draw directly to the screen (i.e. a canvas passed to a View's onDraw() method). Another way of getting around the problem is, therefore, to first render to a bitmap and then draw that to the screen. |
share|improve this answer |
Your Answer |
[Back to Home Page] |
Black DTMF Decoding Algorithm |
Decoding DTMF tones on the minimum PIC hardware - a new algorithm. |
Roman Black - 5th March 2011. |
Note!! This page is about decoding DTMF. If instead you want to generate DTMF I have a nice sinewave DTMF generator algorithm in C code you can see here. |
DTMF is Dual Tone Multi Frequency, (or "Touchtone") these are the sounds made by telephones to dial a number. Each DTMF sound is made by two frequencies which are mixed together. |
Standard DTMF encoding |
DTMF decoding normally uses a specialty IC that receives the DTMF waveform and "decodes" it to one of the 16 possible outputs, usually as a 4bit binary output. These IC's are hard to get for hobby and project use, and can be expensive in small quantities, and require extra components including a 3.79MHz xtal. |
DTMF decoding can be done in software on a high power microcontroller or DSP micro using FFT (Fast Fourier Transform). |
DTMF decoding can be done reasonably well in software using mid power micros using a modified Goertzel's algorithm or a similar form of DFT (Direct Fourier Transform) but this still requires a decent amount of processing power, ROM for the sin/cos tables and the process is complex and not very versatile. |
My DTMF decoding algorithm |
I decided to try a totally different approach. Rather than use a math system like Goertzel or DFT and then try to squeeze it into a tiny low-cost PIC, I would start with the small PIC then try different techniques which are each optimised for the strengths of the small PIC and hopefully combine the techniques to work s... |
Capitalising on the strengths of a small PIC meant starting with the things it could do well; |
1. Internal comparator; good zero-crossing detection |
2. Since the PIC has a xtal; accurate period measurement |
And well, that's about it. :) |
I made up some standard DTMF waveforms in a spreadsheet program, and analysed the waveforms. These waveforms have zero "twist" which means the high DTMF tone and the low DTMF tone have the same amplitude. This is a reasonable enough representation of the received DTMF tone from a telephone line. The minimal hardware di... |
If this was going to be possible using period measurement I needed a way to reduce the period triggering to the most secure parts of the waveform, which was done creating the "HI-LO debounce system" that is very good at removing zero-cross noise and high frequency mis-triggering and produces reliable periods, synchroni... |
The HI-LO debounce system detects the more reliable parts of the DTMF wave, the major peaks and troughs. This is done by cyclic testing using a debounce feature; |
1. Wait until it detects a 220uS period that is at least 90% high (sampled every 5 uS) |
2. Grab the timer value (used to capture a period) |
3. Wait until it detects a 220uS period that is at least 90% low (sampled every 5 uS) |
4. (repeat) |
As you can see above, this seems to work quite well in theory, it eliminates the noise and high frequencies and zero-cross fluff very well, and seems to capture the major periods fairly reliably. It also reduces the need for external hardware as it provides a significant filtering effect by software alone. |
This debounce feature works synergistically to be more than just a period measurement, because it is only triggered on a certain characteristics of the DTMF waveform it perfoms a crude pattern recogniton too, as you can see it triggers only in response to certain major peaks. This will be very useful later. |
Another benefit of this system is that unlike Goertzel or DFT this is not limited to any specific frequency, so the same hardware and code can just as easily detect the dial tone, ring tone, or ringback tones etc, and can detect a single tone if needed or dual tones, all of which may be of benefit in a PIC telephone ap... |
Analysing the periods - ideas? |
Since these periods looked reliable (in theory) and are closely correlated to the frequencies of the 2 sinewaves in the DTMF waveform, the next step was to see if a small PIC could somehow analyse the periods to deduce the DTMF tones. |
Fortunately these periods are fairly long, from 800 to 1600 uS (a synergistic benefit of the HI-LO debounce system) so there is plenty of time even on a small PIC to do some processing or testing between each period capture. |
The obvious thing would be to test the frequency spectrum, (period spectrum?) for the most popular periods that are detected and see if these periods can be directly used to indicate the DTMF frequencies. For convenience, instead of trying to detect the 2 separate sine frequencies it would be simpler to test for a "mat... |
The DTMF 941 1633 example from above produces a lot of periods of about 1155 uS and some periods of about 788 uS, and not much of anything else. In fact there are about 5 of the longer periods for every 2 of the shorter periods. So for 50 periods tested, the spectrum analyser should look something like this; |
Testing it in real world hardware |
First I made a spectrum analyser, this was actually easier than it sounds. I used my trusty MikroElektronika SmartGLCD and wrote some code so it can capture periods on the PIC comparator input using the zero-cross HI-LO debounce system I designed above. This enabled testing of the actual comparator system as well as ju... |
I simplified the HI-LO debounce by just checking for 200uS continuous HI and 200uS continuous LO to trigger each cycle. The DTMF was generated by another PIC using my PIC dual sine accurate DTMF generator (see link at page top). The systems were linked by a 3k trimpot to adjust the DTMF waveform amplitude and a 0.1uF A... |
The software is quite simple; it measures 50 consecutive "debounced" periods, records them as 50 entries in ram, and then draws them on a bar chart where each period is drawn as 1 pixel high on the bar. Only 128 bars were used, keeping it simple as this will be ported to a small PIC 12F or 16F later. |
I really didn't expect this good a result, but it worked perfectly. :) |
Using the simplest period measurement possible, the PIC TMR0 was set to 16uS per timer tick. So the period of 72 shown above = 72 x 16uS = 1152uS. On the right of the bargraph is shown the 6 most common periods, ranked vertically. |
It matches the expected result very well, although each period may be +/- 1 count because of the crude resolution used to measure period. (In reality it tested significantly better than +/- 1 count). The most common period was detected as period 72 and 73, with a total number of samples of 27+8 = 35 samples. The next p... |
This result almost perfectly matches the math model generated in the spreadsheet as the original concept waveform! That predicted 36 and 14 samples for the two main frequencies, the actual hardware produced 35 and 13. |
I set the spectrum analyser to continuously log and display, and turned the DTMF waveform amplitude up and down. The result was always good, with DTMF waveforms ranging from 2.6v p/p (the max) down to about 0.4v p/p and all amplitudes gave a very similar result on the spectrum analyser. This was excellent! |
Can this simple period data decode the DTMF? |
The periods shown below are from my testing in hardware. A period of 100 = 1600uS. Percentages are approximate but seemed reasonably reliable over many sample iterations and different DTMF waveform amplitudes. I have guesstimated fractional periods based on the appearance of the spectrum analyser bars. The time taken t... |
DTMF pair per1 % per2 % |
697 1209 98 56 66 30 |
770 1209 63.3 46 94 44 |
852 1209 60.7 65 90.4 35 |
941 1209 58.1 70 86.5 26 |
697 1336 92 74 62 5 ? |
770 1336 88.7 66 60 26 |
852 1336 85 52 57.6 40 |
941 1336 55 56 82 42 |
697 1477 86 80 114 6 ? |
770 1477 83 88 |
852 1477 80 72 54 26 |
941 1477 76.9 54 52.1 42 |
697 1633 80.7 66 106.3 32 |
770 1633 78 86 102.5 12 |
852 1633 75 90 |
941 1633 72.2 72 49.3 26 |
The figures above were quite reliable, with a fixed frequency source (my xtal locked DTMF sine generator) the periods remained within about 0.3% from test to test. Much less than 1 bar period error on the bargraph. The percentages of the 2 main periods also remained reliable, within 4% and generally better. |
How to decode it? |
If the DTMF generator frequencies were quite exact (say < 0.5% freq error) this would be as easy as using the most common (primary) period. The two closest periods are 85 and 86 so they are separated by 1.1% difference in period. That would already be a workable DTMF decoder, but relies on having a good DTMF generator. |
The DTMF spec requires that the decoder will accept generated DTMF up to +/- 1.5% freq error. Usually the real life DTMF is made by dedicated DTMF generator ICs, and although these are xtal locked their frequencies are approximate and according to some popular IC datasheets the frequencies are normally within 0.3% or s... |
The good news is that it has been extremely easy to get this data, consisting only of a simple comparator, software debounce, and recording 50 consecutive periods. |
Can this meet DTMF decoder requirements? |
The period data from 50 period samples contains more data than just the most common (primary) period. It might be possible to make use of the other data to improve the decoder spec. |
As an example, for these two DTMF "worst case" tones; |
Tone"8" 852 1336 85 52% 57.6 40% |
Tone"3" 697 1477 86 80% 114 6% |
If both use a wider period match, like accepting +/- 1.5% freq error each period filter will be; |
(85) 83.7 to 86.3 |
(86) 84.7 to 87.3 |
So there will be a significant overlap and both primary periods (85 and 86) will be detected. |
But the other data can be processed, as with DTMF Tone"8" the 85 period occurs only 52% of the time and has a second period of 57.6 that occurs 40% of the time. Where the DTMF Tone"3" has period 86 very dominant at 80% and its second period is 114 and occurs only 6% of the time. |
It can use a weighted points system that allocates points for each detected period; |
if(period is 83 to 87); Tone"8" += 1 and Tone"3" += 1 |
if(period is 56 to 59); Tone"3" += 4 |
if(period is 112 to 116); Tone"3" += 20 |
Those figures were just chosen arbitrarily. For 100 tested periods from each Tone, the points result would be; |
Actual DTMF Tone"8" |
Tone"8" points = 52*1 + 40*4 = 212 |
Tone"3" points = 52*1 + 0*20 = 52 |
Actual DTMF Tone"3" |
Tone"8" points = 80*1 + 0*4 = 80 |
Tone"3" points = 80*1 + 6*20 = 200 |
So in the case of those two DTMF tone examples, even though the primary periods were both indistinguishable (85 and 86) there was enough difference in the weighted points that were allocated to clearly identify each DTMF tone and meet the required spec for +/- 1.5% frequency deviation in all the tested periods. |
A practical DTMF decoder? |
Given that we can very easily collect the periods on a small PIC and can very easily add weighted points values into 16 "Tone accumulators" for each period we get, this should be a workable DTMF decoder system. |
My first practical decoder seems to work ok and uses this system; |
1. HI-LO debounce of 220uS continuous state |
2. Record 30 consecutive periods, as bytes, using TMR0 with resolution 16uS |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.