87 lines
2.5 KiB
Scheme
87 lines
2.5 KiB
Scheme
(load "util.scm")
|
|
|
|
(define (integral integrand initial-value dt)
|
|
(define int
|
|
(cons-stream initial-value
|
|
(add-streams (scale-stream integrand dt)
|
|
int)))
|
|
int)
|
|
|
|
(display "\nex-3.73 - RC\n")
|
|
|
|
(define (RC R C dt)
|
|
(define (rc-proc i v0)
|
|
(add-streams
|
|
(scale-stream i R)
|
|
(integral (scale-stream i (/ 1 C)) v0 dt)))
|
|
rc-proc)
|
|
|
|
(define RC1 (RC 5 1 0.5))
|
|
|
|
(assert (take 5 (RC1 ones 4.2))
|
|
'(9.2 9.7 10.2 10.7 11.2))
|
|
|
|
(display "\nex-3.74 - zero-crossings\n")
|
|
|
|
(define sense-data
|
|
(list->stream
|
|
'(1 2 1.5 1 0.5 -0.1 -2 -3 -2 -0.5 0.2 3 4)))
|
|
(define response '(0 0 0 0 0 -1 0 0 0 0 1 0))
|
|
|
|
(define (sign-change-detector s2 s1)
|
|
(cond
|
|
((and (< s1 0) (>= s2 0)) 1)
|
|
((and (>= s1 0) (< s2 0)) -1)
|
|
(else 0)))
|
|
|
|
(define (make-zero-crossings input-stream last-value)
|
|
(cons-stream
|
|
(sign-change-detector (stream-car input-stream) last-value)
|
|
(make-zero-crossings (stream-cdr input-stream)
|
|
(stream-car input-stream))))
|
|
|
|
(define zero-crossings (make-zero-crossings sense-data 0))
|
|
|
|
(assert (take 12 zero-crossings) response)
|
|
|
|
(define zero-crossings
|
|
(stream-map sign-change-detector
|
|
sense-data
|
|
(cons-stream 0 sense-data)))
|
|
|
|
(assert (take 12 zero-crossings) response)
|
|
|
|
(display "\nex-3.75 - averaged zero-crossings\n")
|
|
|
|
; Louis' solution uses the average value as last-value which means that the new
|
|
; value and the previous average are used for averaging and not simply two
|
|
; consecutive values as proposed by Alyssa.
|
|
|
|
(define (make-zero-crossings-avg input-stream last-value last-avpt)
|
|
(let ((avpt (/ (+ (stream-car input-stream) last-value) 2)))
|
|
(cons-stream (sign-change-detector avpt last-avpt)
|
|
(make-zero-crossings-avg (stream-cdr input-stream)
|
|
(stream-car input-stream)
|
|
avpt))))
|
|
|
|
(display (take 12 (make-zero-crossings-avg sense-data 0 0))) (newline)
|
|
|
|
(display "\nex-3.76 - smooth zero-crossings\n")
|
|
|
|
(define (smooth xs)
|
|
(stream-map average xs (stream-cdr xs)))
|
|
|
|
(define (make-zero-crossings-smoothed input-stream last-value)
|
|
(make-zero-crossings (smooth input-stream) last-value))
|
|
|
|
(assert (take 11 (make-zero-crossings-smoothed sense-data 0))
|
|
'(0 0 0 0 0 -1 0 0 0 0 1))
|
|
|
|
(display "\nex-3.77\n")
|
|
|
|
;(display "\nex-3.78\n")
|
|
;(display "\nex-3.79\n")
|
|
;(display "\nex-3.80\n")
|
|
;(display "\nex-3.81\n")
|
|
;(display "\nex-3.82\n")
|