-
Notifications
You must be signed in to change notification settings - Fork 0
/
PulseWidthDetector.mm
134 lines (112 loc) · 3.19 KB
/
PulseWidthDetector.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// -*- Mode: ObjC -*-
//
// Copyright (C) 2011, Brad Howes. All rights reserved.
//
#import "PulseWidthDetector.h"
#import "PulseWidthDetectorController.h"
#import "RunningAverager.h"
#import "UserSettings.h"
@implementation PulseWidthDetector
@synthesize sampleProcessor, lowLevel, highLevel, observer, minHighPulseAmplitude, smoother, smootherValues;
@synthesize maxPulseToPulseWidth;
+ (PulseWidthDetector*)create
{
return [[[PulseWidthDetector alloc] init] autorelease];
}
- (id)init
{
if (self = [super init]) {
self.sampleProcessor = [WaveCycleDetector createWithLowLevel:-0.33 highLevel:0.33];
sampleProcessor.observer = self;
controller = nil;
observer = nil;
maxPulseToPulseWidth = 22000; // 0.5 seconds of samples
[self updateFromSettings];
}
return self;
}
- (void)dealloc
{
self.sampleProcessor = nil;
[controller release];
self.observer = nil;
[super dealloc];
}
- (void)updateFromSettings
{
NSUserDefaults* settings = [NSUserDefaults standardUserDefaults];
self.lowLevel = [settings floatForKey:kSettingsPulseWidthDetectorLowLevelKey];
self.highLevel = [settings floatForKey:kSettingsPulseWidthDetectorHighLevelKey];
self.minHighPulseAmplitude = [settings floatForKey:kSettingsPulseWidthDetectorMinHighAmplitudeKey];
self.smoother = [RunningAverager createForSize:[settings integerForKey:kSettingsPulseWidthDetectorSmoothingKey]];
[self reset];
}
- (void)reset
{
[sampleProcessor reset];
[smoother reset];
state = kUnknownState;
pulseToPulseWidth = 0;
currentValue = 0.0;
}
- (Float32)lowLevel
{
return sampleProcessor.lowLevel;
}
- (void)setLowLevel:(Float32)value
{
sampleProcessor.lowLevel = value;
}
- (Float32)highLevel
{
return sampleProcessor.highLevel;
}
- (void)setHighLevel:(Float32)value
{
sampleProcessor.highLevel = value;
}
- (SignalProcessorController*)controller
{
if (controller == nil) {
controller = [[PulseWidthDetectorController createWithDetector:self] retain];
}
return controller;
}
- (NSString*)smootherValues
{
return [smoother description];
}
- (Float32)updatedDetectionValue
{
return currentValue;
}
- (void)waveCycleDetected:(WaveCycleDetectorInfo*)info
{
if (info.amplitude >= minHighPulseAmplitude) {
if (state != kInHighPulse) {
if (state == kInLowPulse) {
//
// We have a valid pulse-to-pulse detection.
//
if (pulseToPulseWidth < maxPulseToPulseWidth) {
currentValue = [smoother filter:pulseToPulseWidth];
LOG(@"pulseToPulseWidth: %d smoothed: %f", pulseToPulseWidth, currentValue);
if (observer) {
[observer pulseDetected:pulseToPulseWidth filtered:currentValue];
}
}
}
state = kInHighPulse;
pulseToPulseWidth = 0;
}
}
else {
if (state != kInLowPulse) {
if (state == kInHighPulse) {
state = kInLowPulse;
}
}
}
pulseToPulseWidth += info.sampleCount;
}
@end