rename symbol q to symbol p
[liba.git] / python / test / tf.py
bloba2747f8fe7dc2e7f6f42fc6ee4ea6ab439be917d
1 #!/usr/bin/env python
2 import os, sys
4 sys.path.insert(0, os.getcwd())
5 prefix = os.path.join(sys.path[0], "build")
6 if not os.path.exists(prefix):
7 os.mkdir(prefix)
8 try:
9 import liba as a
10 import numpy as np
11 except Exception as e:
12 print(e)
13 exit()
16 class TF:
17 def num_get(self):
18 return self._num
20 def num_set(self, num):
21 self._num = np.array(num, dtype=float)
22 self.input = np.array(len(num) * [0.0], dtype=float)
24 num = property(num_get, num_set, None, None)
26 def den_get(self):
27 return den
29 def den_set(self, den):
30 self._den = np.array(den, dtype=float)
31 self.output = np.array(len(den) * [0.0], dtype=float)
33 den = property(den_get, den_set, None, None)
35 def __init__(self, num, den):
36 self.num = num
37 self.den = den
39 def __call__(self, input: float) -> float:
40 self.input = np.roll(self.input, 1)
41 self.input[0] = input # type: ignore
42 output = self._num @ self.input - self._den @ self.output # type: ignore
43 self.output = np.roll(self.output, 1)
44 self.output[0] = output # type: ignore
45 return output
48 Ts = 0.001
49 data = np.arange(0, 0.2, Ts)
51 try:
52 import control.matlab as ct
54 sysc = ct.tf(133, [1, 25, 0])
55 sysd = ct.c2d(sysc, Ts)
56 print(sysc, sysd)
58 [[num]], [[den]] = ct.tfdata(sysd)
59 except ModuleNotFoundError:
60 num = [6.59492796e-05, 6.54019884e-05]
61 den = [1.0, -1.97530991, 0.97530991]
62 except Exception as e:
63 print(e)
64 exit()
66 tf = a.tf(num, den[1:])
67 tf_ = TF(num, den[1:])
69 for t in data:
70 t = t * 1000
71 tf_(t)
72 tf(t)
73 print(tf_.num, tf_.den, tf_.input, tf_.output)
74 print(tf.num, tf.den, tf.input, tf.output)