summaryrefslogtreecommitdiffstats
path: root/src/libdtmf/dtmf_decode.c
blob: 740d5049850728ccd7c679f8fbb851ce734df566 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* DTMF coder
 *
 * (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include "../libsample/sample.h"
#include "dtmf_decode.h"

//#define DEBUG

#define level2db(level)	(20 * log10(level))
#define db2level(db)	pow(10, (double)db / 20.0)

#define DTMF_LOW_1	697.0
#define DTMF_LOW_2	770.0
#define DTMF_LOW_3	852.0
#define DTMF_LOW_4	941.0
#define DTMF_HIGH_1	1209.0
#define DTMF_HIGH_2	1336.0
#define DTMF_HIGH_3	1477.0
#define DTMF_HIGH_4	1633.0

static const char dtmf_digit[] = "     123A456B789C*0#D";

#ifdef DEBUG
const char *_debug_amplitude(double level)
{
        static char text[42];

	strcpy(text, "                    :                    ");
	if (level > 1.0)
		level = 1.0;
	if (level < -1.0)
		level = -1.0;
	text[20 + (int)(level * 20)] = '*';

	return text;
}
#endif

int dtmf_decode_init(dtmf_dec_t *dtmf, void *priv, void (*recv_digit)(void *priv, char digit, dtmf_meas_t *meas), int samplerate, double max_amplitude, double min_amplitude)
{
	int rc;

	memset(dtmf, 0, sizeof(*dtmf));
	dtmf->priv = priv;
	dtmf->recv_digit = recv_digit;
	dtmf->samplerate = samplerate;
	dtmf->freq_margin = 1.03; /* 1.8 .. 3.5 % */
	dtmf->max_amplitude = max_amplitude;
	dtmf->min_amplitude = min_amplitude;
	dtmf->forward_twist = db2level(4.0);
	dtmf->reverse_twist = db2level(8.0);
	dtmf->time_detect = (int)(0.025 * (double)samplerate);
	dtmf->time_meas = (int)(0.015 * (double)samplerate);
	dtmf->time_pause = (int)(0.010 * (double)samplerate);

	/* init fm demodulator */
	rc = fm_demod_init(&dtmf->demod_low, (double)samplerate, (DTMF_LOW_1 + DTMF_LOW_4) / 2.0, DTMF_LOW_4 - DTMF_LOW_1);
	if (rc < 0)
		goto error;
	rc = fm_demod_init(&dtmf->demod_high, (double)samplerate, (DTMF_HIGH_1 + DTMF_HIGH_4) / 2.0, DTMF_HIGH_4 - DTMF_HIGH_1);
	if (rc < 0)
		goto error;

	/* use fourth order (2 iter) filter, since it is as fast as second order (1 iter) filter */
	iir_lowpass_init(&dtmf->freq_lp[0], 100.0, samplerate, 2);
	iir_lowpass_init(&dtmf->freq_lp[1], 100.0, samplerate, 2);

	return 0;

error:
	dtmf_decode_exit(dtmf);
	return rc;
}

void dtmf_decode_exit(dtmf_dec_t *dtmf)
{
	fm_demod_exit(&dtmf->demod_low);
	fm_demod_exit(&dtmf->demod_high);
}

void dtmf_decode_filter(dtmf_dec_t *dtmf, sample_t *samples, int length, sample_t *frequency_low, sample_t *frequency_high, sample_t *amplitude_low, sample_t *amplitude_high)
{
	sample_t I_low[length], Q_low[length];
	sample_t I_high[length], Q_high[length];
	int i;

	fm_demodulate_real(&dtmf->demod_low, frequency_low, length, samples, I_low, Q_low);
	fm_demodulate_real(&dtmf->demod_high, frequency_high, length, samples, I_high, Q_high);
	/* peak amplitude is the length of I/Q vector
	 * since we filter out the unwanted modulation product, the vector is only half of length */
	for (i = 0; i < length; i++) {
		amplitude_low[i] = sqrt(I_low[i] * I_low[i] + Q_low[i] * Q_low[i]) * 2.0;
		amplitude_high[i] = sqrt(I_high[i] * I_high[i] + Q_high[i] * Q_high[i]) * 2.0;
	}
	iir_process(&dtmf->freq_lp[0], frequency_low, length);
	iir_process(&dtmf->freq_lp[1], frequency_high, length);
}
void dtmf_decode(dtmf_dec_t *dtmf, sample_t *samples, int length)
{
	sample_t frequency_low[length], amplitude_low[length];
	sample_t frequency_high[length], amplitude_high[length];
	double margin, min_amplitude, max_amplitude, forward_twist, reverse_twist, f1, f2;
	int time_detect, time_meas, time_pause;
	int low = 0, high = 0;
	char detected, digit;
	int count;
	int amplitude_ok, twist_ok;
	int i;

	margin = dtmf->freq_margin;
	min_amplitude = dtmf->min_amplitude;
	max_amplitude = dtmf->max_amplitude;
	forward_twist = dtmf->forward_twist;
	reverse_twist = dtmf->reverse_twist;
	time_detect = dtmf->time_detect;
	time_meas = dtmf->time_meas;
	time_pause = dtmf->time_pause;
	detected = dtmf->detected;
	count = dtmf->count;

	/* FM/AM demod */
	dtmf_decode_filter(dtmf, samples, length, frequency_low, frequency_high, amplitude_low, amplitude_high);

	for (i = 0; i < length; i++) {
#ifdef DEBUG
//		printf("%s %.5f\n", _debug_amplitude(samples[i]/2.0), samples[i]/2.0);
#endif
		/* get frequency of low frequencies, correct amplitude drop at cutoff point */
		f1 = frequency_low[i] + (DTMF_LOW_1 + DTMF_LOW_4) / 2.0;
		if (f1 >= DTMF_LOW_1 / margin && f1 <= DTMF_LOW_1 * margin) {
			/* cutoff point */
			amplitude_low[i] /= 0.7071;
			low = 1;
			f1 -= DTMF_LOW_1;
		} else
		if (f1 >= DTMF_LOW_2 / margin && f1 <= DTMF_LOW_2 * margin) {
			amplitude_low[i] /= 1.0734;
			low = 2;
			f1 -= DTMF_LOW_2;
		} else
		if (f1 >= DTMF_LOW_3 / margin && f1 <= DTMF_LOW_3 * margin) {
			amplitude_low[i] /= 1.0389;
			low = 3;
			f1 -= DTMF_LOW_3;
		} else
		if (f1 >= DTMF_LOW_4 / margin && f1 <= DTMF_LOW_4 * margin) {
			/* cutoff point */
			amplitude_low[i] /= 0.7071;
			low = 4;
			f1 -= DTMF_LOW_4;
		} else
			low = 0;
		/* get frequency of high frequencies, correct amplitude drop at cutoff point */
		f2 = frequency_high[i] + (DTMF_HIGH_1 + DTMF_HIGH_4) / 2.0;
		if (f2 >= DTMF_HIGH_1 / margin && f2 <= DTMF_HIGH_1 * margin) {
			/* cutoff point */
			amplitude_high[i] /= 0.7071;
			high = 1;
			f2 -= DTMF_HIGH_1;
		} else
		if (f2 >= DTMF_HIGH_2 / margin && f2 <= DTMF_HIGH_2 * margin) {
			amplitude_high[i] /= 1.0731;
			high = 2;
			f2 -= DTMF_HIGH_2;
		} else
		if (f2 >= DTMF_HIGH_3 / margin && f2 <= DTMF_HIGH_3 * margin) {
			amplitude_high[i] /= 1.0372;
			high = 3;
			f2 -= DTMF_HIGH_3;
		} else
		if (f2 >= DTMF_HIGH_4 / margin && f2 <= DTMF_HIGH_4 * margin) {
			/* cutoff point */
			amplitude_high[i] /= 0.7071;
			high = 4;
			f2 -= DTMF_HIGH_4;
		} else
			high = 0;
		digit = 0;
		amplitude_ok = 0;
		twist_ok = 0;
		if (low && high) {
			digit = dtmf_digit[low*4+high];
			/* check for limits */
			if (amplitude_low[i] <= max_amplitude && amplitude_low[i] >= min_amplitude && amplitude_high[i] <= max_amplitude && amplitude_high[i] >= min_amplitude) {
				amplitude_ok = 1;
#ifdef DEBUG
				printf("%.1f %.1f (limits %.1f .. %.1f) %.1f\n", level2db(amplitude_low[i]), level2db(amplitude_high[i]), level2db(min_amplitude), level2db(max_amplitude), level2db(amplitude_high[i] / amplitude_low[i]));
#endif
				if (amplitude_high[i] / amplitude_low[i] <= forward_twist && amplitude_low[i] / amplitude_high[i] <= reverse_twist)
					twist_ok = 1;
			}
		}

		if (!detected) {
			if (digit && amplitude_ok && twist_ok) {
				if (count == 0) {
					memset(&dtmf->meas, 0, sizeof(dtmf->meas));
				}
				if (count >= time_meas) {
					dtmf->meas.frequency_low += f1;
					dtmf->meas.frequency_high += f2;
					dtmf->meas.amplitude_low += amplitude_low[i];
					dtmf->meas.amplitude_high += amplitude_high[i];
					dtmf->meas.count++;
				}
				count++;
				if (count >= time_detect) {
					detected = digit;
					dtmf->meas.frequency_low /= dtmf->meas.count;
					dtmf->meas.frequency_high /= dtmf->meas.count;
					dtmf->meas.amplitude_low /= dtmf->meas.count;
					dtmf->meas.amplitude_high /= dtmf->meas.count;
					dtmf->meas.count = 1;
					dtmf->recv_digit(dtmf->priv, digit, &dtmf->meas);
				}
			} else
				count = 0;
		} else {
			if (!digit || digit != detected || !amplitude_ok || !twist_ok) {
				count++;
				if (count >= time_pause) {
					detected = 0;
#ifdef DEBUG
					printf("lost!\n");
#endif
				}
			} else
				count = 0;
		}
#ifdef DEBUG
		if (digit)
			printf("DTMF tone='%c' diff frequency=%.1f %.1f amplitude=%.1f %.1f dB (%s) twist=%.1f dB (%s)\n", digit, f1, f2, level2db(amplitude_low[i]), level2db(amplitude_high[i]), (amplitude_ok) ? "OK" : "nok", level2db(amplitude_high[i] / amplitude_low[i]), (twist_ok) ? "OK" : "nok");
#endif

		dtmf->detected = detected;
		dtmf->count = count;
	}
}