如何动态消除蓝牙加速计的噪音?

2024-10-03 19:24:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序,连接到一个蓝牙加速计和读取数据,以确定实时运动,我正试图找出如何平滑噪声,以便我可以更好地表示运动。我发现了一个巴特沃斯过滤器的scipy函数(请原谅我对过滤器的无知),但它似乎只有当你有整个情节,因为它在平滑噪声的前后看点的工作。如何动态平滑噪音?这是我的密码:

def animator():
    global xyz
    fig = plt.figure()

    xyz_mot = fig.add_subplot(111, projection = "3d")
    xyz_mot.set_title("Motion")

    xyz_mot.set_xlim3d(-100, 100)
    xyz_mot.set_ylim3d(-100, 100)
    xyz_mot.set_zlim3d(-100, 100)

    xyz = xyz_mot.scatter(0,0,0)

    ani = FuncAnimation(fig, updateAni, frames=2, interval=50)

    fig.show()

def updateAni(i):
    t = float(time_data[-1] / 1000)**2
    xmot[0] = .5 * acceleration_data[-1].x * t
    ymot[0] = .5 * acceleration_data[-1].y * t
    zmot[0] = .5 * acceleration_data[-1].z * t
    xyz._offsets3d = (xmot, ymot, zmot)
    #print("X Motion: " + str(xmot) + ", Y Motion: " + str(ymot))
    #print(time_data[-1])

加速计数据和时间数据正在从另一个线程添加到数组acceleration_datatime_data。是否有matplotlib/其他库函数来平滑噪声?感谢您的帮助


Tags: 过滤器datatimedeffig噪声motionset
2条回答

查看通过exponential averagingmoving average过滤器运行的数据。指数平均允许你用alpha参数来权衡平均速度和速度。在添加到输出阵列之前,过滤来自加速计的原始数据。你知道吗

下面的代码片段实现了简单的指数平均器

avg = alpha * x +  (1 - alpha) * x_prev
x_prev = x
buffer.append(avg)

我不想把原始加速数据从辅助线程添加到acceleration_data,而是通过使用缓冲区不断地取最后几个测量值的平均值来过滤那里的数据。如果没有线程的代码,我只能给你一些伪代码,但这应该能让你了解它的工作原理

import collections
def thread_func():
    buf = collections.deque(maxlen=10) #take the average of up to the last 10 samples
    while True:
        accel = bluetooth_device.read()
        buf.append(accel)
        if buf: #may want to make sure the buffer isn't empty first to prevent devide by 0
            acceleration_data.append(sum(buf)/len(buf))

相关问题 更多 >