spandsp 3.0.0
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*/
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*/
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/* Convert a power level in dBm0 or dBov to the equivalent result from a Goertzel filter. This is len*len times the actual power, since
61 the DFT calculation accumulates at the square of the number of samples. */
62#if defined(SPANDSP_USE_FIXED_POINT)
63#define goertzel_threshold_dbm0(len,thresh) (int) ((len*len*256.0*256.0/2.0*pow(10.0, (thresh - DBM0_MAX_SINE_POWER)/10.0))
64#define goertzel_threshold_dbmov(len,thresh) (int) ((len*len*256.0*256.0/2.0*pow(10.0, (thresh - DBMOV_MAX_SINE_POWER)/10.0))
65#else
66#define goertzel_threshold_dbm0(len,thresh) (float) ((len*len*32768.0*32768.0/2.0)*pow(10.0, (thresh - DBM0_MAX_SINE_POWER)/10.0))
67#define goertzel_threshold_dbmov(len,thresh) (float) ((len*len*32768.0*32768.0/2.0)*pow(10.0, (thresh - DBMOV_MAX_SINE_POWER)/10.0))
68#endif
69
70/* Convert a power level in dBm0 or dBov to the equivalent energy to expect from an integration over len samples. So, this is len
71 times the actual power. */
72#if defined(SPANDSP_USE_FIXED_POINT)
73#define energy_threshold_dbm0(len,thresh) (int) ((len*256.0*256.0/2.0)*pow(10.0, (thresh - DBM0_MAX_SINE_POWER)/10.0))
74#define energy_threshold_dbmov(len,thresh) (int) ((len*256.0*256.0/2.0)*pow(10.0, (thresh - DBMOV_MAX_SINE_POWER)/10.0))
75#else
76#define energy_threshold_dbm0(len,thresh) (float) ((len*32768.0*32768.0/2.0)*pow(10.0, (thresh - DBM0_MAX_SINE_POWER)/10.0))
77#define energy_threshold_dbmov(len,thresh) (float) ((len*32768.0*32768.0/2.0)*pow(10.0, (thresh - DBMOV_MAX_SINE_POWER)/10.0))
78#endif
79
80/*!
81 Goertzel filter descriptor.
82*/
84
85/*!
86 Goertzel filter state descriptor.
87*/
89
90#if defined(__cplusplus)
91extern "C"
92{
93#endif
94
95/*! \brief Create a descriptor for use with either a Goertzel transform */
96SPAN_DECLARE(void) make_goertzel_descriptor(goertzel_descriptor_t *t,
97 float freq,
98 int samples);
99
100/*! \brief Initialise the state of a Goertzel transform.
101 \param s The Goertzel context. If NULL, a context is allocated.
102 \param t The Goertzel descriptor.
103 \return A pointer to the Goertzel state. */
104SPAN_DECLARE(goertzel_state_t *) goertzel_init(goertzel_state_t *s,
106
107SPAN_DECLARE(int) goertzel_release(goertzel_state_t *s);
108
109SPAN_DECLARE(int) goertzel_free(goertzel_state_t *s);
110
111/*! \brief Reset the state of a Goertzel transform.
112 \param s The Goertzel context. */
113SPAN_DECLARE(void) goertzel_reset(goertzel_state_t *s);
114
115/*! \brief Update the state of a Goertzel transform.
116 \param s The Goertzel context.
117 \param amp The samples to be transformed.
118 \param samples The number of samples.
119 \return The number of samples unprocessed */
120SPAN_DECLARE(int) goertzel_update(goertzel_state_t *s,
121 const int16_t amp[],
122 int samples);
123
124/*! \brief Evaluate the final result of a Goertzel transform.
125 \param s The Goertzel context.
126 \return The result of the transform. The expected result for a pure sine wave
127 signal of level x dBm0, at the very centre of the bin is:
128 [Floating point] ((samples_per_goertzel_block*32768.0/1.4142)*10^((x - DBM0_MAX_SINE_POWER)/20.0))^2
129 [Fixed point] ((samples_per_goertzel_block*256.0/1.4142)*10^((x - DBM0_MAX_SINE_POWER)/20.0))^2 */
130#if defined(SPANDSP_USE_FIXED_POINT)
131SPAN_DECLARE(int32_t) goertzel_result(goertzel_state_t *s);
132#else
133SPAN_DECLARE(float) goertzel_result(goertzel_state_t *s);
134#endif
135
136/*! \brief Update the state of a Goertzel transform.
137 \param s The Goertzel context.
138 \param amp The sample to be transformed. */
139static __inline__ void goertzel_sample(goertzel_state_t *s, int16_t amp)
140{
141#if defined(SPANDSP_USE_FIXED_POINT)
142 int16_t x;
143 int16_t v1;
144#else
145 float v1;
146#endif
147
148 v1 = s->v2;
149 s->v2 = s->v3;
150#if defined(SPANDSP_USE_FIXED_POINT)
151 x = (((int32_t) s->fac*s->v2) >> 14);
152 /* Scale down the input signal to avoid overflows. 9 bits is enough to
153 monitor the signals of interest with adequate dynamic range and
154 resolution. In telephony we generally only start with 13 or 14 bits,
155 anyway. */
156 s->v3 = x - v1 + (amp >> 7);
157#else
158 s->v3 = s->fac*s->v2 - v1 + amp;
159#endif
160 s->current_sample++;
161}
162/*- End of function --------------------------------------------------------*/
163
164/* Scale down the input signal to avoid overflows. 9 bits is enough to
165 monitor the signals of interest with adequate dynamic range and
166 resolution. In telephony we generally only start with 13 or 14 bits,
167 anyway. This is sufficient for the longest Goertzel we currently use. */
168#if defined(SPANDSP_USE_FIXED_POINT)
169#define goertzel_preadjust_amp(amp) (((int16_t) amp) >> 7)
170#else
171#define goertzel_preadjust_amp(amp) ((float) amp)
172#endif
173
174/* Minimal update the state of a Goertzel transform. This is similar to
175 goertzel_sample, but more suited to blocks of Goertzels. It assumes
176 the amplitude is pre-shifted, and does not update the per-state sample
177 count.
178 \brief Update the state of a Goertzel transform.
179 \param s The Goertzel context.
180 \param amp The adjusted sample to be transformed. */
181#if defined(SPANDSP_USE_FIXED_POINT)
182static __inline__ void goertzel_samplex(goertzel_state_t *s, int16_t amp)
183#else
184static __inline__ void goertzel_samplex(goertzel_state_t *s, float amp)
185#endif
186{
187#if defined(SPANDSP_USE_FIXED_POINT)
188 int16_t x;
189 int16_t v1;
190#else
191 float v1;
192#endif
193
194 v1 = s->v2;
195 s->v2 = s->v3;
196#if defined(SPANDSP_USE_FIXED_POINT)
197 x = (((int32_t) s->fac*s->v2) >> 14);
198 s->v3 = x - v1 + amp;
199#else
200 s->v3 = s->fac*s->v2 - v1 + amp;
201#endif
202}
203/*- End of function --------------------------------------------------------*/
204
205/*! Generate a Hamming weighted coefficient set, to be used for a periodogram analysis.
206 \param coeffs The generated coefficients.
207 \param freq The frequency to be matched by the periodogram, in Hz.
208 \param sample_rate The sample rate of the signal, in samples per second.
209 \param window_len The length of the periodogram window. This must be an even number.
210 \return The number of generated coefficients.
211*/
212SPAN_DECLARE(int) periodogram_generate_coeffs(complexf_t coeffs[], float freq, int sample_rate, int window_len);
213
214/*! Generate the phase offset to be expected between successive periodograms evaluated at the
215 specified interval.
216 \param offset A point to the generated phase offset.
217 \param freq The frequency being matched by the periodogram, in Hz.
218 \param sample_rate The sample rate of the signal, in samples per second.
219 \param interval The interval between periodograms, in samples.
220 \return The scaling factor.
221*/
222SPAN_DECLARE(float) periodogram_generate_phase_offset(complexf_t *offset, float freq, int sample_rate, int interval);
223
224/*! Evaluate a periodogram.
225 \param coeffs A set of coefficients generated by periodogram_generate_coeffs().
226 \param amp The complex amplitude of the signal.
227 \param len The length of the periodogram, in samples. This must be an even number.
228 \return The periodogram result.
229*/
230SPAN_DECLARE(complexf_t) periodogram(const complexf_t coeffs[], const complexf_t amp[], int len);
231
232/*! Prepare data for evaluating a set of periodograms.
233 \param sum A vector of sums of pairs of signal samples. This will be half the length of len.
234 \param diff A vector of differences between pairs of signal samples. This will be half the length of len.
235 \param amp The complex amplitude of the signal.
236 \param len The length of the periodogram, in samples. This must be an even number.
237 \return The length of the vectors sum and diff.
238*/
239SPAN_DECLARE(int) periodogram_prepare(complexf_t sum[], complexf_t diff[], const complexf_t amp[], int len);
240
241/*! Evaluate a periodogram, based on data prepared by periodogram_prepare(). This is more efficient
242 than using periodogram() when several periodograms are to be applied to the same signal.
243 \param coeffs A set of coefficients generated by periodogram_generate_coeffs().
244 \param sum A vector of sums produced by periodogram_prepare().
245 \param diff A vector of differences produced by periodogram_prepare().
246 \param len The length of the periodogram, in samples. This must be an even number.
247 \return The periodogram result.
248*/
249SPAN_DECLARE(complexf_t) periodogram_apply(const complexf_t coeffs[], const complexf_t sum[], const complexf_t diff[], int len);
250
251/*! Apply a phase offset, to find the frequency error between periodogram evaluations.
252 specified interval.
253 \param phase_offset A point to the expected phase offset.
254 \param scale The scaling factor to be used.
255 \param last_result A pointer to the previous periodogram result.
256 \param result A pointer to the current periodogram result.
257 \return The frequency error, in Hz.
258*/
259SPAN_DECLARE(float) periodogram_freq_error(const complexf_t *phase_offset, float scale, const complexf_t *last_result, const complexf_t *result);
260
261#if defined(__cplusplus)
262}
263#endif
264
265#endif
266/*- End of file ------------------------------------------------------------*/
Definition complex.h:43
Definition tone_detect.h:33
Definition tone_detect.h:46