1 | #!/usr/bin/python
|
---|
2 | #
|
---|
3 | # Werner Lustermann
|
---|
4 | # ETH Zurich
|
---|
5 | #
|
---|
6 | import numpy as np
|
---|
7 |
|
---|
8 | import fir_filter as fir
|
---|
9 |
|
---|
10 | class DRSSpikes(object):
|
---|
11 | """ remove spikes (single or double false readings) from DRS4 data
|
---|
12 | Strategy:
|
---|
13 | * filter the data, removing the signal, thus spike(s) are clearly visible
|
---|
14 | * search single and double spikes
|
---|
15 | * replace the spike by a value derived from the neighbors
|
---|
16 |
|
---|
17 | """
|
---|
18 |
|
---|
19 | def __init__(self, threshold=7.,
|
---|
20 | single_pattern=np.array( [-0.5, 1.0, -0.5]) ,
|
---|
21 | double_pattern=np.array([-1., 1., 1., -1.]),
|
---|
22 | debug = False):
|
---|
23 | """ initialize spike filter
|
---|
24 | template_single: template of a single slice spike
|
---|
25 | template_double: template of a two slice spike
|
---|
26 |
|
---|
27 | """
|
---|
28 |
|
---|
29 | self.threshold = threshold
|
---|
30 | self.single_pattern = single_pattern * threshold
|
---|
31 | self.double_pattern = double_pattern * threshold
|
---|
32 |
|
---|
33 | self.remove_signal = fir.RemoveSignal()
|
---|
34 | self.debug = debug
|
---|
35 |
|
---|
36 | def __call__(self, data):
|
---|
37 |
|
---|
38 | self.row, self.col = data.shape
|
---|
39 | indicator = self.remove_signal(data)
|
---|
40 | a = indicator.flatten()
|
---|
41 | singles = []
|
---|
42 | doubles = []
|
---|
43 |
|
---|
44 | # a spike in the first or last channel is considered as a filter artefact
|
---|
45 | candidates = np.where(a[1:-2] > self.threshold)
|
---|
46 | # candidates = np.where(a[1:1022] > self.threshold)
|
---|
47 | cc = candidates[0]
|
---|
48 | #print 'cc: ', cc
|
---|
49 | #: find single spikes
|
---|
50 | p = self.single_pattern * np.sign( self.single_pattern )
|
---|
51 | for i, can in enumerate( zip(a[cc], a[cc+1], a[cc+2]) ):
|
---|
52 | #print 'can : p', can, p
|
---|
53 | can = can * np.sign(self.single_pattern)
|
---|
54 | if all(can > p):
|
---|
55 | singles.append(cc[i])
|
---|
56 |
|
---|
57 | #: find double spikes
|
---|
58 | p = self.double_pattern * np.sign( self.double_pattern )
|
---|
59 | for i, can in enumerate( zip(a[cc], a[cc+1], a[cc+2], a[cc+3]) ):
|
---|
60 | #print 'data: ', [data[0,cc[i]+k] for k in range(3)]
|
---|
61 | #print 'can : p', can, p
|
---|
62 | can = can * np.sign(self.double_pattern)
|
---|
63 | if all(can > p):
|
---|
64 | doubles.append(cc[i])
|
---|
65 |
|
---|
66 | if self.debug:
|
---|
67 | print 'singles: ', singles
|
---|
68 | print 'doubles: ', doubles
|
---|
69 |
|
---|
70 | data = self.remove_single_spikes(singles, data)
|
---|
71 | data = self.remove_double_spikes(doubles, data)
|
---|
72 | return data
|
---|
73 |
|
---|
74 | def remove_single_spikes(self, singles, data):
|
---|
75 | data = data.flatten()
|
---|
76 | for spike in singles:
|
---|
77 | data[spike] = (data[spike-1] + data[spike+1]) / 2.
|
---|
78 | return data.reshape(self.row, self.col)
|
---|
79 |
|
---|
80 | def remove_double_spikes(self, doubles, data):
|
---|
81 | data = data.flatten()
|
---|
82 | for spike in doubles:
|
---|
83 | data[spike:spike+2] = (data[spike-1] + data[spike+2]) / 2.
|
---|
84 | return data.reshape(self.row, self.col)
|
---|
85 |
|
---|
86 |
|
---|
87 | def _test():
|
---|
88 |
|
---|
89 | a = np.ones((3,12)) * 3.
|
---|
90 | a[0,3] = 7.
|
---|
91 | a[1,7] = 14.
|
---|
92 | a[1,8] = 14.
|
---|
93 | a[2,4] = 50.
|
---|
94 | a[2,5] = 45.
|
---|
95 | a[2,8] = 20.
|
---|
96 |
|
---|
97 | print a
|
---|
98 |
|
---|
99 | SpikeRemover = DRSSpikes(3., debug=True)
|
---|
100 | print 'single spike pattern ', SpikeRemover.single_pattern
|
---|
101 | print 'double spike pattern ', SpikeRemover.double_pattern
|
---|
102 | afilt = SpikeRemover(a)
|
---|
103 | print afilt
|
---|
104 |
|
---|
105 | if __name__ == '__main__':
|
---|
106 | """ test the class """
|
---|
107 | _test()
|
---|
108 |
|
---|
109 |
|
---|