2
0

awgn_tests.c 4.7 KB

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