Changeset 12981 for fact/tools/pyscripts
- Timestamp:
- 03/01/12 12:32:48 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fact/tools/pyscripts/pyfact/extractor.py
r12952 r12981 1 1 #!/usr/bin/python 2 2 # 3 3 # Dominik Neise, Werner Lustermann … … 6 6 import numpy as np 7 7 import matplotlib.pyplot as plt 8 #import * from fir_filter 8 from generator import * 9 from fir_filter import * 9 10 10 11 class GlobalMaxFinder(object): … … 23 24 24 25 def __call__(self, data): 25 time = np.argmax( data[ : , self.min:self.max ], 1) 26 amplitude = np.max( data[ : , self.min:self.max], 1) 27 return amplitude, time 26 if data.ndim > 1: 27 time = np.argmax( data[ : , self.min:self.max ], 1) 28 amplitude = np.max( data[ : , self.min:self.max], 1) 29 else: 30 time = np.argmax( data[self.min:self.max]) 31 amplitude = np.max( data[self.min:self.max]) 32 return amplitude, time+self.min 28 33 29 34 def __str__(self): 30 35 s = self.name + '\n' 31 36 s += 'window:\n' 32 s += '(min,max) = (' + str(self.min) + ',' + str(self.max) + ') \n'37 s += '(min,max) = (' + str(self.min) + ',' + str(self.max) + ')' 33 38 return s 39 40 def test(self): 41 pass 34 42 35 43 … … 49 57 for pixel in range( data.shape[0] ): 50 58 integral[pixel] = data[pixel, self.min:self.max].sum() 51 return integ tral59 return integral 52 60 53 61 def __str__(self): 54 62 s = self.name + '\n' 55 63 s += 'window:\n' 56 s += '(min,max) = (' + str(self.min) + ',' + str(self.max) + ') \n'64 s += '(min,max) = (' + str(self.min) + ',' + str(self.max) + ')' 57 65 return s 58 66 … … 74 82 for pix_data in data: 75 83 hits = [] 76 for i in range( data.shape[1] ):77 if ( s lope > 0 ):84 for i in range( data.shape[1]-1 ): 85 if ( self.slope > 0 ): 78 86 if ( pix_data[i] > 0 ): 79 87 continue … … 99 107 100 108 109 def plotter(signal, text): 110 x=range(len(signal)) 111 ax = plt.plot(x, signal, 'b.', label='signal') 112 plt.title(text) 113 plt.xlabel('sample') 114 plt.legend() 115 plt.grid(True) 116 plt.show() 117 118 def histplotter(signal, text): 119 plt.xlabel('time of zero crossing') 120 plt.title(text) 121 plt.axis([0, 300,0,1440]) 122 plt.grid(True) 123 n, bins, patches = plt.hist(signal, 3000, facecolor='r', alpha=0.75) 124 plt.show() 125 101 126 102 127 if __name__ == '__main__': 103 128 """ test the extractors """ 129 sg = SignalGenerator() 130 pulse_str = 'len 300 bsl -0.5 noise 0.5 triangle 10 65 8 100' 131 pulse = sg(pulse_str) 132 event = [] 133 for i in range(1440): 134 event.append(sg(pulse_str)) 135 event = np.array(event) 136 print 'test event with 1000 pixel generated, like this:' 137 print pulse_str 138 print 104 139 105 gmf = GlobalMaxFinder((12,40)) 140 # Test GlobalMaxFinder 141 gmf = GlobalMaxFinder(30,250) 106 142 print gmf 107 fwi = FixedWindowIntegrator(1,3) 143 amplitude, time = gmf(event) 144 if abs(amplitude.mean() - 10) < 0.5: 145 print "Test 1: OK GlobalMaxFinder found amplitude correctly", amplitude.mean() 146 if abs(time.mean() - 65) < 2: 147 print "Test 1: OK GlobalMaxFinder found time correctly", time.mean() 148 else: 149 print "BAD: time mean:", time.mean() 150 151 print 152 153 # Test FixedWindowIntegrator 154 fwi = FixedWindowIntegrator(50,200) 108 155 print fwi 156 integral = fwi(event) 157 #value of integral should be: 150*bsl + 8*10/2 + 100*10/2 = 465 158 if abs( integral.mean() - 465) < 2: 159 print "Test 2: OK FixedWindowIntegrator found integral correctly", integral.mean() 160 else: 161 print "Test 2: X FixedWindowIntegrator integral.mean failed:", integral.mean() 162 163 print 164 cfd = CFD() 165 sa = SlidingAverage(8) 166 print sa 167 cfd_out = sa(event) 168 cfd_out = cfd(cfd_out ) 169 cfd_out = sa(cfd_out) 109 170 zx = ZeroXing() 110 171 print zx 172 list_of_list_of_times = zx(cfd_out) 173 times = [] 174 for list_of_times in list_of_list_of_times: 175 times.extend(list_of_times) 176 times = np.array(times) 177 178 hist,bins = np.histogram(times,3000,(0,300)) 179 most_probable_time = np.argmax(hist) 180 print 'most probable time of zero-crossing', most_probable_time/10. 181 print 'this includes filter delays ... for average filter setting 8 this turns out to be 78.8 most of the time' 182 183 #histplotter(times.flatten(), 'times.flatten()') 184 #plotter(cfd_out[0], 'cfd_out') 185 #plotter (pulse, pulse_str) 186 187 188
Note:
See TracChangeset
for help on using the changeset viewer.