简体   繁体   中英

in python how to force lfilter from scipy.signal to work over integers

i have some VHDL piece of code I'd like to simulate. It uses integer FIR coefficients and performs integer adding. The coefficients I use are rescaled from scipy.signal.firwin. I see in my real hardware some perturbances, which come from the filter and which I'd like to simulate in python.

Python however uses for lfilter floating point arithmetic, which is not exactly what I need. I would need integer only arithmetic using rounding to get nearest integer and propagate the signal through the filter.

is there such function or I have to do myself?

thanks

.d.

How about numpy.convolve ?

Here's a simple example using a filter to add the previous two samples.

signal, coeffs = np.array([1,2,3,4,5]), np.array([1, 1])
output = np.convolve(signal, coeffs, mode='valid')

Result:

array([3, 5, 7, 9])

Note the output is a bit shorter than the input because the mode=valid switch avoids calculating samples where there is insufficient data. You can try different switches to get the behavior you want for the edge effects.

It's possible that this function internally calculates floating point and then rounds to integer. As you realized, this has no effect on the ultimate answer. But, if your objective is to save computation time, you should check that this is actually faster for integer input than it is for floating point input. I bet that it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM