amansour
amansour

Reputation: 57

Two versions of the same code return different results

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 :

tf2ss :

Upvotes: 2

Views: 371

Answers (2)

Luis Mendo
Luis Mendo

Reputation: 112659

The 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

Upvotes: 2

xozeracrew
xozeracrew

Reputation: 56

Actually, the line 1 code should be changed to below.

[A,B,C,D]= tf2ss(butter(2,[0.4 0.6]));

But, this also will not give the desired answer since, trf2ss required two inputs as input parameters. The above code gives only one input, which is a vector with two values. In Matlab, vectors are a separate type of variables, hence will not work as we expect some times.

Upvotes: -1

Related Questions