forked from libffe/fitparse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix.c
More file actions
121 lines (107 loc) · 4.16 KB
/
fix.c
File metadata and controls
121 lines (107 loc) · 4.16 KB
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
/*
* Copyright (c) 2014 Kirk Scheibelhut <kjs@scheibo.com>
*
* This file is free software: you may copy, redistribute and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 2 of the License, or (at your
* option) any later version.
*
* This file is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright (c) 2013 Jaime Jofre (jaimeajofre@hotmail.com)
* Copyright (c) 2010 Mark Liversedge (liversedge@gmail.com)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <assert.h>
#include "activity.h"
#include "fix.h"
/* Remove GPS errors and interpolate positional data where the GPS device
* did not record any data, or the data that was recorded is invalid. */
/* used to handle gaps in recording by inserting interpolated/zero samples to
* ensure dataPoints are contiguous in time */
int fix_invalid_gps(Activity *a) {
DataPoint dp, good;
double delta_latitude, delta_longitude;
int errors = 0, last_good = -1;
unsigned i, j;
assert(a != NULL);
/* ignore files without GPS data */
if (!a->last[Latitude] || !a->last[Longitude]) return -1;
for (i = 0; i < a->num_points; i++) {
dp = a->data_points[i];
/* is this one decent? */
if (!(dp.data[Latitude] >= -90 && dp.data[Latitude] <= 90 &&
dp.data[Longitude] >= -180 && dp.data[Longitude] <= 180))
continue;
if (last_good != -1 && (unsigned)(last_good + 1) != i) {
good = a->data_points[last_good];
/* interpolate from last good to here then set last_good to here */
delta_latitude =
(dp.data[Latitude] - good.data[Latitude]) / (double)(i - last_good);
delta_longitude =
(dp.data[Longitude] - good.data[Latitude]) / (double)(i - last_good);
for (j = last_good + 1; j < i; j++) {
a->data_points[j].data[Latitude] =
good.data[Latitude] + ((j - last_good) * delta_latitude);
a->data_points[j].data[Longitude] =
good.data[Longitude] + ((j - last_good) * delta_longitude);
errors++;
}
} else if (last_good == -1) {
/* fill to front */
for (j = 0; j < i; j++) {
a->data_points[j].data[Latitude] = dp.data[Latitude];
a->data_points[j].data[Longitude] = dp.data[Longitude];
errors++;
}
}
last_good = i;
}
/* fill to end... */
if (last_good != -1 && (unsigned)last_good != (a->num_points - 1)) {
good = a->data_points[last_good];
/* fill from last_good to end with last_good */
for (j = last_good + 1; j < a->num_points; j++) {
a->data_points[j].data[Latitude] = good.data[Latitude];
a->data_points[j].data[Longitude] = good.data[Longitude];
errors++;
}
}
if (errors) {
a->errors[InvalidGPS] = errors;
return errors;
} else {
return 0;
}
}
/*
*
* TODO FIX DROPOUTS which adds points should remove trksegs...
int fix_dropouts(Activity *a);
int fix_power(Activity *a);
int fix_heart_rate(Activity *a); // TODO probably needs HR max from athletes
and
other
*/