简体   繁体   English

在python中如何强制scipy.signal中的lfilter处理整数

[英]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. 我有一些要模拟的VHDL代码。 It uses integer FIR coefficients and performs integer adding. 它使用整数FIR系数并执行整数加法。 The coefficients I use are rescaled from scipy.signal.firwin. 我使用的系数从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中进行仿真。

Python however uses for lfilter floating point arithmetic, which is not exactly what I need. 但是,Python使用lfilter浮点算法,这并不是我真正需要的。 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. .d。

How about numpy.convolve ? 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. 请注意,输出比输入短一点,因为mode=valid开关可避免在数据不足的情况mode=valid计算样本。 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. 我打赌是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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