# Converting Python TransferFunction objects to Foton expression¶

In this tutorial, we will demonstrate how to use kontrol.foton to convert transfer functions defined in Python to Foton expressions like zpk([1;2;3],[4;5;6+i*7;6-i*7],8).

We will use the following transfer function for an example

$$\frac{s(s+1)}{s^2+0.01\pi s + \pi^2}\,.$$

For this transfer function, we should expect to see two zeros at 0 and 1 rad/s and two complex poles at $$\pi$$ rad/s.

Update 2021-10-22: * kontrol.TransferFunction has a method kontrol.TransferFunction.foton() and it calls kontrol.foton.tf2foton(). * kontrol.foton.tf2foton() now works with transfer functions with order higher than 20.

[1]:

import control
import numpy as np

s = control.tf("s")
tf = s*(s+1) / (s**2 + 0.01*np.pi*s + np.pi**2)
tf

[1]:

$$\frac{s^2 + s}{s^2 + 0.03142 s + 9.87}$$
[2]:

import kontrol

## By default, it uses zpk([...],[...],...,"s") expression.
foton_expression = kontrol.foton.tf2foton(tf)
print("Default expression:")
print(foton_expression)
print("")

# We can use other format as well.
zpk_s_expression = kontrol.foton.tf2foton(tf, root_location="s")
zpk_f_expression = kontrol.foton.tf2foton(tf, root_location="f")
zpk_n_expression = kontrol.foton.tf2foton(tf, root_location="n")
print("s format:")
print(zpk_s_expression)
print("")
print("f format:")
print(zpk_f_expression)
print("")
print("n format:")
print(zpk_n_expression)

Default expression:
zpk([0.0;-1.0],[-0.015707963267948967+i*3.1415533834361833;-0.015707963267948967+i*-3.1415533834361833],1.0,"s")

s format:
zpk([0.0;-1.0],[-0.015707963267948967+i*3.1415533834361833;-0.015707963267948967+i*-3.1415533834361833],1.0,"s")

f format:
zpk([0.0;-0.15915494309189535],[-0.0025000000000000005+i*0.49999374996093704;-0.0025000000000000005+i*-0.49999374996093704],1.0,"f")

n format:
zpk([-0.0;0.15915494309189535],[0.0025000000000000005+i*0.49999374996093704;0.0025000000000000005+i*-0.49999374996093704],0.6366197723675814,"n")

[3]:

# rpoly expressions are also supported.
rpoly_expression = kontrol.foton.tf2foton(tf, expression="rpoly")
print("rpoly expression:")
print(rpoly_expression)

rpoly expression:
rpoly([1.0;1.0;0.0],[1.0;0.031415926535897934;9.869604401089358],1.0)

[4]:

## kontrol.TransferFunction.foton calls kontrol.foton.tf2foton as well.
kontrol_tf = kontrol.TransferFunction(tf)
kontrol_tf.foton()

[4]:

'zpk([0.0;-1.0],[-0.015707963267948967+i*3.1415533834361833;-0.015707963267948967+i*-3.1415533834361833],1.0,"s")'

[5]:

## Here's what would happen if we have a transfer function that has 50 order.
tf50 = control.ss2tf(control.rss(50))  # a transfer function with 50 order
kontrol_tf50 = kontrol.TransferFunction(tf50)
print(kontrol_tf50.foton())  # I had to use print() because of the "\n" character.

17:15 Kontrol WARNING : The transfer function has order higher than 20. This is not supported by KAGRA's Foton software. The Foton expression is splitted into multiple expressions with less order.

zpk([-2.5232781398584216+i*-1.6578552610252055;-2.5232781398584216+i*1.6578552610252055;-3.08899987170562;-3.1305747149188345+i*-1.3328021454772787;-3.1305747149188345+i*1.3328021454772787;-2.4578294531318092+i*-2.621198727295867;-2.4578294531318092+i*2.621198727295867;-4.397592333503301;-2.486324679538306+i*-5.128581461306449;-2.486324679538306+i*5.128581461306449;-1.3431047499781774+i*-5.92379606895593;-1.3431047499781774+i*5.92379606895593;-6.495046864962012+i*3.362376271247261;-6.495046864962012+i*-3.362376271247261;-0.33652724968526526+i*-8.322380374882975;-0.33652724968526526+i*8.322380374882975;8.712291884892245;-1.6814969149217118+i*-21.19084243355869;-1.6814969149217118+i*21.19084243355869],[-2.500503530286038+i*-1.553952860252672;-2.500503530286038+i*1.553952860252672;-1.727690252288154+i*2.862072391649727;-1.727690252288154+i*-2.862072391649727;-3.3283972440772964+i*0.6111308684269028;-3.3283972440772964+i*-0.6111308684269028;-3.4912706485895413;-3.2698535096923904+i*1.2721826414281387;-3.2698535096923904+i*-1.2721826414281387;-3.242178746285609+i*1.9580469409934114;-3.242178746285609+i*-1.9580469409934114;-1.8614465171553298+i*5.392114058025007;-1.8614465171553298+i*-5.392114058025007;-0.7335496281463658+i*6.344379182298116;-0.7335496281463658+i*-6.344379182298116;-8.002607517624055;-0.5805231819050503+i*-8.181847421712298;-0.5805231819050503+i*8.181847421712298;-0.15834892755829594+i*-17.446193819871674;-0.15834892755829594+i*17.446193819871674],-5.780892575645753,"s")

zpk([-0.8739357214111139+i*-0.1392801468961625;-0.8739357214111139+i*0.1392801468961625;-0.41357350037156704+i*-0.8001291238721325;-0.41357350037156704+i*0.8001291238721325;-1.0454068191343315+i*-0.2940905095536843;-1.0454068191343315+i*0.2940905095536843;-0.8652618296485356+i*0.9417424590792935;-0.8652618296485356+i*-0.9417424590792935;-1.2719900596330618+i*0.48060233083714443;-1.2719900596330618+i*-0.48060233083714443;-1.689455577848595;-1.5849116262047915+i*0.732604937149256;-1.5849116262047915+i*-0.732604937149256;-1.7678307145549357+i*1.0581264346548764;-1.7678307145549357+i*-1.0581264346548764;-2.285895014964026+i*-0.974496775394504;-2.285895014964026+i*0.974496775394504;-2.8918900148104845+i*-0.5670722918281813;-2.8918900148104845+i*0.5670722918281813],[-0.10581552605457377+i*0.7365524321584936;-0.10581552605457377+i*-0.7365524321584936;-0.8046928914520258+i*-0.18663276619070576;-0.8046928914520258+i*0.18663276619070576;-1.0167045842236258+i*-0.06615688617090629;-1.0167045842236258+i*0.06615688617090629;-1.0132075291444036+i*-0.39426302730475454;-1.0132075291444036+i*0.39426302730475454;-0.9454354544243626+i*-0.8842732060091496;-0.9454354544243626+i*0.8842732060091496;-1.2444970761952148+i*0.6655945059474119;-1.2444970761952148+i*-0.6655945059474119;-1.4599785832209897+i*0.9537914374060632;-1.4599785832209897+i*-0.9537914374060632;-1.9996186001182188;-1.7969739271834022+i*-1.0348398411268747;-1.7969739271834022+i*1.0348398411268747;-2.4184174535006457+i*-1.2270125614293188;-2.4184174535006457+i*1.2270125614293188],0.9999999999999996,"s")

zpk([-0.1568765386692976+i*0.005939423033734596;-0.1568765386692976+i*-0.005939423033734596;-0.1961515696207522;-0.21192004686466182;-0.2901688223277827;-0.30849945886679175;-0.4402149752774941+i*0.01777131711622888;-0.4402149752774941+i*-0.01777131711622888;-0.5187034676512221;-0.7324418426101729+i*0.02510872775750683;-0.7324418426101729+i*-0.02510872775750683],[-0.08905845021584304;-0.1593575865865395;-0.19601582228771403;-0.2007364391312313;-0.2121933180602774;-0.30695297537610594;-0.31111400804563527;-0.4179591149625443;-0.44111646392203147;-0.6514863285403999+i*0.05733964679335237;-0.6514863285403999+i*-0.05733964679335237],1.0,"s")