Recursion: IIR filter with `scipy.lfilter`

前端 未结 2 737
南旧
南旧 2021-01-24 03:32

Given some data x:

from pandas_datareader.data import DataReader as dr
x = np.squeeze(dr(\'DTWEXB\', \'fred\').dropna().values)

I

2条回答
  •  旧巷少年郎
    2021-01-24 03:39

    Take the z-transform of your filter, that gives you the values for the numerator b and denominator a:

           alpha
    y(z) = ------------------ x(z)
           1 - (1-alpha) z^-1
    

    So you run

    import scipy.signal
    x[0] /= alpha
    y = scipy.signal.lfilter([alpha], [1, - 1 + alpha], x)
    

    Which yields

    array([ 101.1818    ,  101.176862  ,  101.16819314, ...,  120.9813121 ,
            120.92484874,  120.85786628])
    

    Note I have scaled x[0] to account for the initial condition you wanted.

提交回复
热议问题