summaryrefslogtreecommitdiffstats
path: root/src/control.cpp
blob: 69de3a8200ea48607cb8c9e4ff3f2f08d1f9e227 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#include "control.h"
#include <armadillo>
#include <cassert>
#include <cmath>


namespace ct
{
	TransferFn::TransferFn()
		: num(1)
		, den(1)
	{
		num(0) = 1;
		den(0) = 1;
	}

	TransferFn::TransferFn(math::PolyCx num, math::PolyCx den)
		: num(num)
		, den(den)
	{}

	complex TransferFn::dc_gain() const
	{
		arma::cx_vec z(1, arma::fill::zeros);
		arma::cx_vec n = arma::polyval(num.coeffs, z);
		arma::cx_vec d = arma::polyval(den.coeffs, z);
		return n(0) / d(0);
	}

	bool TransferFn::is_proper() const
	{
		return den.degree() >= num.degree();
	}

	bool TransferFn::is_strictly_proper() const
	{
		return den.degree() >= num.degree();
	}

	TransferFn operator + (const TransferFn& g, const TransferFn& h)
	{
		TransferFn tf(g.num * h.den + h.num * g.den, g.den * g.den);
		tf.canonicalize();
		return tf;
	}

	TransferFn operator - (const TransferFn& g, const TransferFn& h)
	{
		TransferFn tf = g + (-1. * h);
		tf.canonicalize();
		return tf;
	}

	TransferFn operator * (const TransferFn& g, const TransferFn& h)
	{
		TransferFn tf(g.num * h.num, g.den * h.den);
		tf.canonicalize();
		return tf;
	}

	TransferFn operator / (const TransferFn& g, const TransferFn& h)
	{
		TransferFn hinv = TransferFn(h.den, h.num);
		hinv.canonicalize();
		TransferFn tf = g * hinv;
		tf.canonicalize();
		return tf;
	}

	TransferFn operator * (const complex k, const TransferFn& h)
	{
		TransferFn tf(k * h.num, h.den);
		tf.canonicalize();
		return tf;
	}

	TransferFn operator / (const TransferFn& h, const complex k)
	{
		TransferFn tf((1. / k) * h.num, h.den);
		tf.canonicalize();
		return tf;
	}

	TransferFn cancel_zp(const TransferFn& tf, double tol)
	{
		math::PolyCx num(tf.num.coeffs.n_elem);
		math::PolyCx den(tf.den.coeffs.n_elem);
		// TODO: return new tf without poles & zeros that are closer to each other than tol
	}

	TransferFn feedback(const TransferFn& tf, complex k)
	{
		const TransferFn unit(math::PolyCx({1.}), math::PolyCx({1.}));
		const TransferFn ol = k * tf;
		const TransferFn bo = unit - ol;
		const TransferFn fb = unit / bo;
		TransferFn tf_cl = ol * fb;
		// const TransferFn tf_cl = (k * tf) / (unit - k * tf);
		tf_cl.canonicalize();
		return tf_cl;
	}

	void TransferFn::canonicalize()
	{
		complex an = den(0);
		if (std::abs(an) > 0)
		{
			num.coeffs /= an;
			den.coeffs /= an;
		}
	}

	TransferFn& TransferFn::operator = (const TransferFn& other)
	{
		if (this == &other)
			return *this;

		num = math::PolyCx(other.num);
		den = math::PolyCx(other.den);
		return *this;
	}

	LocusSeries::LocusSeries(double start, double end, size_t nsamples)
		: n_samples(nsamples)
		, start(start)
		, end(end)
		, in(arma::logspace(log10(start), log10(end), n_samples))
		, out(1, n_samples, arma::fill::zeros)
	{}

	void rlocus(const TransferFn& tf, LocusSeries& ls)
	{
		using namespace arma;

		CT_ASSERT(tf.is_proper());
		CT_ASSERT(tf.den.degree() > 0);

		// prepare output
		ls.out = cx_mat(tf.den.degree(), ls.n_samples, fill::zeros);

		// compute roots
		for (int i = 0; i < ls.n_samples; i++)
			// FIXME: sort the roots
			ls.out.col(i) = (tf.den + ls.in(i) * tf.num).roots();
	}

	void nyquist(const TransferFn& tf, LocusSeries& ls)
	{
		using namespace arma;
		complex j(0., 1.);
		
		// FIXME missing the countour at infinity and the small contours for poles
		// on the imaginary axis

		// contour from 0 to i\infty
		cx_mat num = polyval(tf.num.coeffs, j * conv_to<cx_vec>::from(ls.in));
		cx_mat den = polyval(tf.den.coeffs, j * conv_to<cx_vec>::from(ls.in));

		if (ls.out.n_rows != ls.n_samples)
			ls.out = cx_mat(1, ls.n_samples);

		for (int i = 0; i < ls.n_samples; i++)
			ls.out(i) = num(i) / den(i);
	}

	SSModel::SSModel(size_t n_in, size_t n_out, size_t n_states)
		: n_in(n_in)
		, n_out(n_out)
		, n_states(n_states)
		, A(n_states, n_states)
		, B(n_states, n_in)
		, C(n_out, n_states)
		, D(n_out, n_in)
	{}

	SSModel ctrb_form(const TransferFn& tf)
	{
		// TODO: change to proper by implementing D
		CT_ASSERT(tf.is_strictly_proper());

		int ord = tf.den.degree();
		SSModel ss(1, 1, ord);

		ss.B(ord - 1) = 1;
		for (int i = 0; i < ord; i++)
		{
			ss.A(ord - 1, i) = tf.den(ord - i);
			if (i < tf.num.coeffs.n_elem)
				ss.C(i) = tf.num(i);
		}
	
		return ss;
	}

	TimeSeries::TimeSeries(double start, double end, size_t n_samples)
		: n_samples(n_samples)
		, start(start)
		, end(end)
		, dt((start - end) / (n_samples - 1))
		, time(arma::linspace(start, end, n_samples))
			// FIXME: do not hardcode SISO
		, in(1, n_samples, arma::fill::zeros)
		, out(1, n_samples, arma::fill::zeros)
	{}

	void response(const SSModel& ss, TimeSeries& ts)
	{
		using namespace arma;

		CT_ASSERT(ts.in.n_rows == ss.n_in);
		CT_ASSERT(ts.in.n_cols == ts.n_samples);

		ts.out = cx_mat(ss.n_out, ts.n_samples, fill::zeros);
		ts.state = cx_mat(ss.n_states, ts.n_samples, fill::zeros);
		// FIXME: non-homogeneous initial condition

		// For the current application we want this to be faster rather than
		// accurate. Hence we use a simulation with ZOH input since it is cheap and
		// probably good enough.
		// FIXME: do not invert matrix like that, but I am too lazy to implement
		// regularization
		const int n_steps = 3;
		const cx_mat Ad = expmat(ss.A * ts.dt / n_steps);
		const cx_mat Bd = (Ad - eye(size(Ad))) * inv(ss.A) * ss.B;

		cx_vec x(ss.n_states), xn;
		for (int k = 0; k < ts.n_samples - 1; k++)
		{
			x = ts.state.col(k);
			for (int l = 0; l < n_steps; l++)
				xn = Ad * x + Bd * ts.in.col(k);
			ts.state.col(k + 1) = xn;
		}

		ts.out = ss.C * ts.state + ss.D * ts.in;
	}


	void step(const SSModel& ss, TimeSeries& ts)
	{
		ts.in = arma::cx_mat(ss.n_in, ts.n_samples);
		ts.in.fill(1.);
		response(ss, ts);
	}
}

// vim: ts=2 sw=2 noet: