Skip to content

Commit 4e4f368

Browse files
authored
Add IIR Filter (TheAlgorithms#2704)
Signed-off-by: Martmists <martmists@gmail.com>
1 parent 4969f9f commit 4e4f368

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

AudioFilters/IIRFilter.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package AudioFilters;
2+
3+
/**
4+
* N-Order IIR Filter
5+
* Assumes inputs are normalized to [-1, 1]
6+
*
7+
* Based on the difference equation from https://en.wikipedia.org/wiki/Infinite_impulse_response
8+
*/
9+
public class IIRFilter {
10+
private final int order;
11+
private final double[] coeffsA;
12+
private final double[] coeffsB;
13+
private final double[] historyX;
14+
private final double[] historyY;
15+
16+
/**
17+
* Construct an IIR Filter
18+
*
19+
* @param order the filter's order
20+
* @throws IllegalArgumentException if order is zero or less
21+
*/
22+
public IIRFilter(int order) throws IllegalArgumentException {
23+
if (order < 1) {
24+
throw new IllegalArgumentException("order must be greater than zero");
25+
}
26+
27+
this.order = order;
28+
coeffsA = new double[order+1];
29+
coeffsB = new double[order+1];
30+
31+
// Sane defaults
32+
coeffsA[0] = 1.0;
33+
coeffsB[0] = 1.0;
34+
35+
historyX = new double[order];
36+
historyY = new double[order];
37+
}
38+
39+
/**
40+
* Set coefficients
41+
* @param aCoeffs Denominator coefficients
42+
* @param bCoeffs Numerator coefficients
43+
* @throws IllegalArgumentException if {@code aCoeffs} or {@code bCoeffs} is not of size {@code order},
44+
* or if {@code aCoeffs[0]} is 0.0
45+
*/
46+
public void setCoeffs(double[] aCoeffs, double[] bCoeffs) throws IllegalArgumentException {
47+
if (aCoeffs.length != order) {
48+
throw new IllegalArgumentException("aCoeffs must be of size " + order + ", got " + aCoeffs.length);
49+
}
50+
51+
if (aCoeffs[0] == 0.0) {
52+
throw new IllegalArgumentException("aCoeffs.get(0) must not be zero");
53+
}
54+
55+
if (bCoeffs.length != order) {
56+
throw new IllegalArgumentException("bCoeffs must be of size " + order + ", got " + bCoeffs.length);
57+
}
58+
59+
for (int i = 0; i <= order; i++) {
60+
coeffsA[i] = aCoeffs[i];
61+
coeffsB[i] = bCoeffs[i];
62+
}
63+
}
64+
65+
/**
66+
* Process a single sample
67+
*
68+
* @param sample the sample to process
69+
* @return the processed sample
70+
*/
71+
public double process(double sample) {
72+
double result = 0.0;
73+
74+
// Process
75+
for (int i = 1; i <= order; i++) {
76+
result += (coeffsB[i] * historyX[i-1] - coeffsA[i] * historyY[i-1]);
77+
}
78+
result = (result + coeffsB[0] * sample) / coeffsA[0];
79+
80+
// Feedback
81+
for (int i = order-1; i > 0; i--) {
82+
historyX[i] = historyX[i-1];
83+
historyY[i] = historyY[i-1];
84+
}
85+
86+
historyX[0] = sample;
87+
historyY[0] = result;
88+
89+
return result;
90+
}
91+
}

DIRECTORY.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
## Audio Filters
3+
* [IIRFilter](https://github.com/TheAlgorithms/Java/blob/master/AudioFilters/IIRFilter.java)
4+
25
## Backtracking
36
* [NQueens](https://github.com/TheAlgorithms/Java/blob/master/Backtracking/NQueens.java)
47
* [PowerSum](https://github.com/TheAlgorithms/Java/blob/master/Backtracking/PowerSum.java)

0 commit comments

Comments
 (0)