相同代码的两个版本 return 不同的结果
Two versions of the same code return different results
我希望将这两行 MATLAB 代码压缩为一行时得到相同的结果,但我没有!
两行代码:
[b,a]= butter(2,[0.4 0.6]) % Transfer function coefficients of the filter
[A,B,C,D] = tf2ss(b,a) % State-space representation of the filter
1 行代码:
[A,B,C,D]= butter(2,[0.4 0.6]) % State-space representation of the filter
黄油:
returns transfer function coefficients
当 b,a
是 Outputs
returns state-space matrices
当 A,B,C,D
是 Outputs
tf2ss :
- 将
transfer function filter parameters
转换为state-space
形式
您获得的两个状态-space表示是有效的。过滤器的状态 space 表示 不是唯一的 。当应用于输入信号时,两者给出相同的结果。
两个状态-space表示不相同的可能原因是它们是通过不同的路径获得的:
在代码的两步版本中,您获得 传递函数 表示,然后转换为 state-space, 使用 tf2ss
.
在单步版本中,butter
internally obtains the zero-pole representation and then converts to state-space, using zp2ss
(至少这是它在 R2018b 中的作用)。
这里检查它们确实是等价的。
[b,a]= butter(2,[0.4 0.6]);
[A2,B2,C2,D2] = tf2ss(b,a); % 2 steps
[A1,B1,C1,D1]= butter(2,[0.4 0.6]); % 1 step
定义一个输入信号:
x = rand(1,100);
从它们的状态 space 表示中创建两个 filter objects:
Hd2 = dfilt.statespace(A2,B2,C2,D2);
Hd1 = dfilt.statespace(A1,B1,C1,D1);
获得两个输出:
y2 = Hd2.filter(x);
y1 = Hd1.filter(x);
比较输出。相差eps
数量级,即可以忽略不计:
max(abs(y1))
max(abs(y2))
max(abs(y1-y2))
ans =
0.348561524872161
ans =
0.348561524872160
ans =
8.153200337090993e-16
您还可以检查两个状态-space表示给出相同的传递函数表示:
[b1,a1] = ss2tf(A1,B1,C1,D1)
[b2,a2] = ss2tf(A2,B2,C2,D2)
b1 =
0.067455273889072 0.000000000000000 -0.134910547778144 0.000000000000000 0.067455273889072
a1 =
1.000000000000000 -0.000000000000001 1.142980502539900 -0.000000000000001 0.412801598096187
b2 =
0.067455273889072 0.000000000000000 -0.134910547778144 -0.000000000000000 0.067455273889072
a2 =
1.000000000000000 -0.000000000000001 1.142980502539899 -0.000000000000002 0.412801598096187
实际上,第一行代码应该改成下面这样。
[A,B,C,D]= tf2ss(butter(2,[0.4 0.6]));
但是,这也不会给出所需的答案,因为 trf2ss 需要两个输入作为输入参数。上面的代码只给出了一个输入,它是一个有两个值的向量。在 Matlab 中,向量是一种独立的变量类型,因此有时不会像我们预期的那样工作。
我希望将这两行 MATLAB 代码压缩为一行时得到相同的结果,但我没有!
两行代码:
[b,a]= butter(2,[0.4 0.6]) % Transfer function coefficients of the filter
[A,B,C,D] = tf2ss(b,a) % State-space representation of the filter
1 行代码:
[A,B,C,D]= butter(2,[0.4 0.6]) % State-space representation of the filter
黄油:
returns
transfer function coefficients
当b,a
是 Outputsreturns
state-space matrices
当A,B,C,D
是 Outputs
tf2ss :
- 将
transfer function filter parameters
转换为state-space
形式
您获得的两个状态-space表示是有效的。过滤器的状态 space 表示 不是唯一的 。当应用于输入信号时,两者给出相同的结果。
两个状态-space表示不相同的可能原因是它们是通过不同的路径获得的:
在代码的两步版本中,您获得 传递函数 表示,然后转换为 state-space, 使用
tf2ss
.在单步版本中,
butter
internally obtains the zero-pole representation and then converts to state-space, usingzp2ss
(至少这是它在 R2018b 中的作用)。
这里检查它们确实是等价的。
[b,a]= butter(2,[0.4 0.6]);
[A2,B2,C2,D2] = tf2ss(b,a); % 2 steps
[A1,B1,C1,D1]= butter(2,[0.4 0.6]); % 1 step
定义一个输入信号:
x = rand(1,100);
从它们的状态 space 表示中创建两个 filter objects:
Hd2 = dfilt.statespace(A2,B2,C2,D2);
Hd1 = dfilt.statespace(A1,B1,C1,D1);
获得两个输出:
y2 = Hd2.filter(x);
y1 = Hd1.filter(x);
比较输出。相差eps
数量级,即可以忽略不计:
max(abs(y1))
max(abs(y2))
max(abs(y1-y2))
ans =
0.348561524872161
ans =
0.348561524872160
ans =
8.153200337090993e-16
您还可以检查两个状态-space表示给出相同的传递函数表示:
[b1,a1] = ss2tf(A1,B1,C1,D1)
[b2,a2] = ss2tf(A2,B2,C2,D2)
b1 =
0.067455273889072 0.000000000000000 -0.134910547778144 0.000000000000000 0.067455273889072
a1 =
1.000000000000000 -0.000000000000001 1.142980502539900 -0.000000000000001 0.412801598096187
b2 =
0.067455273889072 0.000000000000000 -0.134910547778144 -0.000000000000000 0.067455273889072
a2 =
1.000000000000000 -0.000000000000001 1.142980502539899 -0.000000000000002 0.412801598096187
实际上,第一行代码应该改成下面这样。
[A,B,C,D]= tf2ss(butter(2,[0.4 0.6]));
但是,这也不会给出所需的答案,因为 trf2ss 需要两个输入作为输入参数。上面的代码只给出了一个输入,它是一个有两个值的向量。在 Matlab 中,向量是一种独立的变量类型,因此有时不会像我们预期的那样工作。