2
$\begingroup$

I have a time series point process representing neuron spikes. I have computed and plotted autocovariance using acf but now I need to plot the Power Spectral Density.

Power Spectral Density is defined as the Fourier Transform of the autocovariance, so I have calculated this from my data, but I do not understand how to turn it into a frequency vs amplitude plot.

I have used the following code

# X is some set of Wait times between spikes, below is just an example
X <- c(56, 3, 4, 119, 3, 4, 121, 3, 3, 121, 3, 4, 120, 3, 4, 4, 115)
acf <- acf(X,type="covariance")
psd <- fft(acf$acf)

Now psd is a complex valued array across the default 24 lags from the acf function.

How do I turn this array into a PSD plot?

$\endgroup$
2
$\begingroup$

From further research I've discovered that the frequency is given by the index of the FFT multiplied by the sampling rate and divided by the size of the array. And the amplitude is the magnitude of the complex number.

So the full code for such a plot would be as follows

# Load ggplot library
library(ggplot)
# X is some set of Wait times between spikes, below is just an example
X <- c(56, 3, 4, 119, 3, 4, 121, 3, 3, 121, 3, 4, 120, 3, 4, 4, 115)
acf <- acf(X,type="covariance")
ft <- fft(acf$acf)
freq <- (1:nrow(ft))*1000/nrow(ft) #In my case we sample by ms so 1000 hz
A <- (Re(ft)^2 + Im(ft)^2)^.5 #amplitude is magnitude
PSD <- cbind.data.frame(freq,A)
ggplot(PSD, aes(x=freq,y=A)) + geom_line()
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.