comparison dsp/rateconversion/TestResampler.cpp @ 144:b21e97d570be

Fixes to tests
author Chris Cannam
date Wed, 16 Oct 2013 08:16:23 +0100
parents a4aa37f7af28
children c1e98c18628a
comparison
equal deleted inserted replaced
143:a4aa37f7af28 144:b21e97d570be
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 vector<double> 153 vector<double>
154 squareWave(int rate, int freq, int n) 154 squareWave(int rate, double freq, int n)
155 { 155 {
156 //!!! todo: hoist, test 156 //!!! todo: hoist, test
157 vector<double> v(n, 0.0); 157 vector<double> v(n, 0.0);
158 for (int h = 0; h < (rate/4)/freq; ++h) { 158 for (int h = 0; h < (rate/4)/freq; ++h) {
159 double m = h * 2 + 1; 159 double m = h * 2 + 1;
160 double scale = 1 / m; 160 double scale = 1.0 / m;
161 for (int i = 0; i < n; ++i) { 161 for (int i = 0; i < n; ++i) {
162 v[i] += scale * sin(i * 2 * M_PI * freq / rate); 162 double s = scale * sin((i * 2.0 * M_PI * m * freq) / rate);
163 v[i] += s;
163 } 164 }
164 } 165 }
165 return v; 166 return v;
166 } 167 }
167 168
188 189
189 // forward magnitude with size inrate, outrate 190 // forward magnitude with size inrate, outrate
190 191
191 vector<double> inSpectrum(inrate, 0.0); 192 vector<double> inSpectrum(inrate, 0.0);
192 FFTReal(inrate).forwardMagnitude(square.data(), inSpectrum.data()); 193 FFTReal(inrate).forwardMagnitude(square.data(), inSpectrum.data());
194 for (int i = 0; i < inSpectrum.size(); ++i) {
195 inSpectrum[i] /= inrate;
196 }
193 197
194 vector<double> outSpectrum(outrate, 0.0); 198 vector<double> outSpectrum(outrate, 0.0);
195 FFTReal(outrate).forwardMagnitude(maybeSquare.data(), outSpectrum.data()); 199 FFTReal(outrate).forwardMagnitude(maybeSquare.data(), outSpectrum.data());
200 for (int i = 0; i < outSpectrum.size(); ++i) {
201 outSpectrum[i] /= outrate;
202 }
196 203
197 // Don't compare bins any higher than 99% of Nyquist freq of lower sr 204 // Don't compare bins any higher than 99% of Nyquist freq of lower sr
198 int lengthOfInterest = (inrate < outrate ? inrate : outrate) / 2; 205 int lengthOfInterest = (inrate < outrate ? inrate : outrate) / 2;
199 lengthOfInterest = lengthOfInterest - (lengthOfInterest / 100); 206 lengthOfInterest = lengthOfInterest - (lengthOfInterest / 100);
200 207 /*
208 std::cerr << "inSpectrum:" << std::endl;
209 for (int i = 0; i < lengthOfInterest; ++i) {
210 if (i % 5 == 0) std::cerr << std::endl << i << ": ";
211 std::cerr << inSpectrum[i] << " ";
212 }
213
214 std::cerr << "\noutSpectrum:" << std::endl;
215 for (int i = 0; i < lengthOfInterest; ++i) {
216 if (i % 5 == 0) std::cerr << std::endl << i << ": ";
217 std::cerr << outSpectrum[i] << " ";
218 }
219 std::cerr << std::endl;
220 */
201 for (int i = 0; i < lengthOfInterest; ++i) { 221 for (int i = 0; i < lengthOfInterest; ++i) {
202 BOOST_CHECK_SMALL(inSpectrum[i] - outSpectrum[i], 1e-7); 222 BOOST_CHECK_SMALL(inSpectrum[i] - outSpectrum[i], 1e-7);
203 } 223 }
204 } 224 }
205 225