annotate dsp/rateconversion/Resampler.h @ 381:88971211795c

Lower filter cutoff to below target Nyquist when downsampling
author Chris Cannam <c.cannam@qmul.ac.uk>
date Fri, 01 Nov 2013 12:07:08 +0000
parents ad21307eaf99
children 0a47ec0a1a56
rev   line source
c@362 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
c@375 2 /*
c@375 3 QM DSP Library
c@375 4
c@375 5 Centre for Digital Music, Queen Mary, University of London.
c@375 6 This file by Chris Cannam.
c@375 7
c@375 8 This program is free software; you can redistribute it and/or
c@375 9 modify it under the terms of the GNU General Public License as
c@375 10 published by the Free Software Foundation; either version 2 of the
c@375 11 License, or (at your option) any later version. See the file
c@375 12 COPYING included with this distribution for more information.
c@375 13 */
c@362 14
c@362 15 #ifndef RESAMPLER_H
c@362 16 #define RESAMPLER_H
c@362 17
c@362 18 #include <vector>
c@362 19
c@375 20 /**
c@375 21 * Resampler resamples a stream from one integer sample rate to
c@375 22 * another (arbitrary) rate, using a kaiser-windowed sinc filter. The
c@375 23 * results and performance are pretty similar to libraries such as
c@375 24 * libsamplerate, though this implementation does not support
c@375 25 * time-varying ratios (the ratio is fixed on construction).
c@375 26 *
c@375 27 * See also Decimator, which is faster and rougher but supports only
c@375 28 * power-of-two downsampling factors.
c@375 29 */
c@362 30 class Resampler
c@362 31 {
c@362 32 public:
c@362 33 /**
c@362 34 * Construct a Resampler to resample from sourceRate to
c@362 35 * targetRate.
c@362 36 */
c@362 37 Resampler(int sourceRate, int targetRate);
c@374 38
c@374 39 /**
c@374 40 * Construct a Resampler to resample from sourceRate to
c@374 41 * targetRate, using the given filter parameters.
c@374 42 */
c@374 43 Resampler(int sourceRate, int targetRate,
c@374 44 double snr, double bandwidth);
c@374 45
c@362 46 virtual ~Resampler();
c@362 47
c@362 48 /**
c@362 49 * Read n input samples from src and write resampled data to
c@362 50 * dst. The return value is the number of samples written, which
c@362 51 * will be no more than ceil((n * targetRate) / sourceRate). The
c@362 52 * caller must ensure the dst buffer has enough space for the
c@362 53 * samples returned.
c@362 54 */
c@362 55 int process(const double *src, double *dst, int n);
c@362 56
c@362 57 /**
c@362 58 * Return the number of samples of latency at the output due by
c@362 59 * the filter. (That is, the output will be delayed by this number
c@362 60 * of samples relative to the input.)
c@362 61 */
c@362 62 int getLatency() const { return m_latency; }
c@362 63
c@363 64 /**
c@363 65 * Carry out a one-off resample of a single block of n
c@363 66 * samples. The output is latency-compensated.
c@363 67 */
c@363 68 static std::vector<double> resample
c@363 69 (int sourceRate, int targetRate, const double *data, int n);
c@363 70
c@362 71 private:
c@362 72 int m_sourceRate;
c@362 73 int m_targetRate;
c@362 74 int m_gcd;
c@362 75 int m_filterLength;
c@362 76 int m_bufferLength;
c@362 77 int m_latency;
c@381 78 double m_peakToPole;
c@362 79
c@362 80 struct Phase {
c@362 81 int nextPhase;
c@362 82 std::vector<double> filter;
c@362 83 int drop;
c@362 84 };
c@362 85
c@362 86 Phase *m_phaseData;
c@362 87 int m_phase;
c@364 88 std::vector<double> m_buffer;
c@370 89 int m_bufferOrigin;
c@362 90
c@374 91 void initialise(double, double);
c@366 92 double reconstructOne();
c@362 93 };
c@362 94
c@362 95 #endif
c@362 96