How can I create an array based on values from different array Python Numpy

I have an numpy array x (it is a signal). I call a function to find peaks inside x and it is returned the index of where the peaks are. I then take the values around the peaks(+/-90) and save them in another array

 x= x.flatten() 
 peaks, _ = find_peaks(x, distance=180,height=0.70)
 R1_interval = peaks-90
 R2_interval = peaks+90
 z = np.vstack((R1_interval,R2_interval)).T

This is how the first value from array x look like:

[0.25687721, 0.25985362, 0.26261497, 0.26498313, 0.26681214, 0.2680001, 0.26849836, 0.2683168]

This is an example from the array from the first 5000 samples of peaks from x:

[-13,  167,  280,  460,  572,  752,  856, 1036, 1141, 1321, 1425, 1605, 1719, 1899, 1954, 2134, 2312, 2492, 2616, 2796, 2907, 3087, 3192, 3372, 3469, 3649, 3772, 3952, 4080, 4260, 4376, 4556, 4674, 4854]

If I take this code x[z[1]:z[2]] I can print the values from a single beat from my signal. My problem is that I want to create a new array that stores every single beat from array x and the data of the beat are in a single index.

The desired result would be to have ar1[0] equal to x[z[1]:z[2]] and so for all beats in x. How should I do this?


If I understand you right, you already have the peaks in x. And all the index boundaries for each [-90, 90] window around those peaks are in z.

It seems you want to retrieve the values by shifting the array by 1, so that ar1[0] := x[z[1]:z[2]], ar1[1] := x[z[3]:z[4]]… So we’ll work with z[1:-1].

Additionaly we have to loop through the indices two by two. Still with indexing: z[1:-1:2] will go over z‘s even indices while z[2:-1:2] will go over z‘s odd indices. You can zip both lists together and construct your final result:

[x[a:b] for a, b in zip(z[1:-1:2], z[2:-1:2])]