【发布时间】:2015-09-17 08:24:51
【问题描述】:
我正在尝试使用不对称权重计算 S3x3 移动平均值,如 MatLab example 中所述,我不确定从 MatLab 翻译时我对以下内容的解释是否正确:
- 我是否以相同的方式设置矩阵?
- 在这种情况下,
scipy.signal.convolve2d是否与 MatLab 的conv2d相同? - 为什么我的身材这么差?!
在 MatLab 中,过滤器被给出并应用为:
% S3x3 seasonal filter
% Symmetric weights
sW3 = [1/9;2/9;1/3;2/9;1/9];
% Asymmetric weights for end of series
aW3 = [.259 .407;.37 .407;.259 .185;.111 0];
% dat contains data - simplified adaptation from link above
ns = length(dat) ; first = 1:4 ; last = ns - 3:ns;
trend = conv(dat, sW3, 'same');
trend(1:2) = conv2(dat(first), 1, rot90(aW3,2), 'valid');
trend(ns-1:ns) = conv2(dat(last), 1, aW3, 'valid');
我已经使用我自己的数据在 python 中对此进行了解释,我假设这样做时 MatLab 矩阵中的 ; 表示 新行,而空格表示 新列
import numpy as np
from scipy.signal import convolve2d
dat = np.array([0.02360784, 0.0227628 , 0.0386366 , 0.03338596, 0.03141621, 0.03430469])
dat = dat.reshape(dat.shape[0], 1) # in columns
sW3 = np.array([[1/9.],[2/9.],[1/3.],[2/9.],[1/9.]])
aW3 = np.array( [[ 0.259, 0.407],
[ 0.37 , 0.407],
[ 0.259, 0.185],
[ 0.111, 0. ]])
trend = convolve2d(dat, sW3, 'same')
trend[:2] = convolve2d(dat[:2], np.rot90(aW3,2), 'same')
trend[-2:] = convolve2d(dat[-2:], np.rot90(aW3,2), 'same')
绘制数据,拟合度很差……
import matplotlib.pyplot as plt
plt.plot(dat, 'grey', label='raw data', linewidth=4.)
plt.plot(trend, 'b--', label = 'S3x3 trend')
plt.legend()
plt.plot()
【问题讨论】:
标签: python matlab scipy convolution moving-average