comparison spandsp-0.0.3/spandsp-0.0.3/src/super_tone_rx.c @ 5:f762bf195c4b

import spandsp-0.0.3
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 16:00:21 +0200
parents
children
comparison
equal deleted inserted replaced
4:26cd8f1ef0b1 5:f762bf195c4b
1 /*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * super_tone_rx.c - Flexible telephony supervisory tone detection.
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2003 Steve Underwood
9 *
10 * All rights reserved.
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2, as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * $Id: super_tone_rx.c,v 1.13 2006/11/19 14:07:25 steveu Exp $
26 */
27
28 /*! \file */
29
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include <stdlib.h>
35 #include <string.h>
36 #include <stdio.h>
37 #include <fcntl.h>
38 #include <ctype.h>
39 #include <time.h>
40 #include <inttypes.h>
41 #if defined(HAVE_TGMATH_H)
42 #include <tgmath.h>
43 #endif
44 #if defined(HAVE_MATH_H)
45 #include <math.h>
46 #endif
47
48 #include "spandsp/telephony.h"
49 #include "spandsp/tone_detect.h"
50 #include "spandsp/tone_generate.h"
51 #include "spandsp/super_tone_rx.h"
52
53 #define THRESHOLD 8.0e7
54
55 static int add_super_tone_freq(super_tone_rx_descriptor_t *desc, int freq)
56 {
57 int i;
58
59 if (freq == 0)
60 return -1;
61 /* Look for an existing frequency */
62 for (i = 0; i < desc->used_frequencies; i++)
63 {
64 if (desc->pitches[i][0] == freq)
65 return desc->pitches[i][1];
66 }
67 /* Look for an existing tone which is very close. We may need to merge
68 the detectors. */
69 for (i = 0; i < desc->used_frequencies; i++)
70 {
71 if ((desc->pitches[i][0] - 10) <= freq && freq <= (desc->pitches[i][0] + 10))
72 {
73 /* Merge these two */
74 desc->pitches[desc->used_frequencies][0] = freq;
75 desc->pitches[desc->used_frequencies][1] = i;
76 make_goertzel_descriptor(&desc->desc[desc->pitches[i][1]], (float) (freq + desc->pitches[i][0])/2, BINS);
77 desc->used_frequencies++;
78 return desc->pitches[i][1];
79 }
80 }
81 desc->pitches[i][0] = freq;
82 desc->pitches[i][1] = desc->monitored_frequencies;
83 if (desc->monitored_frequencies%5 == 0)
84 {
85 desc->desc = (goertzel_descriptor_t *) realloc(desc->desc, (desc->monitored_frequencies + 5)*sizeof(goertzel_descriptor_t));
86 }
87 make_goertzel_descriptor(&desc->desc[desc->monitored_frequencies++], (float) freq, BINS);
88 desc->used_frequencies++;
89 return desc->pitches[i][1];
90 }
91 /*- End of function --------------------------------------------------------*/
92
93 int super_tone_rx_add_tone(super_tone_rx_descriptor_t *desc)
94 {
95 if (desc->tones%5 == 0)
96 {
97 desc->tone_list = (super_tone_rx_segment_t **) realloc(desc->tone_list, (desc->tones + 5)*sizeof(super_tone_rx_segment_t *));
98 desc->tone_segs = (int *) realloc(desc->tone_segs, (desc->tones + 5)*sizeof(int));
99 }
100 desc->tone_list[desc->tones] = NULL;
101 desc->tone_segs[desc->tones] = 0;
102 desc->tones++;
103 return desc->tones - 1;
104 }
105 /*- End of function --------------------------------------------------------*/
106
107 int super_tone_rx_add_element(super_tone_rx_descriptor_t *desc,
108 int tone,
109 int f1,
110 int f2,
111 int min,
112 int max)
113 {
114 int step;
115
116 step = desc->tone_segs[tone];
117 if (step%5 == 0)
118 {
119 desc->tone_list[tone] = (super_tone_rx_segment_t *) realloc(desc->tone_list[tone], (step + 5)*sizeof(super_tone_rx_segment_t));
120 }
121 desc->tone_list[tone][step].f1 = add_super_tone_freq(desc, f1);
122 desc->tone_list[tone][step].f2 = add_super_tone_freq(desc, f2);
123 desc->tone_list[tone][step].min_duration = min*8;
124 desc->tone_list[tone][step].max_duration = (max == 0) ? 0x7FFFFFFF : max*8;
125 desc->tone_segs[tone]++;
126 return step;
127 }
128 /*- End of function --------------------------------------------------------*/
129
130 static int test_cadence(super_tone_rx_segment_t *pattern,
131 int steps,
132 super_tone_rx_segment_t *test,
133 int rotation)
134 {
135 int i;
136 int j;
137
138 if (rotation >= 0)
139 {
140 /* Check only for the sustaining of a tone in progress. This means
141 we only need to check each block if the latest step is compatible
142 with the tone template. */
143 if (steps < 0)
144 {
145 /* A -ve value for steps indicates we just changed step, and need to
146 check the last one ended within spec. If we don't do this
147 extra test a low duration segment might be accepted as OK. */
148 steps = -steps;
149 j = (rotation + steps - 2)%steps;
150 if (pattern[j].f1 != test[8].f1 || pattern[j].f2 != test[8].f2)
151 return 0;
152 if (pattern[j].min_duration > test[8].min_duration*BINS
153 ||
154 pattern[j].max_duration < test[8].min_duration*BINS)
155 {
156 return 0;
157 }
158 }
159 j = (rotation + steps - 1)%steps;
160 if (pattern[j].f1 != test[9].f1 || pattern[j].f2 != test[9].f2)
161 return 0;
162 if (pattern[j].max_duration < test[9].min_duration*BINS)
163 return 0;
164 }
165 else
166 {
167 /* Look for a complete template match. */
168 for (i = 0; i < steps; i++)
169 {
170 j = i + 10 - steps;
171 if (pattern[i].f1 != test[j].f1 || pattern[i].f2 != test[j].f2)
172 return 0;
173 if (pattern[i].min_duration > test[j].min_duration*BINS
174 ||
175 pattern[i].max_duration < test[j].min_duration*BINS)
176 {
177 return 0;
178 }
179 }
180 }
181 return 1;
182 }
183 /*- End of function --------------------------------------------------------*/
184
185 super_tone_rx_descriptor_t *super_tone_rx_make_descriptor(super_tone_rx_descriptor_t *desc)
186 {
187 if (desc == NULL)
188 {
189 desc = (super_tone_rx_descriptor_t *) malloc(sizeof(super_tone_rx_descriptor_t));
190 if (desc == NULL)
191 return NULL;
192 }
193 desc->tone_list = NULL;
194 desc->tone_segs = NULL;
195
196 desc->used_frequencies = 0;
197 desc->monitored_frequencies = 0;
198 desc->desc = NULL;
199 desc->tones = 0;
200 return desc;
201 }
202 /*- End of function --------------------------------------------------------*/
203
204 int super_tone_rx_free_descriptor(super_tone_rx_descriptor_t *desc)
205 {
206 if (desc)
207 free(desc);
208 return 0;
209 }
210 /*- End of function --------------------------------------------------------*/
211
212 void super_tone_rx_segment_callback(super_tone_rx_state_t *s,
213 void (*callback)(void *data, int f1, int f2, int duration))
214 {
215 s->segment_callback = callback;
216 }
217 /*- End of function --------------------------------------------------------*/
218
219 super_tone_rx_state_t *super_tone_rx_init(super_tone_rx_state_t *s,
220 super_tone_rx_descriptor_t *desc,
221 tone_report_func_t callback,
222 void *user_data)
223 {
224 int i;
225
226 if (desc == NULL)
227 return NULL;
228 if (s == NULL)
229 {
230 s = (super_tone_rx_state_t *) malloc(sizeof(super_tone_rx_state_t) + desc->monitored_frequencies*sizeof(goertzel_state_t));
231 if (s == NULL)
232 return NULL;
233 }
234 if (callback == NULL)
235 return NULL;
236
237 for (i = 0; i < 11; i++)
238 {
239 s->segments[i].f1 = -1;
240 s->segments[i].f2 = -1;
241 s->segments[i].min_duration = 0;
242 }
243 s->segment_callback = NULL;
244 s->tone_callback = callback;
245 s->callback_data = user_data;
246 if (desc)
247 s->desc = desc;
248 s->detected_tone = -1;
249 s->energy = 0.0;
250 s->total_energy = 0.0;
251 for (i = 0; i < desc->monitored_frequencies; i++)
252 goertzel_init(&s->state[i], &s->desc->desc[i]);
253 return s;
254 }
255 /*- End of function --------------------------------------------------------*/
256
257 int super_tone_rx_free(super_tone_rx_state_t *s)
258 {
259 if (s)
260 free(s);
261 return 0;
262 }
263 /*- End of function --------------------------------------------------------*/
264
265 int super_tone_rx(super_tone_rx_state_t *s, const int16_t *amp, int samples)
266 {
267 int i;
268 int j;
269 int k1;
270 int k2;
271 int x;
272 float res[BINS/2];
273 int sample;
274
275 for (sample = 0; sample < samples; sample += x)
276 {
277 x = 0;
278 for (i = 0; i < s->desc->monitored_frequencies; i++)
279 {
280 x = goertzel_update(&s->state[i],
281 amp + sample,
282 samples - sample);
283 if (i == s->desc->monitored_frequencies - 1)
284 {
285 for (j = 0; j < x; j++)
286 s->energy += amp[sample + j]*amp[sample + j];
287 }
288 if (s->state[i].current_sample >= s->state[i].samples)
289 {
290 res[i] = goertzel_result(&s->state[i]);
291 goertzel_init(&s->state[i], &s->desc->desc[i]);
292 if (i == s->desc->monitored_frequencies - 1)
293 {
294 /* Scale the energy so it can be compared to the results from the
295 Goertzel filters. */
296 s->total_energy = s->energy*(s->state[i].samples/2);
297 s->energy = 0;
298 /* Find our two best monitored frequencies, which also have adequate
299 energy. */
300 if (s->total_energy < THRESHOLD)
301 {
302 k1 = -1;
303 k2 = -1;
304 }
305 else
306 {
307 if (res[0] > res[1])
308 {
309 k1 = 0;
310 k2 = 1;
311 }
312 else
313 {
314 k1 = 1;
315 k2 = 0;
316 }
317 for (j = 2; j < s->desc->monitored_frequencies; j++)
318 {
319 if (res[j] >= res[k1])
320 {
321 k2 = k1;
322 k1 = j;
323 }
324 else if (res[j] >= res[k2])
325 {
326 k2 = j;
327 }
328 }
329 if (res[k1] + res[k2] < 0.5*s->total_energy)
330 {
331 k1 = -1;
332 k2 = -1;
333 }
334 else if (res[k1] > 4.0*res[k2])
335 {
336 k2 = -1;
337 }
338 else if (k2 < k1)
339 {
340 j = k1;
341 k1 = k2;
342 k2 = j;
343 }
344 }
345 /* See if this looks different to last time */
346 if (k1 != s->segments[10].f1 || k2 != s->segments[10].f2)
347 {
348 /* It is different, but this might just be a transitional quirk, or
349 a one shot hiccup (eg due to noise). Only if this same thing is
350 seen a second time should we change state. */
351 s->segments[10].f1 = k1;
352 s->segments[10].f2 = k2;
353 /* While things are hopping around, consider this a continuance of the
354 previous state. */
355 s->segments[9].min_duration++;
356 }
357 else
358 {
359 if (k1 != s->segments[9].f1 || k2 != s->segments[9].f2)
360 {
361 if (s->detected_tone >= 0)
362 {
363 /* Test for the continuance of the existing tone pattern, based on our new knowledge of an
364 entire segment length. */
365 if (!test_cadence(s->desc->tone_list[s->detected_tone], -s->desc->tone_segs[s->detected_tone], s->segments, s->rotation++))
366 {
367 s->detected_tone = -1;
368 s->tone_callback(s->callback_data, s->detected_tone);
369 }
370 }
371 if (s->segment_callback)
372 {
373 s->segment_callback(s->callback_data,
374 s->segments[9].f1,
375 s->segments[9].f2,
376 s->segments[9].min_duration*BINS/8);
377 }
378 memcpy (&s->segments[0], &s->segments[1], 9*sizeof(s->segments[0]));
379 s->segments[9].f1 = k1;
380 s->segments[9].f2 = k2;
381 s->segments[9].min_duration = 1;
382 }
383 else
384 {
385 /* This is a continuance of the previous state */
386 if (s->detected_tone >= 0)
387 {
388 /* Test for the continuance of the existing tone pattern. We must do this here, so we can sense the
389 discontinuance of the tone on an excessively long segment. */
390 if (!test_cadence(s->desc->tone_list[s->detected_tone], s->desc->tone_segs[s->detected_tone], s->segments, s->rotation))
391 {
392 s->detected_tone = -1;
393 s->tone_callback(s->callback_data, s->detected_tone);
394 }
395 }
396 s->segments[9].min_duration++;
397 }
398 }
399 if (s->detected_tone < 0)
400 {
401 /* Test for the start of any of the monitored tone patterns */
402 for (j = 0; j < s->desc->tones; j++)
403 {
404 if (test_cadence(s->desc->tone_list[j], s->desc->tone_segs[j], s->segments, -1))
405 {
406 s->detected_tone = j;
407 s->rotation = 0;
408 s->tone_callback(s->callback_data, s->detected_tone);
409 break;
410 }
411 }
412 }
413 }
414 }
415 }
416 }
417 return samples;
418 }
419 /*- End of function --------------------------------------------------------*/
420 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.