Dan's Thoughts Thinking somewhat carefully

14Jul/090

Exercise 1.44 of SICP

Exercise 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.

(define (compose f g)
  (lambda (x) 
    (f (g x))))
(define (repeated fn n)
  (if (= n 1)
    fn
    (compose 
      fn (repeated fn (- n 1)))))
(define (smooth f)
  (define (average a b c)
    (/ (+ a b c) 3))
  (let ((dx 0.00001))
  (lambda (x)
    (average (f (- x dx)) (f x) (f (+ x dx))))))
 
(define (n-fold-smooth f n)
    (if (not (> n 0))
      f
      (let ((smoothed-n (repeated smooth n)))
        (smoothed-n f))))
 
(define (S x)
  (define (>= a b) (not (< a b)))
  (cond ((< x 0) 0.0)
        ((>= x 0) 1.0)))

S is a modified (the argument is not floored) Heaviside step function.
> ((n-fold-smooth S 0) 0)
1
> ((n-fold-smooth S 1) 0)
.6666666666666666
> ((n-fold-smooth S 15) 0)
.5409601581500249

Filed under: lisp, math, SICP Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

Spam protection by WP Captcha-Free

No trackbacks yet.