Find x value given y value plot

I'm essentially needing to find the x value of the maximized psd in this plot. I know that the y value of interest is equal to 20405.33 + 0i. How do I index the x value for this?

The code I'm using for this plot is as follows:
ffx <- fft(spike1list)
conx <- Conj(ffx)
psd <- ffx * conx
freq <- (1:length(spike1list))/length(spike1list)*1000
plot(freq,psd, type = 'l', xlim = c(7, 8), ylim = c(0, 30000))

For a single peak, you can use the which.max() function, which returns the index of the maximum value in a vector:

# Fake data
set.seed(3)
y = cumsum(rnorm(100))
x = 151:250
which.max(y)
[1] 87
x[which.max(y)]
[1] 237
plot(x,y, type="l")
abline(v=x[which.max(y)], col="red")

Rplot04

To find multiple peaks, R has many options, for example, findpeaks from the pracma package. findpeaks(y) returns a matrix, the second column of which gives the indices of the peaks.

library(pracma)

plot(x,y, type="l")
abline(v=x[findpeaks(y)[,2]], col="red")

Rplot03

For more information on tools for spectral analysis in R, see, for example, the CRAN Task Views for Chemometrics and Time Series.

4 Likes

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.