Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
sinc_upsampler.hpp
Go to the documentation of this file.
1/**
2 * @file sinc_upsampler.hpp
3 * @brief A frame-rate signal interpolated at FACTOR steps through each frame, LATENCY frames late
4 */
5
6#ifndef SBL_DSP_RESAMPLING_SINC_UPSAMPLER_HPP_
7#define SBL_DSP_RESAMPLING_SINC_UPSAMPLER_HPP_
8
9#include <cstdint>
11
12namespace sbl::dsp {
13
14/**
15 * @brief A frame-rate signal interpolated at FACTOR steps through each frame, LATENCY frames late
16 *
17 * Each frame takes one sample and gives FACTOR values: the signal at the
18 * ends of the FACTOR equal parts of the frame LATENCY frames back. A sinc
19 * under a Kaiser window of beta 8 over TAPS samples interpolates between
20 * samples; the last value is the sample LATENCY frames back itself. Each set
21 * of taps sums to 1, so a constant passes. On a sine at 48 kHz the error of
22 * the values is under -75 dB of the sine's amplitude up to 15 kHz, -42 dB at
23 * 17.6 kHz and -18 dB at 20 kHz. PmPulse follows its PM signal within a
24 * frame with it.
25 *
26 * Below the signal transport: a sample in, values out, no units. The taps
27 * are generated by scripts/generate_sinc_upsampler.py into
28 * sinc_upsampler_coefficients.hpp. Output does not depend on block size: it
29 * runs a frame at a time.
30 */
32public:
33 static constexpr uint8_t FACTOR = sinc_upsampler::FACTOR; /**< values given per frame */
34 static constexpr uint8_t TAPS = sinc_upsampler::TAPS; /**< samples each value is interpolated from */
35 static constexpr uint8_t LATENCY = TAPS / 2 - 1; /**< frames between a sample and its value */
36
37 /**
38 * @brief One frame: push a sample and interpolate the frame LATENCY frames back.
39 *
40 * @param sample This frame's sample
41 * @param[out] out out[k] is the signal (k + 1) / FACTOR through the frame LATENCY frames back
42 * @context Audio thread; real-time safe.
43 */
44 void process(float sample, float (&out)[FACTOR]) {
45 history_[next_] = sample;
46 history_[next_ + TAPS] = sample;
47 next_ = static_cast<uint8_t>((next_ + 1) % TAPS);
48 const float* h = &history_[next_]; // h[0] is the oldest sample, h[TAPS - 1] this one
49 // The three rows' sums advance together in three variables, so their multiply-adds overlap (see Sinc16).
50 static_assert(FACTOR == 4, "SBL ERROR: SincUpsampler::process sums three rows of taps");
51 float v0 = 0.0f;
52 float v1 = 0.0f;
53 float v2 = 0.0f;
54 for (uint8_t j = 0; j < TAPS; ++j) {
55 const float x = h[j];
56 v0 += x * sinc_upsampler::TAP[0][j];
57 v1 += x * sinc_upsampler::TAP[1][j];
58 v2 += x * sinc_upsampler::TAP[2][j];
59 }
60 out[0] = v0;
61 out[1] = v1;
62 out[2] = v2;
63 out[FACTOR - 1] = h[TAPS - 1 - LATENCY];
64 }
65
66 /** @brief History to zero. */
67 void reset() {
68 for (float& h : history_) {
69 h = 0.0f;
70 }
71 next_ = 0;
72 }
73
74private:
75 float history_[2 * TAPS] = {}; /**< each sample twice, so the last TAPS are contiguous from next_ */
76 uint8_t next_ = 0;
77};
78
79} // namespace sbl::dsp
80
81#endif // SBL_DSP_RESAMPLING_SINC_UPSAMPLER_HPP_
A frame-rate signal interpolated at FACTOR steps through each frame, LATENCY frames late.
void process(float sample, float(&out)[FACTOR])
One frame: push a sample and interpolate the frame LATENCY frames back.
static constexpr uint8_t TAPS
void reset()
History to zero.
static constexpr uint8_t FACTOR
static constexpr uint8_t LATENCY
The audio stack.
Definition dry_wet.hpp:15
Generated by scripts/generate_sinc_upsampler.py; do not edit.