How to find peaks in 1d array
Asked Answered
C

3

5

I am reading a csv file in python and preparing a dataframe out of it. I have a Microsoft Kinect which is recording Arm Abduction exercise and generating this CSV file.

I have this array of Y-Coordinates of ElbowLeft joint. You can visualize this here. Now, I want to come up with a solution which can count number of peaks or local maximum in this array.

Can someone please help me to solve this problem?

Cableway answered 8/4, 2017 at 3:28 Comment(3)
Try scipy.signal.find_peaks_cwt.Ichnology
The newer scipy.signal.find_peaks may work better, unless you are really sure you need to use wavelet convolution.Hylton
Does this answer your question? Finding local maxima/minima with Numpy in a 1D numpy arrayPegram
E
1

You could try to smooth the data with a smoothing filter and then find all values where the value before and after are less than the current value. This assumes you want all peaks in the sequence. The reason you need the smoothing filter is to avoid local maxima. The level of smoothing required will depend on the noise present in your data.

A simple smoothing filter sets the current value to the average of the N values before and N values after the current value in your sequence along with the current value being analyzed.

Esthete answered 8/4, 2017 at 3:34 Comment(1)
I used moving average smoothing to smooth it (with np.convolve). And used argrelextrema from scipy.signal.Cableway
R
5

You can use the find_peaks_cwt function from the scipy.signal module to find peaks within 1-D arrays:

from scipy import signal
import numpy as np

y_coordinates = np.array(y_coordinates) # convert your 1-D array to a numpy array if it's not, otherwise omit this line
peak_widths = np.arange(1, max_peak_width)
peak_indices = signal.find_peaks_cwt(y_coordinates, peak_widths)
peak_count = len(peak_indices) # the number of peaks in the array

More information here: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.find_peaks_cwt.html

Redouble answered 8/4, 2017 at 4:58 Comment(0)
C
2

It's easy, put the data in a 1-d array and compare each value with the neighboors, the n-1 and n+1 data are smaller than n.

Read data as Robert Valencia suggests

   max_local=0
for u in range (1,len(data)-1):

if ((data[u]>data[u-1])&(data[u]>data[u+1])):
                            max_local=max_local+1
Credential answered 8/4, 2017 at 5:54 Comment(1)
This algo will fail for input like data = [3,2,3,6,4,1,2,3,2,1,2,2,2,1]Mastaba
E
1

You could try to smooth the data with a smoothing filter and then find all values where the value before and after are less than the current value. This assumes you want all peaks in the sequence. The reason you need the smoothing filter is to avoid local maxima. The level of smoothing required will depend on the noise present in your data.

A simple smoothing filter sets the current value to the average of the N values before and N values after the current value in your sequence along with the current value being analyzed.

Esthete answered 8/4, 2017 at 3:34 Comment(1)
I used moving average smoothing to smooth it (with np.convolve). And used argrelextrema from scipy.signal.Cableway

© 2022 - 2024 — McMap. All rights reserved.