Sorry I am not sure how to put the title more accurately.
I have an array which I would like to evenly split into 3 arrays, then each array will have a different size which is the downsampled version of the original array via averaging.
Here is what I have:
import numpy as np
a = np.arange(100)
bins = [5, 4, 3]
split_index = [[20, 39], [40, 59], [60, 80]]
b = []
for count, item in enumerate(bins):
start = split_index[count][0]
end = split_index[count][1]
increment = (end - start) // item
b_per_band = []
for i in range(item):
each_slice = a[start + i * increment : start + (i + 1) * increment]
b_per_band.append(each_slice.mean())
b.append(b_per_band)
print(b)
Result:
[[21.0, 24.0, 27.0, 30.0, 33.0], [41.5, 45.5, 49.5, 53.5], [62.5, 68.5, 74.5]]
So I loop through bins, find out how much increment is for each step. Slice it accordingly and append the mean to the result.
But this is really ugly and most importantly has bad performance. As I am dealing with audio spectrum in my case, I would really like to learn a more efficient way to achieving the same result.
Any suggestion?
split_indexfrom an arraya2) for each slice, you calculate 'sub-slices' with lengths given inbins, 3) for each sub-slice, you take the average. Is that correct?