Hello,
I have this Python function that I use for such cases, it requires the Pandas library (
). Well, there are probably neater ways of doing it, but maybe it can help
Best,
Pierre
EDIT: maybe it's less headache prone if you also know how I read OpenSim files into pandas:
Code: Select all
dfCMC_q = pd.read_csv(r"CMC\Kinematics_q.sto", header=8, sep="\t", index_col="time")
Code: Select all
def sync_dfs(df, df1, rolling_mean_size=3):
"""
From two data frames sampled at slightly different times, return two dataframes
at synchronized time points. It uses linear interpolation to fill missing data,
and returns an averaged over some time points data.
Example:
df has 10 entries, each at half a second (.5, 1.5, 2.5 ...)
df1 has 30 entries, each at every third of a second (.33, .66, 1, 1.33, ...)
rolling_mean_size = 3
this function first computes a dataframe "whole" containing time points (.33, .5, .66, 1, 1.33, 1.5, ...)
it fills the missing values by linearly interpolating between two know data points (e.g. between .5 and 1.5)
it then averages over 3 time points, resulting in a "whole" data frame of size 40/3
And it finally returns two dataframes, adaptations of the input ones, both of the same size and sampling times.
"""
whole = pd.merge(df.reset_index(), df1.reset_index(), how='outer', on='time', suffixes=('_x', '_y')).set_index("time").sort_index().interpolate()
whole = whole.rolling(rolling_mean_size).mean()
df_ = whole.loc[:, whole.columns.str.endswith('x')].rename(columns = lambda x : str(x).rsplit('_', 1)[0])
df1_ = whole.loc[:, whole.columns.str.endswith('y')].rename(columns = lambda x : str(x).rsplit('_', 1)[0])
return df_, df1_