dft_cmp.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. ** Copyright (C) 2002-2013 Erik de Castro Lopo <erikd@mega-nerd.com>
  3. **
  4. ** This program is free software; you can redistribute it and/or modify
  5. ** it under the terms of the GNU General Public License as published by
  6. ** the Free Software Foundation; either version 2 of the License, or
  7. ** (at your option) any later version.
  8. **
  9. ** This program is distributed in the hope that it will be useful,
  10. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ** GNU General Public License for more details.
  13. **
  14. ** You should have received a copy of the GNU General Public License
  15. ** along with this program; if not, write to the Free Software
  16. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <math.h>
  21. #include "dft_cmp.h"
  22. #include "utils.h"
  23. #ifndef M_PI
  24. #define M_PI 3.14159265358979323846264338
  25. #endif
  26. #define DFT_SPEC_LENGTH (DFT_DATA_LENGTH / 2)
  27. static void dft_magnitude (const double *data, double *spectrum) ;
  28. static double calc_max_spectral_difference (const double *spec1, const double *spec2) ;
  29. /*--------------------------------------------------------------------------------
  30. ** Public functions.
  31. */
  32. double
  33. dft_cmp_float (int linenum, const float *in_data, const float *test_data, int len, double target_snr, int allow_exit)
  34. { static double orig [DFT_DATA_LENGTH] ;
  35. static double test [DFT_DATA_LENGTH] ;
  36. unsigned k ;
  37. if (len != DFT_DATA_LENGTH)
  38. { printf ("Error (line %d) : dft_cmp_float : Bad input array length.\n", linenum) ;
  39. return 1 ;
  40. } ;
  41. for (k = 0 ; k < ARRAY_LEN (orig) ; k++)
  42. { test [k] = test_data [k] ;
  43. orig [k] = in_data [k] ;
  44. } ;
  45. return dft_cmp_double (linenum, orig, test, len, target_snr, allow_exit) ;
  46. } /* dft_cmp_float */
  47. double
  48. dft_cmp_double (int linenum, const double *orig, const double *test, int len, double target_snr, int allow_exit)
  49. { static double orig_spec [DFT_SPEC_LENGTH] ;
  50. static double test_spec [DFT_SPEC_LENGTH] ;
  51. double snr ;
  52. if (! orig || ! test)
  53. { printf ("Error (line %d) : dft_cmp_double : Bad input arrays.\n", linenum) ;
  54. return 1 ;
  55. } ;
  56. if (len != DFT_DATA_LENGTH)
  57. { printf ("Error (line %d) : dft_cmp_double : Bad input array length.\n", linenum) ;
  58. return 1 ;
  59. } ;
  60. dft_magnitude (orig, orig_spec) ;
  61. dft_magnitude (test, test_spec) ;
  62. snr = calc_max_spectral_difference (orig_spec, test_spec) ;
  63. if (snr > target_snr)
  64. { printf ("\n\nLine %d: Actual SNR (% 4.1f) > target SNR (% 4.1f).\n\n", linenum, snr, target_snr) ;
  65. oct_save_double (orig, test, len) ;
  66. if (allow_exit)
  67. exit (1) ;
  68. } ;
  69. if (snr < -500.0)
  70. snr = -500.0 ;
  71. return snr ;
  72. } /* dft_cmp_double */
  73. /*--------------------------------------------------------------------------------
  74. ** Quick dirty calculation of magnitude spectrum for real valued data using
  75. ** Discrete Fourier Transform. Since the data is real, the DFT is only
  76. ** calculated for positive frequencies.
  77. */
  78. static void
  79. dft_magnitude (const double *data, double *spectrum)
  80. { static double cos_angle [DFT_DATA_LENGTH] = { 0.0 } ;
  81. static double sin_angle [DFT_DATA_LENGTH] ;
  82. double real_part, imag_part ;
  83. int k, n ;
  84. /* If sine and cosine tables haven't been initialised, do so. */
  85. if (cos_angle [0] == 0.0)
  86. for (n = 0 ; n < DFT_DATA_LENGTH ; n++)
  87. { cos_angle [n] = cos (2.0 * M_PI * n / DFT_DATA_LENGTH) ;
  88. sin_angle [n] = -1.0 * sin (2.0 * M_PI * n / DFT_DATA_LENGTH) ;
  89. } ;
  90. /* DFT proper. Since the data is real, only generate a half spectrum. */
  91. for (k = 1 ; k < DFT_SPEC_LENGTH ; k++)
  92. { real_part = 0.0 ;
  93. imag_part = 0.0 ;
  94. for (n = 0 ; n < DFT_DATA_LENGTH ; n++)
  95. { real_part += data [n] * cos_angle [(k * n) % DFT_DATA_LENGTH] ;
  96. imag_part += data [n] * sin_angle [(k * n) % DFT_DATA_LENGTH] ;
  97. } ;
  98. spectrum [k] = sqrt (real_part * real_part + imag_part * imag_part) ;
  99. } ;
  100. spectrum [DFT_DATA_LENGTH - 1] = 0.0 ;
  101. spectrum [0] = spectrum [1] = spectrum [2] = spectrum [3] = spectrum [4] = 0.0 ;
  102. return ;
  103. } /* dft_magnitude */
  104. static double
  105. calc_max_spectral_difference (const double *orig, const double *test)
  106. { double orig_max = 0.0, max_diff = 0.0 ;
  107. int k ;
  108. for (k = 0 ; k < DFT_SPEC_LENGTH ; k++)
  109. { if (orig_max < orig [k])
  110. orig_max = orig [k] ;
  111. if (max_diff < fabs (orig [k] - test [k]))
  112. max_diff = fabs (orig [k] - test [k]) ;
  113. } ;
  114. if (max_diff < 1e-25)
  115. return -500.0 ;
  116. return 20.0 * log10 (max_diff / orig_max) ;
  117. } /* calc_max_spectral_difference */