-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1.44.scm
28 lines (25 loc) · 1.17 KB
/
1.44.scm
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
;;;; 1.44
;; The idea of *smoothing* a function is an important concept in signal processing. If $f$ is a function and $dx$ is some small number, then the smoothed version of $f$ is the function whose value at a point $x$ is the average of $f(x - dx)$, $f(x)$, and $f(x + dx)$. Write a procedure `smooth` that takes as input a procedure that computes $f$ and returns a procedure that computes the smoothed $f$. It is sometimes valuable to repeatedly smooth a function (that is, smooth the smoothed function, and so on) to obtained the *n-fold smoothed function*. Show how to generate the n-fold smoothed function of any given function using `smooth` and `repeated` from exercise 1.43.
;;; Answer
(load "1.43.scm")
(define dx 0.00001)
(define (smooth f)
(lambda (x)
(/ (+ (f (- x dx)) (f x) (f (+ x dx)))
3)))
(define (n-smooth f n)
((repeated smooth n) f))
;; Test
(define fold-values '(1 2 3 4 5 6 7 8 9 10))
(define (display-fold n)
(define (step x) (cond ((< x 0) 0.) (else 1.)))
(display "n=")
(display n)
(display "\t f(0)=")
(display ((n-smooth step n) 0))
(newline))
(display "Smoothing `step` with dx=")
(display dx)
(newline)
(map display-fold
fold-values)