|
| 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 | +} |
0 commit comments