numpy 按局部峰将阵列划分为子阵列

zdwk9cvp  于 2022-11-24  发布在  其他
关注(0)|答案(1)|浏览(105)

嘿,我有一个numpy数组y,有4000多个值。

data=pd.read_csv('samplesdata.csv',sep=";", decimal=",",encoding='latin-1')
sensor_data=data[['Euklidische Norm']]
sensor_data = np.array(sensor_data).ravel()
sensor_data = sensor_data - np.average(sensor_data) 

# Filter requirements.
order = 2
fs = 100  # sample rate, Hz
cutoff = 1

y = butter_lowpass_filter(sensor_data, cutoff, fs, order)

peaks = find_peaks(y*-1, height = 0.00, threshold = None, distance=170)
height = peaks[1]['peak_heights'] #list of heights of peaks
peak_pos = peaks[0]

我有一个另一个数组,它的值是我的数组y的局部峰值,它叫height
我想把数组y划分成子数组,但是有一个条件,height应该是子数组的边界。因为我想得到峰之间的值,包括峰本身。
我想要这样的东西,第一个子阵列应该从第一个局部峰值的值到第二个局部峰值的值,第二个子阵列应该从第二个局部峰值到第三个局部峰值,等等。
有没有人知道怎么解决它,因为我所尝试的一切,我失败了。
更新:Thats the graph where i need the values from
这是我的地方山峰。

[0.06110087, 0.10401105, 0.07522478, 0.09554681, 0.08982648,
       0.09118464, 0.09309628, 0.10440643, 0.08335122, 0.14826715,
       0.09760258, 0.13332452, 0.11009777, 0.1806636 ]

我需要的是图形中局部峰值之间的所有值,如0.06110087和0.10401105之间的值以及0.10401105和0.07522478之间的值等。

f2uvfpb9

f2uvfpb91#

你可以尝试下面的代码,根据你的高度给出的间隔来细分你的y数组(注意,你的高度数组需要排序)

import numpy as np

heights = np.array([0,4,8,12,20,30])

y = np.array([1,25,30,7,12])

def subdivise(arr,heights):
    # np.digitize will assign each item in arr to its corresponding interval in heights
    # the right = True is to include the right border of the intervals ( heights[i-1] < x <= heights[i])
    intervals = np.digitize(arr,heights,right=True)
    # The number of subdivisions is the number of the intervals given by the peaks
    subdivision = [[] for _ in range(intervals.max())]
    # assign each element in arr to its corresponding interval 
    for index,bin_pos in enumerate(intervals):
        subdivision[bin_pos - 1].append(arr[index])
    return subdivision

test = subdivise(y,heights)

# Reult from subdivise 
# [[1], [7], [12], [], [25, 30]]

UPDATE:用于未排序的高度,并在所有匹配的子数组中包含y的值

import random

def subdivise(arr,peak_heights):
    intervals = [(min(peak_heights[i],peak_heights[i + 1]),max(peak_heights[i],peak_heights[i + 1]))
            for i in range(len(peak_heights) - 1)]
    
    subarrays = [[] for _ in range(len(intervals))]

    for value in arr:
        for subarray_index,interval in enumerate(intervals):
            if value > interval[0] and value <= interval[1]:
                subarrays[subarray_index].append(value)
    
    return subarrays

### TEST 
peak_heights = [0.06,0.1,0.07,0.09,0.08]

y = [random.uniform(0,0.1) for _ in range(20)]
print(subdivise(y,peak_heights))

相关问题