00001 /* 00002 * SpanDSP - a series of DSP components for telephony 00003 * 00004 * tone_detect.h - General telephony tone detection. 00005 * 00006 * Written by Steve Underwood <steveu@coppice.org> 00007 * 00008 * Copyright (C) 2001, 2005 Steve Underwood 00009 * 00010 * All rights reserved. 00011 * 00012 * This program is free software; you can redistribute it and/or modify 00013 * it under the terms of the GNU Lesser General Public License version 2.1, 00014 * as published by the Free Software Foundation. 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Lesser General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Lesser General Public 00022 * License along with this program; if not, write to the Free Software 00023 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00024 * 00025 * $Id: tone_detect.h,v 1.42 2008/11/30 10:17:31 steveu Exp $ 00026 */ 00027 00028 #if !defined(_SPANDSP_TONE_DETECT_H_) 00029 #define _SPANDSP_TONE_DETECT_H_ 00030 00031 /*! 00032 Goertzel filter descriptor. 00033 */ 00034 struct goertzel_descriptor_s 00035 { 00036 #if defined(SPANDSP_USE_FIXED_POINT) 00037 int16_t fac; 00038 #else 00039 float fac; 00040 #endif 00041 int samples; 00042 }; 00043 00044 /*! 00045 Goertzel filter state descriptor. 00046 */ 00047 struct goertzel_state_s 00048 { 00049 #if defined(SPANDSP_USE_FIXED_POINT) 00050 int16_t v2; 00051 int16_t v3; 00052 int16_t fac; 00053 #else 00054 float v2; 00055 float v3; 00056 float fac; 00057 #endif 00058 int samples; 00059 int current_sample; 00060 }; 00061 00062 /*! 00063 Goertzel filter descriptor. 00064 */ 00065 typedef struct goertzel_descriptor_s goertzel_descriptor_t; 00066 00067 /*! 00068 Goertzel filter state descriptor. 00069 */ 00070 typedef struct goertzel_state_s goertzel_state_t; 00071 00072 #if defined(__cplusplus) 00073 extern "C" 00074 { 00075 #endif 00076 00077 /*! \brief Create a descriptor for use with either a Goertzel transform */ 00078 void make_goertzel_descriptor(goertzel_descriptor_t *t, 00079 float freq, 00080 int samples); 00081 00082 /*! \brief Initialise the state of a Goertzel transform. 00083 \param s The Goertzel context. If NULL, a context is allocated with malloc. 00084 \param t The Goertzel descriptor. 00085 \return A pointer to the Goertzel state. */ 00086 goertzel_state_t *goertzel_init(goertzel_state_t *s, 00087 goertzel_descriptor_t *t); 00088 00089 /*! \brief Reset the state of a Goertzel transform. 00090 \param s The Goertzel context. */ 00091 void goertzel_reset(goertzel_state_t *s); 00092 00093 /*! \brief Update the state of a Goertzel transform. 00094 \param s The Goertzel context. 00095 \param amp The samples to be transformed. 00096 \param samples The number of samples. 00097 \return The number of samples unprocessed */ 00098 int goertzel_update(goertzel_state_t *s, 00099 const int16_t amp[], 00100 int samples); 00101 00102 /*! \brief Evaluate the final result of a Goertzel transform. 00103 \param s The Goertzel context. 00104 \return The result of the transform. The expected result for a pure sine wave 00105 signal of level x dBm0, at the very centre of the bin is: 00106 [Floating point] ((samples_per_goertzel_block*32768.0/1.4142)*10^((x - DBM0_MAX_SINE_POWER)/20.0))^2 00107 [Fixed point] ((samples_per_goertzel_block*256.0/1.4142)*10^((x - DBM0_MAX_SINE_POWER)/20.0))^2 */ 00108 #if defined(SPANDSP_USE_FIXED_POINT) 00109 int32_t goertzel_result(goertzel_state_t *s); 00110 #else 00111 float goertzel_result(goertzel_state_t *s); 00112 #endif 00113 00114 /*! \brief Update the state of a Goertzel transform. 00115 \param s The Goertzel context. 00116 \param amp The sample to be transformed. */ 00117 static __inline__ void goertzel_sample(goertzel_state_t *s, int16_t amp) 00118 { 00119 #if defined(SPANDSP_USE_FIXED_POINT) 00120 int16_t x; 00121 int16_t v1; 00122 #else 00123 float v1; 00124 #endif 00125 00126 v1 = s->v2; 00127 s->v2 = s->v3; 00128 #if defined(SPANDSP_USE_FIXED_POINT) 00129 x = (((int32_t) s->fac*s->v2) >> 14); 00130 /* Scale down the input signal to avoid overflows. 9 bits is enough to 00131 monitor the signals of interest with adequate dynamic range and 00132 resolution. In telephony we generally only start with 13 or 14 bits, 00133 anyway. */ 00134 s->v3 = x - v1 + (amp >> 7); 00135 #else 00136 s->v3 = s->fac*s->v2 - v1 + amp; 00137 #endif 00138 s->current_sample++; 00139 } 00140 /*- End of function --------------------------------------------------------*/ 00141 00142 /* Scale down the input signal to avoid overflows. 9 bits is enough to 00143 monitor the signals of interest with adequate dynamic range and 00144 resolution. In telephony we generally only start with 13 or 14 bits, 00145 anyway. This is sufficient for the longest Goertzel we currently use. */ 00146 #if defined(SPANDSP_USE_FIXED_POINT) 00147 #define goertzel_preadjust_amp(amp) (((int16_t) amp) >> 7) 00148 #else 00149 #define goertzel_preadjust_amp(amp) ((float) amp) 00150 #endif 00151 00152 /* Minimal update the state of a Goertzel transform. This is similar to 00153 goertzel_sample, but more suited to blocks of Goertzels. It assumes 00154 the amplitude is pre-shifted, and does not update the per-state sample 00155 count. 00156 \brief Update the state of a Goertzel transform. 00157 \param s The Goertzel context. 00158 \param amp The adjusted sample to be transformed. */ 00159 #if defined(SPANDSP_USE_FIXED_POINT) 00160 static __inline__ void goertzel_samplex(goertzel_state_t *s, int16_t amp) 00161 #else 00162 static __inline__ void goertzel_samplex(goertzel_state_t *s, float amp) 00163 #endif 00164 { 00165 #if defined(SPANDSP_USE_FIXED_POINT) 00166 int16_t x; 00167 int16_t v1; 00168 #else 00169 float v1; 00170 #endif 00171 00172 v1 = s->v2; 00173 s->v2 = s->v3; 00174 #if defined(SPANDSP_USE_FIXED_POINT) 00175 x = (((int32_t) s->fac*s->v2) >> 14); 00176 s->v3 = x - v1 + amp; 00177 #else 00178 s->v3 = s->fac*s->v2 - v1 + amp; 00179 #endif 00180 } 00181 /*- End of function --------------------------------------------------------*/ 00182 00183 /*! Generate a Hamming weighted coefficient set, to be used for a periodogram analysis. 00184 \param coeffs The generated coefficients. 00185 \param freq The frequency to be matched by the periodogram, in Hz. 00186 \param sample_rate The sample rate of the signal, in samples per second. 00187 \param window_len The length of the periodogram window. This must be an even number. 00188 \return The number of generated coefficients. 00189 */ 00190 int periodogram_generate_coeffs(complexf_t coeffs[], float freq, int sample_rate, int window_len); 00191 00192 /*! Generate the phase offset to be expected between successive periodograms evaluated at the 00193 specified interval. 00194 \param offset A point to the generated phase offset. 00195 \param freq The frequency being matched by the periodogram, in Hz. 00196 \param sample_rate The sample rate of the signal, in samples per second. 00197 \param interval The interval between periodograms, in samples. 00198 \return The scaling factor. 00199 */ 00200 float periodogram_generate_phase_offset(complexf_t *offset, float freq, int sample_rate, int interval); 00201 00202 /*! Evaluate a periodogram. 00203 \param coeffs A set of coefficients generated by periodogram_generate_coeffs(). 00204 \param amp The complex amplitude of the signal. 00205 \param len The length of the periodogram, in samples. This must be an even number. 00206 \return The periodogram result. 00207 */ 00208 complexf_t periodogram(const complexf_t coeffs[], const complexf_t amp[], int len); 00209 00210 /*! Prepare data for evaluating a set of periodograms. 00211 \param sum A vector of sums of pairs of signal samples. This will be half the length of len. 00212 \param diff A vector of differences between pairs of signal samples. This will be half the length of len. 00213 \param amp The complex amplitude of the signal. 00214 \param len The length of the periodogram, in samples. This must be an even number. 00215 \return The length of the vectors sum and diff. 00216 */ 00217 int periodogram_prepare(complexf_t sum[], complexf_t diff[], const complexf_t amp[], int len); 00218 00219 /*! Evaluate a periodogram, based on data prepared by periodogram_prepare(). This is more efficient 00220 than using periodogram() when several periodograms are to be applied to the same signal. 00221 \param coeffs A set of coefficients generated by periodogram_generate_coeffs(). 00222 \param sum A vector of sums produced by periodogram_prepare(). 00223 \param diff A vector of differences produced by periodogram_prepare(). 00224 \param len The length of the periodogram, in samples. This must be an even number. 00225 \return The periodogram result. 00226 */ 00227 complexf_t periodogram_apply(const complexf_t coeffs[], const complexf_t sum[], const complexf_t diff[], int len); 00228 00229 /*! Apply a phase offset, to find the frequency error between periodogram evaluations. 00230 specified interval. 00231 \param phase_offset A point to the expected phase offset. 00232 \param scale The scaling factor to be used. 00233 \param last_result A pointer to the previous periodogram result. 00234 \param result A pointer to the current periodogram result. 00235 \return The frequency error, in Hz. 00236 */ 00237 float periodogram_freq_error(const complexf_t *phase_offset, float scale, const complexf_t *last_result, const complexf_t *result); 00238 00239 #if defined(__cplusplus) 00240 } 00241 #endif 00242 00243 #endif 00244 /*- End of file ------------------------------------------------------------*/