scipy.signal.
upfirdn#
- scipy.signal.upfirdn(h, x, up=1, down=1, axis=-1, mode='constant', cval=0)[原始碼]#
升採樣、FIR 濾波器和降採樣。
- 參數:
- harray_like
1-D FIR(有限脈衝響應)濾波器係數。
- xarray_like
輸入訊號陣列。
- upint, optional
升採樣率。預設值為 1。
- downint, optional
降採樣率。預設值為 1。
- axisint, optional
要沿其應用線性濾波器的輸入資料陣列軸。濾波器會應用於沿此軸的每個子陣列。預設值為 -1。
- modestr, optional
要使用的訊號延伸模式。集合
{"constant", "symmetric", "reflect", "edge", "wrap"}
對應於numpy.pad
提供的模式。"smooth"
透過基於陣列每一端最後 2 個點的斜率進行延伸來實作平滑延伸。"antireflect"
和"antisymmetric"
是"reflect"
和"symmetric"
的反對稱版本。模式 “line” 基於沿axis
的第一個和最後一個點定義的線性趨勢來延伸訊號。在 1.4.0 版本中新增。
- cvalfloat, optional
當
mode == "constant"
時要使用的常數值。在 1.4.0 版本中新增。
- 返回:
- yndarray
輸出訊號陣列。維度將與 x 相同,但沿 axis 除外,其大小將根據 h、up 和 down 參數而改變。
筆記
此演算法是 Vaidyanathan 教材 [1] 第 129 頁(圖 4.3-8d)上所示方塊圖的實作。
以 P 倍因子進行升採樣(零插入)、長度為
N
的 FIR 濾波以及以 Q 倍因子進行降採樣的直接方法是每個輸出樣本 O(N*Q)。此處使用的多相實作是 O(N/P)。在 0.18 版本中新增。
參考文獻
[1]P. P. Vaidyanathan, Multirate Systems and Filter Banks, 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.]])