annotate src/dsp/Resampler.cpp @ 162:7c444fea4338

Added tag v1.0 for changeset 361b4f8b7b2d
author Chris Cannam <c.cannam@qmul.ac.uk>
date Thu, 07 Aug 2014 19:19:21 +0100
parents edbec47f4a3d
children 1081c73fbbe3
rev   line source
c@119 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@119 2 /*
c@119 3 Constant-Q library
c@119 4 Copyright (c) 2013-2014 Queen Mary, University of London
c@119 5
c@119 6 Permission is hereby granted, free of charge, to any person
c@119 7 obtaining a copy of this software and associated documentation
c@119 8 files (the "Software"), to deal in the Software without
c@119 9 restriction, including without limitation the rights to use, copy,
c@119 10 modify, merge, publish, distribute, sublicense, and/or sell copies
c@119 11 of the Software, and to permit persons to whom the Software is
c@119 12 furnished to do so, subject to the following conditions:
c@119 13
c@119 14 The above copyright notice and this permission notice shall be
c@119 15 included in all copies or substantial portions of the Software.
c@119 16
c@119 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
c@119 18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
c@119 19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
c@119 20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
c@119 21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
c@119 22 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
c@119 23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
c@119 24
c@119 25 Except as contained in this notice, the names of the Centre for
c@119 26 Digital Music; Queen Mary, University of London; and Chris Cannam
c@119 27 shall not be used in advertising or otherwise to promote the sale,
c@119 28 use or other dealings in this Software without prior written
c@119 29 authorization.
c@119 30 */
c@119 31
c@119 32 #include "Resampler.h"
c@119 33
c@122 34 #include "MathUtilities.h"
c@122 35 #include "KaiserWindow.h"
c@122 36 #include "SincWindow.h"
c@119 37
c@119 38 #include <iostream>
c@119 39 #include <vector>
c@119 40 #include <map>
c@119 41 #include <cassert>
c@119 42
c@122 43 #include <pthread.h>
c@122 44
c@119 45 using std::vector;
c@119 46 using std::map;
c@119 47 using std::cerr;
c@119 48 using std::endl;
c@119 49
c@119 50 //#define DEBUG_RESAMPLER 1
c@119 51 //#define DEBUG_RESAMPLER_VERBOSE 1
c@119 52
c@119 53 Resampler::Resampler(int sourceRate, int targetRate) :
c@119 54 m_sourceRate(sourceRate),
c@119 55 m_targetRate(targetRate)
c@119 56 {
c@119 57 initialise(100, 0.02);
c@119 58 }
c@119 59
c@119 60 Resampler::Resampler(int sourceRate, int targetRate,
c@119 61 double snr, double bandwidth) :
c@119 62 m_sourceRate(sourceRate),
c@119 63 m_targetRate(targetRate)
c@119 64 {
c@119 65 initialise(snr, bandwidth);
c@119 66 }
c@119 67
c@119 68 Resampler::~Resampler()
c@119 69 {
c@119 70 delete[] m_phaseData;
c@119 71 }
c@119 72
c@119 73 // peakToPole -> length -> beta -> window
c@119 74 static map<double, map<int, map<double, vector<double> > > >
c@119 75 knownFilters;
c@119 76
c@122 77 static pthread_mutex_t
c@122 78 knownFilterMutex = PTHREAD_MUTEX_INITIALIZER;
c@119 79
c@119 80 void
c@119 81 Resampler::initialise(double snr, double bandwidth)
c@119 82 {
c@119 83 int higher = std::max(m_sourceRate, m_targetRate);
c@119 84 int lower = std::min(m_sourceRate, m_targetRate);
c@119 85
c@119 86 m_gcd = MathUtilities::gcd(lower, higher);
c@119 87 m_peakToPole = higher / m_gcd;
c@119 88
c@119 89 if (m_targetRate < m_sourceRate) {
c@119 90 // antialiasing filter, should be slightly below nyquist
c@119 91 m_peakToPole = m_peakToPole / (1.0 - bandwidth/2.0);
c@119 92 }
c@119 93
c@119 94 KaiserWindow::Parameters params =
c@119 95 KaiserWindow::parametersForBandwidth(snr, bandwidth, higher / m_gcd);
c@119 96
c@119 97 params.length =
c@119 98 (params.length % 2 == 0 ? params.length + 1 : params.length);
c@119 99
c@119 100 params.length =
c@119 101 (params.length > 200001 ? 200001 : params.length);
c@119 102
c@119 103 m_filterLength = params.length;
c@119 104
c@119 105 vector<double> filter;
c@122 106
c@122 107 pthread_mutex_lock(&knownFilterMutex);
c@119 108
c@119 109 if (knownFilters[m_peakToPole][m_filterLength].find(params.beta) ==
c@119 110 knownFilters[m_peakToPole][m_filterLength].end()) {
c@119 111
c@119 112 KaiserWindow kw(params);
c@119 113 SincWindow sw(m_filterLength, m_peakToPole * 2);
c@119 114
c@119 115 filter = vector<double>(m_filterLength, 0.0);
c@119 116 for (int i = 0; i < m_filterLength; ++i) filter[i] = 1.0;
c@119 117 sw.cut(filter.data());
c@119 118 kw.cut(filter.data());
c@119 119
c@119 120 knownFilters[m_peakToPole][m_filterLength][params.beta] = filter;
c@119 121 }
c@119 122
c@119 123 filter = knownFilters[m_peakToPole][m_filterLength][params.beta];
c@122 124
c@122 125 pthread_mutex_unlock(&knownFilterMutex);
c@119 126
c@119 127 int inputSpacing = m_targetRate / m_gcd;
c@119 128 int outputSpacing = m_sourceRate / m_gcd;
c@119 129
c@119 130 #ifdef DEBUG_RESAMPLER
c@119 131 cerr << "resample " << m_sourceRate << " -> " << m_targetRate
c@119 132 << ": inputSpacing " << inputSpacing << ", outputSpacing "
c@119 133 << outputSpacing << ": filter length " << m_filterLength
c@119 134 << endl;
c@119 135 #endif
c@119 136
c@119 137 // Now we have a filter of (odd) length flen in which the lower
c@119 138 // sample rate corresponds to every n'th point and the higher rate
c@119 139 // to every m'th where n and m are higher and lower rates divided
c@119 140 // by their gcd respectively. So if x coordinates are on the same
c@119 141 // scale as our filter resolution, then source sample i is at i *
c@119 142 // (targetRate / gcd) and target sample j is at j * (sourceRate /
c@119 143 // gcd).
c@119 144
c@119 145 // To reconstruct a single target sample, we want a buffer (real
c@119 146 // or virtual) of flen values formed of source samples spaced at
c@119 147 // intervals of (targetRate / gcd), in our example case 3. This
c@119 148 // is initially formed with the first sample at the filter peak.
c@119 149 //
c@119 150 // 0 0 0 0 a 0 0 b 0
c@119 151 //
c@119 152 // and of course we have our filter
c@119 153 //
c@119 154 // f1 f2 f3 f4 f5 f6 f7 f8 f9
c@119 155 //
c@119 156 // We take the sum of products of non-zero values from this buffer
c@119 157 // with corresponding values in the filter
c@119 158 //
c@119 159 // a * f5 + b * f8
c@119 160 //
c@119 161 // Then we drop (sourceRate / gcd) values, in our example case 4,
c@119 162 // from the start of the buffer and fill until it has flen values
c@119 163 // again
c@119 164 //
c@119 165 // a 0 0 b 0 0 c 0 0
c@119 166 //
c@119 167 // repeat to reconstruct the next target sample
c@119 168 //
c@119 169 // a * f1 + b * f4 + c * f7
c@119 170 //
c@119 171 // and so on.
c@119 172 //
c@119 173 // Above I said the buffer could be "real or virtual" -- ours is
c@119 174 // virtual. We don't actually store all the zero spacing values,
c@119 175 // except for padding at the start; normally we store only the
c@119 176 // values that actually came from the source stream, along with a
c@119 177 // phase value that tells us how many virtual zeroes there are at
c@119 178 // the start of the virtual buffer. So the two examples above are
c@119 179 //
c@119 180 // 0 a b [ with phase 1 ]
c@119 181 // a b c [ with phase 0 ]
c@119 182 //
c@119 183 // Having thus broken down the buffer so that only the elements we
c@119 184 // need to multiply are present, we can also unzip the filter into
c@119 185 // every-nth-element subsets at each phase, allowing us to do the
c@119 186 // filter multiplication as a simply vector multiply. That is, rather
c@119 187 // than store
c@119 188 //
c@119 189 // f1 f2 f3 f4 f5 f6 f7 f8 f9
c@119 190 //
c@119 191 // we store separately
c@119 192 //
c@119 193 // f1 f4 f7
c@119 194 // f2 f5 f8
c@119 195 // f3 f6 f9
c@119 196 //
c@119 197 // Each time we complete a multiply-and-sum, we need to work out
c@119 198 // how many (real) samples to drop from the start of our buffer,
c@119 199 // and how many to add at the end of it for the next multiply. We
c@119 200 // know we want to drop enough real samples to move along by one
c@119 201 // computed output sample, which is our outputSpacing number of
c@119 202 // virtual buffer samples. Depending on the relationship between
c@119 203 // input and output spacings, this may mean dropping several real
c@119 204 // samples, one real sample, or none at all (and simply moving to
c@119 205 // a different "phase").
c@119 206
c@119 207 m_phaseData = new Phase[inputSpacing];
c@119 208
c@119 209 for (int phase = 0; phase < inputSpacing; ++phase) {
c@119 210
c@119 211 Phase p;
c@119 212
c@119 213 p.nextPhase = phase - outputSpacing;
c@119 214 while (p.nextPhase < 0) p.nextPhase += inputSpacing;
c@119 215 p.nextPhase %= inputSpacing;
c@119 216
c@119 217 p.drop = int(ceil(std::max(0.0, double(outputSpacing - phase))
c@119 218 / inputSpacing));
c@119 219
c@119 220 int filtZipLength = int(ceil(double(m_filterLength - phase)
c@119 221 / inputSpacing));
c@119 222
c@119 223 for (int i = 0; i < filtZipLength; ++i) {
c@119 224 p.filter.push_back(filter[i * inputSpacing + phase]);
c@119 225 }
c@119 226
c@119 227 m_phaseData[phase] = p;
c@119 228 }
c@119 229
c@119 230 #ifdef DEBUG_RESAMPLER
c@119 231 int cp = 0;
c@119 232 int totDrop = 0;
c@119 233 for (int i = 0; i < inputSpacing; ++i) {
c@119 234 cerr << "phase = " << cp << ", drop = " << m_phaseData[cp].drop
c@119 235 << ", filter length = " << m_phaseData[cp].filter.size()
c@119 236 << ", next phase = " << m_phaseData[cp].nextPhase << endl;
c@119 237 totDrop += m_phaseData[cp].drop;
c@119 238 cp = m_phaseData[cp].nextPhase;
c@119 239 }
c@119 240 cerr << "total drop = " << totDrop << endl;
c@119 241 #endif
c@119 242
c@119 243 // The May implementation of this uses a pull model -- we ask the
c@119 244 // resampler for a certain number of output samples, and it asks
c@119 245 // its source stream for as many as it needs to calculate
c@119 246 // those. This means (among other things) that the source stream
c@119 247 // can be asked for enough samples up-front to fill the buffer
c@119 248 // before the first output sample is generated.
c@119 249 //
c@119 250 // In this implementation we're using a push model in which a
c@119 251 // certain number of source samples is provided and we're asked
c@119 252 // for as many output samples as that makes available. But we
c@119 253 // can't return any samples from the beginning until half the
c@119 254 // filter length has been provided as input. This means we must
c@119 255 // either return a very variable number of samples (none at all
c@119 256 // until the filter fills, then half the filter length at once) or
c@119 257 // else have a lengthy declared latency on the output. We do the
c@119 258 // latter. (What do other implementations do?)
c@119 259 //
c@119 260 // We want to make sure the first "real" sample will eventually be
c@119 261 // aligned with the centre sample in the filter (it's tidier, and
c@119 262 // easier to do diagnostic calculations that way). So we need to
c@119 263 // pick the initial phase and buffer fill accordingly.
c@119 264 //
c@119 265 // Example: if the inputSpacing is 2, outputSpacing is 3, and
c@119 266 // filter length is 7,
c@119 267 //
c@119 268 // x x x x a b c ... input samples
c@119 269 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
c@119 270 // i j k l ... output samples
c@119 271 // [--------|--------] <- filter with centre mark
c@119 272 //
c@119 273 // Let h be the index of the centre mark, here 3 (generally
c@119 274 // int(filterLength/2) for odd-length filters).
c@119 275 //
c@119 276 // The smallest n such that h + n * outputSpacing > filterLength
c@119 277 // is 2 (that is, ceil((filterLength - h) / outputSpacing)), and
c@119 278 // (h + 2 * outputSpacing) % inputSpacing == 1, so the initial
c@119 279 // phase is 1.
c@119 280 //
c@119 281 // To achieve our n, we need to pre-fill the "virtual" buffer with
c@119 282 // 4 zero samples: the x's above. This is int((h + n *
c@119 283 // outputSpacing) / inputSpacing). It's the phase that makes this
c@119 284 // buffer get dealt with in such a way as to give us an effective
c@119 285 // index for sample a of 9 rather than 8 or 10 or whatever.
c@119 286 //
c@119 287 // This gives us output latency of 2 (== n), i.e. output samples i
c@119 288 // and j will appear before the one in which input sample a is at
c@119 289 // the centre of the filter.
c@119 290
c@119 291 int h = int(m_filterLength / 2);
c@119 292 int n = ceil(double(m_filterLength - h) / outputSpacing);
c@119 293
c@119 294 m_phase = (h + n * outputSpacing) % inputSpacing;
c@119 295
c@119 296 int fill = (h + n * outputSpacing) / inputSpacing;
c@119 297
c@119 298 m_latency = n;
c@119 299
c@119 300 m_buffer = vector<double>(fill, 0);
c@119 301 m_bufferOrigin = 0;
c@119 302
c@119 303 #ifdef DEBUG_RESAMPLER
c@119 304 cerr << "initial phase " << m_phase << " (as " << (m_filterLength/2) << " % " << inputSpacing << ")"
c@119 305 << ", latency " << m_latency << endl;
c@119 306 #endif
c@119 307 }
c@119 308
c@119 309 double
c@119 310 Resampler::reconstructOne()
c@119 311 {
c@119 312 Phase &pd = m_phaseData[m_phase];
c@119 313 double v = 0.0;
c@119 314 int n = pd.filter.size();
c@119 315
c@119 316 assert(n + m_bufferOrigin <= (int)m_buffer.size());
c@119 317
c@119 318 const double *const __restrict__ buf = m_buffer.data() + m_bufferOrigin;
c@119 319 const double *const __restrict__ filt = pd.filter.data();
c@119 320
c@119 321 for (int i = 0; i < n; ++i) {
c@119 322 // NB gcc can only vectorize this with -ffast-math
c@119 323 v += buf[i] * filt[i];
c@119 324 }
c@119 325
c@119 326 m_bufferOrigin += pd.drop;
c@119 327 m_phase = pd.nextPhase;
c@119 328 return v;
c@119 329 }
c@119 330
c@119 331 int
c@119 332 Resampler::process(const double *src, double *dst, int n)
c@119 333 {
c@119 334 for (int i = 0; i < n; ++i) {
c@119 335 m_buffer.push_back(src[i]);
c@119 336 }
c@119 337
c@119 338 int maxout = int(ceil(double(n) * m_targetRate / m_sourceRate));
c@119 339 int outidx = 0;
c@119 340
c@119 341 #ifdef DEBUG_RESAMPLER
c@119 342 cerr << "process: buf siz " << m_buffer.size() << " filt siz for phase " << m_phase << " " << m_phaseData[m_phase].filter.size() << endl;
c@119 343 #endif
c@119 344
c@119 345 double scaleFactor = (double(m_targetRate) / m_gcd) / m_peakToPole;
c@119 346
c@119 347 while (outidx < maxout &&
c@119 348 m_buffer.size() >= m_phaseData[m_phase].filter.size() + m_bufferOrigin) {
c@119 349 dst[outidx] = scaleFactor * reconstructOne();
c@119 350 outidx++;
c@119 351 }
c@119 352
c@119 353 m_buffer = vector<double>(m_buffer.begin() + m_bufferOrigin, m_buffer.end());
c@119 354 m_bufferOrigin = 0;
c@119 355
c@119 356 return outidx;
c@119 357 }
c@119 358
c@119 359 vector<double>
c@119 360 Resampler::process(const double *src, int n)
c@119 361 {
c@119 362 int maxout = int(ceil(double(n) * m_targetRate / m_sourceRate));
c@119 363 vector<double> out(maxout, 0.0);
c@119 364 int got = process(src, out.data(), n);
c@119 365 assert(got <= maxout);
c@119 366 if (got < maxout) out.resize(got);
c@119 367 return out;
c@119 368 }
c@119 369
c@119 370 vector<double>
c@119 371 Resampler::resample(int sourceRate, int targetRate, const double *data, int n)
c@119 372 {
c@119 373 Resampler r(sourceRate, targetRate);
c@119 374
c@119 375 int latency = r.getLatency();
c@119 376
c@119 377 // latency is the output latency. We need to provide enough
c@119 378 // padding input samples at the end of input to guarantee at
c@119 379 // *least* the latency's worth of output samples. that is,
c@119 380
c@119 381 int inputPad = int(ceil((double(latency) * sourceRate) / targetRate));
c@119 382
c@119 383 // that means we are providing this much input in total:
c@119 384
c@119 385 int n1 = n + inputPad;
c@119 386
c@119 387 // and obtaining this much output in total:
c@119 388
c@119 389 int m1 = int(ceil((double(n1) * targetRate) / sourceRate));
c@119 390
c@119 391 // in order to return this much output to the user:
c@119 392
c@119 393 int m = int(ceil((double(n) * targetRate) / sourceRate));
c@119 394
c@119 395 #ifdef DEBUG_RESAMPLER
c@119 396 cerr << "n = " << n << ", sourceRate = " << sourceRate << ", targetRate = " << targetRate << ", m = " << m << ", latency = " << latency << ", inputPad = " << inputPad << ", m1 = " << m1 << ", n1 = " << n1 << ", n1 - n = " << n1 - n << endl;
c@119 397 #endif
c@119 398
c@119 399 vector<double> pad(n1 - n, 0.0);
c@119 400 vector<double> out(m1 + 1, 0.0);
c@119 401
c@119 402 int gotData = r.process(data, out.data(), n);
c@119 403 int gotPad = r.process(pad.data(), out.data() + gotData, pad.size());
c@119 404 int got = gotData + gotPad;
c@119 405
c@119 406 #ifdef DEBUG_RESAMPLER
c@119 407 cerr << "resample: " << n << " in, " << pad.size() << " padding, " << got << " out (" << gotData << " data, " << gotPad << " padding, latency = " << latency << ")" << endl;
c@119 408 #endif
c@119 409 #ifdef DEBUG_RESAMPLER_VERBOSE
c@119 410 int printN = 50;
c@119 411 cerr << "first " << printN << " in:" << endl;
c@119 412 for (int i = 0; i < printN && i < n; ++i) {
c@119 413 if (i % 5 == 0) cerr << endl << i << "... ";
c@119 414 cerr << data[i] << " ";
c@119 415 }
c@119 416 cerr << endl;
c@119 417 #endif
c@119 418
c@119 419 int toReturn = got - latency;
c@119 420 if (toReturn > m) toReturn = m;
c@119 421
c@119 422 vector<double> sliced(out.begin() + latency,
c@119 423 out.begin() + latency + toReturn);
c@119 424
c@119 425 #ifdef DEBUG_RESAMPLER_VERBOSE
c@119 426 cerr << "first " << printN << " out (after latency compensation), length " << sliced.size() << ":";
c@119 427 for (int i = 0; i < printN && i < sliced.size(); ++i) {
c@119 428 if (i % 5 == 0) cerr << endl << i << "... ";
c@119 429 cerr << sliced[i] << " ";
c@119 430 }
c@119 431 cerr << endl;
c@119 432 #endif
c@119 433
c@119 434 return sliced;
c@119 435 }
c@119 436