Mercurial > hg > audiostuff
comparison spandsp-0.0.6pre17/tests/tone_detect_tests.c @ 4:26cd8f1ef0b1
import spandsp-0.0.6pre17
| author | Peter Meerwald <pmeerw@cosy.sbg.ac.at> |
|---|---|
| date | Fri, 25 Jun 2010 15:50:58 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 3:c6c5a16ce2f2 | 4:26cd8f1ef0b1 |
|---|---|
| 1 /* | |
| 2 * SpanDSP - a series of DSP components for telephony | |
| 3 * | |
| 4 * tone_detect_tests.c | |
| 5 * | |
| 6 * Written by Steve Underwood <steveu@coppice.org> | |
| 7 * | |
| 8 * Copyright (C) 2007 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: tone_detect_tests.c,v 1.12 2009/05/30 15:23:14 steveu Exp $ | |
| 26 */ | |
| 27 | |
| 28 /*! \page tone_detect_tests_page Tone detection tests | |
| 29 \section tone_detect_tests_page_sec_1 What does it do? | |
| 30 */ | |
| 31 | |
| 32 #if defined(HAVE_CONFIG_H) | |
| 33 #include "config.h" | |
| 34 #endif | |
| 35 | |
| 36 #include <stdlib.h> | |
| 37 #include <stdio.h> | |
| 38 #include <fcntl.h> | |
| 39 #include <string.h> | |
| 40 #include <time.h> | |
| 41 #include <sndfile.h> | |
| 42 | |
| 43 //#if defined(WITH_SPANDSP_INTERNALS) | |
| 44 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES | |
| 45 //#endif | |
| 46 | |
| 47 #include "spandsp.h" | |
| 48 | |
| 49 #define DEC_SAMPLE_RATE 800 | |
| 50 #define DEC_RATIO 10 | |
| 51 #define BLOCK_LEN 56 | |
| 52 #define PG_WINDOW 56 | |
| 53 #define FREQ1 440.0f | |
| 54 #define FREQ2 480.0f | |
| 55 | |
| 56 static int periodogram_tests(void) | |
| 57 { | |
| 58 int i; | |
| 59 int j; | |
| 60 int k; | |
| 61 int len; | |
| 62 complexf_t coeffs[PG_WINDOW/2]; | |
| 63 complexf_t camp[BLOCK_LEN]; | |
| 64 complexf_t last_result; | |
| 65 complexf_t result; | |
| 66 complexf_t phase_offset; | |
| 67 float freq_error; | |
| 68 float pg_scale; | |
| 69 float level; | |
| 70 float scale1; | |
| 71 float scale2; | |
| 72 int32_t phase_rate1; | |
| 73 int32_t phase_rate2; | |
| 74 uint32_t phase_acc1; | |
| 75 uint32_t phase_acc2; | |
| 76 awgn_state_t noise_source_re; | |
| 77 awgn_state_t noise_source_im; | |
| 78 | |
| 79 phase_rate1 = DEC_RATIO*dds_phase_ratef(FREQ1 - 5.0f); | |
| 80 phase_rate2 = DEC_RATIO*dds_phase_ratef(FREQ2); | |
| 81 phase_acc1 = 0; | |
| 82 phase_acc2 = 0; | |
| 83 len = periodogram_generate_coeffs(coeffs, FREQ1, DEC_SAMPLE_RATE, PG_WINDOW); | |
| 84 if (len != PG_WINDOW/2) | |
| 85 { | |
| 86 printf("Test failed\n"); | |
| 87 return -1; | |
| 88 } | |
| 89 pg_scale = periodogram_generate_phase_offset(&phase_offset, FREQ1, DEC_SAMPLE_RATE, PG_WINDOW); | |
| 90 scale1 = dds_scaling_dbm0f(-6.0f); | |
| 91 scale2 = dds_scaling_dbm0f(-6.0f); | |
| 92 | |
| 93 for (k = -50; k < 0; k++) | |
| 94 { | |
| 95 printf("Setting noise to %ddBm0\n", k); | |
| 96 awgn_init_dbm0(&noise_source_re, 1234567, (float) k); | |
| 97 awgn_init_dbm0(&noise_source_im, 7654321, (float) k); | |
| 98 last_result = complex_setf(0.0f, 0.0f); | |
| 99 for (i = 0; i < 100; i++) | |
| 100 { | |
| 101 for (j = 0; j < PG_WINDOW; j++) | |
| 102 { | |
| 103 result = dds_complexf(&phase_acc1, phase_rate1); | |
| 104 camp[j].re = result.re*scale1; | |
| 105 camp[j].im = result.im*scale1; | |
| 106 result = dds_complexf(&phase_acc2, phase_rate2); | |
| 107 camp[j].re += result.re*scale2; | |
| 108 camp[j].im += result.im*scale2; | |
| 109 camp[j].re += awgn(&noise_source_re); | |
| 110 camp[j].im += awgn(&noise_source_im); | |
| 111 } | |
| 112 result = periodogram(coeffs, camp, PG_WINDOW); | |
| 113 level = sqrtf(result.re*result.re + result.im*result.im); | |
| 114 freq_error = periodogram_freq_error(&phase_offset, pg_scale, &last_result, &result); | |
| 115 last_result = result; | |
| 116 if (i == 0) | |
| 117 continue; | |
| 118 | |
| 119 printf("Signal level = %.5f, freq error = %.5f\n", level, freq_error); | |
| 120 if (level < scale1*0.8f || level > scale1*1.2f) | |
| 121 { | |
| 122 printf("Test failed - %ddBm0 of noise, signal is %f (%f)\n", k, level, scale1); | |
| 123 return -1; | |
| 124 } | |
| 125 if (freq_error < -10.0f || freq_error > 10.0f) | |
| 126 { | |
| 127 printf("Test failed - %ddBm0 of noise, %fHz error\n", k, freq_error); | |
| 128 return -1; | |
| 129 } | |
| 130 } | |
| 131 } | |
| 132 return 0; | |
| 133 } | |
| 134 /*- End of function --------------------------------------------------------*/ | |
| 135 | |
| 136 int main(int argc, char *argv[]) | |
| 137 { | |
| 138 if (periodogram_tests()) | |
| 139 exit(2); | |
| 140 printf("Tests passed\n"); | |
| 141 return 0; | |
| 142 } | |
| 143 /*- End of function --------------------------------------------------------*/ | |
| 144 /*- End of file ------------------------------------------------------------*/ |
