from scipy.io import loadmat
import matplotlib.pyplot as plt
import numpy as np
from scipy.signal import spectrogram
Analyzing Rhythms Part 1
Load the data and look at it.
Load modules we’ll need.
Load the data.
= loadmat('Data/EEG-1.mat') # Load the EEG data
data # Data available here
# https://github.com/Mark-Kramer/BU-MA665-MA666/tree/master/Data
#
= data['EEG'].reshape(-1) # Extract the EEG variable
EEG = data['t'][0] # ... and the t variablea t
Look at it.
plt.plot(t,EEG)'Time [s]')
plt.xlabel('EEG'); plt.ylabel(
Preliminaries
Q: What is the sampling interval (dt)?
= "SOMETHING"
dt print(dt)
Q. What is the sampling frequency (f0)?
= "SOMETHING"
f0 print(f0)
Q. What is the number of points in the data (N)?
= "SOMETHING"
N print(N)
Q. What is the total time of the observation (T)?
= "SOMETHING"
T print(T)
Compute the spectrum “by hand”
# Q. What is the Nyquist frequency and frequency resolution?
= "SOMETHING"
fNQ = "SOMETHING"
df
# Q. What is the frequency axis?
= np.arange(0,fNQ,df)
fj
# Then, compute the Fourier transform "by hand".
= EEG
x = np.ndarray(np.size(fj), complex);
X for j in range( np.size(fj) ):
= "SOMETHING"
X[j]
# And the spectrum,
= "SOMETHING"
Sxx
# Plot it,
plot(fj, Sxx.real)0, 100]) # Select frequency range
plt.xlim(['Frequency [Hz]') # Label the axes
plt.xlabel('Power [$\mu V^2$/Hz]'); plt.ylabel(
Compute the power spectrum using the FFT function.
= EEG
x = np.fft.fft(x) # Compute Fourier transform of x
X = "SOMETHING" # Compute spectrum
Sxx = Sxx[0:int(N / 2)].real # Ignore negative frequencies Sxx
Define the frequency axis
= "SOMETHING" # Determine frequency resolution
df = "SOMETHING" # Determine Nyquist frequency
fNQ = np.arange(0,fNQ,df) # Construct frequency axis faxis
Plot the spectrum versus frequency
Q. What do you see?
plt.plot(faxis, Sxx)0, 100]) # Select frequency range
plt.xlim(['Frequency [Hz]') # Label the axes
plt.xlabel('Power [$\mu V^2$/Hz]'); plt.ylabel(
Plot the spectrum versus frequency on a decibel scale.
Q. Now what do you see?
plt.figure()# Plot the spectrum in decibels.
plt.plot(faxis, [???]) 0, 100]) # Select the frequency range.
plt.xlim([-60, 0]) # Select the decibel range.
plt.ylim(['Frequency [Hz]') # Label the axes.
plt.xlabel('Power [dB]'); plt.ylabel(
Plot the spectrum versus frequency on a logarithmic frequency axis.
Q. And now what do you see?
plt.figure()# Log-log scale
plt.[???] 100]) # Select frequency range
plt.xlim([df, -60, 0]) # ... and the decibel range.
plt.ylim(['Frequency [Hz]') # Label the axes.
plt.xlabel('Power [dB]'); plt.ylabel(
Apply Hanning taper
Apply the Hanning taper to the data.
= ???
x_tapered
plt.figure()
plt.plot(t,x); plt.plot(t,x_tapered)
Apply the Hanning taper and look at the spectrum.
= "SOMETHING" # Compute Fourier transform of x.
xf_tapered = "SOMETHING" # Compute the spectrum,
Sxx_tapered = np.real(Sxx_tapered[:int(N / 2)]) # ... and ignore negative frequencies.
Sxx_tapered
plt.figure()10*np.log10(Sxx)) # Plot spectrum of untapered signal.
plt.semilogx(faxis,10*np.log10(Sxx_tapered)) # Plot spectrum vs tapered signal.
plt.semilogx(faxis,1], 100]) # Select frequency range,
plt.xlim([faxis[-70, 20]) # ... and the power range.
plt.ylim(['Frequency [Hz]') # Label the axes
plt.xlabel('Power [$\mu V^2$/Hz]') plt.ylabel(
Spectrogram
# Plot the spectrogram.
= 1 / dt # Define the sampling frequency,
Fs = int(Fs) # ... the interval size,
interval = int(Fs * 0.95) # ... and the overlap intervals
overlap
# Compute the spectrogram
= spectrogram(
f0, t0, Sxx0 # Provide the signal,
EEG, =Fs, # ... the sampling frequency,
fs=interval, # ... the length of a segment,
nperseg=overlap) # ... the number of samples to overlap,
noverlap10 * np.log10(Sxx0),
plt.pcolormesh(t0, f0, ='jet')# Plot the result
cmap# ... with a color bar,
plt.colorbar() 0, 70]) # ... set the frequency range,
plt.ylim(['Time [s]') # ... and label the axes
plt.xlabel('Frequency [Hz]'); plt.ylabel(