In [1]:
import numpy as np
import sympy as sy
from sympy import I, exp
from sympy.utilities.codegen import codegen
In [12]:
z = sy.symbols("z", real=False)
r1,r2,r3, r4, r5, s0,s1, s2,s3, s4, s5 = sy.symbols("r1,r2,r3,r4, r5, s0,s1,s2,s3,s4, s5", real=True)
a, h = sy.symbols("a, h", real=True)
In [25]:
pd1 = 0.4
pd2 = pd1
hh = 0.2
aa = 0
# Will need characteristic polynomial of order 9, since in the left hand side the order of
# the polynomial will be n_A + n_R = 5+4 = 9. R and S will then have 4+5=9 unknowns
Ac = sy.poly(z**3*(z - pd1)*(z-pd2), z )
#Ao = sy.poly(((z-a)**4).subs(a, aa), z )
Ao = sy.poly(((z-a)**4), z )
Acl = Ac*Ao
# Assume anti-aliasing filter corresponding to a delay of two sample periods + extra delay of 1 sample period
A = sy.poly( (z**3*(z-1)*(z-exp(h))).subs(h,hh), z )
B = sy.poly( ((-1+exp(h)-h)*z + 1-(1-h)*exp(h)).subs(h,hh), z)
print A
print B
print Ao
print Ac
In [26]:
R = sy.poly(z**4 + r1*z**3 + r2*z**2 + r3*z**1 + r4, z)
S = sy.poly(s0*z**4 + s1*z**3 + s2*z**2 + s3*z**1 + s4, z)
print A*R+B*S
print Acl
dioph=(A*R+B*S-Acl).all_coeffs()
In [27]:
sol=sy.solve(dioph, (r1,r2, r3, r4, r5, s0,s1,s2, s3, s4, s5))
print sol
In [24]:
print len(dioph)
print dioph
In [28]:
# Reorganize solution expression for matlab code generation
sol_expr = ('RST_example_aliasing', [sol[r1], sol[r2], sol[r3], sol[r4], sol[s0], sol[s1], sol[s2], sol[s3], sol[s4], Ac.subs(z, 1)/B.subs(z,1), a])
In [29]:
# Export to matlab code
[(m_name, m_code)] = codegen(sol_expr, 'octave')
In [30]:
print m_code
with open("/home/kjartan/Dropbox/undervisning/tec/MR2007/matlab/PS4_ship_heading_RST_example_aliasing_3h.m", "w") as text_file:
text_file.write(m_code)
In [20]:
sy.expand((z-a)**3)
Out[20]:
In [ ]: