简体   繁体   English

Scipy 与 Matlab 传递函数的不同结果

[英]Differing results in Scipy vs Matlab transfer functions

I can't understand why the scipy.signal.ss2tf() and scipy.signal.StateSpace().to_tf() give (the same) unexpected result.我不明白为什么scipy.signal.ss2tf()scipy.signal.StateSpace().to_tf()会给出(相同的)意外结果。

Example:例子:

A=[[0, 1, 0], [0, 0, 1], [-3, -4, -2]]
B=[[0], [0], [1]]
C=[[5, 1, 0]]
D=[[0]]

The result for scipy is scipy 的结果是

num = array([[0, 0, 0, 4]]),
den = array([1., 2., 4., 3.])

in Matlab the result is在 Matlab 结果是

num = [0,0,1,5],
den = [1,2,4,3]

It seems that the denominator is always right, I tried other examples, but the numerator of the transfer function doesn't correspond.看来分母总是对的,我试过其他例子,但是转帐function的分子不对应。

Am I using scipy in an incorrect way?我是否以不正确的方式使用 scipy?

(another example) (另一个例子)

A=[[0, 1, 0], [0, 0, 1], [-8, -14, -7]]
B=[[0], [0], [1]]
C=[[15, 5, 0]]
D=[[0]]

This is a bug in SciPy .这是SciPy中的错误。 When ss2tf creates an array to hold the numerator, it uses the data types of the inputs to determine the data type of the numerator array.ss2tf创建一个数组来保存分子时,它使用输入的数据类型来确定分子数组的数据类型。 In your case, the values are all integers, so the data type of the numerator is integer.在您的情况下,这些值都是整数,因此分子的数据类型是 integer。 However, the coefficients for the numerator are the result of a floating point calculation, and therefore are subject to loss of precision.但是,分子的系数是浮点计算的结果,因此会损失精度。 When the computed values are copied into the numerator array, those values are truncated to integers.当计算值被复制到分子数组中时,这些值将被截断为整数。 In your example, that results in a large error.在您的示例中,这会导致很大的错误。 When I reproduce the floating point calculation of the numerator, I get [0.0, 0.0, 0.9999999999999947, 4.999999999999995] , and when those values are copied into the integer array, the result is [0, 0, 0, 4] .当我重现分子的浮点计算时,我得到[0.0, 0.0, 0.9999999999999947, 4.999999999999995] ,当这些值被复制到 integer 数组中时,结果是[0, 0, 0, 4]

A work-around is to ensure that your inputs (or at least A ) contain floating point values.一种解决方法是确保您的输入(或至少A )包含浮点值。 For example,例如,

In [33]: A = [[0., 1., 0.], [0., 0., 1.], [-8., -14., -7.]] 
    ...: B = [[0], [0], [1]] 
    ...: C = [[15, 5, 0]] 
    ...: D = [[0]]                                                              

In [34]: num, den = ss2tf(A, B, C, D)                                           

In [35]: num                                                                    
Out[35]: array([[0.00000000e+00, 1.77635684e-15, 5.00000000e+00, 1.50000000e+01]])

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

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