Plot the acceleration: Difference between revisions
(4 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=== Introduction === | === Introduction === | ||
We use Python Pandas to read the csv file, plot the data and to draw the average value. | We use Python Pandas to read the csv file, plot the data and to draw the average value. There are two different scripts, other for plotting the LEGO data, and the other for LabQuest data. | ||
The data files are [[vlin.csv]], [[File:vsin.csv]], [[File:vlog.csv]] and [[File:Vbum.csv]] | |||
. The files contains data from multiple runs, appended after each other. First the data is grouped by using the time difference, and the corresponding wall clock times are changed to running time. | === Example script 1: LEGO === | ||
The data files are [[File:vlin.csv]], [[File:vsin.csv]], [[File:vlog.csv]] and [[File:Vbum.csv]]. The files contains data from multiple runs, appended after each other. First the data is grouped by using the time difference, and the corresponding wall clock times are changed to running time. | |||
Finally, the grouped data is smoothed and plotted, and the average is drawn. | Finally, the grouped data is smoothed and plotted, and the average is drawn. | ||
Line 10: | Line 12: | ||
See the images at https://www.cod3v.info/index.php?title=Velocity,_acceleration_and_jerk#Analysis_Scripts. | See the images at https://www.cod3v.info/index.php?title=Velocity,_acceleration_and_jerk#Analysis_Scripts. | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
Line 78: | Line 79: | ||
ax.set_ylabel('Value',fontdict={'fontsize':24}) | ax.set_ylabel('Value',fontdict={'fontsize':24}) | ||
</syntaxhighlight> | |||
=== Example script 2: LabQuest === | |||
The data files are [[File:Vernier_LinearAcceleration.csv]], [[File:Vernier_SineAcceleration.csv]] [[File:Vernier_LogAcceleration.csv]] and [[File:Vernier_BumbAcceleration.csv]]. | |||
As the LabQuest is manually triggered, the zero point position need to be adjusted by hand. There is an option to trigger on the measured value, but this was not used. | |||
See the images at https://www.cod3v.info/index.php?title=Velocity,_acceleration_and_jerk#Analysis_Scripts. | |||
<syntaxhighlight lang="python"> | |||
# Load pandas | |||
import pandas as pd | |||
import matplotlib.pyplot as plt | |||
# Read CSV file into DataFrame df | |||
f = 'Vernier_LinearAcceleration.csv' | |||
#f = 'Vernier_SineAcceleration.csv' | |||
f = 'Vernier_LogAcceleration.csv' | |||
#f = 'Vernier_BumbAcceleration.csv' | |||
df = pd.read_csv( f ) | |||
df.columns = ["t1", "acc1", "t2", "acc2","t3", "acc3"] | |||
df['acc1'] = df['acc1'].apply(lambda x: -1*x) | |||
df['acc2'] = df['acc2'].apply(lambda x: -1*x) | |||
df['acc3'] = df['acc3'].apply(lambda x: -1*x) | |||
# Show datafra fme | |||
ax = plt.gca() | |||
if f == 'BumbAcceleration.csv': | |||
df.t2 = df.t2 - 1.1 | |||
df.t3 = df.t3 - 2.1 | |||
elif f == 'LinearAcceleration.csv': | |||
df.t1 = df.t1 - 2.5 | |||
df.t3 = df.t3 - 2.1 | |||
elif f == 'SineAcceleration.csv': | |||
df.t1 = df.t1 - 1.9 | |||
df.t3 = df.t3 - 0.5 | |||
#colors = {'D':'red', 'E':'blue', 'F':'green', 'G':'black'} | |||
df.rolling(window=50).mean().plot(x='t1',y='acc1',color=['r'],ax=ax, title=f) | |||
df.rolling(window=50).mean().plot(x='t2',y='acc2',color=['g'],ax=ax) | |||
df.rolling(window=50).mean().plot(x='t3',y='acc3',color=['b'],ax=ax) | |||
ax.set_xlabel('time [s]',fontdict={'fontsize':24}) | |||
ax.set_ylabel('Value',fontdict={'fontsize':24}) | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 23:40, 29 September 2020
Introduction
We use Python Pandas to read the csv file, plot the data and to draw the average value. There are two different scripts, other for plotting the LEGO data, and the other for LabQuest data.
Example script 1: LEGO
The data files are File:Vlin.csv, File:Vsin.csv, File:Vlog.csv and File:Vbum.csv. The files contains data from multiple runs, appended after each other. First the data is grouped by using the time difference, and the corresponding wall clock times are changed to running time.
Finally, the grouped data is smoothed and plotted, and the average is drawn.
See the images at https://www.cod3v.info/index.php?title=Velocity,_acceleration_and_jerk#Analysis_Scripts.
# Load pandas
import pandas as pd
import matplotlib.pyplot as plt
# Read CSV file into DataFrame df
df = pd.read_csv('vlin.csv', names=["time", "accV", "run"])
#df = pd.read_csv('vsin.csv', names=["time", "accV", "run"])
#df = pd.read_csv('vlog.csv', names=["time", "accV", "run"])
#df = pd.read_csv('vbum.csv', names=["time", "accV", "run"])
#The accelerometer is aligned to wrong direction; Change the sign:
df['accV'] = df['accV'].apply(lambda x: -1*x)
#If difference > 0.1s = 100 ms.
I = df.time[ df.time.diff() > 0.1].index.tolist()
i0 = 0
nro = 0
for nro, i in enumerate(I):
print( nro, i )
df.loc[i0:i, 'run'] = nro
# Change the zero
zeroVal = df.loc[i0, 'time']
df.loc[i0:i, 'time'] = df['time'].apply(lambda x: x-zeroVal)
i0 = i+1
df.loc[i0:, 'run'] = nro+1
# Change the zero
zeroVal = df.loc[i0, 'time']
df.loc[i0:, 'time'] = df['time'].apply(lambda x: x-zeroVal)
print( nro )
# Show dataframe
win = 50
ax = plt.gca()
df.groupby('run').rolling(window=win).mean().plot(x='time',y='accV',color=['r', 'g', 'b'],ax=ax)
#df.rolling(window=1).mean().plot( )
#
# The average
#
import numpy as np
T = np.linspace(0,8, 1000)
dfI = pd.DataFrame(T, columns = ['T'])
y_sum = np.zeros(np.size(T))
#dfI.set_index()
for i in range(len(df.run.unique())):
mask = df.loc[:,'run']==df.run.unique()[i]
y = np.interp(T, df[mask].time.rolling(window=win).mean(), df[mask].accV.rolling(window=win).mean())
y_sum = y_sum + y
dfI['acc_' + str(i)] = y
#dfI.assign()
y_sum = y_sum/(i+1)
dfI['mean'] = y_sum
#dfI.plot()
#dfI['mean'].plot(x='T',y='mean',color=['r', 'g', 'b'],ax=ax)
dfI.plot(x='T',y='mean',ax=ax, color='b', linewidth='5', title='Linear')
ax.set_xlabel('time [s]',fontdict={'fontsize':24})
ax.set_ylabel('Value',fontdict={'fontsize':24})
Example script 2: LabQuest
The data files are File:Vernier LinearAcceleration.csv, File:Vernier SineAcceleration.csv File:Vernier LogAcceleration.csv and File:Vernier BumbAcceleration.csv.
As the LabQuest is manually triggered, the zero point position need to be adjusted by hand. There is an option to trigger on the measured value, but this was not used.
See the images at https://www.cod3v.info/index.php?title=Velocity,_acceleration_and_jerk#Analysis_Scripts.
# Load pandas
import pandas as pd
import matplotlib.pyplot as plt
# Read CSV file into DataFrame df
f = 'Vernier_LinearAcceleration.csv'
#f = 'Vernier_SineAcceleration.csv'
f = 'Vernier_LogAcceleration.csv'
#f = 'Vernier_BumbAcceleration.csv'
df = pd.read_csv( f )
df.columns = ["t1", "acc1", "t2", "acc2","t3", "acc3"]
df['acc1'] = df['acc1'].apply(lambda x: -1*x)
df['acc2'] = df['acc2'].apply(lambda x: -1*x)
df['acc3'] = df['acc3'].apply(lambda x: -1*x)
# Show datafra fme
ax = plt.gca()
if f == 'BumbAcceleration.csv':
df.t2 = df.t2 - 1.1
df.t3 = df.t3 - 2.1
elif f == 'LinearAcceleration.csv':
df.t1 = df.t1 - 2.5
df.t3 = df.t3 - 2.1
elif f == 'SineAcceleration.csv':
df.t1 = df.t1 - 1.9
df.t3 = df.t3 - 0.5
#colors = {'D':'red', 'E':'blue', 'F':'green', 'G':'black'}
df.rolling(window=50).mean().plot(x='t1',y='acc1',color=['r'],ax=ax, title=f)
df.rolling(window=50).mean().plot(x='t2',y='acc2',color=['g'],ax=ax)
df.rolling(window=50).mean().plot(x='t3',y='acc3',color=['b'],ax=ax)
ax.set_xlabel('time [s]',fontdict={'fontsize':24})
ax.set_ylabel('Value',fontdict={'fontsize':24})