Speed detection can be calculated using information about peaks of sensor signal and distance of each sensor.
Speed formula:
$speed = \frac{distance}{time}$
When:
In [1]:
from IPython.display import display
from matplotlib import pyplot as plt
import numpy as np
import pandas as pd
import peakutils
import sys
# local
sys.path.insert(0, '../')
from pywim.utils import storage
from pywim.utils.stats import iqr
%matplotlib inline
In [2]:
f = storage.open_file('../data/wim_day_001_01_20170130.h5')
dset = f[list(f.keys())[0]]
df = storage.dataset_to_dataframe(dset)
In [3]:
# information on the file
paddle = len(max(dset.attrs, key=lambda v: len(v)))
print('METADATA')
print('='*80)
for k in dset.attrs:
print('{}:'.format(k).ljust(paddle, ' '), dset.attrs[k], sep='\t')
df.plot()
plt.show()
In [4]:
# distance between sensors
sensors_delta_distance = np.array(dset.attrs['sensors_distance'])
# x axis: time
x = df.index.values
sensors_peak_time = []
sensors_delta_time = [None]
for k in df.keys():
# y axis: volts
y = df[k].values
indexes = peakutils.indexes(y, thres=0.5, min_dist=30)
sensors_peak_time.append(x[indexes])
print('\nPeak time (s) on sensor {}:'.format(k))
print(x[indexes])
for i in range(1, len(sensors_peak_time)):
sensors_delta_time.append(sensors_peak_time[i]-sensors_peak_time[i-1])
# the information about first sensor should be equal to the second sensor
sensors_delta_time[0] = sensors_delta_time[1]
print('\nΔtime:')
display(sensors_delta_time)
print('\nΔdistance:', sensors_delta_distance)
In [5]:
sensors_delta_speed = []
for i in range(len(sensors_delta_distance)):
sensors_delta_speed.append(sensors_delta_distance[i]/sensors_delta_time[i])
# the information about first sensor should be equal to the second sensor
sensors_delta_speed[0] = sensors_delta_speed[1]
print('\nΔspeed:')
display(sensors_delta_speed)
In [6]:
speed_values = np.array([])
for sensor_speeds in sensors_delta_speed[1:]:
speed_values = np.concatenate((speed_values, sensor_speeds))
print('Speed values with rejecting outliers:')
display(speed_values)
speed = iqr.reject_outliers(pd.Series(speed_values)).mean()
print('Speed average: {} (m/s)'.format(speed))