comparison dsp/rateconversion/TestResampler.cpp @ 366:767947956fc1

More resampler fixes (particularly to latency calculation) and tests
author Chris Cannam <c.cannam@qmul.ac.uk>
date Mon, 14 Oct 2013 16:15:32 +0100
parents b73dad5e6201
children f8fc21365a8c
comparison
equal deleted inserted replaced
365:b73dad5e6201 366:767947956fc1
21 testResamplerOneShot(int sourceRate, 21 testResamplerOneShot(int sourceRate,
22 int targetRate, 22 int targetRate,
23 int n, 23 int n,
24 double *in, 24 double *in,
25 int m, 25 int m,
26 double *expected) 26 double *expected,
27 int skip)
27 { 28 {
28 vector<double> resampled = Resampler::resample(sourceRate, targetRate, 29 vector<double> resampled = Resampler::resample(sourceRate, targetRate,
29 in, n); 30 in, n);
30 BOOST_CHECK_EQUAL(resampled.size(), m); 31 if (skip == 0) {
32 BOOST_CHECK_EQUAL(resampled.size(), m);
33 }
31 for (int i = 0; i < m; ++i) { 34 for (int i = 0; i < m; ++i) {
32 BOOST_CHECK_SMALL(resampled[i] - expected[i], 1e-8); 35 BOOST_CHECK_SMALL(resampled[i + skip] - expected[i], 1e-8);
33 } 36 }
34 } 37 }
35 38
36 void 39 void
37 testResampler(int sourceRate, 40 testResampler(int sourceRate,
43 { 46 {
44 // Here we provide the input in chunks (of varying size) 47 // Here we provide the input in chunks (of varying size)
45 48
46 Resampler r(sourceRate, targetRate); 49 Resampler r(sourceRate, targetRate);
47 int latency = r.getLatency(); 50 int latency = r.getLatency();
48 std::cerr << "latency = " << latency << std::endl;
49 51
50 int m1 = m + latency; 52 int m1 = m + latency;
51 int n1 = int((m1 * sourceRate) / targetRate); 53 int n1 = int((m1 * sourceRate) / targetRate);
52 54
53 double *inPadded = new double[n1]; 55 double *inPadded = new double[n1];
65 int chunkSize = 1; 67 int chunkSize = 1;
66 int got = 0; 68 int got = 0;
67 int i = 0; 69 int i = 0;
68 70
69 while (true) { 71 while (true) {
70 std::cerr << "i = " << i << ", n1 = " << n1 << ", chunkSize = " << chunkSize << std::endl;
71 got += r.process(inPadded + i, outPadded + got, chunkSize); 72 got += r.process(inPadded + i, outPadded + got, chunkSize);
72 i = i + chunkSize; 73 i = i + chunkSize;
73 chunkSize = chunkSize + 1; 74 chunkSize = chunkSize + 1;
74 if (i + 1 >= n1) { 75 if (i >= n1) {
75 break; 76 break;
76 } else if (i + chunkSize >= n1) { 77 } else if (i + chunkSize >= n1) {
77 chunkSize = n1 - i; 78 chunkSize = n1 - i;
79 } else if (chunkSize > 15) {
80 chunkSize = 1;
78 } 81 }
79 } 82 }
80 83
81 // int got = r.process(inPadded, outPadded, n1); 84 BOOST_CHECK_EQUAL(got, m1);
82 std::cerr << i << " in, " << got << " out" << std::endl;
83 85
84 BOOST_CHECK_EQUAL(got, m1);
85 /*
86 std::cerr << "results including latency padding:" << std::endl;
87 for (int i = 0; i < m1; ++i) {
88 std::cerr << outPadded[i] << " ";
89 if (i % 6 == 5) std::cerr << "\n";
90 }
91 std::cerr << "\n";
92 */
93 for (int i = latency; i < m1; ++i) { 86 for (int i = latency; i < m1; ++i) {
94 BOOST_CHECK_SMALL(outPadded[i] - expected[i-latency], 1e-8); 87 BOOST_CHECK_SMALL(outPadded[i] - expected[i-latency], 1e-8);
95 } 88 }
89
96 delete[] outPadded; 90 delete[] outPadded;
97 delete[] inPadded; 91 delete[] inPadded;
98 } 92 }
99 93
100 BOOST_AUTO_TEST_CASE(sameRateOneShot) 94 BOOST_AUTO_TEST_CASE(sameRateOneShot)
101 { 95 {
102 double d[] = { 0, 0.1, -0.3, -0.4, -0.3, 0, 0.5, 0.2, 0.8, -0.1 }; 96 double d[] = { 0, 0.1, -0.3, -0.4, -0.3, 0, 0.5, 0.2, 0.8, -0.1 };
103 testResamplerOneShot(4, 4, 10, d, 10, d); 97 testResamplerOneShot(4, 4, 10, d, 10, d, 0);
104 } 98 }
105 99
106 BOOST_AUTO_TEST_CASE(sameRate) 100 BOOST_AUTO_TEST_CASE(sameRate)
107 { 101 {
108 double d[] = { 0, 0.1, -0.3, -0.4, -0.3, 0, 0.5, 0.2, 0.8, -0.1 }; 102 double d[] = { 0, 0.1, -0.3, -0.4, -0.3, 0, 0.5, 0.2, 0.8, -0.1 };
109 testResampler(4, 4, 10, d, 10, d); 103 testResampler(4, 4, 10, d, 10, d);
110 } 104 }
111 105
106 BOOST_AUTO_TEST_CASE(interpolatedMisc)
107 {
108 // Interpolating any signal by N should give a signal in which
109 // every Nth sample is the original signal
110 double in[] = { 0, 0.1, -0.3, -0.4, -0.3, 0, 0.5, 0.2, 0.8, -0.1 };
111 int n = sizeof(in)/sizeof(in[0]);
112 for (int factor = 2; factor < 10; ++factor) {
113 vector<double> out = Resampler::resample(6, 6 * factor, in, n);
114 for (int i = 0; i < n; ++i) {
115 BOOST_CHECK_SMALL(out[i * factor] - in[i], 1e-5);
116 }
117 }
118 }
119
120 BOOST_AUTO_TEST_CASE(interpolatedSine)
121 {
122 double in[1000];
123 double out[2000];
124 for (int i = 0; i < 1000; ++i) {
125 in[i] = sin(i * M_PI / 2.0);
126 }
127 for (int i = 0; i < 2000; ++i) {
128 out[i] = sin(i * M_PI / 4.0);
129 }
130 testResamplerOneShot(8, 16, 1000, in, 200, out, 400);
131 }
132
112 BOOST_AUTO_TEST_SUITE_END() 133 BOOST_AUTO_TEST_SUITE_END()
113 134