- Timestamp:
- 03/22/12 21:15:57 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fact/tools/pyscripts/pyfact/fir_filter.py
r13143 r13180 6 6 import numpy as np 7 7 from scipy import signal 8 8 9 import matplotlib.pyplot as plt 9 10 … … 98 99 99 100 """ 100 npoints = 100 101 from plotters import Plotter 102 from generator import SignalGenerator 103 generate = SignalGenerator() 104 plot = Plotter('_test_SlidingAverage') 105 101 106 safilter = SlidingAverage(8) 102 signal = np.zeros(npoints) 103 signal[10:50] += 20. 104 105 # add noise to the signal 106 sigma = 1.5 107 signal += np.random.randn(npoints) * sigma 107 print safilter 108 108 109 print safilter 110 #print 'signal in: ', signal 111 #print 'signal out: ', rsfilter(signal) 112 x=range(npoints) 113 plt.plot(x, signal, 'b', label='original') 114 plt.plot(x, safilter(signal), 'r', label='filtered') 115 plt.title(safilter.name) 116 plt.xlabel('sample') 117 plt.legend() 118 plt.grid(True) 119 plt.show() 120 109 signal = generate('len 100 noise 1.5 step 20 10 50') 110 filtered = safilter(signal) 111 plot( [signal, filtered] , ['original', 'filtered'] ) 112 113 raw_input('press any key to go on') 114 plt.close(plot.figure) 115 121 116 122 117 def _test_CFD(): … … 124 119 125 120 """ 121 from plotters import Plotter 122 from generator import SignalGenerator 123 generate = SignalGenerator() 124 plot = Plotter('_test_CFD') 126 125 127 filt = CFD(8, 0.6) 126 sa = SlidingAverage(3) 127 print 'I apply a weak smooting with a filter length of 3' 128 cfd = CFD(8, 0.6) 129 print cfd 128 130 129 npoints = 100 130 signal = np.ones(npoints) * 10. 131 signal[20:30] += np.linspace(0., 100., 10) 132 signal[30:90] += np.linspace(100., 0., 60) 133 # add noise to the signal 134 sigma = 1.5 135 signal += np.random.randn(npoints) * sigma 131 signal = generate('len 100 noise 1.5 bsl -20 triangle 30 30 8 50') 132 filtered = cfd(sa(signal)) 133 plot( [signal, filtered] , ['original', 'filtered'] ) 136 134 137 print filt 138 #print 'signal in: ', signal 139 #print 'signal out: ', rsfilter(signal) 140 x=range(npoints) 141 plt.plot(x, signal, 'b.', label='original') 142 plt.plot(x, filt(signal), 'r.', label='filtered') 143 plt.title(filt.name) 144 plt.xlabel('sample') 145 plt.legend() 146 plt.grid(True) 147 plt.show() 148 135 raw_input('press any key to go on') 136 plt.close(plot.figure) 149 137 150 138 def _test_RemoveSignal(): … … 152 140 153 141 """ 142 from plotters import Plotter 143 from generator import SignalGenerator 144 generate = SignalGenerator() 145 plot = Plotter('_test_RemoveSignal') 154 146 155 rsfilter = RemoveSignal() 147 remove_signal = RemoveSignal() 148 print remove_signal 156 149 157 npoints = 100 158 signal = np.ones(npoints) * 10. 159 signal[10:20] += np.linspace(0., 100., 10) 160 signal[20:80] += np.linspace(100., 0., 60) 161 # add noise to the signal 162 sigma = 3. 163 signal += np.random.randn(npoints) * sigma 150 signal = generate('len 100 noise 2 bsl -20 triangle 20 30 8 40 50 30 spike 50 50 15 50 80 50') 151 filtered = remove_signal(signal) 152 plot( [signal, filtered] , ['original', 'filtered'] ) 164 153 165 print rsfilter 166 #print 'signal in: ', signal 167 #print 'signal out: ', rsfilter(signal) 168 x=range(npoints) 169 plt.plot(x, signal, 'b.', label='original') 170 plt.plot(x, rsfilter(signal), 'r.', label='filtered') 171 plt.title(rsfilter.name) 172 plt.xlabel('sample') 173 plt.legend() 174 plt.grid(True) 175 plt.show() 154 raw_input('press any key to go on') 155 plt.close(plot.figure) 176 156 177 157 def _test(filter_type, sig, noise_sigma = 1.): 158 from plotters import Plotter 178 159 179 160 filt = filter_type … … 182 163 sig += np.random.randn(samples) * noise_sigma 183 164 184 print filt 185 x=range(samples) 186 plt.plot(x, sig, 'b.', label='original') 187 plt.plot(x, filt(sig), 'r.', label='filtered') 188 plt.title(filt.name) 189 plt.xlabel('sample') 190 plt.legend() 191 plt.grid(True) 192 plt.show() 193 165 plot = Plotter('_test with ' + str(filt.name)) 166 plot( [sig, filt(sig)], ['original', 'filtered'] ) 167 raw_input('press any key to go on') 168 plt.close(plot.figure) 194 169 195 170 if __name__ == '__main__': … … 197 172 198 173 _test_SlidingAverage() 174 _test_CFD() 199 175 _test_RemoveSignal() 200 _test_CFD()176 201 177 tsig = np.ones(100) 202 178 _test(filter_type=SlidingAverage(8), sig=tsig, noise_sigma=3.)
Note:
See TracChangeset
for help on using the changeset viewer.