I am trying to do the following in Python and I wanted to know if there are any already written functions in numpy. I will simplify the scenario to be more clear:
I have two arrays:
voltage array with 100 entries time array with 100 entries
The time array goes from 101ns to 200ns in 1ns increments and the voltage array oscillates normally in the 5Volt range.
I wanted to know if there was any way to bin this data into 24 binned arrays where the edges are 100,124,148,172,196 and we can throw away the leftover voltage.
The bins should include the maximum of all of the voltages between the edges.
I also wanted to emphasize that I prioritize speed above all, I will be running this script millions of times although the array size will probably not exceed 10000.
If there is anyway to do "easily" do this without installing new libraries I would appreciate it, but if they have already been optimized for this I am willing to take a look.
Thank you!
Use NumPy, it has built-in functions that can bin your data:
import numpy as np
time_array = np.arange(101, 201, 1)
voltage_array = np.random.uniform(0, 5, 100)
bin_edges = [100, 124, 148, 172, 196]
bin_indices = np.digitize(time_array, bin_edges)
max_voltages = np.zeros(len(bin_edges) - 1)
for i in range(1, len(bin_edges)):
voltages_in_bin = voltage_array[bin_indices == i]
if voltages_in_bin.size > 0:
max_voltages[i - 1] = np.max(voltages_in_bin)
print("Maximum voltages in each bin:", max_voltages)