How to find peaks in 1d array

让人想犯罪 __ 提交于 2019-12-23 16:32:24

问题


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?


回答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.




回答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



回答3:


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



来源:https://stackoverflow.com/questions/43289838/how-to-find-peaks-in-1d-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!