| 1 |
/* |
| 2 |
* This file is part of libsidplayfp, a SID player engine. |
| 3 |
* |
| 4 |
* Copyright 2012-2013 Leandro Nini <[email protected]> |
| 5 |
* Copyright 2007-2010 Antti Lankila |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation; either version 2 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program; if not, write to the Free Software |
| 19 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#include <map> |
| 23 |
#include <memory> |
| 24 |
#include <ctime> |
| 25 |
#include <iostream> |
| 26 |
#include <iomanip> |
| 27 |
#include <cmath> |
| 28 |
|
| 29 |
#include "siddefs-fp.h" |
| 30 |
|
| 31 |
#include "Resampler.h" |
| 32 |
#include "TwoPassSincResampler.h" |
| 33 |
|
| 34 |
/** |
| 35 |
* Simple sin waveform in, power output measurement function. |
| 36 |
* It would be far better to use FFT. |
| 37 |
*/ |
| 38 |
int main(int argc, const char* argv[]) |
| 39 |
{ |
| 40 |
const double RATE = 985248.4; |
| 41 |
const int RINGSIZE = 2048; |
| 42 |
|
| 43 |
std::auto_ptr<reSIDfp::TwoPassSincResampler> r(reSIDfp::TwoPassSincResampler::create(RATE, 48000.0, 20000.0)); |
| 44 |
|
| 45 |
std::map<double, double> results; |
| 46 |
clock_t start = clock(); |
| 47 |
|
| 48 |
for (double freq = 1000.; freq < RATE / 2.; freq *= 1.01) |
| 49 |
{ |
| 50 |
/* prefill resampler buffer */ |
| 51 |
int k = 0; |
| 52 |
double omega = 2 * M_PI * freq / RATE; |
| 53 |
|
| 54 |
for (int j = 0; j < RINGSIZE; j ++) |
| 55 |
{ |
| 56 |
int signal = static_cast<int>(32768.0 * sin(k++ * omega) * sqrt(2)); |
| 57 |
r->input(signal); |
| 58 |
} |
| 59 |
|
| 60 |
int n = 0; |
| 61 |
float pwr = 0; |
| 62 |
|
| 63 |
/* Now, during measurement stage, put 100 cycles of waveform through filter. */ |
| 64 |
for (int j = 0; j < 100000; j ++) |
| 65 |
{ |
| 66 |
int signal = static_cast<int>(32768.0 * sin(k++ * omega) * sqrt(2)); |
| 67 |
|
| 68 |
if (r->input(signal)) |
| 69 |
{ |
| 70 |
float out = r->output(); |
| 71 |
pwr += out * out; |
| 72 |
n += 1; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
results.insert(std::make_pair(freq, 10 * log10(pwr / n))); |
| 77 |
} |
| 78 |
|
| 79 |
clock_t end = clock(); |
| 80 |
|
| 81 |
for (std::map<double, double>::iterator it = results.begin(); it != results.end(); ++it) |
| 82 |
{ |
| 83 |
std::cout << std::fixed << std::setprecision(0) << std::setw(6) << (*it).first << " Hz " << (*it).second << " dB" << std::endl; |
| 84 |
} |
| 85 |
|
| 86 |
std::cout << "Filtering time " << (end - start) * 1000. / CLOCKS_PER_SEC << " ms" << std::endl; |
| 87 |
} |