0

I have four arrays of signal, I am trying to find the peaks in those signal. I am following this blog, but not able to detect peaks accurately. Plots of four signal looks like this :

enter image description here

My code to generate the peaks is :

def plot_peaks(time, singal):
    index_data  = indexes = scipy.signal.argrelextrema(
            np.array(signal),
            comparator=np.greater,order=2
        )
    plt.plot(time,singal)
    plt.plot(time[index_data[0]],signal[index_data[0]], alpha = 0.5,marker = 'o', mec = 'r',ms = 9, ls = ":",label='%d %s' % (index_data[0].size-1, 'Peaks'))
    plt.legend(loc='best', framealpha=.5, numpoints=1)
    plt.xlabel('Time(s)', fontsize=14)
    plt.ylabel('Amplitude', fontsize=14)

which is resulting like this:

enter image description here

But I want to show the maximum peaks only, but this code is generating a lot of minor peaks too. How to accurately generate maximum peaks?

I tried scipy code, but Confused with parameters that function

peaks, _ = find_peaks(x, height=0)
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88

1 Answers1

1

Without access to the input data itself it is hard to present a working solution. I can however give some general tips that might enable you to solve the problem yourself.

The difference between what you call "minor" peaks as opposed to "maximum" peaks is their prominence. The prominence is a measure of how much a peaks height stands out compared to the surrounding signal. For more context have a look at the example section, this wikipedia article on topographic prominence or this answer.

scipy.signal.find_peaks provides a way to to only select peaks with a certain prominence. Try to replace scipy.signal.argrelextrema with find_peaks like so

def plot_peaks(time, signal, prominence=None):
    index_data, _ = scipy.signal.find_peaks(
        np.array(signal),
        prominence=prominence
    )
    plt.plot(time, signal)
    plt.plot(time[index_data[0]], signal[index_data[0]], alpha = 0.5,marker = 'o', mec = 'r',ms = 9, ls = ":",label='%d %s' % (index_data[0].size-1, 'Peaks'))
    plt.legend(loc='best', framealpha=.5, numpoints=1)
    plt.xlabel('Time(s)', fontsize=14)
    plt.ylabel('Amplitude', fontsize=14)

Because your signals 1 to 4 have very different amplitude levels I didn't provide a default value that would work for all your signals, you'll have to try yourself which value works best for each plot. E.g. plot_peaks(time, signal_1, prominence=0.1) might be a good starting value for signal 1, plot_peaks(time, signal_2, prominence=1500) for signal 2 and so on...

lagru
  • 196
  • 7