# Are MATLAB function zp2tf() and tf2zp() are complementary or not?

I was under impression that given, pole, zero and gain the transfer function (filter coefficients b and a) is fixed. Therefore, if we designing a filter using cheby2() for some coefficients to get the pole zero and gain by using

[z, p,k ] = cheby2( N, Rs, Ws, 'stop');


I assumed that we can get back the [z,p] by

[hB,hA] = zp2tf(z,p,k);

[z1,p1,Hek] = tf2zp(hB,hA);


In other words z1=z and p1=p. But this is not true as shown by my MATLAB code. Can anyone please tell me what am I missing?

This happens frequently if your poles are reasonably close to the unit circle. Consider the following example

%% TF2ZP is problematic
fs = 44100;
% 6th order lowpass, fc = 50Hz, sampled at 44.1kHz
[z,p,k] = cheby2(6,80,50*2/fs);
% to transfer function
[b,a] = zp2tf(z,p,k);
% back to zpk
[z1,p1,k1] = tf2zp(b,a);
display([p p1]);


Displaying the poles side by side, you get

         Correct Poles                  TF2ZPK
0.99729 + 0.00078062i       1.0007 +          0i
0.99729 - 0.00078062i      0.99983 +  0.0033549i
0.99814 +  0.0019985i      0.99983 -  0.0033549i
0.99814 -  0.0019985i      0.99691 +  0.0030633i
0.99936 +  0.0025696i      0.99691 -  0.0030633i
0.99936 -  0.0025696i      0.99535 +          0i


The poles are very different and one pole has actually moved out of the unit circle and the resulting filter is unstable.

This is caused by limited numerical precision. $$tf2zp()$$ requires calculating the roots of a polynomial which is a numerically tricky problem especially if the roots are very close together. $$zp2tf()$$ works perfectly fine but $$tf2zp()$$ doesn't.

Morale of the story: If you have poles close to the unit circle, keep your filters in ZPK or SOS representation and avoid transfer functions. Avoid $$tf2zp()$$ if possible.

Are MATLAB function zp2tf() and tf2zp() are complementary or not?

Sort of. They are intended to be complimentary but like any complimentary pair this is subject to numerical noise of the implementation. In this particular instance there are reasonable use cases, where the numerical noise can be substantial to the point where the function becomes useless.