View Javadoc

1   package org.lsst.ccs.utilities.dsp;
2   
3   import java.util.Arrays;
4   
5   /**
6    * 
7    * @author aubourg
8    * 
9    */
10  public class MedianFilter implements DigitalFilter {
11  
12  	/**
13  	 * 
14  	 */
15  	private static final long serialVersionUID = 4525960504716400209L;
16  
17  	public MedianFilter(int sz) {
18  		if (sz % 2 == 0) {
19  			sz++;
20  		}
21  		this.sz = sz;
22  		history = new double[sz];
23  		tmp = new double[sz];
24  	}
25  
26  	public void reset() {
27  		Arrays.fill(history, 0);
28  		ip = 0;
29  	}
30  
31  	public int getDelay() {
32  		return sz / 2;
33  	}
34  
35  	public double flush() {
36  		return apply(0);
37  	}
38  
39  	public double apply(double signal) {
40  		ip++;
41  		if (ip >= sz) {
42  			ip = 0;
43  		}
44  		history[ip] = signal;
45  		System.arraycopy(history, 0, tmp, 0, tmp.length);
46  		Arrays.sort(tmp);
47  		return tmp[sz / 2];
48  	}
49  
50  	public double freqResp(double freq) {
51  		return 0;
52  	}
53  
54  	public int getOrder() {
55  		return sz;
56  	}
57  
58  	int sz;
59  
60  	double[] history;
61  
62  	double[] tmp;
63  
64  	int ip;
65  
66  	@Override
67  	public Object clone() {
68  		try {
69  			MedianFilter f = (MedianFilter) super.clone();
70  			f.history = new double[sz];
71  			f.tmp = new double[sz];
72  			return f;
73  		} catch (CloneNotSupportedException e) {
74  			return null;
75  		}
76  	}
77  
78  }