Exercise 1.44 introduces the idea of smoothing a function, a concept from signal processing. The smoothed version of a function f is the function whose value at x is the average of f(x), f(x + dx), and f(x - dx), where dx is some small value.
We're to write a procedure
smooththat takes as its input a procedure that computes f(x) and returns a procedure that computes the smoothed f. Since it is sometimes valuable to repeatedly smooth a function, we also need to show how to generate an n-fold smoothed function using
repeatedprocedure from exercise 1.43. The exercise doesn't specify a value for dx, so we'll pass that in as a parameter to
smoothprocedure is fairy straightforward to implement, but a little bit complicated to test. Here is the implementation:
(define (smooth f dx)
(/ (+ (f x)
(f (+ x dx))
(f (- x dx)))
But what should we use for test data? It would be nice to have some independent data to use to verify that the function works, but we don't have any. We can create some using a spreadsheet though (click the image to enlarge).
You can see from the image the affects of smoothing on the sine wave function. It's already a smooth function, so instead of the intended affect of smoothing out noise, we've just damped the function. Still, this gives us data to test with. Here's the output of our smooth function at 90 and 180 degrees.
> (sin (/ pi 2))
> ((smooth sin 0.7) (/ pi 2))
> (sin pi)
> ((smooth sin 0.7) pi)
To complete the exercise we need to write an n-fold smooth function using the
(define (n-fold-smooth f dx n)
(repeated (smooth f dx) n))
Repeatedly smoothing the sine function in this way should have the affect of simply damping the function even more.
> ((n-fold-smooth sin 0.7 2) (/ pi 2))
> ((n-fold-smooth sin 0.7 3) (/ pi 2))
> ((n-fold-smooth sin 0.7 4) (/ pi 2))
For links to all of the SICP lecture notes and exercises that I've done so far, see The SICP Challenge.