网站的站外优化,深圳大型网站建设公司,wordpress分页滑动,微信小商店在一款智能手表中#xff0c; 平时手表处于息屏的状态#xff0c; 用于节省功耗#xff0c;延长使用时间。 在用户进行抬手的时候#xff0c;其实是希望能够及时看一下时间、消息通知等信息的。这时手表应该能够检测到用户的抬手动作#xff0c;自动进行屏幕的点亮。当用户…在一款智能手表中 平时手表处于息屏的状态 用于节省功耗延长使用时间。 在用户进行抬手的时候其实是希望能够及时看一下时间、消息通知等信息的。这时手表应该能够检测到用户的抬手动作自动进行屏幕的点亮。当用户垂手返回的时候也应该识别到及时进行屏幕的关闭。
对于这种动作的侦测采用加速度传感器是常见的选择。加速度传感采用固定的频率进行X轴、Y轴、Z轴三轴数据的采集放置在FIFO队列中 MCU可以定期去获取这些数据 对于这些数据进行处理和特征识别。 当识别出抬手、放手时进行相应的逻辑处理。
1. 数据
这个特征是怎么样的呢我们可以先看一下在反复进行抬手、放手动作时所抓取到的数据。 wrist_algo_timeout_handler(82): 1730099274701: wrist: i 0, x -8.842969, y 2.871094, z 0.976172 wrist_algo_timeout_handler(82): 1730099274703: wrist: i 1, x -8.842969, y 2.890234, z 0.899609 wrist_algo_timeout_handler(82): 1730099274705: wrist: i 2, x -8.823829, y 2.909375, z 0.861328 wrist_algo_timeout_handler(82): 1730099274707: wrist: i 3, x -8.862109, y 2.928516, z 0.861328 wrist_algo_timeout_handler(82): 1730099274710: wrist: i 4, x -8.900391, y 2.947656, z 0.861328 wrist_algo_timeout_handler(82): 1730099274712: wrist: i 5, x -8.900391, y 2.966797, z 0.861328 wrist_algo_timeout_handler(82): 1730099274714: wrist: i 6, x -8.900391, y 2.966797, z 0.861328 wrist_algo_timeout_handler(82): 1730099274716: wrist: i 7, x -8.881250, y 2.909375, z 0.842188 wrist_algo_timeout_handler(82): 1730099274719: wrist: i 8, x -8.919532, y 2.947656, z 0.842188 wrist_algo_timeout_handler(82): 1730099274721: wrist: i 9, x -8.919532, y 2.966797, z 0.861328 wrist_algo_timeout_handler(82): 1730099274723: wrist: i 10, x -8.957812, y 2.966797, z 0.899609 wrist_algo_timeout_handler(82): 1730099274726: wrist: i 11, x -8.900391, y 2.928516, z 0.899609 wrist_algo_timeout_handler(82): 1730099274728: wrist: i 12, x -8.938672, y 2.947656, z 0.880469 wrist_algo_timeout_handler(82): 1730099274730: wrist: i 13, x -8.957812, y 2.909375, z 0.861328 wrist_algo_timeout_handler(82): 1730099274733: wrist: i 14, x -8.957812, y 2.928516, z 0.861328 wrist_algo_timeout_handler(82): 1730099274735: wrist: i 15, x -8.976954, y 2.966797, z 0.880469 wrist_algo_timeout_handler(82): 1730099274737: wrist: i 16, x -8.996094, y 3.005078, z 0.823047 wrist_algo_timeout_handler(82): 1730099274739: wrist: i 17, x -9.034375, y 3.024219, z 0.861328 wrist_algo_timeout_handler(82): 1730099274742: wrist: i 18, x -8.957812, y 3.062500, z 0.861328 wrist_algo_timeout_handler(82): 1730099274744: wrist: i 19, x -8.996094, y 3.062500, z 0.880469 wrist_algo_timeout_handler(82): 1730099274746: wrist: i 20, x -8.919532, y 3.024219, z 0.880469 wrist_algo_timeout_handler(82): 1730099274749: wrist: i 21, x -8.900391, y 3.043360, z 0.880469 wrist_algo_timeout_handler(82): 1730099274751: wrist: i 22, x -8.919532, y 3.024219, z 0.880469 wrist_algo_timeout_handler(82): 1730099274753: wrist: i 23, x -8.996094, y 3.043360, z 0.899609 wrist_algo_timeout_handler(82): 1730099274756: wrist: i 24, x -8.938672, y 3.043360, z 0.995313 wrist_algo_timeout_handler(82): 1730099275212: wrist: i 0, x -8.919532, y 3.005078, z 1.014453 wrist_algo_timeout_handler(82): 1730099275214: wrist: i 1, x -8.976954, y 3.024219, z 1.033594 wrist_algo_timeout_handler(82): 1730099275216: wrist: i 2, x -8.919532, y 3.005078, z 1.033594 wrist_algo_timeout_handler(82): 1730099275218: wrist: i 3, x -8.900391, y 3.024219, z 1.052734 wrist_algo_timeout_handler(82): 1730099275221: wrist: i 4, x -8.881250, y 2.947656, z 1.014453 wrist_algo_timeout_handler(82): 1730099275223: wrist: i 5, x -8.862109, y 2.947656, z 1.014453 wrist_algo_timeout_handler(82): 1730099275225: wrist: i 6, x -8.938672, y 2.966797, z 0.995313 wrist_algo_timeout_handler(82): 1730099275228: wrist: i 7, x -8.976954, y 3.005078, z 0.976172 wrist_algo_timeout_handler(82): 1730099275230: wrist: i 8, x -8.957812, y 3.005078, z 0.995313 wrist_algo_timeout_handler(82): 1730099275232: wrist: i 9, x -8.957812, y 2.966797, z 0.957031 ...................................................... 2. 波形
对于三轴的数据 我们可以进行波形绘制 观察出具体的特征。
import os
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MultipleLocator#从Log中解析出来X、Y、Z三轴的原始数据
raw_data_file data/data.txt
count 0
lineNo 0
listX []
listY []
listZ []
listV []with open(raw_data_file, r, encodingutf-8) as fread:for text in fread.readlines():lineNo 1xyz text.split(,)xyz.pop(0)x xyz.pop(0)y xyz.pop(0)z xyz.pop(0)dataX float(x[4:])dataY float(y[4:])dataZ float(z[4:])dataV np.sqrt(dataX*dataX dataY*dataY dataZ*dataZ)listX.append(dataX)listY.append(dataY)listZ.append(dataZ)listV.append(dataV)count 1print(count no : str(count))plt.figure()
axplt.subplot(1,1,1)
ax.plot(listX[0:count], labelX, colorred)
ax.plot(listY[0:count], labelY, colorblue)
ax.plot(listZ[0:count], labelZ, colorgreen)ax.grid(True)
ax.xaxis.set_major_locator(MultipleLocator(10))
ax.yaxis.set_major_locator(MultipleLocator(1))
plt.show()
具体的波形可以如下图
.
可以截取一段看得更加清楚 3. 算法
主要的算法依据是当手臂下垂时假设X轴垂直于表盘Z轴垂直于地面如果处于静止状态则X轴的值为0g Z轴为1g当抬手转动到用户面前的时候这时X轴的值为1gZ轴的值为0g 当垂手灭屏的时候与之相反所以需要重点关注两个轴向的数据变化则可以实现抬手、垂手这两个动作的识别。
下面我们介绍采用代码进行动作识别的过程。
# 1. 进行滤波
# 2. 进行动态阈值
# 3. 进行动态精度
# 4. 判断峰值侦测动作类型
一些全局的变量定义如下
class peak_value:def __init__(self):self.newMax 0.0self.newMin 0.0self.oldMax 0.0self.oldMin 0.0SAMPLE_SIZE 100
DYNAMIC_PRECISION 0.01
FILTER_CNT 4
NULL 0
UP 1
DOWN 2
MAXTIMEOUT 10 #翻腕时间阈值设定为400ms
THRESHOLD 2.0 #翻腕的值应该超过1.0g
DELAYCOUNT 10 #应该延迟几个点进行判断
filtered_sample_x 0.0
filter_fifo_x []
filtered_sample_z 0.0
filter_fifo_z []
peak_x peak_value()
peak_z peak_value()
threshold_x 0.0
threshold_z 0.0
th_x []
th_z []
sample_size_x 0
sample_size_z 0
newSample_x 0.0
oldSample_x 0.0
newSample_z 0.0
oldSample_z 0.0
hand_on 0
hand_off 0
timeCount 0
lastTimeCount_x 0
lastTimeCount_z 0
timeInterval_x 0
timeInterval_z 0
x_status NULL
z_status NULLdef MAX(a, b):if (ab):return aelse:return bdef MIN(a, b):if (ab):return aelse:return b
3.1 滤波
滤波的方式有很多种 在这里我们采用均值滤波 可以消除一下毛刺。不能过滤的过于平滑 会失去特征。
#进行均值滤波
def filter_calculate_x(sample_x):global filtered_sample_x, filter_fifo_xfilter_fifo_x.pop(0)filter_fifo_x.append(sample_x)x_sum 0.0for i in range(0, FILTER_CNT):x_sum filter_fifo_x[i]filtered_sample_x x_sum/FILTER_CNTdef filter_calculate_z(sample_z):global filtered_sample_z, filter_fifo_zfilter_fifo_z.pop(0)filter_fifo_z.append(sample_z)z_sum 0.0for i in range(0, FILTER_CNT):z_sum filter_fifo_z[i]filtered_sample_z z_sum/FILTER_CNT
3.2 动态阈值
一个轴向上从0G到1G另一个轴向上从1G到0G这是比较理想的情况 其实用户在佩戴过程中 是不会有这么准确的值的。应该是采用动态阈值的情况不能采用绝对的值。
#采用设置动态阈值的方式
sample_fifo_x []
def peak_update_x(cur_sample_x):global sample_size_x, peak_x, threshold_x, sample_fifo_xsample_fifo_x.append(cur_sample_x)sample_size_x 1if (sample_size_x SAMPLE_SIZE):peak_x.newMax sample_fifo_x[0]peak_x.newMin sample_fifo_x[0]for i in range(0, SAMPLE_SIZE):peak_x.newMax MAX(peak_x.newMax, sample_fifo_x[i])peak_x.newMin MIN(peak_x.newMin, sample_fifo_x[i])peak_x.oldMax peak_x.newMaxpeak_x.oldMin peak_x.newMinthreshold_x (peak_x.oldMax peak_x.oldMin)/2sample_fifo_x.pop(0)sample_fifo_z []
def peak_update_z(cur_sample_z):global sample_size_z, peak_z, threshold_z, sample_fifo_zsample_fifo_z.append(cur_sample_z)sample_size_z 1if (sample_size_z SAMPLE_SIZE):peak_z.newMax sample_fifo_z[0]peak_z.newMin sample_fifo_z[0]for i in range(0, SAMPLE_SIZE):peak_z.newMax MAX(peak_z.newMax, sample_fifo_z[i])peak_z.newMin MIN(peak_z.newMin, sample_fifo_z[i])peak_z.oldMax peak_z.newMaxpeak_z.oldMin peak_z.newMinthreshold_z (peak_z.oldMax peak_z.oldMin)/2sample_fifo_z.pop(0)
3.3 动态精度
对于相邻的点如果变化很小 可以丢弃掉。
#动态精度
def slide_update_x(cur_sample_x):res Falseglobal newSample_x, oldSample_xif (abs(cur_sample_x - newSample_x) DYNAMIC_PRECISION):oldSample_x newSample_xnewSample_x cur_sample_xres Trueelse:oldSample_x newSample_xreturn resdef slide_update_z(cur_sample_z):res Falseglobal newSample_z, oldSample_zif (abs(cur_sample_z - newSample_z) DYNAMIC_PRECISION):oldSample_z newSample_znewSample_z cur_sample_zres Trueelse:oldSample_z newSample_zreturn res3.4 动作识别
对于一段数据设置完阈值后进行两轴向数据的判断从而识别出抬手、放手的动作。
#姿态判断
def detect_tilt():global hand_on, hand_off, oldSample_x, oldSample_z, newSample_x, newSample_z, threshold_x, threshold_z, lastTimeCount_x, lastTimeCount_z, timeInterval_x, timeInterval_z, x_status, z_status, timeCounttimeInterval_x timeCount - lastTimeCount_xtimeInterval_z timeCount - lastTimeCount_zif (timeInterval_x MAXTIMEOUT):x_status NULLif (timeInterval_z MAXTIMEOUT):z_status NULLif (np.abs(newSample_x - peak_x.oldMax) THRESHOLD or np.abs(newSample_x - peak_x.oldMin) THRESHOLD):if (oldSample_x threshold_x and newSample_x threshold_x):x_status DOWNlastTimeCount_x timeCount#print(x down : str(timeCount))if (oldSample_x threshold_x and newSample_x threshold_x):x_status UPlastTimeCount_x timeCount#print(x up : str(timeCount))if (np.abs(newSample_z - peak_z.oldMax) THRESHOLD or np.abs(newSample_z - peak_z.oldMin) THRESHOLD):if (oldSample_z threshold_z and newSample_z threshold_z):z_status DOWNlastTimeCount_z timeCount#print(z down : str(timeCount))if (oldSample_z threshold_z and newSample_z threshold_z):z_status UPlastTimeCount_z timeCount#print(z up : str(timeCount))if (x_status UP and z_status DOWN):hand_on 1print(timeCount : str(timeCount))print(hand on : str(hand_on))x_status NULLz_status NULLif (x_status DOWN and z_status UP):hand_off 1print(timeCount : str(timeCount) )print(hand off : str(hand_off))x_status NULLz_status NULL这几个函数的具体调用过程如下
#进行第一次X轴、Z轴阈值设定
peak_x.newMax listX[0]
peak_x.newMin listX[0]
peak_x.oldMax listX[0]
peak_x.oldMin listX[0]peak_z.newMax listZ[0]
peak_z.newMin listZ[0]
peak_z.oldMax listZ[0]
peak_z.oldMin listZ[0]for i in range(0, SAMPLE_SIZE):cur_sample_x listX[i]cur_sample_z listZ[i]peak_update_x(cur_sample_x)peak_update_z(cur_sample_z)#设定第一个滤波器的数据
for i in range(0, FILTER_CNT):cur_sample_x listX[i]cur_sample_z listZ[i]filter_fifo_x.append(cur_sample_x)filter_fifo_z.append(cur_sample_z)#循环获取Sample的点 进行动态阈值的更新 抬手垂手动作姿势的侦测
mean_listX []
mean_listY []
mean_listZ []listX_buffer []
listZ_buffer []for i in range(0, count):timeCount 1cur_sample_x listX[i]cur_sample_z listZ[i]filter_calculate_x(cur_sample_x)filter_calculate_z(cur_sample_z)peak_update_x(filtered_sample_x)peak_update_z(filtered_sample_z)th_x.append(threshold_x)th_z.append(threshold_z)listX_buffer.append(filtered_sample_x)listZ_buffer.append(filtered_sample_z)if (timeCount DELAYCOUNT):x listX_buffer.pop(0)z listZ_buffer.pop(0)mean_listX.append(x)mean_listZ.append(z)slide_update_x(x)slide_update_z(z)detect_tilt()print(hand on : str(hand_on))
print(hand off : str(hand_off))3.5 波形示意
可以把增加了动态阈值和滤波后的波形与原始波形进行对比 如下所示
#绘制波形
#前面几个值要补齐
for i in range(0, DELAYCOUNT):mean_listX.insert(0, 0.0)mean_listZ.insert(0, 0.0)count len(mean_listX)
plt.figure()
axplt.subplot(1,1,1)
ax.plot(mean_listX[0:count], labelX, colorred)
ax.plot(th_x[0:count], ls --, labelth_x, color red)
ax.plot(mean_listZ[0:count], labelZ, colorgreen)
ax.plot(th_z[0:count], ls --, labelth_z, color green)
ax.grid(True)
ax.xaxis.set_major_locator(MultipleLocator(10))
ax.yaxis.set_major_locator(MultipleLocator(1))
plt.show() 文章转载自: http://www.morning.yhgbd.cn.gov.cn.yhgbd.cn http://www.morning.dmwjl.cn.gov.cn.dmwjl.cn http://www.morning.qgxnw.cn.gov.cn.qgxnw.cn http://www.morning.lyrgp.cn.gov.cn.lyrgp.cn http://www.morning.xrpwk.cn.gov.cn.xrpwk.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.fsjcn.cn.gov.cn.fsjcn.cn http://www.morning.ljhnn.cn.gov.cn.ljhnn.cn http://www.morning.rqdx.cn.gov.cn.rqdx.cn http://www.morning.yrpd.cn.gov.cn.yrpd.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.kgtyj.cn.gov.cn.kgtyj.cn http://www.morning.jqbpn.cn.gov.cn.jqbpn.cn http://www.morning.zlrsy.cn.gov.cn.zlrsy.cn http://www.morning.rsnd.cn.gov.cn.rsnd.cn http://www.morning.lpzyq.cn.gov.cn.lpzyq.cn http://www.morning.jqkrt.cn.gov.cn.jqkrt.cn http://www.morning.yhgbd.cn.gov.cn.yhgbd.cn http://www.morning.rqlqd.cn.gov.cn.rqlqd.cn http://www.morning.xkzr.cn.gov.cn.xkzr.cn http://www.morning.mqmxg.cn.gov.cn.mqmxg.cn http://www.morning.rgmd.cn.gov.cn.rgmd.cn http://www.morning.pzrpz.cn.gov.cn.pzrpz.cn http://www.morning.gynls.cn.gov.cn.gynls.cn http://www.morning.nrxsl.cn.gov.cn.nrxsl.cn http://www.morning.wrfk.cn.gov.cn.wrfk.cn http://www.morning.jlmrx.cn.gov.cn.jlmrx.cn http://www.morning.ygwbg.cn.gov.cn.ygwbg.cn http://www.morning.lmyq.cn.gov.cn.lmyq.cn http://www.morning.tbqxh.cn.gov.cn.tbqxh.cn http://www.morning.dcccl.cn.gov.cn.dcccl.cn http://www.morning.wmrgp.cn.gov.cn.wmrgp.cn http://www.morning.prmbb.cn.gov.cn.prmbb.cn http://www.morning.btrfm.cn.gov.cn.btrfm.cn http://www.morning.hgsylxs.com.gov.cn.hgsylxs.com http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.czxrg.cn.gov.cn.czxrg.cn http://www.morning.tjwfk.cn.gov.cn.tjwfk.cn http://www.morning.kqrql.cn.gov.cn.kqrql.cn http://www.morning.lyhry.cn.gov.cn.lyhry.cn http://www.morning.qdbcd.cn.gov.cn.qdbcd.cn http://www.morning.nfdty.cn.gov.cn.nfdty.cn http://www.morning.qdbcd.cn.gov.cn.qdbcd.cn http://www.morning.rxnxl.cn.gov.cn.rxnxl.cn http://www.morning.qshxh.cn.gov.cn.qshxh.cn http://www.morning.snyqb.cn.gov.cn.snyqb.cn http://www.morning.jbxmb.cn.gov.cn.jbxmb.cn http://www.morning.zyslyq.cn.gov.cn.zyslyq.cn http://www.morning.hffpy.cn.gov.cn.hffpy.cn http://www.morning.rfpb.cn.gov.cn.rfpb.cn http://www.morning.rmlz.cn.gov.cn.rmlz.cn http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.rxyz.cn.gov.cn.rxyz.cn http://www.morning.wfkbk.cn.gov.cn.wfkbk.cn http://www.morning.smkxm.cn.gov.cn.smkxm.cn http://www.morning.cyfsl.cn.gov.cn.cyfsl.cn http://www.morning.rnyhx.cn.gov.cn.rnyhx.cn http://www.morning.bdfph.cn.gov.cn.bdfph.cn http://www.morning.nxfuke.com.gov.cn.nxfuke.com http://www.morning.pjxw.cn.gov.cn.pjxw.cn http://www.morning.qzpw.cn.gov.cn.qzpw.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.mxdhy.cn.gov.cn.mxdhy.cn http://www.morning.yngtl.cn.gov.cn.yngtl.cn http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn http://www.morning.roymf.cn.gov.cn.roymf.cn http://www.morning.thxfn.cn.gov.cn.thxfn.cn http://www.morning.rshijie.com.gov.cn.rshijie.com http://www.morning.xsctd.cn.gov.cn.xsctd.cn http://www.morning.mtrrf.cn.gov.cn.mtrrf.cn http://www.morning.jwfqq.cn.gov.cn.jwfqq.cn http://www.morning.frqtc.cn.gov.cn.frqtc.cn http://www.morning.lzttq.cn.gov.cn.lzttq.cn http://www.morning.yrdkl.cn.gov.cn.yrdkl.cn http://www.morning.tnbsh.cn.gov.cn.tnbsh.cn http://www.morning.mnrqq.cn.gov.cn.mnrqq.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.stcds.cn.gov.cn.stcds.cn http://www.morning.tfzjl.cn.gov.cn.tfzjl.cn