Mercurial > hg > qm-dsp
comparison dsp/rateconversion/TestResampler.cpp @ 143:a4aa37f7af28
Some fixes, and start on spectrum test
author | Chris Cannam |
---|---|
date | Tue, 15 Oct 2013 18:27:19 +0100 |
parents | f8fc21365a8c |
children | b21e97d570be |
comparison
equal
deleted
inserted
replaced
142:f8fc21365a8c | 143:a4aa37f7af28 |
---|---|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ | 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ |
2 | 2 |
3 #include "Resampler.h" | 3 #include "Resampler.h" |
4 | |
5 #include "qm-dsp/base/Window.h" | |
6 #include "qm-dsp/dsp/transforms/FFT.h" | |
4 | 7 |
5 #include <iostream> | 8 #include <iostream> |
6 | 9 |
7 #include <cmath> | 10 #include <cmath> |
8 | 11 |
145 out[i] = sin(i * M_PI / 4.0); | 148 out[i] = sin(i * M_PI / 4.0); |
146 } | 149 } |
147 testResamplerOneShot(16, 8, 2000, in, 200, out, 256); | 150 testResamplerOneShot(16, 8, 2000, in, 200, out, 256); |
148 } | 151 } |
149 | 152 |
153 vector<double> | |
154 squareWave(int rate, int 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 / m; | |
161 for (int i = 0; i < n; ++i) { | |
162 v[i] += scale * sin(i * 2 * M_PI * freq / rate); | |
163 } | |
164 } | |
165 return v; | |
166 } | |
167 | |
168 void | |
169 testSpectrum(int inrate, int outrate) | |
170 { | |
171 // One second of a square wave | |
172 int freq = 500; | |
173 | |
174 std::cerr << "inrate = " << inrate << ", outrate = " << outrate << ", freq * outrate / inrate = " << (double(freq) * double(outrate)) / double(inrate) << std::endl; | |
175 | |
176 std::cerr << "making square wave... "; | |
177 vector<double> square = | |
178 squareWave(inrate, freq, inrate); | |
179 std::cerr << "done" << std::endl; | |
180 | |
181 vector<double> maybeSquare = | |
182 Resampler::resample(inrate, outrate, square.data(), square.size()); | |
183 | |
184 BOOST_CHECK_EQUAL(maybeSquare.size(), outrate); | |
185 | |
186 Window<double>(HanningWindow, inrate).cut(square.data()); | |
187 Window<double>(HanningWindow, outrate).cut(maybeSquare.data()); | |
188 | |
189 // forward magnitude with size inrate, outrate | |
190 | |
191 vector<double> inSpectrum(inrate, 0.0); | |
192 FFTReal(inrate).forwardMagnitude(square.data(), inSpectrum.data()); | |
193 | |
194 vector<double> outSpectrum(outrate, 0.0); | |
195 FFTReal(outrate).forwardMagnitude(maybeSquare.data(), outSpectrum.data()); | |
196 | |
197 // Don't compare bins any higher than 99% of Nyquist freq of lower sr | |
198 int lengthOfInterest = (inrate < outrate ? inrate : outrate) / 2; | |
199 lengthOfInterest = lengthOfInterest - (lengthOfInterest / 100); | |
200 | |
201 for (int i = 0; i < lengthOfInterest; ++i) { | |
202 BOOST_CHECK_SMALL(inSpectrum[i] - outSpectrum[i], 1e-7); | |
203 } | |
204 } | |
205 | |
206 BOOST_AUTO_TEST_CASE(spectrum) | |
207 { | |
208 int rates[] = { 8000, 22050, 44100, 48000 }; | |
209 for (int i = 0; i < sizeof(rates)/sizeof(rates[0]); ++i) { | |
210 for (int j = 0; j < sizeof(rates)/sizeof(rates[0]); ++j) { | |
211 testSpectrum(rates[i], rates[j]); | |
212 } | |
213 } | |
214 } | |
215 | |
150 BOOST_AUTO_TEST_SUITE_END() | 216 BOOST_AUTO_TEST_SUITE_END() |
151 | 217 |