You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
6.1KB

  1. /*
  2. ** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
  3. ** All rights reserved.
  4. **
  5. ** This code is released under 2-clause BSD license. Please see the
  6. ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
  7. */
  8. #include "config.h"
  9. #include "util.h"
  10. #if (HAVE_FFTW3 == 1)
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <math.h>
  15. #include <fftw3.h>
  16. #define MAX_SPEC_LEN (1<<18)
  17. #define MAX_PEAKS 10
  18. static void log_mag_spectrum (double *input, int len, double *magnitude) ;
  19. static void smooth_mag_spectrum (double *magnitude, int len) ;
  20. static double find_snr (const double *magnitude, int len, int expected_peaks) ;
  21. typedef struct
  22. { double peak ;
  23. int index ;
  24. } PEAK_DATA ;
  25. double
  26. calculate_snr (float *data, int len, int expected_peaks)
  27. { static double magnitude [MAX_SPEC_LEN] ;
  28. static double datacopy [MAX_SPEC_LEN] ;
  29. double snr = 200.0 ;
  30. int k ;
  31. if (len > MAX_SPEC_LEN)
  32. { printf ("%s : line %d : data length too large.\n", __FILE__, __LINE__) ;
  33. exit (1) ;
  34. } ;
  35. for (k = 0 ; k < len ; k++)
  36. datacopy [k] = data [k] ;
  37. /* Pad the data just a little to speed up the FFT. */
  38. while ((len & 0x1F) && len < MAX_SPEC_LEN)
  39. { datacopy [len] = 0.0 ;
  40. len ++ ;
  41. } ;
  42. log_mag_spectrum (datacopy, len, magnitude) ;
  43. smooth_mag_spectrum (magnitude, len / 2) ;
  44. snr = find_snr (magnitude, len, expected_peaks) ;
  45. return snr ;
  46. } /* calculate_snr */
  47. /*==============================================================================
  48. ** There is a slight problem with trying to measure SNR with the method used
  49. ** here; the side lobes of the windowed FFT can look like a noise/aliasing peak.
  50. ** The solution is to smooth the magnitude spectrum by wiping out troughs
  51. ** between adjacent peaks as done here.
  52. ** This removes side lobe peaks without affecting noise/aliasing peaks.
  53. */
  54. static void linear_smooth (double *mag, PEAK_DATA *larger, PEAK_DATA *smaller) ;
  55. static void
  56. smooth_mag_spectrum (double *mag, int len)
  57. { PEAK_DATA peaks [2] ;
  58. int k ;
  59. memset (peaks, 0, sizeof (peaks)) ;
  60. /* Find first peak. */
  61. for (k = 1 ; k < len - 1 ; k++)
  62. { if (mag [k - 1] < mag [k] && mag [k] >= mag [k + 1])
  63. { peaks [0].peak = mag [k] ;
  64. peaks [0].index = k ;
  65. break ;
  66. } ;
  67. } ;
  68. /* Find subsequent peaks ans smooth between peaks. */
  69. for (k = peaks [0].index + 1 ; k < len - 1 ; k++)
  70. { if (mag [k - 1] < mag [k] && mag [k] >= mag [k + 1])
  71. { peaks [1].peak = mag [k] ;
  72. peaks [1].index = k ;
  73. if (peaks [1].peak > peaks [0].peak)
  74. linear_smooth (mag, &peaks [1], &peaks [0]) ;
  75. else
  76. linear_smooth (mag, &peaks [0], &peaks [1]) ;
  77. peaks [0] = peaks [1] ;
  78. } ;
  79. } ;
  80. } /* smooth_mag_spectrum */
  81. static void
  82. linear_smooth (double *mag, PEAK_DATA *larger, PEAK_DATA *smaller)
  83. { int k ;
  84. if (smaller->index < larger->index)
  85. { for (k = smaller->index + 1 ; k < larger->index ; k++)
  86. mag [k] = (mag [k] < mag [k - 1]) ? 0.999 * mag [k - 1] : mag [k] ;
  87. }
  88. else
  89. { for (k = smaller->index - 1 ; k >= larger->index ; k--)
  90. mag [k] = (mag [k] < mag [k + 1]) ? 0.999 * mag [k + 1] : mag [k] ;
  91. } ;
  92. } /* linear_smooth */
  93. /*==============================================================================
  94. */
  95. static int
  96. peak_compare (const void *vp1, const void *vp2)
  97. { const PEAK_DATA *peak1, *peak2 ;
  98. peak1 = (const PEAK_DATA*) vp1 ;
  99. peak2 = (const PEAK_DATA*) vp2 ;
  100. return (peak1->peak < peak2->peak) ? 1 : -1 ;
  101. } /* peak_compare */
  102. static double
  103. find_snr (const double *magnitude, int len, int expected_peaks)
  104. { PEAK_DATA peaks [MAX_PEAKS] ;
  105. int k, peak_count = 0 ;
  106. double snr ;
  107. memset (peaks, 0, sizeof (peaks)) ;
  108. /* Find the MAX_PEAKS largest peaks. */
  109. for (k = 1 ; k < len - 1 ; k++)
  110. { if (magnitude [k - 1] < magnitude [k] && magnitude [k] >= magnitude [k + 1])
  111. { if (peak_count < MAX_PEAKS)
  112. { peaks [peak_count].peak = magnitude [k] ;
  113. peaks [peak_count].index = k ;
  114. peak_count ++ ;
  115. qsort (peaks, peak_count, sizeof (PEAK_DATA), peak_compare) ;
  116. }
  117. else if (magnitude [k] > peaks [MAX_PEAKS - 1].peak)
  118. { peaks [MAX_PEAKS - 1].peak = magnitude [k] ;
  119. peaks [MAX_PEAKS - 1].index = k ;
  120. qsort (peaks, MAX_PEAKS, sizeof (PEAK_DATA), peak_compare) ;
  121. } ;
  122. } ;
  123. } ;
  124. if (peak_count < expected_peaks)
  125. { printf ("\n%s : line %d : bad peak_count (%d), expected %d.\n\n", __FILE__, __LINE__, peak_count, expected_peaks) ;
  126. return -1.0 ;
  127. } ;
  128. /* Sort the peaks. */
  129. qsort (peaks, peak_count, sizeof (PEAK_DATA), peak_compare) ;
  130. snr = peaks [0].peak ;
  131. for (k = 1 ; k < peak_count ; k++)
  132. if (fabs (snr - peaks [k].peak) > 10.0)
  133. return fabs (peaks [k].peak) ;
  134. return snr ;
  135. } /* find_snr */
  136. static void
  137. log_mag_spectrum (double *input, int len, double *magnitude)
  138. { fftw_plan plan = NULL ;
  139. double maxval ;
  140. int k ;
  141. if (input == NULL || magnitude == NULL)
  142. return ;
  143. plan = fftw_plan_r2r_1d (len, input, magnitude, FFTW_R2HC, FFTW_ESTIMATE | FFTW_PRESERVE_INPUT) ;
  144. if (plan == NULL)
  145. { printf ("%s : line %d : create plan failed.\n", __FILE__, __LINE__) ;
  146. exit (1) ;
  147. } ;
  148. fftw_execute (plan) ;
  149. fftw_destroy_plan (plan) ;
  150. maxval = 0.0 ;
  151. for (k = 1 ; k < len / 2 ; k++)
  152. { /*
  153. ** From : http://www.fftw.org/doc/Real_002dto_002dReal-Transform-Kinds.html#Real_002dto_002dReal-Transform-Kinds
  154. **
  155. ** FFTW_R2HC computes a real-input DFT with output in “halfcomplex” format, i.e. real and imaginary parts
  156. ** for a transform of size n stored as:
  157. **
  158. ** r0, r1, r2, ..., rn/2, i(n+1)/2-1, ..., i2, i1
  159. */
  160. double re = magnitude [k] ;
  161. double im = magnitude [len - k] ;
  162. magnitude [k] = sqrt (re * re + im * im) ;
  163. maxval = (maxval < magnitude [k]) ? magnitude [k] : maxval ;
  164. } ;
  165. memset (magnitude + len / 2, 0, len / 2 * sizeof (magnitude [0])) ;
  166. /* Don't care about DC component. Make it zero. */
  167. magnitude [0] = 0.0 ;
  168. /* log magnitude. */
  169. for (k = 0 ; k < len ; k++)
  170. { magnitude [k] = magnitude [k] / maxval ;
  171. magnitude [k] = (magnitude [k] < 1e-15) ? -200.0 : 20.0 * log10 (magnitude [k]) ;
  172. } ;
  173. return ;
  174. } /* log_mag_spectrum */
  175. #else /* ! (HAVE_LIBFFTW && HAVE_LIBRFFTW) */
  176. double
  177. calculate_snr (float *data, int len, int expected_peaks)
  178. { double snr = 200.0 ;
  179. data = data ;
  180. len = len ;
  181. expected_peaks = expected_peaks ;
  182. return snr ;
  183. } /* calculate_snr */
  184. #endif