You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Python中按每秒时间段筛选运动数据保留最大位置值

Python 按秒筛选振动数据的最高位置值

核心实现方案

通过秒级时间分组,提取每组内位置最大值对应的原始数据行,同时保留时间与位置的对应关系,确保结果按时间顺序排列,完美适配阻尼指数拟合需求。

具体操作步骤(基于Pandas)

假设你的数据已整理为包含time(浮点型秒数)和position列的DataFrame:

  1. 生成秒级分组标识
    将时间转为整数作为分组键(正数场景下,astype(int)和numpy.floor()效果一致,直接截断小数即可):

    import pandas as pd
    import numpy as np
    
    # 替换为你的真实数据
    data = pd.DataFrame({
        'time': [0.2, 0.5, 0.8, 1.3, 1.7, 2.1, 2.6, 3.2],
        'position': [5.2, 5.8, 5.5, 4.9, 5.1, 4.5, 4.7, 4.2]
    })
    
    # 添加秒级分组列
    data['second_group'] = data['time'].astype(int)
    
  2. 提取每组位置最大值的原始数据
    用idxmax()定位每组中位置最大值的索引,再通过索引提取原数据行,确保保留对应的原始时间而非整数时间:

    # 获取每组位置最大值的行索引
    max_pos_indices = data.groupby('second_group')['position'].idxmax()
    # 筛选目标数据并删除分组列
    filtered_data = data.loc[max_pos_indices].drop('second_group', axis=1)
    
  3. 确保时间顺序
    按时间列排序(分组后默认按组顺序,此步骤为保险操作):

    filtered_data = filtered_data.sort_values('time').reset_index(drop=True)
    

处理后结果示例

timeposition
0.55.8
1.75.1
2.64.7
3.24.2

后续阻尼指数拟合(可选)

用scipy拟合指数衰减曲线(适配弹簧-质量系统阻尼模型):

from scipy.optimize import curve_fit

# 定义阻尼振动的指数衰减函数
def damping_curve(t, A, beta, C):
    return A * np.exp(-beta * t) + C

# 拟合参数,popt返回拟合得到的A、beta(阻尼系数)、C
popt, pcov = curve_fit(damping_curve, filtered_data['time'], filtered_data['position'])

内容的提问来源于stack exchange,提问作者Elaina Jeansonne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 12:13:29