如何将大熊猫中的加速度计数据插入固定采样率?

时间:2016-09-04 20:29:32

标签: python pandas

我有一个带有加速计数据的框架,从iPhone设备收集。平均采样率是100Hz,但我希望有一个固定的采样率,内插所有数据(x,y,z)。大熊猫有可能吗?

例如,这是我的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
...

我需要它具有100Hz的固定采样范围,因此ts看起来像: 0.00, 0.01, 0.02, 0.03 ... 0.99, 1.00, 1.01 ...

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

使用ts索引您的数据框:

df = df.set_index('ts')

创建您需要的索引:

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

将其与当前索引合并,并重新索引数据框以获取其他行:

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

内插:

df.interpolate()

答案 1 :(得分:1)

这可能是一种更有效的方法,但您可以使用scipy将每列插入到感兴趣的时间数组中,然后根据插值数据创建新的数据帧。

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()
相关问题