tone_detect_tests.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /*! \page tone_detect_tests_page Tone detection tests
  26. \section tone_detect_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 <fcntl.h>
  34. #include <string.h>
  35. #include <time.h>
  36. #include <sndfile.h>
  37. #include "spandsp.h"
  38. #define DEC_SAMPLE_RATE 800
  39. #define DEC_RATIO 10
  40. #define BLOCK_LEN 56
  41. #define PG_WINDOW 56
  42. #define FREQ1 440.0f
  43. #define FREQ2 480.0f
  44. static int periodogram_tests(void)
  45. {
  46. int i;
  47. int j;
  48. int k;
  49. int len;
  50. complexf_t coeffs[PG_WINDOW/2];
  51. complexf_t camp[BLOCK_LEN];
  52. complexf_t last_result;
  53. complexf_t result;
  54. complexf_t phase_offset;
  55. float freq_error;
  56. float pg_scale;
  57. float level;
  58. float scale1;
  59. float scale2;
  60. int32_t phase_rate1;
  61. int32_t phase_rate2;
  62. uint32_t phase_acc1;
  63. uint32_t phase_acc2;
  64. awgn_state_t *noise_source_re;
  65. awgn_state_t *noise_source_im;
  66. phase_rate1 = DEC_RATIO*dds_phase_ratef(FREQ1 - 5.0f);
  67. phase_rate2 = DEC_RATIO*dds_phase_ratef(FREQ2);
  68. phase_acc1 = 0;
  69. phase_acc2 = 0;
  70. len = periodogram_generate_coeffs(coeffs, FREQ1, DEC_SAMPLE_RATE, PG_WINDOW);
  71. if (len != PG_WINDOW/2)
  72. {
  73. printf("Test failed\n");
  74. return -1;
  75. }
  76. pg_scale = periodogram_generate_phase_offset(&phase_offset, FREQ1, DEC_SAMPLE_RATE, PG_WINDOW);
  77. scale1 = dds_scaling_dbm0f(-6.0f);
  78. scale2 = dds_scaling_dbm0f(-6.0f);
  79. for (k = -50; k < 0; k++)
  80. {
  81. printf("Setting noise to %ddBm0\n", k);
  82. noise_source_re = awgn_init_dbm0(NULL, 1234567, (float) k);
  83. noise_source_im = awgn_init_dbm0(NULL, 7654321, (float) k);
  84. last_result = complex_setf(0.0f, 0.0f);
  85. for (i = 0; i < 100; i++)
  86. {
  87. for (j = 0; j < PG_WINDOW; j++)
  88. {
  89. result = dds_complexf(&phase_acc1, phase_rate1);
  90. camp[j].re = result.re*scale1;
  91. camp[j].im = result.im*scale1;
  92. result = dds_complexf(&phase_acc2, phase_rate2);
  93. camp[j].re += result.re*scale2;
  94. camp[j].im += result.im*scale2;
  95. camp[j].re += awgn(noise_source_re);
  96. camp[j].im += awgn(noise_source_im);
  97. }
  98. result = periodogram(coeffs, camp, PG_WINDOW);
  99. level = sqrtf(result.re*result.re + result.im*result.im);
  100. freq_error = periodogram_freq_error(&phase_offset, pg_scale, &last_result, &result);
  101. last_result = result;
  102. if (i == 0)
  103. continue;
  104. printf("Signal level = %.5f, freq error = %.5f\n", level, freq_error);
  105. if (level < scale1*0.8f || level > scale1*1.2f)
  106. {
  107. printf("Test failed - %ddBm0 of noise, signal is %f (%f)\n", k, level, scale1);
  108. return -1;
  109. }
  110. if (freq_error < -10.0f || freq_error > 10.0f)
  111. {
  112. printf("Test failed - %ddBm0 of noise, %fHz error\n", k, freq_error);
  113. return -1;
  114. }
  115. }
  116. awgn_free(noise_source_re);
  117. awgn_free(noise_source_im);
  118. }
  119. return 0;
  120. }
  121. /*- End of function --------------------------------------------------------*/
  122. int main(int argc, char *argv[])
  123. {
  124. if (periodogram_tests())
  125. exit(2);
  126. printf("Tests passed\n");
  127. return 0;
  128. }
  129. /*- End of function --------------------------------------------------------*/
  130. /*- End of file ------------------------------------------------------------*/