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