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?

3
  • Try scipy.signal.find_peaks_cwt. Commented Apr 8, 2017 at 3:39
  • 2
    The newer scipy.signal.find_peaks may work better, unless you are really sure you need to use wavelet convolution. Commented Apr 15, 2019 at 2:15
  • Does this answer your question? Finding local maxima/minima with Numpy in a 1D numpy array Commented Feb 8, 2023 at 8:57

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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 

1 Comment

This algo will fail for input like data = [3,2,3,6,4,1,2,3,2,1,2,2,2,1]
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.

1 Comment

I used moving average smoothing to smooth it (with np.convolve). And used argrelextrema from scipy.signal.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.