Kleomenis Katevas
Kleomenis Katevas

Reputation: 764

How can I interpolate accelerometer data in pandas into fixed sampling rate?

I have a frame with accelerometer data, collected from an iPhone device. The avg sampling rate is 100Hz, but I'd like to have a fixed one with all the data (x, y, z) interpolated. Is that possible in pandas?

For example, here is the head of my df:

ts          x           y           z
0.006960    -0.075324   -0.175405   0.167105
0.016970    -0.048325   -0.186265   0.180108
0.026949    -0.017635   -0.158964   0.215963
0.036959    -0.097063   -0.063350   0.256945
0.046969    -0.139939   -0.046091   0.179085
...

What I need it to have a fixed sampling rage of 100Hz, so that the ts looks like: 0.00, 0.01, 0.02, 0.03 ... 0.99, 1.00, 1.01 ...

Thank you for you help in advance.

Upvotes: 1

Views: 1253

Answers (2)

Mister Brainley
Mister Brainley

Reputation: 662

There's probably a more efficient way to do this, but you could use scipy to interpolate each column to the time array of interest, and then create a new dataframe from the interpolated data.

import numpy as np
import pandas as pd
from scipy import interpolate

# define the time points of interest
fs = 100  # sampling rate
T = 1/fs  # period
ts = np.arange(0, 0.05, T)

# create a dictionary of interpolated data
interp_data = {}

# loop through the columns and populate
for key in ['x', 'y', 'z']:
    # fit the univariate spline to the data
    spl = interpolate.UnivariateSpline(df['ts'], df[key])
    # compute interpolated values on new time points
    interp_data[key] = spl(ts)

# convert to data frame
interp_frame = pd.DataFrame(interp_data, index=ts)
interp_frame.index.name = 'ts'
interp_frame.head()

Upvotes: 1

Zeugma
Zeugma

Reputation: 32095

Index your dataframe with ts:

df = df.set_index('ts')

Create the index you need:

index2 = pd.Index(pd.np.arange(0.00, 1.00, .01))

Merge it with the current index and reindex the dataframe to get additional rows:

df = df.reindex(df.index.union(index2))

Interpolate:

df.interpolate()

Upvotes: 3

Related Questions