MATLAB的sgolay(k,f)的Python等效项是什么? [英] What is the Python equivalent for MATLAB's sgolay(k, f)?
问题描述
我在MATLAB中有一个函数
I have a function in MATLAB
[b,g] = sgolay(k, f);
它输出一个f x f矩阵.
It outputs a f x f matrix.
当我在Python中对相同的k和f值运行相同时,使用:
When I run the same for the same values of k and f in Python, using:
scipy.signal.savgol_coeffs(f, k)
它输出仅由f个元素组成的完全不同的数组.
It outputs an entirely different array of only f elements.
考虑的值是:
k = 4,f = 21
k = 4, f = 21
savol_filter()
接受三个参数,包括数组,而sgolay()
仅接受两个参数.另外,savo_coeffs
不会生成所需的矩阵.
savol_filter()
takes three arguments, including the array, whereas sgolay()
takes only two. Also, the savo_coeffs
is not generating the required matrix.
在matlab中获取sgolay(k,f)生成的矩阵的Python等效项是什么?
What is the Python equivalent for obtaining the matrix generated by sgolay(k, f) in matlab?
推荐答案
如果您检查Matlab的sgolay
函数,您会看到中心行与SciPy的savgol_coeffs
返回的一维数组相同. b
的上半部分和下半部分(每个部分中都有(framelen - 1)/2
行)是将应用于信号两端的Savitzky-Golay滤波器的系数,其中滤波器不对称.也就是说,对于信号两端的(framelen - 1)/2
值,每个滤波后的值都是使用一组不同的系数来计算的.
If you inspect the matrix b
returned by Matlab's sgolay
function, you'll see that the center row is the same as the 1-d array returned by SciPy's savgol_coeffs
. The upper and lower halves of b
, with (framelen - 1)/2
rows in each part, are the coefficients of the Savitzky-Golay filters to be applied to the ends of the signal, where the filter is not symmetric. That is, for the (framelen - 1)/2
values at each end of the signal, each filtered value is computed using a different set of coefficients.
您可以使用savgol_coeffs
通过遍历pos
参数来生成b
.以下ipython会话显示了一个示例.
You can use savgol_coeffs
to generate b
by iterating over the pos
argument. The following ipython session shows an example.
In [74]: import numpy as np
In [75]: from scipy.signal import savgol_coeffs
In [76]: np.set_printoptions(precision=11, linewidth=90)
In [77]: order = 3
In [78]: windowlen = 5
这些是对称(即居中)Savitzky-Golay滤波器的系数.一维数组应与sgolay
返回的矩阵的中心行:
These are the coefficients of the symmetric (i.e. centered) Savitzky-Golay filter. The 1-d array should match the center row of the matrix returned by sgolay
:
In [79]: savgol_coeffs(windowlen, order)
Out[79]: array([-0.08571428571, 0.34285714286, 0.48571428571, 0.34285714286, -0.08571428571])
如果设置pos=windowlen-1
,则将获得设计用于评估窗口一端的滤波器的系数.这些应该匹配sgolay
返回的数组的第一行:
If we set pos=windowlen-1
, we get coefficients designed for evaluating the filter at one end of the window. These should match the first row of the array returned by sgolay
:
In [80]: savgol_coeffs(windowlen, order, pos=windowlen-1)
Out[80]: array([ 0.98571428571, 0.05714285714, -0.08571428571, 0.05714285714, -0.01428571429])
类似地,pos=0
给出窗口另一端的系数.这些应该匹配sgolay
返回的矩阵的最后一行:
Similarly, pos=0
gives the coefficients for the other end of the window. These should match the last row of the matrix returned by sgolay
:
In [81]: savgol_coeffs(windowlen, order, pos=0)
Out[81]: array([-0.01428571429, 0.05714285714, -0.08571428571, 0.05714285714, 0.98571428571])
这是与Matlab sgolay
的返回值匹配的完整数组:
Here's the full array to match the return value of Matlab's sgolay
:
In [82]: b = np.array([savgol_coeffs(windowlen, order, pos=p) for p in range(windowlen-1, -1, -1)])
In [83]: b
Out[83]:
array([[ 0.98571428571, 0.05714285714, -0.08571428571, 0.05714285714, -0.01428571429],
[ 0.05714285714, 0.77142857143, 0.34285714286, -0.22857142857, 0.05714285714],
[-0.08571428571, 0.34285714286, 0.48571428571, 0.34285714286, -0.08571428571],
[ 0.05714285714, -0.22857142857, 0.34285714286, 0.77142857143, 0.05714285714],
[-0.01428571429, 0.05714285714, -0.08571428571, 0.05714285714, 0.98571428571]])
如果将其与Matlab中b = sgolay(3, 5)
的结果进行比较,您会发现它们是相同的.
If you compare this to the result of b = sgolay(3, 5)
in Matlab, you'll see that they are the same.
要获取由sgolay
返回的g
矩阵,您必须调用savgol_coeffs
并将deriv
设置为range(order+1)
中的值,对数组进行反转和转置,并按导数阶.要反转系数,可以使用形式为::-1
的切片,也可以使用savgol_coeffs
的use
选项.
To get the g
matrix returned by sgolay
, you'll have to call savgol_coeffs
with deriv
set to the values in range(order+1)
, reverse and transpose the array, and scale by the factorial of the derivative order. To reverse the coefficients, you could use a slice of the form ::-1
, or you can use the use
option of savgol_coeffs
.
这是使用savgol_coeffs
生成具有order=3
和windowlen=5
的g
矩阵的一种方法:
Here's one way to use savgol_coeffs
to generate the g
matrix with order=3
and windowlen=5
:
In [12]: import numpy as np
In [13]: from scipy.signal import savgol_coeffs
In [14]: from scipy.special import factorial
In [15]: np.set_printoptions(precision=11, linewidth=90, suppress=True)
In [16]: order = 3
In [17]: windowlen = 5
In [18]: g = np.array([savgol_coeffs(windowlen, order, deriv=d, use='dot') for d in range(order+1)]).T / factorial(np.arange(order+1))
In [19]: g
Out[19]:
array([[-0.08571428571, 0.08333333333, 0.14285714286, -0.08333333333],
[ 0.34285714286, -0.66666666667, -0.07142857143, 0.16666666667],
[ 0.48571428571, 0. , -0.14285714286, 0. ],
[ 0.34285714286, 0.66666666667, -0.07142857143, -0.16666666667],
[-0.08571428571, -0.08333333333, 0.14285714286, 0.08333333333]])
您没有说为什么要在Python中使用完整的windowlen
x windowlen
数组.您不需要使用savgol_filter
.
You don't say why you want the full windowlen
x windowlen
array in Python. You don't need it to use savgol_filter
.
这篇关于MATLAB的sgolay(k,f)的Python等效项是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!