Changeset 13330 for fact/tools/pyscripts/pyfact/fir_filter.py
- Timestamp:
- 04/11/12 08:55:46 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fact/tools/pyscripts/pyfact/fir_filter.py
r13327 r13330 30 30 31 31 *data* 1D or 2D numpy array 32 """33 length = max(len(self.a),len(self.b))-134 if length > 0:35 if ( data.ndim == 1):36 initial = np.ones(length)37 initial *= data[0]38 elif ( data.ndim == 2):39 initial = np.ones( (data.shape[0], length) )40 for i in range(data.shape[0]):41 initial[i,:] *= data[i,0]42 else:43 print 'HELP.'44 pass45 32 46 filtered, zf = signal.lfilter(self.b, self.a, data, zi=initial) 33 remark: 34 I did not understand how to use the initial filter conditions of lfilter() 35 to produce the output, I expected. 36 So I apply the filters as follows. 37 the filter *delay* is equal to its length-1 38 Then I extend the input data by this delay-length, adding copies of the 39 first value. 40 Then the filter runs ovter this extended data. 41 The output will have a filter artifact in the first samples, which 42 will be cut off anyway, because they were artificially added before. 43 """ 44 delay = max(len(self.a),len(self.b))-1 45 46 if ( data.ndim == 1): 47 initial = np.ones(delay) 48 initial *= data[0] 49 elif ( data.ndim == 2): 50 initial = np.ones( (data.shape[0], delay) ) 51 for i in range(data.shape[0]): 52 initial[i,:] *= data[i,0] 47 53 else: 48 filtered= signal.lfilter(self.b, self.a, data) 49 filtered = filtered.reshape(data.shape) 54 print 'HELP.' 55 pass 56 data = np.hstack( (initial,data) ) 57 58 filtered= signal.lfilter(self.b, self.a, data) 59 if ( data.ndim == 1): 60 filtered = filtered[delay:] 61 elif ( data.ndim == 2): 62 filtered = filtered[:,delay:] 63 50 64 return filtered 51 65 … … 123 137 124 138 139 def _test_SlidingAverage2(): 140 """ test the sliding average function 141 use a step function as input 142 """ 143 from plotters import Plotter 144 from generator import SignalGenerator 145 generate = SignalGenerator() 146 plot = Plotter('_test_SlidingAverage') 147 148 safilter = SlidingAverage(8) 149 print safilter 150 151 signal = np.ones( (6,20) ) * 3.0 152 filtered = safilter(signal) 153 #plot( [signal[0], filtered[0]] , ['original', 'filtered'] ) 154 155 raw_input('press any key to go on') 156 plt.close(plot.figure) 157 158 159 125 160 def _test_CFD(): 126 161 """ test the remove signal function
Note:
See TracChangeset
for help on using the changeset viewer.