Mercurial > hg > audiostuff
comparison spandsp-0.0.6pre17/tests/awgn_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 * awgn_tests.c | |
| 5 * | |
| 6 * Written by Steve Underwood <steveu@coppice.org> | |
| 7 * | |
| 8 * Copyright (C) 2001 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: awgn_tests.c,v 1.18 2008/11/30 12:38:27 steveu Exp $ | |
| 26 */ | |
| 27 | |
| 28 /*! \page awgn_tests_page AWGN tests | |
| 29 \section awgn_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 <string.h> | |
| 39 | |
| 40 //#if defined(WITH_SPANDSP_INTERNALS) | |
| 41 #define SPANDSP_EXPOSE_INTERNAL_STRUCTURES | |
| 42 //#endif | |
| 43 | |
| 44 #include "spandsp.h" | |
| 45 | |
| 46 #if !defined(M_PI) | |
| 47 # define M_PI 3.14159265358979323846 /* pi */ | |
| 48 #endif | |
| 49 | |
| 50 #define OUT_FILE_NAME "awgn.wav" | |
| 51 | |
| 52 /* Some simple sanity tests for the Gaussian noise generation routines */ | |
| 53 | |
| 54 int main (int argc, char *argv[]) | |
| 55 { | |
| 56 int i; | |
| 57 int j; | |
| 58 int clip_high; | |
| 59 int clip_low; | |
| 60 int total_samples; | |
| 61 int idum = 1234567; | |
| 62 int16_t value; | |
| 63 double total; | |
| 64 double x; | |
| 65 double p; | |
| 66 double o; | |
| 67 double error; | |
| 68 int bins[65536]; | |
| 69 awgn_state_t noise_source; | |
| 70 | |
| 71 /* Generate noise at several RMS levels between -50dBm and 0dBm. Noise is | |
| 72 generated for a large number of samples (1,000,000), and the RMS value | |
| 73 of the noise is calculated along the way. If the resulting level is | |
| 74 close to the requested RMS level, at least the scaling of the noise | |
| 75 should be Ok. At high level some clipping may distort the result a | |
| 76 little. */ | |
| 77 for (j = -50; j <= 0; j += 5) | |
| 78 { | |
| 79 clip_high = 0; | |
| 80 clip_low = 0; | |
| 81 total = 0.0; | |
| 82 awgn_init_dbm0(&noise_source, idum, (float) j); | |
| 83 total_samples = 1000000; | |
| 84 for (i = 0; i < total_samples; i++) | |
| 85 { | |
| 86 value = awgn(&noise_source); | |
| 87 if (value == 32767) | |
| 88 clip_high++; | |
| 89 else if (value == -32768) | |
| 90 clip_low++; | |
| 91 total += ((double) value)*((double) value); | |
| 92 } | |
| 93 error = 100.0*(1.0 - sqrt(total/total_samples)/noise_source.rms); | |
| 94 printf("RMS = %.3f (expected %d) %.2f%% error [clipped samples %d+%d]\n", | |
| 95 log10(sqrt(total/total_samples)/32768.0)*20.0 + DBM0_MAX_POWER, | |
| 96 j, | |
| 97 error, | |
| 98 clip_low, | |
| 99 clip_high); | |
| 100 /* We don't check the result at 0dBm0, as there will definitely be a lot of error due to clipping */ | |
| 101 if (j < 0 && fabs(error) > 0.2) | |
| 102 { | |
| 103 printf("Test failed.\n"); | |
| 104 exit(2); | |
| 105 } | |
| 106 } | |
| 107 /* Now look at the statistical spread of the results, by collecting data in | |
| 108 bins from a large number of samples. Use a fairly high noise level, but | |
| 109 low enough to avoid significant clipping. Use the Gaussian model to | |
| 110 predict the real probability, and present the results for graphing. */ | |
| 111 memset(bins, 0, sizeof(bins)); | |
| 112 clip_high = 0; | |
| 113 clip_low = 0; | |
| 114 awgn_init_dbm0(&noise_source, idum, -15); | |
| 115 total_samples = 10000000; | |
| 116 for (i = 0; i < total_samples; i++) | |
| 117 { | |
| 118 value = awgn(&noise_source); | |
| 119 if (value == 32767) | |
| 120 clip_high++; | |
| 121 else if (value == -32768) | |
| 122 clip_low++; | |
| 123 bins[value + 32768]++; | |
| 124 } | |
| 125 o = noise_source.rms; | |
| 126 for (i = 0; i < 65536 - 10; i++) | |
| 127 { | |
| 128 x = i - 32768; | |
| 129 /* Find the real probability for this bin */ | |
| 130 p = (1.0/(o*sqrt(2.0*M_PI)))*exp(-(x*x)/(2.0*o*o)); | |
| 131 /* Now do a little smoothing on the real data to get a reasonably | |
| 132 steady answer */ | |
| 133 x = 0; | |
| 134 for (j = 0; j < 10; j++) | |
| 135 x += bins[i + j]; | |
| 136 x /= 10.0; | |
| 137 x /= total_samples; | |
| 138 /* Now send it out for graphing. */ | |
| 139 printf("%6d %.7f %.7f\n", i - 32768, x, p); | |
| 140 } | |
| 141 | |
| 142 printf("Tests passed.\n"); | |
| 143 return 0; | |
| 144 } | |
| 145 /*- End of function --------------------------------------------------------*/ | |
| 146 /*- End of file ------------------------------------------------------------*/ | 
