I expect to get the same results when compressing these two MATLAB lines of code into one, but I don't!
Code in 2 lines:
[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
Code in 1 line:
[A,B,C,D]= butter(2,[0.4 0.6]) % State-space representation of the filter
butter :
returns the transfer function coefficients
when b,a
are Outputs
returns the state-space matrices
when A,B,C,D
are Outputs
tf2ss :
transfer function filter parameters
to state-space
formThe two state-space representations you are obtaining are valid. The state-space representation of a filter is not unique. The two give the same results when applied to an input signal.
The likely reason why the two state-space representations are not the same is that they are obtained following different routes:
In the two-step version of your code, you obtain the transfer-function representation and then convert to state-space, using tf2ss
.
In the one-step version, butter
internally obtains the zero-pole representation and then converts to state-space, using zp2ss
(at least this it what it does in R2018b).
Here is a check that they are indeed equivalent.
[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
Define an input signal:
x = rand(1,100);
Create the two filter objects from their state-space representations:
Hd2 = dfilt.statespace(A2,B2,C2,D2);
Hd1 = dfilt.statespace(A1,B1,C1,D1);
Obtain the two outputs:
y2 = Hd2.filter(x);
y1 = Hd1.filter(x);
Compare the outputs. The difference is of the order of eps
, that is, neglibible:
max(abs(y1))
max(abs(y2))
max(abs(y1-y2))
ans =
0.348561524872161
ans =
0.348561524872160
ans =
8.153200337090993e-16
You can also check that both state-space representations give the same transfer-function representation:
[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