Let's suppose I have data from a sensor, in the format:
data = [100, 140, 200, 250, 210, 300, 350, 380, 400, 333, 352, 399, 599, 899]
I want to loose resolution from this data so that I can modify its format in C++ later for the embedded device.
I want to force the resolution of the data. For example: no step must be lower than 50, then, all values have to be round up or down, I don't care, a multiply of 50. The result must be:
data = [100, 150, 200, 250, 250, 300, 350, 400, 400, 350, 400, 400, 600, 900]
I tried this code:
def curate_data(data: list, step_size: int = 1000):
def _round(number, increment, offset):
return round((number - offset) / increment) * increment + offset
curated_data = [data[0]]
for i in range(1, len(data)):
append_value = int(data[i])
if data[i] - curated_data[-1] < step_size:
# round to nearest multiple of step_size
append_value = _round(number=data[i], increment=step_size, offset=0)
curated_data.append(append_value)
return curated_data
but it does not generate the result I am expecting.
How could I manage this data to have the required resolution? Thanks
You can use floordiv and recalculate. You stated that you don't care if it rounds up or down. This will round according to the rules round is expected to follow.
data = [100, 140, 200, 250, 210, 300, 350, 380, 400, 333, 352, 399, 599, 899]
# round integers to multiples of step
def iround_by(num:int, step:int=50) -> int:
return int((step/2+num)//step*step)
out = list(map(iround_by, data))
print(out)
[100, 150, 200, 250, 200, 300, 350, 400, 400, 350, 350, 400, 600, 900]