- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
因此,我开始尝试使用从加速度计获得的数据(即 x、y 和 来计算用户所走的步数z 坐标。
我正在尝试实现 this算法,但我目前停留在局部最大值部分。 Matlab 有一个内置的 findpeaks()
方法,它可以定位给定数据集的所有局部最大值。
下面是我尝试实现该算法,但我仍然从中得到了非常巨大的结果。起初,使用由 20
实际步数组成的数据集,算法计算出所走的步数为 990+
。我对其进行了调整和调试,并设法将这个数字降低到 660
左右……然后是 110
,最后是当前的 45
。目前我只是卡住了,感觉我的 findpeaks()
方法是错误的。
这是我的类实现
import Foundation
class StepCounter
{
private var xAxes: [Double] = [Double]()
private var yAxes: [Double] = [Double]()
private var zAxes: [Double] = [Double]()
private var rmsValues: [Double] = [Double]()
init(graphPoints: GraphPoints)
{
xAxes = graphPoints.xAxes
yAxes = graphPoints.yAxes
zAxes = graphPoints.zAxes
rmsValues = graphPoints.rmsValues
}
func numberOfSteps()-> Int
{
var pointMagnitudes: [Double] = rmsValues
removeGravityEffectsFrom(&pointMagnitudes)
let minimumPeakHeight: Double = standardDeviationOf(pointMagnitudes)
let peaks = findPeaks(&pointMagnitudes)
var totalNumberOfSteps: Int = Int()
for thisPeak in peaks
{
if thisPeak > minimumPeakHeight
{
totalNumberOfSteps += 1
}
}
return totalNumberOfSteps
}
// TODO: dummy method for the time being. replaced with RMS values from controller itself
private func calculateMagnitude()-> [Double]
{
var pointMagnitudes: [Double] = [Double]()
for i in 0..<xAxes.count
{
let sumOfAxesSquare: Double = pow(xAxes[i], 2) + pow(yAxes[i], 2) + pow(zAxes[i], 2)
pointMagnitudes.append(sqrt(sumOfAxesSquare))
}
return pointMagnitudes
}
private func removeGravityEffectsFrom(inout magnitudesWithGravityEffect: [Double])
{
let mean: Double = calculateMeanOf(rmsValues)
for i in 0..<magnitudesWithGravityEffect.count
{
magnitudesWithGravityEffect[i] -= mean
}
}
// Reference: https://en.wikipedia.org/wiki/Standard_deviation
private func standardDeviationOf(magnitudes: [Double])-> Double
{
var sumOfElements: Double = Double()
var mutableMagnitudes: [Double] = magnitudes
// calculates the numerator of the equation
/* no need to do (mutableMagnitudes[i] = mutableMagnitudes[i] - mean)
* because it has already been done when the gravity effect was removed
* from the dataset
*/
for i in 0..<mutableMagnitudes.count
{
mutableMagnitudes[i] = pow(mutableMagnitudes[i], 2)
}
// sum the elements
for thisElement in mutableMagnitudes
{
sumOfElements += thisElement
}
let sampleVariance: Double = sumOfElements/Double(mutableMagnitudes.count)
return sqrt(sampleVariance)
}
// Reference: http://www.mathworks.com/help/signal/ref/findpeaks.html#examples
private func findPeaks(inout magnitudes: [Double])-> [Double]
{
var peaks: [Double] = [Double]()
// ignore the first element
peaks.append(max(magnitudes[1], magnitudes[2]))
for i in 2..<magnitudes.count
{
if i != magnitudes.count - 1
{
peaks.append(max(magnitudes[i], magnitudes[i - 1], magnitudes[i + 1]))
}
else
{
break
}
}
// TODO:Does this affect the number of steps? Are they clumsly lost or foolishly added?
peaks = Array(Set(peaks)) // removing duplicates.
return peaks
}
private func calculateMeanOf(magnitudes: [Double])-> Double
{
var sumOfElements: Double = Double()
for thisElement in magnitudes
{
sumOfElements += thisElement
}
return sumOfElements/Double(magnitudes.count)
}
}`
有了这个datasheet ,实际采取的步数是 20
,但我一直在 45
左右。即使当我尝试使用包含 30
实际步骤的数据集时,计算出的数字也接近 100s。
任何帮助/指导将不胜感激
PS:数据表格式为X,Y,Z,RMS (均方根)
最佳答案
此函数适用于您提供的示例。它将高原视为一个峰,并允许具有相同值的多个峰。唯一的问题是——@user3386109 指出——如果数据中有很多小的振荡,你会得到比实际存在的更多的峰值。如果您要处理这样的数据,您可能希望在此计算中实现数据集的方差。
此外,由于您没有更改传入的变量,因此无需使用 inout
private func findPeaks(magnitudes: [Double]) -> [Double] {
var peaks = [Double]()
// Only store initial point, if it is larger than the second. You can ignore in most data sets
if max(magnitudes[0], magnitudes[1]) == magnitudes[0] { peaks.append(magnitudes[0]) }
for i in 1..<magnitudes.count - 2 {
let maximum = max(magnitudes[i - 1], magnitudes[i], magnitudes[i + 1])
// magnitudes[i] is a peak iff it's greater than it's surrounding points
if maximum == magnitudes[i] && magnitudes[i] != magnitudes[i+1] {
peaks.append(magnitudes[i])
}
}
return peaks
}
更新我注意到我的解决方案不会在集合的末尾找到局部最大值。我已经对其进行了更新并将其实现为 Collection
的扩展。这可以很容易地适应 Sequence
,但我不确定这是否有意义。
extension Collection where Element: Comparable {
func localMaxima() -> [Element] {
return localMaxima(in: startIndex..<endIndex)
}
func localMaxima(in range: Range<Index>) -> [Element] {
var slice = self[range]
var maxima = [Element]()
var previousIndex: Index? = nil
var currentIndex = slice.startIndex
var nextIndex = slice.index(after: currentIndex)
while currentIndex < slice.endIndex {
defer {
previousIndex = currentIndex
currentIndex = nextIndex
nextIndex = slice.index(after: nextIndex)
}
let current = slice[currentIndex]
let next = slice[nextIndex]
// For the first element, there is no previous
if previousIndex == nil, Swift.max(current, next) == current {
maxima.append(current)
continue
}
// For the last element, there is no next
if nextIndex == slice.endIndex {
let previous = slice[previousIndex!]
if Swift.max(previous, current) == current {
maxima.append(current)
}
continue
}
let previous = slice[previousIndex!]
let maximum = Swift.max(previous, current, next)
// magnitudes[i] is a peak iff it's greater than it's surrounding points
if maximum == current && current != next {
maxima.append(current)
}
}
return maxima
}
}
值得一提的是,这是 Sequence
的扩展
extension Sequence where Element: Comparable {
func localMaxima() -> [Element] {
var maxima = [Element]()
var iterator = self.makeIterator()
var previous: Element? = nil
guard var current = iterator.next() else { return [] }
while let next = iterator.next() {
defer {
previous = current
current = next
}
// For the first element, there is no previous
if previous == nil, Swift.max(current, next) == current {
maxima.append(current)
continue
}
let maximum = Swift.max(previous!, current, next)
// magnitudes[i] is a peak iff it's greater than it's surrounding points
if maximum == current && current != next {
maxima.append(current)
}
}
// For the last element, there is no next
if Swift.max(previous!, current) == current {
maxima.append(current)
}
return maxima
}
}
关于ios - 查找给定数据集的局部最大值点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38311225/
初学者 android 问题。好的,我已经成功写入文件。例如。 //获取文件名 String filename = getResources().getString(R.string.filename
我已经将相同的图像保存到/data/data/mypackage/img/中,现在我想显示这个全屏,我曾尝试使用 ACTION_VIEW 来显示 android 标准程序,但它不是从/data/dat
我正在使用Xcode 9,Swift 4。 我正在尝试使用以下代码从URL在ImageView中显示图像: func getImageFromUrl(sourceUrl: String) -> UII
我的 Ubuntu 安装 genymotion 有问题。主要是我无法调试我的数据库,因为通过 eclipse 中的 DBMS 和 shell 中的 adb 我无法查看/data/文件夹的内容。没有显示
我正在尝试用 PHP 发布一些 JSON 数据。但是出了点问题。 这是我的 html -- {% for x in sets %}
我观察到两种方法的结果不同。为什么是这样?我知道 lm 上发生了什么,但无法弄清楚 tslm 上发生了什么。 > library(forecast) > set.seed(2) > tts lm(t
我不确定为什么会这样!我有一个由 spring data elasticsearch 和 spring data jpa 使用的类,但是当我尝试运行我的应用程序时出现错误。 Error creatin
在 this vega 图表,如果我下载并转换 flare-dependencies.json使用以下 jq 到 csv命令, jq -r '(map(keys) | add | unique) as
我正在提交一个项目,我必须在其中创建一个带有表的 mysql 数据库。一切都在我这边进行,所以我只想检查如何将我所有的压缩文件发送给使用不同计算机的人。基本上,我如何为另一台计算机创建我的数据库文件,
我有一个应用程序可以将文本文件写入内部存储。我想仔细看看我的电脑。 我运行了 Toast.makeText 来显示路径,它说:/数据/数据/我的包 但是当我转到 Android Studio 的 An
我喜欢使用 Genymotion 模拟器以如此出色的速度加载 Android。它有非常好的速度,但仍然有一些不稳定的性能。 如何从 Eclipse 中的文件资源管理器访问 Genymotion 模拟器
我需要更改 Silverlight 中文本框的格式。数据通过 MVVM 绑定(bind)。 例如,有一个 int 属性,我将 1 添加到 setter 中的值并调用 OnPropertyChanged
我想向 Youtube Data API 提出请求,但我不需要访问任何用户信息。我只想浏览公共(public)视频并根据搜索词显示视频。 我可以在未经授权的情况下这样做吗? 最佳答案 YouTube
我已经设置了一个 Twilio 应用程序,我想向人们发送更新,但我不想回复单个文本。我只是想让他们在有问题时打电话。我一切正常,但我想在发送文本时显示传入文本,以确保我不会错过任何问题。我正在使用 p
我有一个带有表单的网站(目前它是纯 HTML,但我们正在切换到 JQuery)。流程是这样的: 接受用户的输入 --- 5 个整数 通过 REST 调用网络服务 在服务器端运行一些计算...并生成一个
假设我们有一个名为 configuration.js 的文件,当我们查看内部时,我们会看到: 'use strict'; var profile = { "project": "%Projec
这部分是对 Previous Question 的扩展我的: 我现在可以从我的 CI Controller 成功返回 JSON 数据,它返回: {"results":[{"id":"1","Sourc
有什么有效的方法可以删除 ios 中 CBL 的所有文档存储?我对此有疑问,或者,如果有人知道如何从本质上使该应用程序像刚刚安装一样,那也会非常有帮助。我们正在努力确保我们的注销实际上将应用程序设置为
我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在
我正在为服务于发布请求的 API 调用运行单元测试。我正在传递请求正文,并且必须将响应作为帐户数据返回。但我只收到断言错误 注意:数据是从 Azure 中获取的 spec.js const accou
我是一名优秀的程序员,十分优秀!