- (void)updateWithYValue:(CGFloat)newY { if (!isnan(newY)) { if (!_shouldResetConfiguration) { _minValue = newY; _maxValue = newY; _startValue = NO; _didCheckerFindingTarget = NO; _shouldResetConfiguration = YES; } else { if (!_didCheckerFindingTarget) { _maxValue = MAX(_maxValue, newY); _minValue = MIN(_minValue, newY); CGFloat gapFromMaxValue = (ABS(_maxValue - newY)); CGFloat gapFromMinValue = (ABS(newY - _minValue)); if (gapFromMaxValue >= self.minDistanceChangeForAllowRecordNumber) { _didCheckerFindingTarget = YES; _findingTopValue = NO; _criticalValue = newY; _maxValue = newY; _foundTargetCount = 1; } else if (gapFromMinValue >= self.minDistanceChangeForAllowRecordNumber) { _didCheckerFindingTarget = YES; _findingTopValue = YES; _criticalValue = newY; _minValue = newY; _foundTargetCount = 1; } } else { if (_findingTopValue) { if (newY >= _criticalValue) { _maxValue = MAX(newY, _maxValue); _minValue = newY; CGFloat gapFromMaxValue = (ABS(_maxValue - newY)); if (gapFromMaxValue >= self.minDistanceChangeForAllowRecordNumber) { _criticalValue = newY; _findingTopValue = NO; if (_foundTargetCount % 2 == 0) { [self addOneChangeCount]; } } } else { CGFloat gapFromMaxValue = (ABS(_maxValue - newY)); _minValue = MIN(_minValue, newY); CGFloat gapFromMinValue = (ABS(newY - _minValue)); if (gapFromMaxValue >= self.minDistanceChangeForAllowRecordNumber) { _foundTargetCount += 1; _criticalValue = newY; _minValue = newY; _findingTopValue = NO; if (_foundTargetCount % 2 == 0) { [self addOneChangeCount]; printf("==============>2 "); } } else if (gapFromMinValue >= self.minDistanceChangeForAllowRecordNumber) { ///异常,需要忽略一半波形 _criticalValue = newY; _maxValue = newY; _findingTopValue = YES; } } } else { if (newY <= _criticalValue) { _minValue = MIN(newY, _minValue); _maxValue = newY; CGFloat gapFromMinValue = (ABS(newY - _minValue)); if (gapFromMinValue >= self.minDistanceChangeForAllowRecordNumber) { _foundTargetCount += 1; _criticalValue = newY; _findingTopValue = YES; if (_foundTargetCount % 2 == 0) { [self addOneChangeCount]; } } } else { CGFloat gapFromMinValue = (ABS(newY - _minValue)); _maxValue = MAX(_maxValue, newY); CGFloat gapFromMaxValue = (ABS(_maxValue - newY)); if (gapFromMinValue >= self.minDistanceChangeForAllowRecordNumber) { _foundTargetCount += 1; _criticalValue = newY; _maxValue = newY; _findingTopValue = YES; if (_foundTargetCount % 2 == 0) { [self addOneChangeCount]; } } else if (gapFromMaxValue >= self.minDistanceChangeForAllowRecordNumber) { ///异常,需要忽略一半波形 _criticalValue = newY; _minValue = newY; _findingTopValue = NO; } } } } } } }
假设第一个点为最低值,根据下一个点判断趋势,记录探测接下来的值,记录max和min,当当前点达到触发临界差值,认定触发了一个波峰/波谷。若为波峰,保留最高值,探测下面的值,当探测到最高点到当前点差值到达临界值认为下到一个波谷,计数加1。