-
Notifications
You must be signed in to change notification settings - Fork 2
/
circuits.rkt
366 lines (231 loc) · 8.04 KB
/
circuits.rkt
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#|
lti-freq-domain-toolbox | Functions for studying LTI (linear time-invariant) dynamical systems
Copyright (C) 2014-2023 Ioannis Stefanis
This file is part of lti-freq-domain-toolbox.
lti-freq-domain-toolbox is free software: you can redistribute it and/or modify it under the terms of
the GNU General Public License Version 3 as published by the Free Software Foundation.
lti-freq-domain-toolbox is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License Version 3 for more details.
You should have received a copy of the GNU General Public License Version 3 along with lti-freq-domain-toolbox.
If not, see <https://www.gnu.org/licenses/>.
|#
#lang racket
(require math/base)
(require math/number-theory)
(require "math_library/general.rkt")
(require "math_library/symbolic_algebra.rkt")
(require "elements/general.rkt")
(require "elements/block.rkt")
(require "elements/tf.rkt")
(require "elements/adder.rkt")
(require "features/plot_freq_domain.rkt")
(provide (all-defined-out))
; ////////// D1. Predefined circuits //////////
; //// Basic components:
(define (integrator block1)
(define tf1 (tf '(1) '(1 0) block1))
block1)
(define (sine block1)
(define tf1 (tf '(1) '(1 0 1) block1))
block1)
(define (phase-delay-circuit block1)
(define tf1 (tf '(5 1) '(8 1) block1))
block1)
; //// Controllers:
(define (pi-controller kp ki block1)
(define tf1 (tf (list kp ki) ; writen this way so as to accept symbols (and so work with tune)
(list 1 0)
block1))
block1)
(define (pd-controller kp kd block1)
(define tf1 (tf (list kd kp)
'(1)
block1))
block1)
(define (pid-controller kp ki kd block1) ; kp=proportional gain, ki=integral gain, kd=derivative gain
(define tf1 (tf (list kd kp ki)
(list 1 0)
block1))
block1)
; //// Chebyshev filters:
(define (chebyshev-type1 n e w0 block1) ; n=polynomial order, e=ripple factor, w0=cutoff frequency
(define (theta m)
(/ (* pi (- (* 2 m) 1)) (* 2 n)))
(define (spm m)
(+ (- (* (sinh (/ (asinh (/ 1 e)) n))
(sin (theta m))))
(* (make-rectangular 0 1.0) (cosh (/ (asinh (/ 1 e)) n)) (cos (theta m)))))
(set-chebyshev-threshold! (/ 1 (sqrt (+ 1 (* e e)))))
;(define b (block))
;(define tf0 (tf '(1) (list (* (expt 2 (- n 1)) e)) b))
(define tf0 (tf '(1) (list (* (expt 2 (- n 1)) e)) block1))
;(define tf1 (tf '(1) (list 1 (- (spm 1))) b))
;(define tf2 (tf '(1) (list 1 (- (spm 2))) b))
;(define tf3 (tf '(1) (list 1 (- (spm 3))) b))
(define (connection-function tfm m)
(when (< m n)
(let ;((f1 (tf '(1) (list 1 (- (spm (+ m 1)))) b)))
((f1 (tf '(1) (list 1 (- (spm (+ m 1)))) block1)))
(connect tfm f1)
(connection-function f1 (+ m 1)))))
(connection-function tf0 0)
block1)
; //// Delay components:
; approximating e-sT using pade functions
; (the arithmetic methods fail when trying to model delay by just adding e-sT as a tf):
#|
(define (pade5 T)
(tf (list (* T T T T)
(* -120 T T T)
(* 1260 T T)
(* -6720 T)
15120)
(list (* T T T T T)
(* 25 T T T T)
(* 300 T T T)
(* 2100 T T)
(* 8400 T)
15120)))
|#
(define (pade m n T block1) ; the higher the m,n, the wider the region of convergence
; the values returned for the greater values of i are rather small,
; so must be multiplied by an appropriate coefficient:
(define coeff (/ 1000000 (expt T 5)))
(define (p i)
; 100
(/ (* coeff
(expt -1 i)
(factorial (+ m n (- 0 i)))
(factorial m))
(* (factorial (+ m n))
(factorial i)
(factorial (- m i)))))
(define (q i)
(/ (* coeff
(factorial (+ m n (- 0 i)))
(factorial n))
(* (factorial (+ m n))
(factorial i)
(factorial (- n i)))))
#|
(displayln (p 0))
(displayln (p 1))
(displayln (p 2))
(displayln (p 3))
(displayln (p 4))
(displayln (p 5))
|#
;(displayln (map (λ (x) (* (p x) (expt T x))) (integers m)))
(define tf1 (tf (map (λ (x) (* (p x) (expt T x))) (integers m)) ; integers from 0 to m
(map (λ (x) (* (q x) (expt T x))) (integers n))
block1))
tf1)
(define (pade-delay T block1) ; time T<7 for a good approximation, if using m,n=6
(newline)
(newline)
(displayln "Delay approximated by a Padé [6/6] polynomial")
(define tf2 (pade 6 6 T block1))
(connect tf2 (cadr (get-tfs block1)))
block1)
; ////////// D2. Circuit examples //////////
(define (feedback-loop-test1 block1) ;parent block
(define tf1 (make-tf (make-ratio (make-poly-dense 's '(1 0 1 1 0))
(make-poly-dense 's '(1)))
block1))
(define tf2 (tf '(1 0) '(2 1 0) block1))
(define tf3 (tf '(1 0) '(1 0 1 0) block1))
(define tf4 (tf '(1) '(1 0) block1))
(define tf5 (tf '(1) '(1 0 0) block1))
(define tf6 (tf '(2 0) '(3 0 1) block1))
(define add1 (make-adder block1))
(define add2 (adder block1))
(connect-serially tf1 tf2)
(connect tf2 add1)
(connect add1 tf1)
(connect tf1 add2)
(connect add2 tf3)
(connect tf3 add2)
block1) ; return the block where the elements were stored so as to be passed to simplify
(define (multiple-outputs-test1 block1)
(define tf1 (tf '(1 0 1 1 0) '(1) block1))
(define tf2 (tf '(1 0) '(2 1 0) block1))
(define tf3 (tf '(1 0) '(1 0 1 0) block1))
(define tf4 (tf '(1) '(1 0) block1))
(define tf5 (tf '(1) '(1 0 0) block1))
(define tf6 (tf '(2 0) '(3 0 1) block1))
(define add1 (adder block1))
(define add2 (adder block1))
(connect add1 tf2)
(connect tf2 tf3)
(connect tf2 tf4)
(connect tf2 tf5)
block1)
(define (serial-adders-test1 block1)
(define tf1 (tf '(1 0 1 1 0) '(1) block1))
(define tf2 (tf '(1 0) '(2 1 0) block1))
(define tf3 (tf '(1 0) '(1 0 1 0) block1))
(define tf4 (tf '(1) '(1 0) block1))
(define tf5 (tf '(1) '(1 0 0) block1))
(define tf6 (tf '(2 0) '(3 0 1) block1))
(define add1 (adder block1))
(define add2 (adder block1))
(connect add1 add2)
(connect add2 tf5)
(connect tf5 add1)
block1)
(define (parallel-tfs-test1 block1)
(define tf1 (tf '(1 0 1 1 0) '(1) block1))
(define tf2 (tf '(1 0) '(2 1 0) block1))
(define tf3 (tf '(1 0) '(1 0 1 0) block1))
(define tf4 (tf '(1) '(1 0) block1))
(define tf5 (tf '(1) '(1 0 0) block1))
(define tf6 (tf '(2 0) '(3 0 1) block1))
(define add1 (adder block1))
(define add2 (adder block1))
(connect add1 tf1)
(connect add1 tf2)
(connect add1 tf3)
(connect tf1 add2)
(connect tf2 add2)
(connect tf3 add2)
block1)
(define (circuit1 block1)
(define tf1 (tf '(-10 0 1) '(1 2 1) block1))
block1)
(define (circuit2 block1)
(define tf1 (make-tf (make-ratio (make-poly-dense 's '(2))
(make-poly-dense 's '(1 1)))
block1))
(define tf2 (tf '(1 2 5) '(8) block1))
(connect tf1 tf2)
block1)
(define (circuit3 block1)
(define tf1 (tf '(1 0 0 0 0) '(1 1 0) block1))
(define tf2 (tf '(1 0) '(2 1 0) block1))
(define tf3 (tf '(1 0) '(1 0 1 0) block1))
(define tf4 (tf '(1) '(1 0) block1))
(define tf5 (tf '(1) '(1 0 0) block1))
(define tf6 (tf '(2 0) '(3 0 1) block1))
(define tf7 (tf '(1) '(4 0 0) block1))
(define add1 (adder block1))
(define add2 (adder block1))
(define add3 (adder block1))
(define add4 (adder block1))
(connect add1 tf2)
(connect add1 tf3)
(connect add1 tf4)
(connect tf2 add2)
(connect tf3 add2)
(connect tf3 add3)
(connect tf4 add3)
(connect add2 tf1)
(connect tf1 add1)
(connect add2 tf5)
(connect tf5 add4)
(connect add3 add4)
(connect add4 tf6)
(connect tf6 add4)
(connect tf6 tf7)
(connect tf7 add1)
block1)