Changeset 14486 for fact/tools
- Timestamp:
- 10/17/12 15:42:12 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fact/tools/pyscripts/pyfact/fir_filter.py
r13330 r14486 7 7 from scipy import signal 8 8 9 # For FastSlidingAverage 10 from scipy import weave 11 from scipy.weave import converters 9 12 10 13 class FirFilter(object): … … 87 90 a[0] = len(b) 88 91 FirFilter.__init__(self, b, a, 'sliding average') 89 92 93 94 95 96 class FastSlidingAverage( object ): 97 def __init__(self, shape, length=8): 98 """ initialize the object 99 length: lenght of the averaging window 100 """ 101 self.length = length 102 # allocate memory for the filtered data once 103 self.filtered = np.zeros( shape, np.float64 ) 104 self.shape = shape 105 106 def __call__(self, data): 107 if self.shape != data.shape: 108 raise TypeException('data has wrong shape') 109 110 length = self.length 111 numpix = data.shape[0] 112 numslices = data.shape[1] 113 114 filtered = self.filtered 115 cppcode = """ 116 double sum = 0; 117 for (int pix=0; pix<numpix; pix++) 118 { 119 for ( int sl=0; sl<numslices-length; ++sl) 120 { 121 for ( int i=0; i<length; ++i) 122 { 123 sum += data(pix,sl+i); 124 } 125 filtered(pix,sl) = sum/length; 126 } 127 } 128 """ 129 130 ## this seems to run a little bit faster. 131 132 cppcode2 = """ 133 double sum = 0; 134 for (int pix=0; pix<numpix; pix++) 135 { 136 sum = 0; 137 for ( int i=0; i<length-1; ++i) 138 { 139 sum += data(pix,i); 140 } 141 for ( int sl=length-1; sl<numslices-length; ++sl) 142 { 143 sum += data(pix,sl); 144 filtered(pix,sl) = sum/length; 145 sum -= data(pix,sl-(length-1) ); 146 } 147 } 148 """ 149 150 weave.inline( cppcode2, 151 [ 'length' , 'numpix', 'numslices', 'data', 'filtered'], 152 type_converters=converters.blitz) 153 154 return filtered 90 155 91 156 class CFD(FirFilter):
Note:
See TracChangeset
for help on using the changeset viewer.