pythonarraysscipy

find_peaks - trimming both peak array and prominences


I have two arrays of data from a scipy signal.find_peaks function.

    p, prom_data = signal.find_peaks(spec, prominence=0.000010)
    prom = prom_data['prominences']

p contains a list of indices where the peaks in spec are over the prominence and prom is a list of the heights of the peaks in p.

example:

p = np.arr[ 3, 7, 10, 14, 16, 19, ]
prom = np.arr[ 99, 77, 100, 140, 50, 82 ]

Then some unnecessary data is trimmed from p with:

    # REMOVE PEAKS OUT OF BAND
    p = p[(p >= 10) & (p <= 30)] # should remove 3 and 7
    

But this operation does no remove the corresponding data from prom (i.e. 99 and 77)

Q: How can I remove the corresponding heights from prom ?


Solution

  • Here I save the indexes of values you delete in p and then delete values on those indexes from the second array too.

    import numpy as np
    
    p = np.array([3, 7, 10, 14, 16, 31])
    prom = np.array([99, 77, 100, 140, 50, 82])
    
    original_indexes = np.arange(len(p))
    
    p_filtered = p[(p >= 10) & (p <= 30)]
    
    removed_indexes = original_indexes[~((p >= 10) & (p <= 30))]
    
    prom_filtered = np.delete(prom, removed_indexes)
    
    print("filtered array p:\n", p_filtered)
    print("removed indexes:\n", removed_indexes)
    print("filtered array prom:\n", prom_filtered)