Plot the acceleration: Difference between revisions

From wikiluntti
Line 3: Line 3:
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.  


The data files are [[File:vlin.csv]]
The data files are [[vlin.csv]], [[File:vsin.csv]], [[File:vlog.csv]] and [[File:Vbum.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.  
. 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.  



Revision as of 22:54, 29 September 2020

Introduction

We use Python Pandas to read the csv file, plot the data and to draw the average value.

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.

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.

Example script

# 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})