# Load modules we'll need.
import numpy as np
import matplotlib.pyplot as plt
Analyzing Rhythms Part 3 (Spectra of spike trains)
Example: a randomly spiking neuron
To start, let’s create a fake spike train for a randomly spiking neuron, and compute the autocovariance and spectrum.
= 5000; # Number of bins.
N = 0.001; # Duration of each bin [s].
dt = N*dt; # Total time of observation [s].
T = np.arange(0,N)*dt; # Time axis for plotting
tm
= 5 # Average firing rate [Hz]
lambda0 = lambda0*dt; # Probability of a spike in a time bin
p0 = np.random.binomial(1,p0,N)# Create the spike train as "coin flips"
dn
# Plot it.
plt.plot(tm, dn) 'Time [s]'); plt.xlabel(
# Compute the autocovariance.
= "SOMETHING
ac_xx
# Plot it.
= np.arange(-N + 1, N) # Create a lag axis,
lags * dt, ac_xx) # ... and plot the result.
plt.plot(lags 'Lag [s]')
plt.xlabel('Autocovariance');
plt.ylabel(
print('lambda0*dt = ',lambda0*dt) # Compare expected r_{nn}[0] to computed value.
print('r_{nn}[0] = ',ac_xx[N-1])
# Compute the spectrum.
= "SOMETHING" # Compute the FT,
Dj = "SOMETHING" # ... and the spectrum.
Pj
= np.fft.fftfreq(N, dt) # Create frequency axis.
f
# Plot the spectrum.
plt.plot(f, np.real(Pj)) = "SOMETHING" # And our guess from in-class analysis.
in_class_guess *np.ones(N), 'b')
plt.plot(f,in_class_guess'Frequency [Hz]'); plt.xlabel(
# Repeat the entire simulation many times, and plot the average autocovariance and spectrum
= 1000 # Number of times to repeat the simulation.
K = np.zeros(K) # Vector to store estimate of lambda.
lambda_est = np.zeros([K,np.size(Pj)]) # Matrix to store spectra,
P = np.zeros([K,2*N-1]) # ... and AC.
AC
for k in np.arange(K): # For each repeat,
= np.random.binomial(1,p0,N) # ... create a new spike train.
dn
= "SOMETHING"
lambda_est[k] # Compute the AC,
= "SOMETHING"
ac_xx = ac_xx # ... and save the result.
AC[k,:]
= "SOMETHING" # Compute the FT,
Dj = "SOMETHING" # ... and the spectrum,
Pj = Pj # ... and save the result.
P[k,:]
# Plot it.
plt.figure() *dt, np.mean(AC,0))
plt.plot(lags'Lag [s]')
plt.xlabel('Autocovariance');
plt.ylabel(
plt.figure()0)) # Plot the spectrum, averaged over repeats
plt.plot(f, np.mean(P,= "SOMETHING" # Use lambda_est to compute our guess from in-class analysis.
in_class_guess *np.ones(N), 'b')
plt.plot(f,in_class_guess'Frequency [Hz]'); plt.xlabel(
Example: a randomly spiking neuron + refractory period
Now, let’s create a fake spike train for a randomly spiking neuron with a refractory period, and compute the autocovariance and spectrum.
= 5000; # Number of bins.
N = 0.001; # Duration of each bin [s].
dt = N*dt; # Total time of observation [s].
T = np.arange(0,N)*dt; # Time axis for plotting
tm
= 5 # Average firing rate [Hz]
lambda0 = lambda0*dt; # Probability of a spike in a time bin
p0 = np.random.binomial(1,p0,N)# Create the spike train as "coin flips"
dn = 10 # Add a refractory period
refractory_period for i in np.arange(N):
if dn[i]==1:
+1:i+refractory_period] = 0
dn[i
# Plot it.
plt.plot(tm, dn) 'Time [s]'); plt.xlabel(
# Compute the autocovariance.
# Compute the spectrum.
# Repeat the entire simulation many times, and plot the average autocovariance and spectrum