Sound Byte Libs 0.5.1-121-g3358a44
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
dc_blocker.hpp
Go to the documentation of this file.
1// sbl/dsp/dc_blocker.hpp — DC offset removal filter
2//
3// First-order high-pass IIR that rejects DC while passing audio.
4// Essential for feedback paths (reverb, delay, waveshaping) where
5// DC offset accumulates and eats headroom.
6//
7// Transfer function: H(z) = (1 - z^-1) / (1 - R * z^-1)
8// Default R = 0.995 gives ~16 Hz cutoff at 48 kHz (-3 dB).
9//
10// Usage:
11// sbl::dsp::DcBlocker dc;
12// for (auto& s : buffer) s = dc.process(s);
13
14#ifndef SBL_DSP_PRIMITIVES_DC_BLOCKER_HPP_
15#define SBL_DSP_PRIMITIVES_DC_BLOCKER_HPP_
16
17#include <cstdint>
18
19namespace sbl::dsp::primitives {
20
21class DcBlocker {
22public:
23 /// @note All public methods are ISR-safe — bounded computation, no I/O.
24
25 /**
26 * @brief Process one sample through the DC blocker
27 * @param x Input sample
28 * @return Output with DC removed
29 */
30 float process(float x) {
31 float y = x - x1_ + r_ * y1_;
32 x1_ = x;
33 y1_ = y;
34 return y;
35 }
36
37 /**
38 * @brief Process a block of float samples in-place
39 * @param buf Sample buffer
40 * @param frames Number of samples
41 */
42 void process(float* buf, uint16_t frames) {
43 for (uint16_t i = 0; i < frames; ++i) {
44 buf[i] = process(buf[i]);
45 }
46 }
47
48 /**
49 * @brief Set the pole coefficient
50 *
51 * Higher values (closer to 1.0) = lower cutoff frequency.
52 * Default 0.995 ≈ 16 Hz at 48 kHz sample rate.
53 *
54 * @param r Pole coefficient, typically 0.99-0.999
55 */
56 void set_coefficient(float r) { r_ = r; }
57
58 /** @brief Reset filter state to zero */
59 void reset() { x1_ = 0.0f; y1_ = 0.0f; }
60
61 static constexpr float DEFAULT_POLE = 0.995f; // ~16 Hz @ 48 kHz
62
63private:
64 float r_ = DEFAULT_POLE;
65 float x1_ = 0.0f;
66 float y1_ = 0.0f;
67};
68
69} // namespace sbl::dsp::primitives
70
71#endif // SBL_DSP_PRIMITIVES_DC_BLOCKER_HPP_
float process(float x)
Process one sample through the DC blocker.
void process(float *buf, uint16_t frames)
Process a block of float samples in-place.
static constexpr float DEFAULT_POLE
void set_coefficient(float r)
Set the pole coefficient.
void reset()
Reset filter state to zero.
Stateful, single-concern building blocks.