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