scipy.signal.

upfirdn#

scipy.signal.upfirdn(h, x, up=1, down=1, axis=-1, mode='constant', cval=0)[source]#

上采样、FIR 滤波和下采样。

参数:
harray_like

一维 FIR(有限脉冲响应)滤波器系数。

xarray_like

输入信号数组。

upint, 可选

上采样率。默认为 1。

downint, 可选

下采样率。默认为 1。

axisint, 可选

应用线性滤波的输入数据数组的轴。滤波器将沿此轴应用于每个子数组。默认为 -1。

modestr, 可选

要使用的信号扩展模式。集合 {"constant", "symmetric", "reflect", "edge", "wrap"} 对应于 numpy.pad 提供的模式。"smooth" 通过基于数组两端最后 2 个点的斜率进行扩展来实现平滑扩展。"antireflect""antisymmetric""reflect""symmetric" 的反对称版本。模式 “line” 基于沿 axis 定义的第一个和最后一个点的线性趋势扩展信号。

在 1.4.0 版本中新增。

cvalfloat, 可选

mode == "constant" 时使用的常数值。

在 1.4.0 版本中新增。

返回:
yndarray

输出信号数组。除 axis 轴外,维度将与 x 相同,该轴将根据 hupdown 参数改变大小。

备注

该算法是 Vaidyanathan 文本 [1] 第 129 页(图 4.3-8d)所示框图的实现。

通过零插入进行 P 倍上采样、长度为 N 的 FIR 滤波以及 Q 倍下采样的直接方法,每输出样本的复杂度为 O(N*Q)。这里使用的多相实现复杂度为 O(N/P)。

在 0.18 版本中新增。

参考文献

[1]

P. P. Vaidyanathan,《多速率系统与滤波器组》,Prentice Hall,1993。

示例

简单操作

>>> import numpy as np
>>> from scipy.signal import upfirdn
>>> upfirdn([1, 1, 1], [1, 1, 1])   # FIR filter
array([ 1.,  2.,  3.,  2.,  1.])
>>> upfirdn([1], [1, 2, 3], 3)  # upsampling with zeros insertion
array([ 1.,  0.,  0.,  2.,  0.,  0.,  3.])
>>> upfirdn([1, 1, 1], [1, 2, 3], 3)  # upsampling with sample-and-hold
array([ 1.,  1.,  1.,  2.,  2.,  2.,  3.,  3.,  3.])
>>> upfirdn([.5, 1, .5], [1, 1, 1], 2)  # linear interpolation
array([ 0.5,  1. ,  1. ,  1. ,  1. ,  1. ,  0.5])
>>> upfirdn([1], np.arange(10), 1, 3)  # decimation by 3
array([ 0.,  3.,  6.,  9.])
>>> upfirdn([.5, 1, .5], np.arange(10), 2, 3)  # linear interp, rate 2/3
array([ 0. ,  1. ,  2.5,  4. ,  5.5,  7. ,  8.5])

将单个滤波器应用于多个信号

>>> x = np.reshape(np.arange(8), (4, 2))
>>> x
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7]])

沿 x 的最后一个维度应用

>>> h = [1, 1]
>>> upfirdn(h, x, 2)
array([[ 0.,  0.,  1.,  1.],
       [ 2.,  2.,  3.,  3.],
       [ 4.,  4.,  5.,  5.],
       [ 6.,  6.,  7.,  7.]])

沿 x 的第 0 个维度应用

>>> upfirdn(h, x, 2, axis=0)
array([[ 0.,  1.],
       [ 0.,  1.],
       [ 2.,  3.],
       [ 2.,  3.],
       [ 4.,  5.],
       [ 4.,  5.],
       [ 6.,  7.],
       [ 6.,  7.]])