Skip to content
This repository was archived by the owner on May 8, 2025. It is now read-only.

Commit d80df6f

Browse files
committed
Merge pull request #34 from adriangibbons/data-every-second
Data every second
2 parents e300a38 + a8eea31 commit d80df6f

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,10 @@ In addition, public functions provide a short-hand way to access commonly used e
114114
- sport()
115115

116116
###Optional Parameters
117-
There are four optional parameters that can be passed as an associative array when the phpFITFileAnalysis object is instantiated. These are:
117+
There are five optional parameters that can be passed as an associative array when the phpFITFileAnalysis object is instantiated. These are:
118118

119119
- fix_data
120+
- data_every_second
120121
- units
121122
- pace
122123
- garmin_timestamps
@@ -125,6 +126,7 @@ For example:
125126
```php
126127
$options = [
127128
'fix_data' => ['cadence', 'distance'],
129+
'data_every_second' => true
128130
'units' => 'statute',
129131
'pace' => true,
130132
'garmin_timestamps' => true
@@ -208,6 +210,21 @@ The result would be:
208210
var_dump($pFFA->data_mesgs['record']['distance']); // ['100'=>3.62, '101'=>4.01, '102'=>6.30, '103'=>8.59, '104'=>10.88];
209211
```
210212

213+
####Data Every Second
214+
Some of Garmin's Fitness devices offer the choice of Smart Recording or Every Second Recording.
215+
216+
Smart Recording records key points where the fitness device changes direction, speed, heart rate or elevation. This recording type records less track points and will potentially have gaps between timestamps of greater than one second.
217+
218+
You can force timestamps to be regular one second intervals by setting the option:
219+
```php
220+
$options = ['data_every_second' => true];
221+
```
222+
Missing timestamps will have data interpolated as per the ```fix_data``` option above.
223+
224+
If the ```fix_data``` option is not specified in conjunction with ```data_every_second``` then ```'fix_data' => ['all']``` is assumed.
225+
226+
*Note that you may experience degraded performance using the ```fix_data``` option. Improving the performance will be explored - it is likely the ```interpolateMissingData()``` function is sub-optimal.*
227+
211228
####Set Units
212229
By default, **metric** units (identified in the table below) are assumed.
213230

src/phpFITFileAnalysis.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,10 +1152,25 @@ private function fixData($options)
11521152
}
11531153

11541154
// Return if no option set
1155-
if (empty($options['fix_data'])) {
1155+
if (empty($options['fix_data']) && empty($options['data_every_second'])) {
11561156
return;
11571157
}
11581158

1159+
// If $options['data_every_second'], then create timestamp array for every second from min to max
1160+
if (!empty($options['data_every_second']) && !(is_string($options['data_every_second']) && strtolower($options['data_every_second']) === 'false')) {
1161+
// If user has not specified the data to be fixed, assume all
1162+
if (empty($options['fix_data'])) {
1163+
$options['fix_data'] = ['all'];
1164+
}
1165+
1166+
$min_ts = min($this->data_mesgs['record']['timestamp']);
1167+
$max_ts = max($this->data_mesgs['record']['timestamp']);
1168+
unset($this->data_mesgs['record']['timestamp']);
1169+
for ($i=$min_ts; $i<=$max_ts; ++$i) {
1170+
$this->data_mesgs['record']['timestamp'][] = $i;
1171+
}
1172+
}
1173+
11591174
// Check if valid option(s) provided
11601175
array_walk($options['fix_data'], function (&$value) {
11611176
$value = strtolower($value);
@@ -1274,8 +1289,9 @@ private function interpolateMissingData(&$missing_keys, &$array)
12741289

12751290
$min_key = min(array_keys($array));
12761291
$max_key = max(array_keys($array));
1292+
$count = count($missing_keys);
12771293

1278-
for ($i=0; $i<count($missing_keys); ++$i) {
1294+
for ($i=0; $i<$count; ++$i) {
12791295
if ($missing_keys[$i] !== 0) {
12801296
// Interpolating outside recorded range is impossible - use edge values instead
12811297
if ($missing_keys[$i] > $max_key) {
@@ -1292,7 +1308,7 @@ private function interpolateMissingData(&$missing_keys, &$array)
12921308
$prev_value = current($array);
12931309
$next_value = next($array);
12941310
}
1295-
for ($j=$i+1; $j<count($missing_keys); ++$j) {
1311+
for ($j=$i+1; $j<$count; ++$j) {
12961312
if ($missing_keys[$j] < key($array)) {
12971313
$num_points++;
12981314
} else {
@@ -1656,7 +1672,8 @@ public function partitionData($record_field = '', $thresholds = null, $percentag
16561672

16571673
foreach ($this->data_mesgs['record'][$record_field] as $value) {
16581674
$key = 0;
1659-
for ($key; $key<count($thresholds); ++$key) {
1675+
$count = count($thresholds);
1676+
for ($key; $key<$count; ++$key) {
16601677
if ($value < $thresholds[$key]) {
16611678
$result[$key]++;
16621679
goto loop_end;
@@ -1670,7 +1687,8 @@ public function partitionData($record_field = '', $thresholds = null, $percentag
16701687
$keys = [];
16711688

16721689
if ($labels_for_keys === true) {
1673-
for ($i=0; $i<count($thresholds); ++$i) {
1690+
$count = count($thresholds);
1691+
for ($i=0; $i<$count; ++$i) {
16741692
$keys[] = $thresholds[$i] . (isset($thresholds[$i+1]) ? '-'.($thresholds[$i+1] - 1) : '+');
16751693
}
16761694
$result = array_combine($keys, $result);

0 commit comments

Comments
 (0)