检测从手机的加速计周期性数据 [英] Detecting periodic data from the phone's accelerometer

查看:269
本文介绍了检测从手机的加速计周期性数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Android应用程序,我需要检测用户环境(如走路或开车最小)

I am developing an Android app and I am need to detect user context (if walking or driving at minimal)

我使用的加速​​度计和所有轴的总和来检测accleration载体。这是工作pretty的很好的方式而走,我可以看到一些periodics值。但我需要以编程方式检测这些poeriods。

I am using accelerometer and sum of all axes to detect the accleration vector. It is working pretty well in the way I can see some periodics values while walking. But I need to detect these poeriods programmatically.

请有什么样的数学函数来检测期间设定值吗?我听说傅里叶变换对于那些可用的,但我真的不知道如何实现它。它看起来pretty的复杂:)

Please is there any kind of math function to detect period in set of values? I heard Fourier transformation is usable for that, but I really dont know how to implement it. It looks pretty complicated :)

请帮忙

推荐答案

检测数据的周期性最简单的方法是的自相关。这也相当容易实现。要获得自相关您只需乘以你的数据,每个数据点与移位的每个数据点i 。下面是一些伪code:

The simplest way to detect periodicity of data is autocorrelation. This is also fairly simple to implement. To get the autocorrelation at i you simply multiply each data point of your data with each data point shifted by i. Here is some pseudocode:

for i = 0 to length( data ) do
  autocorrel[ i ] = 0;
  for j = 0 to length( data ) do
     autocorrel[ i ] += data( j ) * data( ( j + i ) mod length( data ) )
  done
done

这会给你值的数组。最高的周期与highes价值指数。这样,您就可以提取任何周期性的部分(通常有不止一个)。

This will give you an array of values. The highest "periodicity" is at the index with the highes value. This way you can extract any periodic parts (there usually is more than one).

此外,我建议你不要试图实现自己的FFT中的应用程序。虽然这种算法是非常良好的学习,有很多人可以做错误这是很难测试,它也可能是您的实现会比那些已经上市慢得多。如果有可能你的系统上,我建议你使用 FFTW 这是不可能在任何方面,当打谈到FFT的实现。

Also I would suggest you do not try implement your own FFT in an application. Although this algorithm is very good for learning, there is much one can do wrong which is hard to test and it is also likely that your implementation will be much slower than those which are already available. If it is possible on your system I would suggest you use the FFTW which is impossible to beat in any respect, when it comes to FFT implementations.

编辑:

解释,为什么这个工程即使在值不重复exactely:

Explanation, why this works even on values which do not repeat exactely:

通常的和完全正确的方法来计算自相关,从您的数据substract的平均值。比方说,你有 [1,2,1.2,1.8] 。然后,你可以从每个样品中提取1.5让你有 [ - 5,5,-.3 .3] 。现在,如果你本身在零的OFSET乘这个,底片将由正片和负片由阳性相乘,得到( - 5)^ 2 +(0.5)^ 2 +(-.3 )^ 2 +(0.3)^ 2 = 0.68 。以一种消极的偏移将与积极收益乘以( - 5)* +(0.5)*(5)( - 3)+(-.3)*(3) +(0.3)*( - 5)= - 。64 。两种在偏移再次底片将由正片和负片由阳性相乘。在三个类似的东西的情况进行了一个偏移偏移再次发生。正如你所看到的,你正值在0和2偏移量(周期)和负价值在1和4。

The usual and fully correct way to calculate the autocorrelation is, to substract the mean from your data. Let's say you have [1, 2, 1.2, 1.8 ]. Then you could extract 1.5 from each sample leaving you with [-.5, .5, -.3, .3 ]. Now if you multiply this with itself at an ofset of zero, negatives will be multiplied by negatives and positives by positives, yielding (-.5)^2 + (.5)^2 + (-.3)^2 + (.3)^2=.68. At an offset of one negatives will be multiplied with positives yielding (-.5)*(.5) + (.5)*(-.3) + (-.3)*(.3) + (.3)*(-.5)=-.64. At an offset of two again negatives will be multiplied by negatives and positives by positives. At offset of three something similar to the situation for an offset of one happens again. As you can see, you get positive values at offsets of 0 and 2 (the periods) and negative values at 1 and 4.

现在只检测到它是没有必要substract的平均周期。如果你只是离开了样品原样,则suqared平均将在每个另外添加。因为同样的值将对于每个计算出的系数被添加,所述比较将产生相同的结果时,如果先减去的平均值。在最坏的情况要么你的数据类型可能运行在(如果你使用某种整型),或者你可能会舍入误差时的值开始变得变大(如果你使用float,通常这不是一个问题)。在这种情况下,首先发生substract均值和尝试,如果你的结果变得更好。

Now to only detect the period it is not necessary to substract the mean. If you just leave the samples as-is, the suqared mean will be added at each addition. Since the same value will be added for each calculated coefficient, the comparison will yield the same results as if you first subtracted the mean. At worst either your datatype might run over (in case you use some kind of integral type), or you might get round off errors when the values start getting to big (in case you use float, usually this is not a problem). In case this happens first substract the mean and try if your results get better.

使用自相关与某种快速傅立叶变换的最强的缺点是速度。 Autocorelation需要为O(n ^ 2)其中,作为FFT只需要 O(N日志(N))。如果你需要计算很长序列的周期,很多时候,autocorelation可能不会在你的案件工作。

The strongest drawback of using autocorrelation vs. some kind of fast fourier transformation is the the speed. Autocorelation takes O(n^2) where as a FFT only takes O(n log(n)). In case you need to calculate the period of very long sequences very often, autocorelation might not work in your case.

如果你想知道如何傅里叶变换工作,并且什么这些东西约实部和虚部,幅度和相位(看看在code发表马努为例)是指,我建议你看看<一href="http://www.analog.com/en/processors-dsp/learning-and-development/content/scientist_engineers_guide/fca.html"相对=nofollow>这本书。

If you want to know how the fourier transformation works, and what all this stuff about real part, and imaginary part, magnitude and phase (have a look at the code posted by Manu for example) means, I suggest you have a look at this book.

EDIT2:

在大多数情况下,数据既不是完全周期性的,也不是完全混乱和非周期性。通常您的信息就会由几个周期性compenents,具有不同强度。一个时期是一个时间差,通过它可以改变你的数据,使其类似于本身。自相关计算的相似数据是,如果通过一定量的转变吧。因此,它为您提供了所有可能的时间段的强度。这意味着,不存在的重复值的索引,因为当数据是完全的周期性,所有索引将重复。具有最强值的索引,给你的移位,在该数据是最相似的本身。因此,该指数给出了一个时间偏移,而不是一个索引数据。为了理解这一点,它明白,如何以时间序列可被认为是为被由完全周期函数的总和(正弦基函数)是重要的。

In most cases data is neither fully periodic nor fully chaotic and aperiodic. Usually your data will be composed of several periodic compenents, with varying strength. A period is a time difference by which you can shift your data to make it similar to itself. The autocorrelation calculates how similar the data is, if you shift it by a certain amount. Thus it gives you the strength of all possible periods. This means, there is not "index of repeating value", because when the data is perfectly periodic, all indexes will repeat. The index with the strongest value, gives you the shift, at which the data is most similar to itself. Thus this index gives a time offset, not an index into your data. In order to understand this, it is important to understand, how a time series can be thought of as being made up of the sum of perfectly periodic functions (sinusoidal base functions).

如果您需要检测这个很长的时间序列,它通常也最好在你的数据滑动窗口,只是检查这个更小的数据帧的时期。但是你要知道,你的窗口将增加额外的时间到你的数据,而你必须知道。

If you need to detect this for very long time series, it is usually also best to slide a window over your data and just check for the period of this smaller data frame. However you have to be aware that your window will add additional periods to your data, of which you have to be aware.

更多我张贴在上次编辑的链接。

More in the link I posted in the last edit.

这篇关于检测从手机的加速计周期性数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆