Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find peaks in 1d array

Tags:

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?

like image 376
Pranav Masariya Avatar asked Apr 08 '17 03:04

Pranav Masariya


2 Answers

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

like image 98
Robert Valencia Avatar answered Sep 24 '22 10:09

Robert Valencia


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
like image 33
Duquesinho Avatar answered Sep 21 '22 10:09

Duquesinho