简体   繁体   English

Python Scipy lfilter中的小数精度?

[英]Decimal precision in Python Scipy lfilter?

Does anybody has idea about decimal precision in python (lfilter). 是否有人对python(lfilter)中的小数精度有所了解。 According to their documentation for Matlab filter() and SciPy lfilter(), it seems like they should be equivalent. 根据他们关于Matlab filter()和SciPy lfilter()的文档,似乎它们应该等效。 I am doing the some calculations in matlab and in python ... unfortunately the results which I have obtained are differ and I don't have any idea why ? 我在matlab和python中进行了一些计算...不幸的是,我获得的结果是不同的,我不知道为什么吗?

As an input I am using the same sets of coefficients : 作为输入,我使用了相同的系数集:

b1 = np.array([0.987968850910341,  -1.975937701820681,   0.987968850910341]) 
a1 = np.array([1.000000000000000,  -1.975859694926928,   0.976015708714434])

and following code for filter in python: 以及以下用于python中过滤器的代码:

filtered = lfilter(b1, a1, signal_in, axis=0 )

and in matlab: 在matlab中:

filtered = filter(b1, a1, signal_in)

but the results are differ ... and those from matlab are correct one. 但是结果是不同的……而来自matlab的结果是正确的。 However there should be agreement on the fourth place after decimal. 但是,应在小数点后第四位达成一致。

I will be so grateful for any suggestion what is wrong here. 如果有任何建议,我将不胜感激。

greetings 问候

here is piece of my input data: 这是我的输入数据:

    108699371.346
108699354.081
108699336.818
108699319.553
108699302.288
108699285.023
108699267.757
108699250.494
108699233.231
108699215.969
108699198.707
108699181.443
108699164.18
108699146.917
108699129.654
108699112.393
108699095.133
108699077.872
108699060.611
108699043.351
108699026.091
108699008.832
108698991.572
108698974.312
108698957.053
108698939.794
108698922.536
108698905.274
108698888.014
108698870.754
108698853.494
108698836.234
108698818.976
108698801.719
108698784.462
108698767.205
108698749.95
108698732.693
108698715.436
108698698.179

At least on my system (scipy 0.16.0, Python 3.4.3, MATLAB r2014b, all 64bit), the results are identical. 至少在我的系统上(scipy 0.16.0,Python 3.4.3,MATLAB r2014b,所有64位),结果是相同的。 However, MATLAB and numpy display them differently. 但是,MATLAB和numpy显示它们的方式有所不同。 numpy displays the results to 8 decimal places by default, while MATLAB displays the results to only 4 decimal places by default. numpy默认将结果显示为小数点后8位,而MATLAB默认将结果显示为小数点后4位。 However, internally, both have many more places of precision (both use double precision floating point numbers, which have 15-17 decimal places of precision). 但是,在内部,两者都具有更多的精度位(均使用双精度浮点数,其精度为15-17个小数位)。

If I use sprintf to display the MATLAB results to 8 decimal places, the results are identical to the scipy results (using a character-by-character comparison). 如果我使用sprintf将MATLAB结果显示到小数点后8位,则结果与scipy结果相同(使用逐个字符的比较)。 So it isn't that scipy is doing anything wrong, or even anything different numerically, numpy just has different defaults for how it displays floating-point numbers than MATLAB. 因此,并不是scipy做错了什么,甚至数字上也没有做任何不同,numpy对于浮点数的显示方式与MATLAB仅有不同的默认值。

So what numpy prints by default: 那么numpy默认打印什么:

array([  1.07391593e+08,   1.04799110e+08,   1.02252456e+08,
         9.97509288e+07,   9.72938364e+07,   9.48804965e+07,
         9.25102361e+07,   9.01823919e+07,   8.78963097e+07,
         8.56513445e+07,   8.34468604e+07,   8.12822305e+07,
         7.91568365e+07,   7.70700690e+07,   7.50213272e+07,
         7.30100186e+07,   7.10355593e+07,   6.90973734e+07,
         6.71948935e+07,   6.53275598e+07,   6.34948208e+07,
         6.16961327e+07,   5.99309594e+07,   5.81987724e+07,
         5.64990510e+07,   5.48312815e+07,   5.31949579e+07,
         5.15895811e+07,   5.00146595e+07,   4.84697083e+07,
         4.69542498e+07,   4.54678129e+07,   4.40099335e+07,
         4.25801542e+07,   4.11780240e+07,   3.98030987e+07,
         3.84549400e+07,   3.71331165e+07,   3.58372028e+07,
         3.45667795e+07])

What MATLAB prints by default: MATLAB默认打印的内容:

filtered =

    1.0e+08 *

    Columns 1 through 14

        1.0739    1.0480    1.0225    0.9975    0.9729    0.9488    0.9251    0.9018    0.8790    0.8565    0.8345    0.8128    0.7916    0.7707

    Columns 15 through 28

        0.7502    0.7301    0.7104    0.6910    0.6719    0.6533    0.6349    0.6170    0.5993    0.5820    0.5650    0.5483    0.5319    0.5159

    Columns 29 through 40

        0.5001    0.4847    0.4695    0.4547    0.4401    0.4258    0.4118    0.3980    0.3845    0.3713    0.3584    0.3457

What MATLAB prints when I set it to use 8 decimal places of precision with sprintf('%0.8e, ',filtered) : 当我将sprintf('%0.8e, ',filtered)设置为使用8位小数精度时,MATLAB打印的内容:

1.07391593e+08, 1.04799110e+08, 1.02252456e+08, 9.97509288e+07, 9.72938364e+07, 9.48804965e+07, 9.25102361e+07, 9.01823919e+07, 8.78963097e+07, 8.56513445e+07, 8.34468604e+07, 8.12822305e+07, 7.91568365e+07, 7.70700690e+07, 7.50213272e+07, 7.30100186e+07, 7.10355593e+07, 6.90973734e+07, 6.71948935e+07, 6.53275598e+07, 6.34948208e+07, 6.16961327e+07, 5.99309594e+07, 5.81987724e+07, 5.64990510e+07, 5.48312815e+07, 5.31949579e+07, 5.15895811e+07, 5.00146595e+07, 4.84697083e+07, 4.69542498e+07, 4.54678129e+07, 4.40099335e+07, 4.25801542e+07, 4.11780240e+07, 3.98030987e+07, 3.84549400e+07, 3.71331165e+07, 3.58372028e+07, 3.45667795e+07, 

The scipy and MATLAB results set to have the same format: scipy和MATLAB结果集具有相同的格式:

scipy:  1.07391593e+08 1.04799110e+08 1.02252456e+08 9.97509288e+07 9.72938364e+07 9.48804965e+07 9.25102361e+07 9.01823919e+07 8.78963097e+07 8.56513445e+07 8.34468604e+07 8.12822305e+07 7.91568365e+07 7.70700690e+07 7.50213272e+07 7.30100186e+07 7.10355593e+07 6.90973734e+07 6.71948935e+07 6.53275598e+07 6.34948208e+07 6.16961327e+07 5.99309594e+07 5.81987724e+07 5.64990510e+07 5.48312815e+07 5.31949579e+07 5.15895811e+07 5.00146595e+07 4.84697083e+07 4.69542498e+07 4.54678129e+07 4.40099335e+07 4.25801542e+07 4.11780240e+07 3.98030987e+07 3.84549400e+07 3.71331165e+07 3.58372028e+07 3.45667795e+07 
MATLAB: 1.07391593e+08 1.04799110e+08 1.02252456e+08 9.97509288e+07 9.72938364e+07 9.48804965e+07 9.25102361e+07 9.01823919e+07 8.78963097e+07 8.56513445e+07 8.34468604e+07 8.12822305e+07 7.91568365e+07 7.70700690e+07 7.50213272e+07 7.30100186e+07 7.10355593e+07 6.90973734e+07 6.71948935e+07 6.53275598e+07 6.34948208e+07 6.16961327e+07 5.99309594e+07 5.81987724e+07 5.64990510e+07 5.48312815e+07 5.31949579e+07 5.15895811e+07 5.00146595e+07 4.84697083e+07 4.69542498e+07 4.54678129e+07 4.40099335e+07 4.25801542e+07 4.11780240e+07 3.98030987e+07 3.84549400e+07 3.71331165e+07 3.58372028e+07 3.45667795e+07

Also, you don't need the axis argument for the scipy filter, at least if you use a vector (1D array). 另外,至少在使用向量(一维数组)的情况下,不需要scipy过滤器的axis参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM