I have a timeseries for which I need PSD values using R. The data was sampled at non uniform intervals but I did a spline interpolation with the predict command to interpolate readings at exactly 0.01 seconds. I could obtain amplitude values from spec.pgram quite correctly but they are not psd values. However the psd values from the pspectrum command of the psd package are only between 0 and 0.5Hz while my area of interest extends to about 1.2Hz. The time series is: here
Note that your time points are not equidistant. For the sake of this answer, we'll assume a frequency of 12 samples per second.
You have to specify the frequency for psd::pspectrum
. Assuming your data is loaded as a data.frame
called x
:
out <- pspectrum(x[, 2], x.frqsamp = 12)
plot(out)
The pspectrum
function also has a more detailed plot:
out <- pspectrum(x[, 2], x.frqsamp = 12, plot = TRUE)
Alternative
You can also use stats::spectrum
, but it will require you to create a ts
object:
our_ts <- ts(data = x[, 2],
start = 0,
frequency = 12)
plot(stats::spectrum(our_ts))
EDIT: Given new dataset (freq = 100)
x <- read.csv("test2.csv", header = F)
out <- pspectrum(x[, 2], x.frqsamp = 100)
out$freq[which.max(out$spec)]
# [1] 0.265708
our_ts <- ts(data = x[, 2], start = 4, frequency = 100)
out2 <- stats::spectrum(our_ts)
out2$freq[which.max(out2$spec)]
# [1] 0.2777778