1 package org.lsst.ccs.utilities.dsp;
2
3
4
5
6
7
8 public class FIRFilter implements DigitalFilter {
9
10
11
12 private static final long serialVersionUID = 2786444222861355300L;
13
14 double rate;
15
16 int order;
17
18 double[] history;
19
20 int ip;
21
22 double[] aCoeff;
23
24 protected FIRFilter(double[] coeff) {
25 aCoeff = coeff;
26 order = coeff.length - 1;
27 history = new double[order + 1];
28 ip = 0;
29 }
30
31 public void reset() {
32 for (int i = 0; i <= order; i++) {
33 history[i] = 0;
34 }
35 ip = 0;
36 }
37
38 public double getRate() {
39 return rate;
40 }
41
42 public int getDelay() {
43 return (order) / 2;
44 }
45
46 public double flush() {
47 return apply(0);
48 }
49
50 public double apply(double signal) {
51 ip++;
52 if (ip >= order) {
53 ip = 0;
54 }
55 history[ip] = signal;
56 double resp = 0;
57 for (int i = 0; i <= order; i++) {
58 int j = (ip - i) % (order + 1);
59 if (j < 0) {
60 j += order + 1;
61 }
62
63
64 resp += aCoeff[i] * history[j];
65 }
66 return resp;
67 }
68
69 public double freqResp(double freq) {
70
71 return 0;
72 }
73
74 public int getOrder() {
75 return order;
76 }
77
78 public void print() {
79 System.out.println("FIRFilter order " + order);
80 for (int i = 0; i <= order; i++) {
81 System.out.println(" a[" + i + "] = " + aCoeff[i]);
82 }
83 System.out.println("---");
84 }
85
86 @Override
87 public Object clone() {
88 try {
89 FIRFilter f = (FIRFilter) super.clone();
90 f.history = new double[order + 1];
91 return f;
92 } catch (CloneNotSupportedException e) {
93 return null;
94 }
95 }
96 }