forked from higan-emu/higan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathone-pole.hpp
46 lines (36 loc) · 1.04 KB
/
one-pole.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma once
#include <nall/dsp/dsp.hpp>
//one-pole first-order IIR filter
namespace nall::DSP::IIR {
struct OnePole {
enum class Type : uint {
LowPass,
HighPass,
};
auto reset(Type type, double cutoffFrequency, double samplingFrequency) -> void;
auto process(double in) -> double; //normalized sample (-1.0 to +1.0)
private:
Type type;
double cutoffFrequency;
double samplingFrequency;
double a0, b1; //coefficients
double z1; //first-order IIR
};
inline auto OnePole::reset(Type type, double cutoffFrequency, double samplingFrequency) -> void {
this->type = type;
this->cutoffFrequency = cutoffFrequency;
this->samplingFrequency = samplingFrequency;
z1 = 0.0;
double x = cos(2.0 * Math::Pi * cutoffFrequency / samplingFrequency);
if(type == Type::LowPass) {
b1 = +2.0 - x - sqrt((+2.0 - x) * (+2.0 - x) - 1);
a0 = 1.0 - b1;
} else {
b1 = -2.0 - x + sqrt((-2.0 - x) * (-2.0 - x) - 1);
a0 = 1.0 + b1;
}
}
inline auto OnePole::process(double in) -> double {
return z1 = in * a0 + z1 * b1;
}
}