pythonmatlabnumpydata-analysisspectra

Trouble Finding Spectrum Peaks on Python/ Google Colab


I have a spectrum (of an oil sample) as a 2D array in a cvs file that i want to find the peaks for in wavelengths 600 - 1800 cm-1. I've tried the scipy.signal.find_peaks but that takes a 1D array and I have a 2D array with the wavelengths and corresponding peak values. any help would be appreactiated since im very beginner at python

Edit: I also tried doing the following:

from detecta import detect_peaks

ind = detect_peaks(df)

where df is the name of my array (which has two columns) and an error pops up: ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)


Solution

  • scipy.signal.find_peaks() only takes a one-dimensional array containing the peaks. So you should be able to just select the column in your DataFrame with the peaks as so:

    # note that find_peaks returns an array of peak indices, and a dictionary of properties 
    ind, properties = scipy.signal.find_peaks(df["name of column with peaks"]) 
    

    Then if you only want the peaks, select the rows using the ind array you just created:

    peak_df = df[df.index.isin(ind)]