Sound Byte Libs 29c5ff3
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#pragma once
15
16#include <cstdint>
17
18namespace sbl::dsp {
19
20class DcBlocker {
21public:
22 /// @note All public methods are ISR-safe — bounded computation, no I/O.
23
24 /**
25 * @brief Process one sample through the DC blocker
26 * @param x Input sample
27 * @return Output with DC removed
28 */
29 float process(float x) {
30 float y = x - x1_ + r_ * y1_;
31 x1_ = x;
32 y1_ = y;
33 return y;
34 }
35
36 /**
37 * @brief Process a block of float samples in-place
38 * @param buf Sample buffer
39 * @param frames Number of samples
40 */
41 void process(float* buf, uint16_t frames) {
42 for (uint16_t i = 0; i < frames; ++i) {
43 buf[i] = process(buf[i]);
44 }
45 }
46
47 /**
48 * @brief Set the pole coefficient
49 *
50 * Higher values (closer to 1.0) = lower cutoff frequency.
51 * Default 0.995 ≈ 16 Hz at 48 kHz sample rate.
52 *
53 * @param r Pole coefficient, typically 0.99-0.999
54 */
55 void set_coefficient(float r) { r_ = r; }
56
57 /** @brief Reset filter state to zero */
58 void reset() { x1_ = 0.0f; y1_ = 0.0f; }
59
60private:
61 float r_ = 0.995f;
62 float x1_ = 0.0f;
63 float y1_ = 0.0f;
64};
65
66} // namespace sbl::dsp
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.
void set_coefficient(float r)
Set the pole coefficient.
void reset()
Reset filter state to zero.
DSP atoms for audio signal processing.
Definition allpass.hpp:22