| 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 <stdio.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <string.h> |
| 16 |
#include <math.h> |
| 17 |
#include <time.h> |
| 18 |
|
| 19 |
#if (HAVE_FFTW3) |
| 20 |
|
| 21 |
#include <fftw3.h> |
| 22 |
|
| 23 |
#include <samplerate.h> |
| 24 |
|
| 25 |
#include "util.h" |
| 26 |
|
| 27 |
#define BUFFER_LEN 50000 |
| 28 |
#define MAX_FREQS 4 |
| 29 |
#define MAX_RATIOS 6 |
| 30 |
#define MAX_SPEC_LEN (1<<15) |
| 31 |
|
| 32 |
#ifndef M_PI |
| 33 |
#define M_PI 3.14159265358979323846264338 |
| 34 |
#endif |
| 35 |
|
| 36 |
enum |
| 37 |
{ BOOLEAN_FALSE = 0, |
| 38 |
BOOLEAN_TRUE = 1 |
| 39 |
} ; |
| 40 |
|
| 41 |
typedef struct |
| 42 |
{ int freq_count ; |
| 43 |
double freqs [MAX_FREQS] ; |
| 44 |
|
| 45 |
double src_ratio ; |
| 46 |
int pass_band_peaks ; |
| 47 |
|
| 48 |
double snr ; |
| 49 |
double peak_value ; |
| 50 |
} SINGLE_TEST ; |
| 51 |
|
| 52 |
typedef struct |
| 53 |
{ int converter ; |
| 54 |
int tests ; |
| 55 |
int do_bandwidth_test ; |
| 56 |
SINGLE_TEST test_data [10] ; |
| 57 |
} CONVERTER_TEST ; |
| 58 |
|
| 59 |
static double snr_test (SINGLE_TEST *snr_test_data, int number, int converter, int verbose) ; |
| 60 |
static double find_peak (float *output, int output_len) ; |
| 61 |
static double bandwidth_test (int converter, int verbose) ; |
| 62 |
|
| 63 |
int |
| 64 |
main (int argc, char *argv []) |
| 65 |
{ CONVERTER_TEST snr_test_data [] = |
| 66 |
{ |
| 67 |
{ SRC_ZERO_ORDER_HOLD, |
| 68 |
8, |
| 69 |
BOOLEAN_FALSE, |
| 70 |
{ { 1, { 0.01111111111 }, 3.0, 1, 28.0, 1.0 }, |
| 71 |
{ 1, { 0.01111111111 }, 0.6, 1, 36.0, 1.0 }, |
| 72 |
{ 1, { 0.01111111111 }, 0.3, 1, 36.0, 1.0 }, |
| 73 |
{ 1, { 0.01111111111 }, 1.0, 1, 150.0, 1.0 }, |
| 74 |
{ 1, { 0.01111111111 }, 1.001, 1, 38.0, 1.0 }, |
| 75 |
{ 2, { 0.011111, 0.324 }, 1.9999, 2, 14.0, 1.0 }, |
| 76 |
{ 2, { 0.012345, 0.457 }, 0.456789, 1, 12.0, 1.0 }, |
| 77 |
{ 1, { 0.3511111111 }, 1.33, 1, 10.0, 1.0 } |
| 78 |
} |
| 79 |
}, |
| 80 |
|
| 81 |
{ SRC_LINEAR, |
| 82 |
8, |
| 83 |
BOOLEAN_FALSE, |
| 84 |
{ { 1, { 0.01111111111 }, 3.0, 1, 73.0, 1.0 }, |
| 85 |
{ 1, { 0.01111111111 }, 0.6, 1, 73.0, 1.0 }, |
| 86 |
{ 1, { 0.01111111111 }, 0.3, 1, 73.0, 1.0 }, |
| 87 |
{ 1, { 0.01111111111 }, 1.0, 1, 150.0, 1.0 }, |
| 88 |
{ 1, { 0.01111111111 }, 1.001, 1, 77.0, 1.0 }, |
| 89 |
{ 2, { 0.011111, 0.324 }, 1.9999, 2, 15.0, 0.94 }, |
| 90 |
{ 2, { 0.012345, 0.457 }, 0.456789, 1, 25.0, 0.96 }, |
| 91 |
{ 1, { 0.3511111111 }, 1.33, 1, 22.0, 0.99 } |
| 92 |
} |
| 93 |
}, |
| 94 |
|
| 95 |
#ifdef ENABLE_SINC_FAST_CONVERTER |
| 96 |
{ SRC_SINC_FASTEST, |
| 97 |
9, |
| 98 |
BOOLEAN_TRUE, |
| 99 |
{ { 1, { 0.01111111111 }, 3.0, 1, 100.0, 1.0 }, |
| 100 |
{ 1, { 0.01111111111 }, 0.6, 1, 99.0, 1.0 }, |
| 101 |
{ 1, { 0.01111111111 }, 0.3, 1, 100.0, 1.0 }, |
| 102 |
{ 1, { 0.01111111111 }, 1.0, 1, 150.0, 1.0 }, |
| 103 |
{ 1, { 0.01111111111 }, 1.001, 1, 100.0, 1.0 }, |
| 104 |
{ 2, { 0.011111, 0.324 }, 1.9999, 2, 97.0, 1.0 }, |
| 105 |
{ 2, { 0.012345, 0.457 }, 0.456789, 1, 100.0, 0.5 }, |
| 106 |
{ 2, { 0.011111, 0.45 }, 0.6, 1, 97.0, 0.5 }, |
| 107 |
{ 1, { 0.3511111111 }, 1.33, 1, 97.0, 1.0 } |
| 108 |
} |
| 109 |
}, |
| 110 |
#endif |
| 111 |
|
| 112 |
#ifdef ENABLE_SINC_MEDIUM_CONVERTER |
| 113 |
{ SRC_SINC_MEDIUM_QUALITY, |
| 114 |
9, |
| 115 |
BOOLEAN_TRUE, |
| 116 |
{ { 1, { 0.01111111111 }, 3.0, 1, 145.0, 1.0 }, |
| 117 |
{ 1, { 0.01111111111 }, 0.6, 1, 132.0, 1.0 }, |
| 118 |
{ 1, { 0.01111111111 }, 0.3, 1, 138.0, 1.0 }, |
| 119 |
{ 1, { 0.01111111111 }, 1.0, 1, 157.0, 1.0 }, |
| 120 |
{ 1, { 0.01111111111 }, 1.001, 1, 148.0, 1.0 }, |
| 121 |
{ 2, { 0.011111, 0.324 }, 1.9999, 2, 127.0, 1.0 }, |
| 122 |
{ 2, { 0.012345, 0.457 }, 0.456789, 1, 123.0, 0.5 }, |
| 123 |
{ 2, { 0.011111, 0.45 }, 0.6, 1, 126.0, 0.5 }, |
| 124 |
{ 1, { 0.43111111111 }, 1.33, 1, 121.0, 1.0 } |
| 125 |
} |
| 126 |
}, |
| 127 |
#endif |
| 128 |
|
| 129 |
#ifdef ENABLE_SINC_BEST_CONVERTER |
| 130 |
{ SRC_SINC_BEST_QUALITY, |
| 131 |
9, |
| 132 |
BOOLEAN_TRUE, |
| 133 |
{ { 1, { 0.01111111111 }, 3.0, 1, 147.0, 1.0 }, |
| 134 |
{ 1, { 0.01111111111 }, 0.6, 1, 147.0, 1.0 }, |
| 135 |
{ 1, { 0.01111111111 }, 0.3, 1, 148.0, 1.0 }, |
| 136 |
{ 1, { 0.01111111111 }, 1.0, 1, 155.0, 1.0 }, |
| 137 |
{ 1, { 0.01111111111 }, 1.001, 1, 148.0, 1.0 }, |
| 138 |
{ 2, { 0.011111, 0.324 }, 1.9999, 2, 146.0, 1.0 }, |
| 139 |
{ 2, { 0.012345, 0.457 }, 0.456789, 1, 147.0, 0.5 }, |
| 140 |
{ 2, { 0.011111, 0.45 }, 0.6, 1, 144.0, 0.5 }, |
| 141 |
{ 1, { 0.43111111111 }, 1.33, 1, 145.0, 1.0 } |
| 142 |
} |
| 143 |
}, |
| 144 |
#endif |
| 145 |
|
| 146 |
} ; /* snr_test_data */ |
| 147 |
|
| 148 |
double best_snr, snr, freq3dB ; |
| 149 |
int j, k, converter, verbose = 0 ; |
| 150 |
|
| 151 |
if (argc == 2 && strcmp (argv [1], "--verbose") == 0) |
| 152 |
verbose = 1 ; |
| 153 |
|
| 154 |
puts ("") ; |
| 155 |
|
| 156 |
for (j = 0 ; j < ARRAY_LEN (snr_test_data) ; j++) |
| 157 |
{ best_snr = 5000.0 ; |
| 158 |
|
| 159 |
converter = snr_test_data [j].converter ; |
| 160 |
|
| 161 |
printf (" Converter %d : %s\n", converter, src_get_name (converter)) ; |
| 162 |
printf (" %s\n", src_get_description (converter)) ; |
| 163 |
|
| 164 |
for (k = 0 ; k < snr_test_data [j].tests ; k++) |
| 165 |
{ snr = snr_test (&(snr_test_data [j].test_data [k]), k, converter, verbose) ; |
| 166 |
if (best_snr > snr) |
| 167 |
best_snr = snr ; |
| 168 |
} ; |
| 169 |
|
| 170 |
printf (" Worst case Signal-to-Noise Ratio : %.2f dB.\n", best_snr) ; |
| 171 |
|
| 172 |
if (snr_test_data [j].do_bandwidth_test == BOOLEAN_FALSE) |
| 173 |
{ puts (" Bandwith test not performed on this converter.\n") ; |
| 174 |
continue ; |
| 175 |
} |
| 176 |
|
| 177 |
freq3dB = bandwidth_test (converter, verbose) ; |
| 178 |
|
| 179 |
printf (" Measured -3dB rolloff point : %5.2f %%.\n\n", freq3dB) ; |
| 180 |
} ; |
| 181 |
|
| 182 |
fftw_cleanup () ; |
| 183 |
|
| 184 |
return 0 ; |
| 185 |
} /* main */ |
| 186 |
|
| 187 |
/*============================================================================== |
| 188 |
*/ |
| 189 |
|
| 190 |
static double |
| 191 |
snr_test (SINGLE_TEST *test_data, int number, int converter, int verbose) |
| 192 |
{ static float data [BUFFER_LEN + 1] ; |
| 193 |
static float output [MAX_SPEC_LEN] ; |
| 194 |
|
| 195 |
SRC_STATE *src_state ; |
| 196 |
SRC_DATA src_data ; |
| 197 |
|
| 198 |
double output_peak, snr ; |
| 199 |
int k, output_len, input_len, error ; |
| 200 |
|
| 201 |
if (verbose != 0) |
| 202 |
{ printf ("\tSignal-to-Noise Ratio Test %d.\n" |
| 203 |
"\t=====================================\n", number) ; |
| 204 |
printf ("\tFrequencies : [ ") ; |
| 205 |
for (k = 0 ; k < test_data->freq_count ; k++) |
| 206 |
printf ("%6.4f ", test_data->freqs [k]) ; |
| 207 |
|
| 208 |
printf ("]\n\tSRC Ratio : %8.4f\n", test_data->src_ratio) ; |
| 209 |
} |
| 210 |
else |
| 211 |
{ printf ("\tSignal-to-Noise Ratio Test %d : ", number) ; |
| 212 |
fflush (stdout) ; |
| 213 |
} ; |
| 214 |
|
| 215 |
/* Set up the output array. */ |
| 216 |
if (test_data->src_ratio >= 1.0) |
| 217 |
{ output_len = MAX_SPEC_LEN ; |
| 218 |
input_len = (int) ceil (MAX_SPEC_LEN / test_data->src_ratio) ; |
| 219 |
if (input_len > BUFFER_LEN) |
| 220 |
input_len = BUFFER_LEN ; |
| 221 |
} |
| 222 |
else |
| 223 |
{ input_len = BUFFER_LEN ; |
| 224 |
output_len = (int) ceil (BUFFER_LEN * test_data->src_ratio) ; |
| 225 |
output_len &= ((~0u) << 4) ; |
| 226 |
if (output_len > MAX_SPEC_LEN) |
| 227 |
output_len = MAX_SPEC_LEN ; |
| 228 |
input_len = (int) ceil (output_len / test_data->src_ratio) ; |
| 229 |
} ; |
| 230 |
|
| 231 |
memset (output, 0, sizeof (output)) ; |
| 232 |
|
| 233 |
/* Generate input data array. */ |
| 234 |
gen_windowed_sines (test_data->freq_count, test_data->freqs, 1.0, data, input_len) ; |
| 235 |
|
| 236 |
/* Perform sample rate conversion. */ |
| 237 |
if ((src_state = src_new (converter, 1, &error)) == NULL) |
| 238 |
{ printf ("\n\nLine %d : src_new() failed : %s.\n\n", __LINE__, src_strerror (error)) ; |
| 239 |
exit (1) ; |
| 240 |
} ; |
| 241 |
|
| 242 |
src_data.end_of_input = 1 ; /* Only one buffer worth of input. */ |
| 243 |
|
| 244 |
src_data.data_in = data ; |
| 245 |
src_data.input_frames = input_len ; |
| 246 |
|
| 247 |
src_data.src_ratio = test_data->src_ratio ; |
| 248 |
|
| 249 |
src_data.data_out = output ; |
| 250 |
src_data.output_frames = output_len ; |
| 251 |
|
| 252 |
if ((error = src_process (src_state, &src_data))) |
| 253 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 254 |
exit (1) ; |
| 255 |
} ; |
| 256 |
|
| 257 |
src_state = src_delete (src_state) ; |
| 258 |
|
| 259 |
if (verbose != 0) |
| 260 |
printf ("\tOutput Len : %ld\n", src_data.output_frames_gen) ; |
| 261 |
|
| 262 |
if (abs ((int) (src_data.output_frames_gen - output_len)) > 4) |
| 263 |
{ printf ("\n\nLine %d : output data length should be %d.\n\n", __LINE__, output_len) ; |
| 264 |
exit (1) ; |
| 265 |
} ; |
| 266 |
|
| 267 |
/* Check output peak. */ |
| 268 |
output_peak = find_peak (output, src_data.output_frames_gen) ; |
| 269 |
|
| 270 |
if (verbose != 0) |
| 271 |
printf ("\tOutput Peak : %6.4f\n", output_peak) ; |
| 272 |
|
| 273 |
if (fabs (output_peak - test_data->peak_value) > 0.01) |
| 274 |
{ printf ("\n\nLine %d : output peak (%6.4f) should be %6.4f\n\n", __LINE__, output_peak, test_data->peak_value) ; |
| 275 |
save_oct_float ("snr_test.dat", data, BUFFER_LEN, output, output_len) ; |
| 276 |
exit (1) ; |
| 277 |
} ; |
| 278 |
|
| 279 |
/* Calculate signal-to-noise ratio. */ |
| 280 |
snr = calculate_snr (output, src_data.output_frames_gen, test_data->pass_band_peaks) ; |
| 281 |
|
| 282 |
if (snr < 0.0) |
| 283 |
{ /* An error occurred. */ |
| 284 |
save_oct_float ("snr_test.dat", data, BUFFER_LEN, output, src_data.output_frames_gen) ; |
| 285 |
exit (1) ; |
| 286 |
} ; |
| 287 |
|
| 288 |
if (verbose != 0) |
| 289 |
printf ("\tSNR Ratio : %.2f dB\n", snr) ; |
| 290 |
|
| 291 |
if (snr < test_data->snr) |
| 292 |
{ printf ("\n\nLine %d : SNR (%5.2f) should be > %6.2f dB\n\n", __LINE__, snr, test_data->snr) ; |
| 293 |
exit (1) ; |
| 294 |
} ; |
| 295 |
|
| 296 |
if (verbose != 0) |
| 297 |
puts ("\t-------------------------------------\n\tPass\n") ; |
| 298 |
else |
| 299 |
puts ("Pass") ; |
| 300 |
|
| 301 |
return snr ; |
| 302 |
} /* snr_test */ |
| 303 |
|
| 304 |
static double |
| 305 |
find_peak (float *data, int len) |
| 306 |
{ double peak = 0.0 ; |
| 307 |
int k = 0 ; |
| 308 |
|
| 309 |
for (k = 0 ; k < len ; k++) |
| 310 |
if (fabs (data [k]) > peak) |
| 311 |
peak = fabs (data [k]) ; |
| 312 |
|
| 313 |
return peak ; |
| 314 |
} /* find_peak */ |
| 315 |
|
| 316 |
|
| 317 |
static double |
| 318 |
find_attenuation (double freq, int converter, int verbose) |
| 319 |
{ static float input [BUFFER_LEN] ; |
| 320 |
static float output [2 * BUFFER_LEN] ; |
| 321 |
|
| 322 |
SRC_DATA src_data ; |
| 323 |
double output_peak ; |
| 324 |
int error ; |
| 325 |
|
| 326 |
gen_windowed_sines (1, &freq, 1.0, input, BUFFER_LEN) ; |
| 327 |
|
| 328 |
src_data.end_of_input = 1 ; /* Only one buffer worth of input. */ |
| 329 |
|
| 330 |
src_data.data_in = input ; |
| 331 |
src_data.input_frames = BUFFER_LEN ; |
| 332 |
|
| 333 |
src_data.src_ratio = 1.999 ; |
| 334 |
|
| 335 |
src_data.data_out = output ; |
| 336 |
src_data.output_frames = ARRAY_LEN (output) ; |
| 337 |
|
| 338 |
if ((error = src_simple (&src_data, converter, 1))) |
| 339 |
{ printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ; |
| 340 |
exit (1) ; |
| 341 |
} ; |
| 342 |
|
| 343 |
output_peak = find_peak (output, ARRAY_LEN (output)) ; |
| 344 |
|
| 345 |
if (verbose) |
| 346 |
printf ("\tFreq : %6f InPeak : %6f OutPeak : %6f Atten : %6.2f dB\n", |
| 347 |
freq, 1.0, output_peak, 20.0 * log10 (1.0 / output_peak)) ; |
| 348 |
|
| 349 |
return 20.0 * log10 (1.0 / output_peak) ; |
| 350 |
} /* find_attenuation */ |
| 351 |
|
| 352 |
static double |
| 353 |
bandwidth_test (int converter, int verbose) |
| 354 |
{ double f1, f2, a1, a2 ; |
| 355 |
double freq, atten ; |
| 356 |
|
| 357 |
f1 = 0.35 ; |
| 358 |
a1 = find_attenuation (f1, converter, verbose) ; |
| 359 |
|
| 360 |
f2 = 0.495 ; |
| 361 |
a2 = find_attenuation (f2, converter, verbose) ; |
| 362 |
|
| 363 |
if (a1 > 3.0 || a2 < 3.0) |
| 364 |
{ printf ("\n\nLine %d : cannot bracket 3dB point.\n\n", __LINE__) ; |
| 365 |
exit (1) ; |
| 366 |
} ; |
| 367 |
|
| 368 |
while (a2 - a1 > 1.0) |
| 369 |
{ freq = f1 + 0.5 * (f2 - f1) ; |
| 370 |
atten = find_attenuation (freq, converter, verbose) ; |
| 371 |
|
| 372 |
if (atten < 3.0) |
| 373 |
{ f1 = freq ; |
| 374 |
a1 = atten ; |
| 375 |
} |
| 376 |
else |
| 377 |
{ f2 = freq ; |
| 378 |
a2 = atten ; |
| 379 |
} ; |
| 380 |
} ; |
| 381 |
|
| 382 |
freq = f1 + (3.0 - a1) * (f2 - f1) / (a2 - a1) ; |
| 383 |
|
| 384 |
return 200.0 * freq ; |
| 385 |
} /* bandwidth_test */ |
| 386 |
|
| 387 |
#else /* (HAVE_FFTW3) == 0 */ |
| 388 |
|
| 389 |
/* Alternative main function when librfftw is not available. */ |
| 390 |
|
| 391 |
int |
| 392 |
main (void) |
| 393 |
{ puts ("\n" |
| 394 |
"****************************************************************\n" |
| 395 |
" This test cannot be run without FFTW (http://www.fftw.org/).\n" |
| 396 |
" Both the real and the complex versions of the library are\n" |
| 397 |
" required.") ; |
| 398 |
puts ("****************************************************************\n") ; |
| 399 |
|
| 400 |
return 0 ; |
| 401 |
} /* main */ |
| 402 |
|
| 403 |
#endif |
| 404 |
|