Mercurial > hg > qm-dsp
comparison tests/TestResampler.cpp @ 476:2de6184b2ce0
Untabify
author | Chris Cannam <cannam@all-day-breakfast.com> |
---|---|
date | Thu, 30 May 2019 18:30:58 +0100 |
parents | e48b3f641038 |
children |
comparison
equal
deleted
inserted
replaced
475:64fc3009d0a3 | 476:2de6184b2ce0 |
---|---|
20 using std::endl; | 20 using std::endl; |
21 using std::vector; | 21 using std::vector; |
22 | 22 |
23 void | 23 void |
24 testResamplerOneShot(int sourceRate, | 24 testResamplerOneShot(int sourceRate, |
25 int targetRate, | 25 int targetRate, |
26 int n, | 26 int n, |
27 double *in, | 27 double *in, |
28 int m, | 28 int m, |
29 double *expected, | 29 double *expected, |
30 int skip) | 30 int skip) |
31 { | 31 { |
32 vector<double> resampled = Resampler::resample(sourceRate, targetRate, | 32 vector<double> resampled = Resampler::resample(sourceRate, targetRate, |
33 in, n); | 33 in, n); |
34 if (skip == 0) { | 34 if (skip == 0) { |
35 BOOST_CHECK_EQUAL(resampled.size(), m); | 35 BOOST_CHECK_EQUAL(resampled.size(), m); |
36 } | 36 } |
37 for (int i = 0; i < m; ++i) { | 37 for (int i = 0; i < m; ++i) { |
38 BOOST_CHECK_SMALL(resampled[i + skip] - expected[i], 1e-6); | 38 BOOST_CHECK_SMALL(resampled[i + skip] - expected[i], 1e-6); |
39 } | 39 } |
40 } | 40 } |
41 | 41 |
42 void | 42 void |
43 testResampler(int sourceRate, | 43 testResampler(int sourceRate, |
44 int targetRate, | 44 int targetRate, |
45 int n, | 45 int n, |
46 double *in, | 46 double *in, |
47 int m, | 47 int m, |
48 double *expected) | 48 double *expected) |
49 { | 49 { |
50 // Here we provide the input in chunks (of varying size) | 50 // Here we provide the input in chunks (of varying size) |
51 | 51 |
52 Resampler r(sourceRate, targetRate); | 52 Resampler r(sourceRate, targetRate); |
53 int latency = r.getLatency(); | 53 int latency = r.getLatency(); |
57 | 57 |
58 double *inPadded = new double[n1]; | 58 double *inPadded = new double[n1]; |
59 double *outPadded = new double[m1]; | 59 double *outPadded = new double[m1]; |
60 | 60 |
61 for (int i = 0; i < n1; ++i) { | 61 for (int i = 0; i < n1; ++i) { |
62 if (i < n) inPadded[i] = in[i]; | 62 if (i < n) inPadded[i] = in[i]; |
63 else inPadded[i] = 0.0; | 63 else inPadded[i] = 0.0; |
64 } | 64 } |
65 | 65 |
66 for (int i = 0; i < m1; ++i) { | 66 for (int i = 0; i < m1; ++i) { |
67 outPadded[i] = -999.0; | 67 outPadded[i] = -999.0; |
68 } | 68 } |
69 | 69 |
70 int chunkSize = 1; | 70 int chunkSize = 1; |
71 int got = 0; | 71 int got = 0; |
72 int i = 0; | 72 int i = 0; |
73 | 73 |
74 while (true) { | 74 while (true) { |
75 got += r.process(inPadded + i, outPadded + got, chunkSize); | 75 got += r.process(inPadded + i, outPadded + got, chunkSize); |
76 i = i + chunkSize; | 76 i = i + chunkSize; |
77 chunkSize = chunkSize + 1; | 77 chunkSize = chunkSize + 1; |
78 if (i >= n1) { | 78 if (i >= n1) { |
79 break; | 79 break; |
80 } else if (i + chunkSize >= n1) { | 80 } else if (i + chunkSize >= n1) { |
81 chunkSize = n1 - i; | 81 chunkSize = n1 - i; |
82 } else if (chunkSize > 15) { | 82 } else if (chunkSize > 15) { |
83 chunkSize = 1; | 83 chunkSize = 1; |
84 } | 84 } |
85 } | 85 } |
86 | 86 |
87 BOOST_CHECK_EQUAL(got, m1); | 87 BOOST_CHECK_EQUAL(got, m1); |
88 | 88 |
89 for (int i = latency; i < m1; ++i) { | 89 for (int i = latency; i < m1; ++i) { |
90 BOOST_CHECK_SMALL(outPadded[i] - expected[i-latency], 1e-8); | 90 BOOST_CHECK_SMALL(outPadded[i] - expected[i-latency], 1e-8); |
91 } | 91 } |
92 | 92 |
93 delete[] outPadded; | 93 delete[] outPadded; |
94 delete[] inPadded; | 94 delete[] inPadded; |
95 } | 95 } |
111 // Interpolating any signal by N should give a signal in which | 111 // Interpolating any signal by N should give a signal in which |
112 // every Nth sample is the original signal | 112 // every Nth sample is the original signal |
113 double in[] = { 0, 0.1, -0.3, -0.4, -0.3, 0, 0.5, 0.2, 0.8, -0.1 }; | 113 double in[] = { 0, 0.1, -0.3, -0.4, -0.3, 0, 0.5, 0.2, 0.8, -0.1 }; |
114 int n = sizeof(in)/sizeof(in[0]); | 114 int n = sizeof(in)/sizeof(in[0]); |
115 for (int factor = 2; factor < 10; ++factor) { | 115 for (int factor = 2; factor < 10; ++factor) { |
116 vector<double> out = Resampler::resample(6, 6 * factor, in, n); | 116 vector<double> out = Resampler::resample(6, 6 * factor, in, n); |
117 for (int i = 0; i < n; ++i) { | 117 for (int i = 0; i < n; ++i) { |
118 BOOST_CHECK_SMALL(out[i * factor] - in[i], 1e-5); | 118 BOOST_CHECK_SMALL(out[i * factor] - in[i], 1e-5); |
119 } | 119 } |
120 } | 120 } |
121 } | 121 } |
122 | 122 |
123 BOOST_AUTO_TEST_CASE(interpolatedSine) | 123 BOOST_AUTO_TEST_CASE(interpolatedSine) |
124 { | 124 { |
125 // Interpolating a sinusoid should give us a sinusoid, once we've | 125 // Interpolating a sinusoid should give us a sinusoid, once we've |
126 // dropped the first few samples | 126 // dropped the first few samples |
127 double in[1000]; | 127 double in[1000]; |
128 double out[2000]; | 128 double out[2000]; |
129 for (int i = 0; i < 1000; ++i) { | 129 for (int i = 0; i < 1000; ++i) { |
130 in[i] = sin(i * M_PI / 2.0); | 130 in[i] = sin(i * M_PI / 2.0); |
131 } | 131 } |
132 for (int i = 0; i < 2000; ++i) { | 132 for (int i = 0; i < 2000; ++i) { |
133 out[i] = sin(i * M_PI / 4.0); | 133 out[i] = sin(i * M_PI / 4.0); |
134 } | 134 } |
135 testResamplerOneShot(8, 16, 1000, in, 200, out, 512); | 135 testResamplerOneShot(8, 16, 1000, in, 200, out, 512); |
136 } | 136 } |
137 | 137 |
138 BOOST_AUTO_TEST_CASE(decimatedSine) | 138 BOOST_AUTO_TEST_CASE(decimatedSine) |
140 // Decimating a sinusoid should give us a sinusoid, once we've | 140 // Decimating a sinusoid should give us a sinusoid, once we've |
141 // dropped the first few samples | 141 // dropped the first few samples |
142 double in[2000]; | 142 double in[2000]; |
143 double out[1000]; | 143 double out[1000]; |
144 for (int i = 0; i < 2000; ++i) { | 144 for (int i = 0; i < 2000; ++i) { |
145 in[i] = sin(i * M_PI / 8.0); | 145 in[i] = sin(i * M_PI / 8.0); |
146 } | 146 } |
147 for (int i = 0; i < 1000; ++i) { | 147 for (int i = 0; i < 1000; ++i) { |
148 out[i] = sin(i * M_PI / 4.0); | 148 out[i] = sin(i * M_PI / 4.0); |
149 } | 149 } |
150 testResamplerOneShot(16, 8, 2000, in, 200, out, 256); | 150 testResamplerOneShot(16, 8, 2000, in, 200, out, 256); |
151 } | 151 } |
152 | 152 |
153 double | 153 double |
249 squareWave(int rate, double freq, int n) | 249 squareWave(int rate, double freq, int n) |
250 { | 250 { |
251 //!!! todo: hoist, test | 251 //!!! todo: hoist, test |
252 vector<double> v(n, 0.0); | 252 vector<double> v(n, 0.0); |
253 for (int h = 0; h < (rate/4)/freq; ++h) { | 253 for (int h = 0; h < (rate/4)/freq; ++h) { |
254 double m = h * 2 + 1; | 254 double m = h * 2 + 1; |
255 double scale = 1.0 / m; | 255 double scale = 1.0 / m; |
256 for (int i = 0; i < n; ++i) { | 256 for (int i = 0; i < n; ++i) { |
257 double s = scale * sin((i * 2.0 * M_PI * m * freq) / rate); | 257 double s = scale * sin((i * 2.0 * M_PI * m * freq) / rate); |
258 v[i] += s; | 258 v[i] += s; |
259 } | 259 } |
260 } | 260 } |
261 return v; | 261 return v; |
262 } | 262 } |
263 | 263 |
264 void | 264 void |
266 { | 266 { |
267 // One second of a square wave | 267 // One second of a square wave |
268 int freq = 500; | 268 int freq = 500; |
269 | 269 |
270 vector<double> square = | 270 vector<double> square = |
271 squareWave(inrate, freq, inrate); | 271 squareWave(inrate, freq, inrate); |
272 | 272 |
273 vector<double> maybeSquare = | 273 vector<double> maybeSquare = |
274 Resampler::resample(inrate, outrate, square.data(), square.size()); | 274 Resampler::resample(inrate, outrate, square.data(), square.size()); |
275 | 275 |
276 BOOST_CHECK_EQUAL(maybeSquare.size(), outrate); | 276 BOOST_CHECK_EQUAL(maybeSquare.size(), outrate); |
277 | 277 |
278 Window<double>(HanningWindow, inrate).cut(square.data()); | 278 Window<double>(HanningWindow, inrate).cut(square.data()); |
279 Window<double>(HanningWindow, outrate).cut(maybeSquare.data()); | 279 Window<double>(HanningWindow, outrate).cut(maybeSquare.data()); |
281 // forward magnitude with size inrate, outrate | 281 // forward magnitude with size inrate, outrate |
282 | 282 |
283 vector<double> inSpectrum(inrate, 0.0); | 283 vector<double> inSpectrum(inrate, 0.0); |
284 FFTReal(inrate).forwardMagnitude(square.data(), inSpectrum.data()); | 284 FFTReal(inrate).forwardMagnitude(square.data(), inSpectrum.data()); |
285 for (int i = 0; i < (int)inSpectrum.size(); ++i) { | 285 for (int i = 0; i < (int)inSpectrum.size(); ++i) { |
286 inSpectrum[i] /= inrate; | 286 inSpectrum[i] /= inrate; |
287 } | 287 } |
288 | 288 |
289 vector<double> outSpectrum(outrate, 0.0); | 289 vector<double> outSpectrum(outrate, 0.0); |
290 FFTReal(outrate).forwardMagnitude(maybeSquare.data(), outSpectrum.data()); | 290 FFTReal(outrate).forwardMagnitude(maybeSquare.data(), outSpectrum.data()); |
291 for (int i = 0; i < (int)outSpectrum.size(); ++i) { | 291 for (int i = 0; i < (int)outSpectrum.size(); ++i) { |
292 outSpectrum[i] /= outrate; | 292 outSpectrum[i] /= outrate; |
293 } | 293 } |
294 | 294 |
295 // Don't compare bins any higher than 96% of Nyquist freq of lower sr | 295 // Don't compare bins any higher than 96% of Nyquist freq of lower sr |
296 int lengthOfInterest = (inrate < outrate ? inrate : outrate) / 2; | 296 int lengthOfInterest = (inrate < outrate ? inrate : outrate) / 2; |
297 lengthOfInterest = lengthOfInterest - (lengthOfInterest / 25); | 297 lengthOfInterest = lengthOfInterest - (lengthOfInterest / 25); |
298 | 298 |
299 for (int i = 0; i < lengthOfInterest; ++i) { | 299 for (int i = 0; i < lengthOfInterest; ++i) { |
300 BOOST_CHECK_SMALL(inSpectrum[i] - outSpectrum[i], 1e-7); | 300 BOOST_CHECK_SMALL(inSpectrum[i] - outSpectrum[i], 1e-7); |
301 } | 301 } |
302 } | 302 } |
303 /* | 303 /* |
304 BOOST_AUTO_TEST_CASE(spectrum) | 304 BOOST_AUTO_TEST_CASE(spectrum) |
305 { | 305 { |
306 int rates[] = { 8000, 22050, 44100, 48000 }; | 306 int rates[] = { 8000, 22050, 44100, 48000 }; |
307 for (int i = 0; i < (int)(sizeof(rates)/sizeof(rates[0])); ++i) { | 307 for (int i = 0; i < (int)(sizeof(rates)/sizeof(rates[0])); ++i) { |
308 for (int j = 0; j < (int)(sizeof(rates)/sizeof(rates[0])); ++j) { | 308 for (int j = 0; j < (int)(sizeof(rates)/sizeof(rates[0])); ++j) { |
309 testSpectrum(rates[i], rates[j]); | 309 testSpectrum(rates[i], rates[j]); |
310 } | 310 } |
311 } | 311 } |
312 } | 312 } |
313 */ | 313 */ |
314 BOOST_AUTO_TEST_SUITE_END() | 314 BOOST_AUTO_TEST_SUITE_END() |
315 | 315 |