# Load modules we'll need.
from scipy.io import loadmat
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import spectrogram
Analyzing Rhythms Part 2b (Autocovariance)
# Load the data.
= loadmat('AC_Example.mat') # Load the data,
data # Data available here
# https://github.com/Mark-Kramer/BU-MA665-MA666/tree/master/Data
#
= data['d'][0] # ... from the first electrode.
d = data['t'][0] # Load the time axis
t = np.size(d,0) # Store number of observations.
N = t[1]-t[0] # Store sampling interval. dt
Load the data and look at it.
Q. Do you see rhythms?
Conclusions
# Code to compute the spectrum.
Compute the spectrum.
Q. What do you find? What rhythms are present in the data?
Conclusions
# Code to compute the autocovariance.
Compute the autocovariance.
Q. What do you find? Is it consistent with the spectrum?
Conclusions
Example: Spectrum = FT{Autocovariance}
Make a simple signal.
= 0.001
dt = 1000
N = np.arange(0,N)*dt
t = np.sin(2*np.pi*10*t) + np.random.randn(N) d
Compute the autocovariance with a modifications: circular-shift the data
= [];
rxx = np.arange(-int(N/2),int(N/2));
lags for idx,L in enumerate(lags):
= np.append(rxx, 1/N*np.sum(np.roll(d,L) * d))
rxx
plt.plot(lags, rxx)'Lags [indices]');
plt.xlabel('Autocovariance rxx'); plt.ylabel(
Compute the spectrum via FT and directly from the data.
# Compute the spectrum from the FT{rxx}
= 2*dt*np.fft.fft(rxx)
Sxx_via_rxx
# Compute the spectrum from the data.
= t[-1];
T = np.fft.fft(d);
xf = 2 * dt ** 2 / T * (xf * xf.conj())
Sxx
10*np.log10(Sxx))
plt.plot(10*np.log10(Sxx_via_rxx), 'o')
plt.plot('Freq Index')
plt.xlabel('Direct Sxx', 'Sxx via rxx'})
plt.legend({0,150]); plt.xlim([