-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rkt
409 lines (268 loc) · 9.83 KB
/
main.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#|
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)
;for testing:
;(require "math_library/general.rkt")
;(require "math_library/numerical_analysis.rkt")
;(require "math_library/symbolic_algebra.rkt")
(require "elements/general.rkt")
(require "elements/block.rkt")
(require "elements/tf.rkt")
(require "elements/adder.rkt")
(require "util/metrics.rkt")
(require "util/display_modes.rkt")
(require "features/total_tf_parsing.rkt")
(require "features/characteristic_numbers.rkt")
(require "features/plot_freq_domain.rkt")
(require "features/plot_time_domain.rkt")
(require "circuits.rkt")
(provide (all-from-out "elements/general.rkt"))
(provide (all-from-out "elements/block.rkt"))
(provide (all-from-out "elements/tf.rkt"))
(provide (all-from-out "elements/adder.rkt"))
(provide (all-from-out "util/metrics.rkt"))
(provide (all-from-out "util/display_modes.rkt"))
(provide (all-from-out "features/total_tf_parsing.rkt"))
(provide (all-from-out "features/characteristic_numbers.rkt"))
(provide (all-from-out "features/plot_freq_domain.rkt"))
(provide (all-from-out "features/plot_time_domain.rkt"))
(provide (all-from-out "circuits.rkt"))
(provide (all-defined-out))
(define-namespace-anchor n_anchor)
(define anchor (namespace-anchor->namespace n_anchor))
;initialize a, b, c, d blocks
(define a (block))
(define b (block))
(define c (block))
(define d (block))
(newline)
; ////////// Examples //////////
(define examples_list
'(
(define (displayc s)
(newline)
(newline)
(display (make-string 52 #\_))
(newline)
(newline)
(display s)
(newline))
;I. simplification examples:
(displayc "> (bode (circuit1 a))")
(bode (circuit1 a))
(displayc "> (bode (circuit2 a))")
(bode (circuit2 a))
(displayc "> (bode (circuit3 a))")
(bode (circuit3 a))
(displayc "> (bode a)")
(define tf1 (tf '(1) '(0.3 0.1 1) a))
(bode a) ;the value of the block's total tf is that of the latest installed
;(bode (feedback-loop-test1 a))
;(bode (multiple-outputs-test1 a))
;(bode (serial-adders-test1 a))
;(bode (parallel-tfs-test1 a))
;II. elementary blocks:
(displayc "> (bode (phase-delay-circuit a))")
(bode (phase-delay-circuit a))
(displayc "> (bode (integrator a))")
(bode (integrator a))
(displayc "> (bode (sine a))")
(bode (sine a))
(displayc "> (bode (pi-controller 5 8 a))")
(bode (pi-controller 5 8 a))
(displayc "> (bode (pd-controller 5 8 a))")
(bode (pd-controller 5 8 a))
(displayc "> (bode (pid-controller 5 8 4 a))")
(bode (pid-controller 5 8 4 a))
(displayc "> (bode (pid-controller 600 100 3 a))")
(bode (pid-controller 600 100 3 a))
(displayc "> (evolve (pid-controller 6 7 3 a))")
(evolve (pid-controller 6 7 3 a))
(displayc "> (evolve (pid-controller 6 17 3 a))")
(evolve (pid-controller 6 17 3 a))
;III. by function:
;F:
(displayc "> (F a)")
(define sinx (tf '(1) '(1 0 1 0) a))
(F a) ;not: (F sinx)
(displayc "> (F (integrator a))")
(F (integrator a))
;Bode
(displayc "> (bode a)")
;(bode (tf '(1) '(1 0) a)) ;it doesn't work this way
(define tf1 (tf '(1) '(1 0) a)) ;it works
(bode a)
(displayc "> (bode (chebyshev-type1 4 8 1 a))")
(bode (chebyshev-type1 4 8 1 a))
(displayc "> (bode (chebyshev-type1 6 1 1 a))")
(bode (chebyshev-type1 6 1 1 a))
;(displayc "> (bode b)")
;(define tf1 (tf '(2 3) '(1 0) b))
;(define tf2 (tf '(4 3 6) '(2 1) b))
;(connect tf1 tf2)
;(bode b)
(displayc "> (bode a)")
(define tf1 (tf '(2 3) '(1 0) b))
(define tf2 (tf '(4 3 6) '(2 1) a))
(connect tf1 tf2)
(bode a)
(displayc "> (bode a)")
(define tf1 (tf '(5 0) '(1 1 1) a)) ;band-pass filter
(bode a)
(displayc "> (bode a)")
(define tf1 (tf '(-0.1 -0.2 -0.1) '(1 0 -0.1) a))
(bode a)
(displayc "> (bode a)")
(define tf1 (tf '(1 0 0 0) '(1) a))
(bode a)
(displayc "> (bode a)")
(define tf1 (tf '(1 0 1) '(1) a))
(bode a)
(displayc "> (bode a)")
(define tf1 (tf '(1) '(1 0 1) a))
(bode a)
(displayc "> (bode a)")
(define tf1 (tf '(1) '(1 0 0 0) a))
(bode a)
(displayc "> (bode a)")
(define tf1 (tf '(1 1.25 2) '(0.25 0) a))
(bode a)
(displayc "> (bode a)")
(define tf1 (tf '(0.05 0.05 10) '(0.1 0.8 1 1 0.1) a))
(bode a)
(displayc "> (bode a)")
(define tf1 (tf '(0.05 0.05 0.5) '(0.1 0.8 1 1 0.1) a))
(bode a)
;Compare
(displayc "> (compare c1 c2)")
(define c1 (block))
(define c2 (block))
(define tf1 (tf '(1) '(1 0 1) c1)) ;could use a and b which are already defined
(define tf2 (tf '(5) '(1 0 1) c2))
(compare c1 c2)
;works because the pid functions return the block
(displayc "> (compare (pid-controller 6 7 3 a) (pi-controller 7 8 b))")
(compare (pid-controller 6 7 3 a) (pi-controller 7 8 b))
;adding f(w) functions of the frequency w:
(displayc "> (compare a b)")
(define tf1 (tf '(0.02 fw1) '(1) a))
(define tf2 (tf '(fw2) '(1) b)) ;comb filter
(compare a b)
;doesn't work - compare takes as inputs blocks, not functions
;- tf function returns the transfer function not the block
;(compare (tf '(0.02 fw1) '(1) a) (tf '(fw2) '(1) b))
;doesn't work since the two functions are defined in the same block:
;(define tf1 (tf '(1) '(1 0 1) a))
;(define tf2 (tf '(5) '(1 0 1) a))
;(compare a a)
;Tune
(displayc "> (tune (pi-controller 5 'y a) '(= AR 140) 0.01)")
(tune (pi-controller 5 'y a) '(= AR 140) 0.01)
;Nyquist
(displayc "> (nyquist (circuit3 a))")
(nyquist (circuit3 a))
;block re-initialization:
;if the elements stored inside a block will no longer be used, it can be manually re-initialized
;
;warning: errors may arise if previously stored elements are attempted to be used again
(re-initialize-block! a)
;Step
(displayc "> (step (sine a) 5)")
(step (sine a) 5)
(define tf1 (tf '(-8 5) '(0.4 2.2 1) a))
(define tf2 (tf '(1) '(0.3 0.1 1) b))
(displayc "> (step a 5)")
(step a 5) ;inverse response
(displayc "> (step b 5)")
(step b 5) ;with damping
;doesn't work, unless raising the lower limit of the first function being evaluated inside step to 1
;(define tf3 (tf '(1) '(1 0 1) a))
;(define delay1 (tf '((/ 1 (exp (* s 1)))) '(1) a)) ;or try: (define delay1 (tf '(fw4) '(1) a))
;(connect delay1 tf3)
;(step a 1)
;Impulse
(displayc "> (impulse (sine a))")
(impulse (sine a))
;Trajectory
(displayc "> (trajectory (sine a))")
(trajectory (sine a))
(displayc "> (trajectory a)")
(define tf1 (tf '(1) '(0.3 0.1 1) a))
(trajectory a) ;with damping
;Delay addition
;Method I: using Pade functions:
(displayc "> (step (pade-delay 2 (sine a)) 2)")
(step (pade-delay 2 (sine a)) 2)
;(step (pade-delay 1 (pade-delay 1 (sine a))) 2) ;that also works, but is very slow with pade[6/6]
;Method II: using f(w) functions - they work only in the s-domain:
;(define tf1 (tf '((* 5 fw3)) '(1 1) a))
;(bode a)
;Methods comparison:
(displayc "> (compare (pade-delay 1 (sine b)) a)")
(define tf1 (tf '(1) '(1 0 1) a))
(define fw-delay1 (tf '(fw3) '(1) a)) ;fw3 is defined in: ../features/text_generation.rkt
(connect fw-delay1 tf1)
(compare (pade-delay 1 (sine b)) a)
#|
;VI. abstraction using blocks:
;serial connections:
(define a1 (block a))
(define a2 (block a))
(define tf1 (tf '(1) '(0.3 0.1 1) a1))
(define tf2 (tf '(1) '(5 0.1 1) a1))
(connect tf1 tf2)
(bode a1)
(define tf3 (tf '(1) '(5 0.1 1) a2))
(connect a1 a2)
;(set! display-mode 'test)
;(simplify a)
(bode a)
(compare a a1)
;feedback loop:
(define a1 (block a))
(define a2 (block a))
(define tf1 (tf '(1) '(0.3 0.1 1) a1)) ;also try this: (define tf1 (tf '(1) '(0.3 fw1 1) a1))
(define tf2 (tf '(1) '(6 0.1 1) a1))
(connect tf1 tf2)
(bode a1)
(define tf3 (tf '(1) '(5 0.1 1) a2))
(connect a1 a2)
;(bode a) ;10^6
(define a3 (block a))
(define tf4 (tf '(1) '(4 0.1 1) a3))
(connect a2 a3)
;(bode a) ;10^8
(define a4 (block a))
(define tf5 (tf '(1) '(1) a4))
(connect a2 a4)
(define add1 (make-adder a))
(connect add1 a1) ;must put an adder in front of it
(connect a3 add1)
;(set! display-mode 'test)
;(simplify a)
(bode a)
(compare a a1)
|#
))
; run the examples
(define (run-examples . speed)
(if (and (not (null? speed)) (eq? (car speed) 'slow))
(begin (map (λ (x)
(eval x anchor)
(sleep 5))
examples_list) 'all_examples_run)
(begin (map (λ(x) (eval x anchor)) examples_list)
'all_examples_run)))
;(run-examples)