Goertzel algorithm
The Goertzel algorithm, proposed by Gerald Goertzel in 1958, is a category of discrete Fourier transform for digital signal processing, and is designed to calculate the energy of a particular frequency signal from a given sample for evaluation of validity.
The algorithm has several key parameters:
- Sample rate R, which is the number of samples per second that the data needs to be analyzed
- Target frequency F, which refers to the value of this frequency that needs to be detected and evaluated
- The number of sampled values in the detection section N, that is, every n sampling this algorithm will be evaluated for frequency f
- The detection section contains the total number of cycles of the target frequency K
It is clear that the above parameters should have such a relationship:
The k value should be an integer and be of the right size. If too large, not conducive to the detection of aging, if too small, then the detection may be inaccurate. For example more than 10 or even more than 20 should be relatively reasonable. For example, R is 8000, need to detect the frequency of 800hz, n value 100, then K is 10, very good. However, the frequency of detection is sometimes not so complete, such as 697, then n should be worth what? n within 1000, can not get an integer k, we can only retreat and second, find a rounding error of the least. For example, N takes a 115,k with a result of 10.019375, rounded to 10, and has a smaller error.
With the above parameters, we then calculate the radian ω that each sample occupies in a period of one target frequency. Since n samples represent K-cycles (2π), then ω should calculate this:
Note here that, because the K value may have been rounded, so the above two formulas must be calculated successively, must not be combined to simplify the k out!
Then we can get the coefficient C that the post-calculation will frequently encounter:
The above parameters can be calculated in advance and do not have to be recalculated in each sample analysis. After that, the analysis is calculated for n samples
Initialize First:
Then according to the order of n sampling each value s (this s is generally a 16-bit integer, the value range between 32768 to 32767, if you get an already encoded media stream, such as g.711 encoding, then need to decode first), do the following calculation:
When the above calculation is complete, we can get the energy value p of the frequency F reflected in these n samples:
DTMF Recognition
The above is the whole idea of Goertzel algorithm. If we want to use it for DTMF recognition, we still need to do some work. DTMF recognition, we need to according to a given time of sampling, to maximize the elimination of noise interference, the effective DTMF signal recognition
We know that DTMF has 8 frequencies: 697, 770, 852, 941, 1209, 1336, 1477, 1633, 4 symbols are determined by 4 combinations of the first 22 frequencies and 16 frequencies. So when we are dealing with sampling for a given period of time, we need to divide it into multiple sampling sections for each nth sample, and then calculate each frequency's energy p by using the Goertzel algorithm for 8 frequencies respectively.
In order to complete the calculation of these energies, we need to start to calculate the coefficient c separately for 8 different frequencies, and the choice of parameter N is very important, because the K values of 8 frequencies are different, we want to make as much as possible the K value of 8 frequency rounding after the error as small as possible, after testing, at the sampling rate of 8000, n= 205 should be an optimal value, you can do a test:
n=
After calculating the energy p for the n sampled values for each of the 8 frequencies, we can begin to evaluate if the energy values are sufficient to indicate that the N samples contain a certain DTMF symbol
The correspondence between DTMF symbols and frequencies is as follows:
F |
1209 |
1336 |
1477 |
1633 |
679 |
1 |
2 |
3 |
A |
770 |
4 |
5 |
6 |
B |
852 |
7 |
8 |
9 |
C |
941 |
* |
0 |
# |
D |
We take the maximum value from the energy p corresponding to 1209, 1336, 1477 and 16,334 frequencies, as PX, and remove the maximum py from the energy p corresponding to 679,770,852 and 9,414 frequencies. Then the frequency combination of PX and py is very likely to represent the recognition of a DTMF symbol. However, we also need to make a series of judgments to further evaluate:
- are px and py strong enough? We can set a threshold, if both PX and PY are below this threshold, then n samples are evaluated to not recognize the DTMF symbol. Reference [2] suggests that the threshold value is 400000.
- is the difference between PX and py too large? The normal DTMF signal, the two energies should be close, then if the difference is large, we are considered invalid. The recommended method in reference [2] is: if py < Px * 0.398, then it is considered invalid. If px < Py * 0.158 is also considered invalid. But in fact, we changed the 0.158 to 0.6, and the recognition effect was better.
- Are there many other frequencies of energy p that are close to PX and py? reference [2] recommended method is: First take near PX and the larger one in the Py, set to PM, if the other frequency p value of more than 2 to achieve the PM 1%, then considered to be noise caused, considered invalid.
If the above three inspection levels are passed, we can evaluate the N samples to include a DTMF symbol, which corresponds to the frequency combination of PX and py.
Resources:
[1] Https://en.wikipedia.org/wiki/Goertzel_algorithm
[2] https://en.wikipedia.org/w/index.php?title=Goertzel_algorithm&oldid=17802166
Identifying DTMF signals using the Goertzel algorithm