ThinkDSP
This notebook contains solutions to exercises in Chapter 5: Autocorrelation
Copyright 2015 Allen Downey
Exercise 1
If you did the exercises in the previous chapter, you downloaded the historical price of BitCoins and estimated the power spectrum of the price changes. Using the same data, compute the autocorrelation of BitCoin prices. Does the autocorrelation function drop off quickly? Is there evidence of periodic behavior?
Here's the autocorrelation function using the statistical definition, which unbiases, normalizes, and standardizes; that is, it shifts the mean to zero, divides through by standard deviation, and divides the sum by N.
The ACF drops off slowly as lag increases, suggesting some kind of pink noise.
We can compare my implementation of autocorr
with np.correlate
, which uses the definition of correlation used in signal processing. It doesn't unbias, normalize, or standardize the wave.
The second half of the result corresponds to positive lags:
We can standardize the results after the fact by dividing through by lengths
:
Now we can compare the two.
Even after standardizing, the results look substantially different.
For this dataset, the statistical definition of ACF is probably more appropriate.
Exercise 2
The example code in chap05.ipynb
shows how to use autocorrelation to estimate the fundamental frequency of a periodic signal. Encapsulate this code in a function called estimate_fundamental
, and use it to track the pitch of a recorded sound.
To see how well it works, try superimposing your pitch estimates on a spectrogram of the recording.
I'll use the same example from chap05.ipynb
. Here's the spectrogram:
And here's a function that encapsulates the code from Chapter 5. In general, finding the first, highest peak in the autocorrelation function is tricky. I kept it simple by specifying the range of lags to search.
Here's an example of how it works.
And here's a loop that tracks pitch over the sample.
The ts
are the mid-points of each segment.
Here's the pitch-tracking curve superimposed on the spectrogram:
Looks pretty good!