comparison tests/TestResampler.cpp @ 375:ad21307eaf99

Integrate resampler and tests into build system etc
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 21 Oct 2013 09:40:22 +0100
parents
children 88971211795c
comparison
equal deleted inserted replaced
374:3e5f13ac984f 375:ad21307eaf99
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 #include "dsp/rateconversion/Resampler.h"
4
5 #include "base/Window.h"
6 #include "dsp/transforms/FFT.h"
7
8 #include <iostream>
9
10 #include <cmath>
11
12 #define BOOST_TEST_DYN_LINK
13 #define BOOST_TEST_MAIN
14
15 #include <boost/test/unit_test.hpp>
16
17 BOOST_AUTO_TEST_SUITE(TestResampler)
18
19 using std::cout;
20 using std::endl;
21 using std::vector;
22
23 void
24 testResamplerOneShot(int sourceRate,
25 int targetRate,
26 int n,
27 double *in,
28 int m,
29 double *expected,
30 int skip)
31 {
32 vector<double> resampled = Resampler::resample(sourceRate, targetRate,
33 in, n);
34 if (skip == 0) {
35 BOOST_CHECK_EQUAL(resampled.size(), m);
36 }
37 for (int i = 0; i < m; ++i) {
38 BOOST_CHECK_SMALL(resampled[i + skip] - expected[i], 1e-6);
39 }
40 }
41
42 void
43 testResampler(int sourceRate,
44 int targetRate,
45 int n,
46 double *in,
47 int m,
48 double *expected)
49 {
50 // Here we provide the input in chunks (of varying size)
51
52 Resampler r(sourceRate, targetRate);
53 int latency = r.getLatency();
54
55 int m1 = m + latency;
56 int n1 = int((m1 * sourceRate) / targetRate);
57
58 double *inPadded = new double[n1];
59 double *outPadded = new double[m1];
60
61 for (int i = 0; i < n1; ++i) {
62 if (i < n) inPadded[i] = in[i];
63 else inPadded[i] = 0.0;
64 }
65
66 for (int i = 0; i < m1; ++i) {
67 outPadded[i] = -999.0;
68 }
69
70 int chunkSize = 1;
71 int got = 0;
72 int i = 0;
73
74 while (true) {
75 got += r.process(inPadded + i, outPadded + got, chunkSize);
76 i = i + chunkSize;
77 chunkSize = chunkSize + 1;
78 if (i >= n1) {
79 break;
80 } else if (i + chunkSize >= n1) {
81 chunkSize = n1 - i;
82 } else if (chunkSize > 15) {
83 chunkSize = 1;
84 }
85 }
86
87 BOOST_CHECK_EQUAL(got, m1);
88
89 for (int i = latency; i < m1; ++i) {
90 BOOST_CHECK_SMALL(outPadded[i] - expected[i-latency], 1e-8);
91 }
92
93 delete[] outPadded;
94 delete[] inPadded;
95 }
96
97 BOOST_AUTO_TEST_CASE(sameRateOneShot)
98 {
99 double d[] = { 0, 0.1, -0.3, -0.4, -0.3, 0, 0.5, 0.2, 0.8, -0.1 };
100 testResamplerOneShot(4, 4, 10, d, 10, d, 0);
101 }
102
103 BOOST_AUTO_TEST_CASE(sameRate)
104 {
105 double d[] = { 0, 0.1, -0.3, -0.4, -0.3, 0, 0.5, 0.2, 0.8, -0.1 };
106 testResampler(4, 4, 10, d, 10, d);
107 }
108
109 BOOST_AUTO_TEST_CASE(interpolatedMisc)
110 {
111 // Interpolating any signal by N should give a signal in which
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 };
114 int n = sizeof(in)/sizeof(in[0]);
115 for (int factor = 2; factor < 10; ++factor) {
116 vector<double> out = Resampler::resample(6, 6 * factor, in, n);
117 for (int i = 0; i < n; ++i) {
118 BOOST_CHECK_SMALL(out[i * factor] - in[i], 1e-5);
119 }
120 }
121 }
122
123 BOOST_AUTO_TEST_CASE(interpolatedSine)
124 {
125 // Interpolating a sinusoid should give us a sinusoid, once we've
126 // dropped the first few samples
127 double in[1000];
128 double out[2000];
129 for (int i = 0; i < 1000; ++i) {
130 in[i] = sin(i * M_PI / 2.0);
131 }
132 for (int i = 0; i < 2000; ++i) {
133 out[i] = sin(i * M_PI / 4.0);
134 }
135 testResamplerOneShot(8, 16, 1000, in, 200, out, 512);
136 }
137
138 BOOST_AUTO_TEST_CASE(decimatedSine)
139 {
140 // Decimating a sinusoid should give us a sinusoid, once we've
141 // dropped the first few samples
142 double in[2000];
143 double out[1000];
144 for (int i = 0; i < 2000; ++i) {
145 in[i] = sin(i * M_PI / 8.0);
146 }
147 for (int i = 0; i < 1000; ++i) {
148 out[i] = sin(i * M_PI / 4.0);
149 }
150 testResamplerOneShot(16, 8, 2000, in, 200, out, 256);
151 }
152
153 vector<double>
154 squareWave(int rate, double freq, int n)
155 {
156 //!!! todo: hoist, test
157 vector<double> v(n, 0.0);
158 for (int h = 0; h < (rate/4)/freq; ++h) {
159 double m = h * 2 + 1;
160 double scale = 1.0 / m;
161 for (int i = 0; i < n; ++i) {
162 double s = scale * sin((i * 2.0 * M_PI * m * freq) / rate);
163 v[i] += s;
164 }
165 }
166 return v;
167 }
168
169 void
170 testSpectrum(int inrate, int outrate)
171 {
172 // One second of a square wave
173 int freq = 500;
174
175 vector<double> square =
176 squareWave(inrate, freq, inrate);
177
178 vector<double> maybeSquare =
179 Resampler::resample(inrate, outrate, square.data(), square.size());
180
181 BOOST_CHECK_EQUAL(maybeSquare.size(), outrate);
182
183 Window<double>(HanningWindow, inrate).cut(square.data());
184 Window<double>(HanningWindow, outrate).cut(maybeSquare.data());
185
186 // forward magnitude with size inrate, outrate
187
188 vector<double> inSpectrum(inrate, 0.0);
189 FFTReal(inrate).forwardMagnitude(square.data(), inSpectrum.data());
190 for (int i = 0; i < (int)inSpectrum.size(); ++i) {
191 inSpectrum[i] /= inrate;
192 }
193
194 vector<double> outSpectrum(outrate, 0.0);
195 FFTReal(outrate).forwardMagnitude(maybeSquare.data(), outSpectrum.data());
196 for (int i = 0; i < (int)outSpectrum.size(); ++i) {
197 outSpectrum[i] /= outrate;
198 }
199
200 // Don't compare bins any higher than 99% of Nyquist freq of lower sr
201 int lengthOfInterest = (inrate < outrate ? inrate : outrate) / 2;
202 lengthOfInterest = lengthOfInterest - (lengthOfInterest / 100);
203
204 for (int i = 0; i < lengthOfInterest; ++i) {
205 BOOST_CHECK_SMALL(inSpectrum[i] - outSpectrum[i], 1e-7);
206 }
207 }
208
209 BOOST_AUTO_TEST_CASE(spectrum)
210 {
211 int rates[] = { 8000, 22050, 44100, 48000 };
212 for (int i = 0; i < (int)(sizeof(rates)/sizeof(rates[0])); ++i) {
213 for (int j = 0; j < (int)(sizeof(rates)/sizeof(rates[0])); ++j) {
214 testSpectrum(rates[i], rates[j]);
215 }
216 }
217 }
218
219 BOOST_AUTO_TEST_SUITE_END()
220