20#include "thread/Thread.h"
36 m_sourceRate(sourceRate),
37 m_targetRate(targetRate)
43 double snr,
double bandwidth) :
44 m_sourceRate(sourceRate),
45 m_targetRate(targetRate)
56static map<double, map<int, map<double, vector<double> > > >
87 vector<double> filter;
98 sw.
cut(filter.data());
99 kw.
cut(filter.data());
110#ifdef DEBUG_RESAMPLER
112 <<
": inputSpacing " << inputSpacing <<
", outputSpacing "
189 for (
int phase = 0; phase < inputSpacing; ++phase) {
197 p.
drop = int(ceil(std::max(0.0,
double(outputSpacing - phase))
203 for (
int i = 0; i < filtZipLength; ++i) {
204 p.
filter.push_back(filter[i * inputSpacing + phase]);
210#ifdef DEBUG_RESAMPLER
213 for (
int i = 0; i < inputSpacing; ++i) {
220 cerr <<
"total drop = " << totDrop << endl;
274 m_phase = (h + n * outputSpacing) % inputSpacing;
276 int fill = (h + n * outputSpacing) / inputSpacing;
283#ifdef DEBUG_RESAMPLER
299 const double *
const __restrict__ filt = pd.
filter.data();
301 for (
int i = 0; i < n; ++i) {
303 v += buf[i] * filt[i];
314 for (
int i = 0; i < n; ++i) {
321#ifdef DEBUG_RESAMPLER
327 while (outidx < maxout &&
343 vector<double> out(maxout, 0.0);
344 int got =
process(src, out.data(), n);
345 assert(got <= maxout);
346 if (got < maxout) out.resize(got);
361 int inputPad = int(ceil((
double(latency) * sourceRate) / targetRate));
365 int n1 = n + inputPad;
369 int m1 = int(ceil((
double(n1) * targetRate) / sourceRate));
373 int m = int(ceil((
double(n) * targetRate) / sourceRate));
375#ifdef DEBUG_RESAMPLER
376 cerr <<
"n = " << n <<
", sourceRate = " << sourceRate <<
", targetRate = " << targetRate <<
", m = " << m <<
", latency = " << latency <<
", inputPad = " << inputPad <<
", m1 = " << m1 <<
", n1 = " << n1 <<
", n1 - n = " << n1 - n << endl;
379 vector<double> pad(n1 - n, 0.0);
380 vector<double> out(m1 + 1, 0.0);
382 int gotData = r.
process(data, out.data(), n);
383 int gotPad = r.
process(pad.data(), out.data() + gotData, pad.size());
384 int got = gotData + gotPad;
386#ifdef DEBUG_RESAMPLER
387 cerr <<
"resample: " << n <<
" in, " << pad.size() <<
" padding, " << got <<
" out (" << gotData <<
" data, " << gotPad <<
" padding, latency = " << latency <<
")" << endl;
389#ifdef DEBUG_RESAMPLER_VERBOSE
391 cerr <<
"first " << printN <<
" in:" << endl;
392 for (
int i = 0; i < printN && i < n; ++i) {
393 if (i % 5 == 0) cerr << endl << i <<
"... ";
394 cerr << data[i] <<
" ";
399 int toReturn = got - latency;
400 if (toReturn > m) toReturn = m;
402 vector<double> sliced(out.begin() + latency,
403 out.begin() + latency + toReturn);
405#ifdef DEBUG_RESAMPLER_VERBOSE
406 cerr <<
"first " << printN <<
" out (after latency compensation), length " << sliced.size() <<
":";
407 for (
int i = 0; i < printN && i < sliced.size(); ++i) {
408 if (i % 5 == 0) cerr << endl << i <<
"... ";
409 cerr << sliced[i] <<
" ";
static map< double, map< int, map< double, vector< double > > > > knownFilters
static Mutex knownFilterMutex
Kaiser window: A windower whose bandwidth and sidelobe height (signal-noise ratio) can be specified.
static Parameters parametersForBandwidth(double attenuation, double bandwidth, double samplerate)
Obtain the parameters necessary for a Kaiser window of the given attenuation in dB and transition ban...
void cut(double *src) const
static int gcd(int a, int b)
Return the greatest common divisor of natural numbers a and b.
Resampler resamples a stream from one integer sample rate to another (arbitrary) rate,...
std::vector< double > m_buffer
void initialise(double, double)
int getLatency() const
Return the number of samples of latency at the output due by the filter.
static std::vector< double > resample(int sourceRate, int targetRate, const double *data, int n)
Carry out a one-off resample of a single block of n samples.
Resampler(int sourceRate, int targetRate)
Construct a Resampler to resample from sourceRate to targetRate.
int process(const double *src, double *dst, int n)
Read n input samples from src and write resampled data to dst.
A window containing values of the sinc function, i.e.
void cut(double *src) const
std::vector< double > filter