spandsp: tone_detect.h Source File

spandsp 0.0.6

tone_detect.h
1/*
2 * SpanDSP - a series of DSP components for telephony
3 *
4 * tone_detect.h - General telephony tone detection.
5 *
6 * Written by Steve Underwood <steveu@coppice.org>
7 *
8 * Copyright (C) 2001, 2005 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 Lesser General Public License version 2.1,
14 * as 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 Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26#if !defined(_SPANDSP_TONE_DETECT_H_)
27#define _SPANDSP_TONE_DETECT_H_
28
29/*!
30 Goertzel filter descriptor.
31*/
32 struct goertzel_descriptor_s
33{
34#if defined(SPANDSP_USE_FIXED_POINT)
35 int16_t fac;
36#else
37 float fac;
38#endif
39 int samples;
40};
41
42/*!
43 Goertzel filter state descriptor.
44*/
45 struct goertzel_state_s
46{
47#if defined(SPANDSP_USE_FIXED_POINT)
48 int16_t v2;
49 int16_t v3;
50 int16_t fac;
51#else
52 float v2;
53 float v3;
54 float fac;
55#endif
56 int samples;
57 int current_sample;
58};
59
60/*!
61 Goertzel filter descriptor.
62*/
63typedef struct goertzel_descriptor_s goertzel_descriptor_t;
64
65/*!
66 Goertzel filter state descriptor.
67*/
68typedef struct goertzel_state_s goertzel_state_t;
69
70#if defined(__cplusplus)
71extern "C"
72{
73#endif
74
75/*! \brief Create a descriptor for use with either a Goertzel transform */
76SPAN_DECLARE(void) make_goertzel_descriptor(goertzel_descriptor_t *t,
77 float freq,
78 int samples);
79
80/*! \brief Initialise the state of a Goertzel transform.
81 \param s The Goertzel context. If NULL, a context is allocated with malloc.
82 \param t The Goertzel descriptor.
83 \return A pointer to the Goertzel state. */
84SPAN_DECLARE(goertzel_state_t *) goertzel_init(goertzel_state_t *s,
85 goertzel_descriptor_t *t);
86
87SPAN_DECLARE(int) goertzel_release(goertzel_state_t *s);
88
89SPAN_DECLARE(int) goertzel_free(goertzel_state_t *s);
90
91/*! \brief Reset the state of a Goertzel transform.
92 \param s The Goertzel context. */
93SPAN_DECLARE(void) goertzel_reset(goertzel_state_t *s);
94
95/*! \brief Update the state of a Goertzel transform.
96 \param s The Goertzel context.
97 \param amp The samples to be transformed.
98 \param samples The number of samples.
99 \return The number of samples unprocessed */
100SPAN_DECLARE(int) goertzel_update(goertzel_state_t *s,
101 const int16_t amp[],
102 int samples);
103
104/*! \brief Evaluate the final result of a Goertzel transform.
105 \param s The Goertzel context.
106 \return The result of the transform. The expected result for a pure sine wave
107 signal of level x dBm0, at the very centre of the bin is:
108 [Floating point] ((samples_per_goertzel_block*32768.0/1.4142)*10^((x - DBM0_MAX_SINE_POWER)/20.0))^2
109 [Fixed point] ((samples_per_goertzel_block*256.0/1.4142)*10^((x - DBM0_MAX_SINE_POWER)/20.0))^2 */
110#if defined(SPANDSP_USE_FIXED_POINT)
111SPAN_DECLARE(int32_t) goertzel_result(goertzel_state_t *s);
112#else
113SPAN_DECLARE(float) goertzel_result(goertzel_state_t *s);
114#endif
115
116/*! \brief Update the state of a Goertzel transform.
117 \param s The Goertzel context.
118 \param amp The sample to be transformed. */
119static __inline__ void goertzel_sample(goertzel_state_t *s, int16_t amp)
120{
121#if defined(SPANDSP_USE_FIXED_POINT)
122 int16_t x;
123 int16_t v1;
124#else
125 float v1;
126#endif
127
128 v1 = s->v2;
129 s->v2 = s->v3;
130#if defined(SPANDSP_USE_FIXED_POINT)
131 x = (((int32_t) s->fac*s->v2) >> 14);
132 /* Scale down the input signal to avoid overflows. 9 bits is enough to
133 monitor the signals of interest with adequate dynamic range and
134 resolution. In telephony we generally only start with 13 or 14 bits,
135 anyway. */
136 s->v3 = x - v1 + (amp >> 7);
137#else
138 s->v3 = s->fac*s->v2 - v1 + amp;
139#endif
140 s->current_sample++;
141}
142/*- End of function --------------------------------------------------------*/
143
144/* Scale down the input signal to avoid overflows. 9 bits is enough to
145 monitor the signals of interest with adequate dynamic range and
146 resolution. In telephony we generally only start with 13 or 14 bits,
147 anyway. This is sufficient for the longest Goertzel we currently use. */
148#if defined(SPANDSP_USE_FIXED_POINT)
149#define goertzel_preadjust_amp(amp) (((int16_t) amp) >> 7)
150#else
151#define goertzel_preadjust_amp(amp) ((float) amp)
152#endif
153
154/* Minimal update the state of a Goertzel transform. This is similar to
155 goertzel_sample, but more suited to blocks of Goertzels. It assumes
156 the amplitude is pre-shifted, and does not update the per-state sample
157 count.
158 \brief Update the state of a Goertzel transform.
159 \param s The Goertzel context.
160 \param amp The adjusted sample to be transformed. */
161#if defined(SPANDSP_USE_FIXED_POINT)
162static __inline__ void goertzel_samplex(goertzel_state_t *s, int16_t amp)
163#else
164static __inline__ void goertzel_samplex(goertzel_state_t *s, float amp)
165#endif
166{
167#if defined(SPANDSP_USE_FIXED_POINT)
168 int16_t x;
169 int16_t v1;
170#else
171 float v1;
172#endif
173
174 v1 = s->v2;
175 s->v2 = s->v3;
176#if defined(SPANDSP_USE_FIXED_POINT)
177 x = (((int32_t) s->fac*s->v2) >> 14);
178 s->v3 = x - v1 + amp;
179#else
180 s->v3 = s->fac*s->v2 - v1 + amp;
181#endif
182}
183/*- End of function --------------------------------------------------------*/
184
185/*! Generate a Hamming weighted coefficient set, to be used for a periodogram analysis.
186 \param coeffs The generated coefficients.
187 \param freq The frequency to be matched by the periodogram, in Hz.
188 \param sample_rate The sample rate of the signal, in samples per second.
189 \param window_len The length of the periodogram window. This must be an even number.
190 \return The number of generated coefficients.
191*/
192SPAN_DECLARE(int) periodogram_generate_coeffs(complexf_t coeffs[], float freq, int sample_rate, int window_len);
193
194/*! Generate the phase offset to be expected between successive periodograms evaluated at the
195 specified interval.
196 \param offset A point to the generated phase offset.
197 \param freq The frequency being matched by the periodogram, in Hz.
198 \param sample_rate The sample rate of the signal, in samples per second.
199 \param interval The interval between periodograms, in samples.
200 \return The scaling factor.
201*/
202SPAN_DECLARE(float) periodogram_generate_phase_offset(complexf_t *offset, float freq, int sample_rate, int interval);
203
204/*! Evaluate a periodogram.
205 \param coeffs A set of coefficients generated by periodogram_generate_coeffs().
206 \param amp The complex amplitude of the signal.
207 \param len The length of the periodogram, in samples. This must be an even number.
208 \return The periodogram result.
209*/
210SPAN_DECLARE(complexf_t) periodogram(const complexf_t coeffs[], const complexf_t amp[], int len);
211
212/*! Prepare data for evaluating a set of periodograms.
213 \param sum A vector of sums of pairs of signal samples. This will be half the length of len.
214 \param diff A vector of differences between pairs of signal samples. This will be half the length of len.
215 \param amp The complex amplitude of the signal.
216 \param len The length of the periodogram, in samples. This must be an even number.
217 \return The length of the vectors sum and diff.
218*/
219SPAN_DECLARE(int) periodogram_prepare(complexf_t sum[], complexf_t diff[], const complexf_t amp[], int len);
220
221/*! Evaluate a periodogram, based on data prepared by periodogram_prepare(). This is more efficient
222 than using periodogram() when several periodograms are to be applied to the same signal.
223 \param coeffs A set of coefficients generated by periodogram_generate_coeffs().
224 \param sum A vector of sums produced by periodogram_prepare().
225 \param diff A vector of differences produced by periodogram_prepare().
226 \param len The length of the periodogram, in samples. This must be an even number.
227 \return The periodogram result.
228*/
229SPAN_DECLARE(complexf_t) periodogram_apply(const complexf_t coeffs[], const complexf_t sum[], const complexf_t diff[], int len);
230
231/*! Apply a phase offset, to find the frequency error between periodogram evaluations.
232 specified interval.
233 \param phase_offset A point to the expected phase offset.
234 \param scale The scaling factor to be used.
235 \param last_result A pointer to the previous periodogram result.
236 \param result A pointer to the current periodogram result.
237 \return The frequency error, in Hz.
238*/
239SPAN_DECLARE(float) periodogram_freq_error(const complexf_t *phase_offset, float scale, const complexf_t *last_result, const complexf_t *result);
240
241#if defined(__cplusplus)
242}
243#endif
244
245#endif
246/*- End of file ------------------------------------------------------------*/
complexf_t
Definition complex.h:43
goertzel_descriptor_s
Definition tone_detect.h:33
goertzel_state_s
Definition tone_detect.h:46