我有一个脚本,我正在使用它从加速度计中提取数据。我可以指定加速计的带宽(250赫兹),它将采样的数据速率设置为500赫兹。你知道吗
我面临的问题是.txt
文件没有保存我所期望的样本数,我在这里请求支持,以了解我将数据写入文本文件的代码行是否是造成瓶颈的原因。你知道吗
一旦这个文件被保存在数据收集scipt中,我就在Spyder脚本中打开.txt
文件,这将创建数据的实时计算和可视化,因为它是由第一个脚本保存的。你知道吗
以下几行用于“data\u pull”脚本,该脚本从加速计读取数据。下面的几行是我用来写入文本文件的。你知道吗
file = open("C:\\Users\\User\\Documents\\test.txt","a")
writer = csv.writer(file)
while True:
writer.writerow(convert2g(readField()))
file.flush()
下面显示了从加速计提取数据脚本中的两个函数,以供参考。我没有包括整个脚本,因为它与保存和写入文本文件无关。你知道吗
def convert2g(raw_accs):
return [acc / CONVERT_CONST for acc in raw_accs]
def readField():
values = myBoard.Read(0x02, 18) #check register map
bin_mask = 0b11110000
acc_x_lsb = values[0] & bin_mask
acc_x_msb = values[1] << 8
acc_x = (acc_x_msb | acc_x_lsb)
twos_x = twos_comp(acc_x) >> 4
acc_y_lsb = values[2] & bin_mask
acc_y_msb = values[3] << 8
acc_y = (acc_y_msb | acc_y_lsb)
twos_y = twos_comp(acc_y) >> 4
acc_z_lsb = values[4] & bin_mask
acc_z_msb = values[5] << 8
acc_z = (acc_z_msb | acc_z_lsb)
twos_z = twos_comp(acc_z) >> 4
return twos_x, twos_y, twos_z
下面的代码是我用来构建动画的代码。你知道吗
import pandas as pd
import sys
import numpy as np
import easygui
import matplotlib.pyplot as plt
import matplotlib.animation as animation
def animate(i):
data = pd.read_csv("C:\\Users\\User\\Documents\\test.txt")
data.columns = ['X', 'Y', 'Z']
xar = range(len(data))
yar = pd.DataFrame(data['Z'])
yar = yar[1050:]
xar = xar[1050:]
std = yar.rolling(window=10000).std()
std = std.as_matrix()
yar = data.as_matrix()
yar = yar[1050:len(data)]
ax1.clear()
ax1.set_xlabel("Sample Number")
ax1.set_ylabel("Standard Deviation (g)")
ax3.clear()
ax3.set_xlabel("Sample Number")
ax3.set_ylabel("Acceleration (g)")
ax1.plot(xar, std)
ax3.plot(xar, yar)
ax1.set_title('Rolling Standard Deviation')
ax3.set_title('Original Data')
fig, (ax1, ax3) = plt.subplots(2, sharex = True)
fig.subplots_adjust(hspace=1.5)
ani = animation.FuncAnimation(fig, animate, interval=.01)
plt.show()
目前没有回答
相关问题 更多 >
编程相关推荐