-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIIRFilterFirstOrder.cpp
More file actions
120 lines (83 loc) · 3.57 KB
/
IIRFilterFirstOrder.cpp
File metadata and controls
120 lines (83 loc) · 3.57 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//#include "../JuceLibraryCode/JuceHeader.h"
#include <math.h>
#include "IIRFilterFirstOrder.h"
#define SNAP_TO_ZERO(n) if (! (n < -1.0e-8f || n > 1.0e-8f)) n = 0;
IIRCoefficientsSinglePole::IIRCoefficientsSinglePole() noexcept
{
//zeromem (coefficients, sizeof (coefficients));
//I don't know why memset won't work
coefficients[0] = 0.f;
coefficients[1] = 0.f;
coefficients[2] = 0.f;
}
IIRCoefficientsSinglePole::~IIRCoefficientsSinglePole() noexcept {}
IIRCoefficientsSinglePole::IIRCoefficientsSinglePole (double c0, double c1, double c2, double c3) noexcept
{
auto a = 1.0 / c2;
coefficients[0] = (float) (c0 * a);
coefficients[1] = (float) (c1 * a);
coefficients[2] = (float) (c3 * a);
}
IIRCoefficientsSinglePole IIRCoefficientsSinglePole::makeLowPass (double sampleRate,
double frequency) noexcept
{
//return makeLowPass (sampleRate, frequency, 1.0 / MathConstants<double>::sqrt2);
return makeLowPass (sampleRate, frequency, 1.0 / sqrt(2.0));
}
IIRCoefficientsSinglePole IIRCoefficientsSinglePole::makeLowPass (double sampleRate,
double frequency,
double Q) noexcept
{
//jassert (sampleRate > 0.0);
//jassert (frequency > 0 && frequency <= static_cast<float> (sampleRate * 0.5));
//auto n = std::tan (MathConstants<double>::pi * frequency / sampleRate);
//ensure valid sample rate and freq and we're below nyquist
if (sampleRate <= 0.0 || frequency <= 0.0 || frequency > static_cast<float>(sampleRate * 0.5))
{
frequency = 140.0;
sampleRate = 48000;
}
auto n = tan(M_PI * frequency / sampleRate);
return IIRCoefficientsSinglePole(n, n, n + 1, n - 1);
}
IIRCoefficientsSinglePole IIRCoefficientsSinglePole::makeHighPass (double sampleRate,
double frequency) noexcept
{
return makeHighPass (sampleRate, frequency, 1.0 / sqrt(2.0));
}
IIRCoefficientsSinglePole IIRCoefficientsSinglePole::makeHighPass (double sampleRate,
double frequency,
double Q) noexcept
{
//jassert (sampleRate > 0.0);
//jassert (frequency > 0 && frequency <= static_cast<float> (sampleRate * 0.5));
//auto n = std::tan (MathConstants<double>::pi * frequency / sampleRate);
//ensure valid sample rate and freq and we're below nyquist
if (sampleRate <= 0.0 || frequency <= 0.0 || frequency > static_cast<float>(sampleRate * 0.5))
{
frequency = 140.0;
sampleRate = 48000;
}
auto n = tan(M_PI * frequency / sampleRate);
return IIRCoefficientsSinglePole(1, -1, n + 1, n - 1);
}
//==============================================================================
IIRFilterFirstOrder::IIRFilterFirstOrder() noexcept {}
IIRFilterFirstOrder::~IIRFilterFirstOrder() noexcept {}
void IIRFilterFirstOrder::setCoefficients (const IIRCoefficientsSinglePole& newCoefficients) noexcept
{
coefficients = newCoefficients;
}
//==============================================================================
void IIRFilterFirstOrder::reset() noexcept
{
v1 = 0.f;
}
float IIRFilterFirstOrder::processSingleSampleRaw (float in) noexcept
{
auto out = coefficients.coefficients[0] * in + v1;
//JUCE_SNAP_TO_ZERO (out);
SNAP_TO_ZERO(out);
v1 = coefficients.coefficients[1] * in - coefficients.coefficients[2] * out;
return out;
}