封装SyPy筛选器
通过封装来自两个库模块的几个函数,我们可以清楚地了解数字滤波器是什么样子的。此外,通过将__init__拆分成两个部分,可以动态重置过滤器的创建参数和内部条件。
选择 | 换行 | 行号
- """ Encapulates a scipy filter. Given type, freq and order,
- create the coefficients and internal conditions, etc."""
- import scipy.signal.filter_design as fd
- import scipy.signal.signaltools as st
- _filterTypeDict = {'elliptic':'ellip', 'Butterworth':'butter',
- 'Chebyshev I':'cheby1', 'Chebyshev II':'cheby2',
- 'Bessel':'bessel'}
- DefaultFilterType = 'Butterworth'
- DefaultFilterOrder = 2
- DefaultFilterFreq = 0.1
- class FilterObject(object):
- def __init__(self, channelNumber, nTaps, freq,
- bType='lowpass', fType="butter", output='ba'):
- self.channelNumber = channelNumber
- self.InitFilter(nTaps, freq, bType, fType, output)
- def InitFilter(self, nTaps, freq, bType, fType, output):
- print "taps = %d: freq = %f" % (nTaps, freq)
- b, a = fd.iirfilter(nTaps, freq, btype=bType, ftype=fType, output=output)
- self.b, self.a = b, a
- self.ic = st.lfiltic(b, a, (0.0,))
- def Filter(self, dataArray):
- resTuple = st.lfilter(self.b, self.a, dataArray, zi=self.ic)
- self.ic = resTuple[-1]
- return resTuple[0]
- if __name__ == "__main__":
- x = (1.5, 1.4, 1.5, 1.6, 1.5, 1.4, 1.5, 1.6)
- f = FilterObject(2, .05)
- for a in range(20):
- print f.Filter(x)
这是另一个很好的例子。
_filterTypeDict就是"名称损坏"的一个例子。这是一种使模块作用域变量保持半私有的方法。
当您导入myModule时
模块作用域类、函数和变量由引用
MyModule.myClass()、myModule.myFunction()、myModule.myVariable。
使用导入模块的对象时
从myModule导入*
除了_SemiPrivate变量之外,该模块中的所有名称都被放入导入模块的作用域中。它们是半私密的,因为您仍然可以
A=myModel._SemiPrivate(名称已导入到当前作用域中,并在其前面添加了模块的名称)。