ama*_*our 0 python matlab numpy scipy
我想将此 MATLAB 代码翻译成 Python,我想我做的一切都是正确的,即使我没有得到相同的结果。
MATLAB 脚本:
n=2 %Filter_Order
Wn=[0.4 0.6] %# Normalized cutoff frequencies
[b,a] = butter(n,Wn,'bandpass') % Transfer function coefficients of the filter
Run Code Online (Sandbox Code Playgroud)
蟒蛇脚本:
import numpy as np
from scipy import signal
n=2 #Filter_Order
Wn=np.array([0.4,0.6]) # Normalized cutoff frequencies
b, a = signal.butter(n, Wn, btype='band') #Transfer function coefficients of the filter
Run Code Online (Sandbox Code Playgroud)
a MATLAB 中的系数: 1, -5.55e-16, 1.14, -1.66e-16, 0.41
a Python中的系数: 1, -2.77e-16, 1.14, -1.94e-16, 0.41
难道这只是一个精度问题,因为两个不同的值(第二个和第四个)都在10^(-16)?!
b另一方面,系数是相同的。
您的机器精度大约是1e-16(在 MATLAB 中,可以使用 轻松检查eps(),我认为在 Python 中大致相同)。因此,您正在处理的“错误”与机器精度有关,即实际上无法在拟合精度内计算。
另外值得注意的是 MATLAB ~= Python(或 != 在 Python 中),因此butter()一方面和另一方面的实现signal.butter()会略有不同,即使您使用完全相同的数字,由于两种语言的翻译方式到机器码。
系数相差 16 个数量级并不重要;较小的将基本上被忽略。如果您确实需要精确值,请考虑使用符号数学或某种可变精度算术(vpa()在 MATLAB 中),但我想在您的情况下,差异无关紧要。