-
Notifications
You must be signed in to change notification settings - Fork 1
/
AccelerometerFilter.m
167 lines (130 loc) · 3.51 KB
/
AccelerometerFilter.m
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#import "AccelerometerFilter.h"
// Implementation of the basic filter. All it does is mirror input to output.
@implementation AccelerometerFilter
@synthesize x, y, z, adaptive;
@synthesize fx, fy, fz;
-(void)addAcceleration:(UIAcceleration*)accel
{
x = accel.x;
y = accel.y;
z = accel.z;
}
- (void)addAttitude:(CMAttitude *)attitude
{
fx = attitude.roll;
fy = attitude.pitch;
fz = attitude.yaw;
}
- (void)addRotation:(CMRotationRate )rotation
{
fx = rotation.x;
fy = rotation.y;
fz = rotation.z;
}
-(NSString*)name
{
return @"You should not see this";
}
@end
#define kAccelerometerMinStep 0.02
#define kAccelerometerNoiseAttenuation 3.0
double Norm(double x, double y, double z)
{
return sqrt(x * x + y * y + z * z);
}
double Clamp(double v, double min, double max)
{
if(v > max)
return max;
else if(v < min)
return min;
else
return v;
}
// See http://en.wikipedia.org/wiki/Low-pass_filter for details low pass filtering
@implementation LowpassFilter
-(id)initWithSampleRate:(double)rate cutoffFrequency:(double)freq
{
self = [super init];
if(self != nil)
{
double dt = 1.0 / rate;
double RC = 1.0 / freq;
filterConstant = dt / (dt + RC);
}
return self;
}
-(void)addAcceleration:(UIAcceleration*)accel
{
double alpha = filterConstant;
if(adaptive)
{
double d = Clamp(fabs(Norm(x, y, z) - Norm(accel.x, accel.y, accel.z)) / kAccelerometerMinStep - 1.0, 0.0, 1.0);
alpha = (1.0 - d) * filterConstant / kAccelerometerNoiseAttenuation + d * filterConstant;
}
x = accel.x * alpha + x * (1.0 - alpha);
y = accel.y * alpha + y * (1.0 - alpha);
z = accel.z * alpha + z * (1.0 - alpha);
}
-(void)addRotation:(CMRotationRate)rotation
{
double alpha = filterConstant;
if(adaptive)
{
double d = Clamp(fabs(Norm(fx, fy, fz) - Norm(rotation.x, rotation.y, rotation.z)) / kAccelerometerMinStep - 1.0, 0.0, 1.0);
alpha = (1.0 - d) * filterConstant / kAccelerometerNoiseAttenuation + d * filterConstant;
}
fx = rotation.x * alpha + fx * (1.0 - alpha);
fy = rotation.y * alpha + fy * (1.0 - alpha);
fz = rotation.z * alpha + fz * (1.0 - alpha);
}
-(void)addAttitude:(CMAttitude*)atti
{
double alpha = filterConstant;
if(adaptive)
{
double d = Clamp(fabs(Norm(fx, fy, fz) - Norm(atti.roll, atti.pitch, atti.yaw)) / kAccelerometerMinStep - 1.0, 0.0, 1.0);
alpha = (1.0 - d) * filterConstant / kAccelerometerNoiseAttenuation + d * filterConstant;
}
fx = atti.roll* alpha + fx * (1.0 - alpha);
fy = atti.pitch * alpha + fy * (1.0 - alpha);
fz = atti.yaw * alpha + fz * (1.0 - alpha);
}
-(NSString*)name
{
return adaptive ? @"Adaptive Lowpass Filter" : @"Lowpass Filter";
}
@end
// See http://en.wikipedia.org/wiki/High-pass_filter for details on high pass filtering
@implementation HighpassFilter
-(id)initWithSampleRate:(double)rate cutoffFrequency:(double)freq
{
self = [super init];
if(self != nil)
{
double dt = 1.0 / rate;
double RC = 1.0 / freq;
filterConstant = RC / (dt + RC);
}
return self;
}
-(void)addAcceleration:(UIAcceleration*)accel
{
double alpha = filterConstant;
if(adaptive)
{
double d = Clamp(fabs(Norm(x, y, z) - Norm(accel.x, accel.y, accel.z)) / kAccelerometerMinStep - 1.0, 0.0, 1.0);
alpha = d * filterConstant / kAccelerometerNoiseAttenuation + (1.0 - d) * filterConstant;
}
x = alpha * (x + accel.x - lastX);
y = alpha * (y + accel.y - lastY);
z = alpha * (z + accel.z - lastZ);
lastX = accel.x;
lastY = accel.y;
lastZ = accel.z;
}
-(NSString*)name
{
return adaptive ? @"Adaptive Highpass Filter" : @"Highpass Filter";
}
@end