terrencetec / kontrol

KAGRA control python package
MIT License
11 stars 0 forks source link

The function foton.tf2zpk produces wrong output #39

Closed naibafomsare closed 1 year ago

naibafomsare commented 2 years ago

Problem

The function kontrol.core.foton.tf2zpk produces a wrong output for a high pass filter.

Steps to reproduce the problem

import control 
import kontrol
import numpy as np

s = control.TransferFunction.s

fc = 0.003 # 3 mHz
wc = 2*np.pi*fc
hp = s**4 / (s+wc)**4
hps = control.TransferFunction.s

kontrol.core.foton.tf2zpk(hp, root_location='n', significant_figures=6)

Produces this output, which is incorrect:

'zpk([-0;-0;-0;-0],[0.003000+i*0.000000;0.003000+i*-0.000000;0.003000+i*0.000000;0.003000+i*-0.000000],1.23457e+10,"n")'

Desired behaviour

It should produce

'zpk( [ 0 ; 0 ; 0 ; 0 ] , [ 0.003000 ; 0.003000 ; 0.003000 ; 0.003000 ] ,1.23457e+10,"n")'
terrencetec commented 2 years ago

Hi @naibafomsare.

Thank you for using Kontrol.

I think the behavior that you showed is the indented behavior. All numbers in the zpk models the tf2zpk() produced are identical to what you expected. It is correct, but not clean, i.e. -0 vs 0. The imaginary term can be omitted if you invoke the itol option which is defaulted to 1e-25. When the imaginary number of a root is smaller than the real part by that factor, the root will be treated as a real number instead. Though, I am not sure it works, I haven't been actively using this option as it's unnecessary.

Anyway, your output should exactly produce the filter you expected in Foton and so the functionality is still good. It's just that the foton expression is not reduced to its simplest form.