SICP Exercise 3.75 zero-crossings on noisy signal

Exercise 3.75.  Unfortunately, Alyssa's zero-crossing detector in exercise 3.74 proves to be insufficient, because the noisy signal from the sensor leads to spurious zero crossings. Lem E. Tweakit, a hardware specialist, suggests that Alyssa smooth the signal to filter out the noise before extracting the zero crossings. Alyssa takes his advice and decides to extract the zero crossings from the signal constructed by averaging each value of the sense data with the previous value. She explains the problem to her assistant, Louis Reasoner, who attempts to implement the idea, altering Alyssa's program as follows:

(define (make-zero-crossings input-stream last-value)
  (let ((avpt (/ (+ (stream-car input-stream) last-value) 2)))
    (cons-stream (sign-change-detector avpt last-value)
                 (make-zero-crossings (stream-cdr input-stream)
                                      avpt))))


This does not correctly implement Alyssa's plan. Find the bug that Louis has installed and fix it without changing the structure of the program. (Hint: You will need to increase the number of arguments to make-zero-crossings.)

SOLUTION

Explanation: Louis Reasoner's logic does not average the right quantities. Instead of averaging each value of the sensor data with the previous value, it averages each value with the previous *average*. So the smoothed signal will gradually drift away from the raw sensor signal.

The attached graphs show how the different signals compare with each other. The first graph shows what happens when we do the two types of average-smoothing of a stream of random values. We can see that my implementation more closely tracks the raw signal than Louis' implementation.

The second graph shows the same graphs for a values produced by a parabolic function:

f(x) = x^2 - 15

It is easy to see how Louis's stream of average values gradually diverges from the 
raw signal whereas my average stream tracks the raw stream more closely.

The code and tests are here.





Comments

Popular posts from this blog

SICP Exercise 2.56 differentiation rule

SICP Exercise 1.28 (Miller-Rabin Test)

SICP Exercise 4.18 a alternative strategy for interpreting internal definitions