|
105 | 105 | signals = extract_accdata( fileContents(fbodyInd), firmware, ...
|
106 | 106 | compression, Data.meta.axes );
|
107 | 107 |
|
108 |
| - % Check number of data points |
109 |
| - [signals, Data.meta] = correct_length(signals, Data.meta, filePath); |
| 108 | + % Check sample rate |
| 109 | + try |
| 110 | + Data.meta.hz = correct_hz(length(signals), Data.meta.hz, ... |
| 111 | + Data.meta.duration); |
| 112 | + catch ME |
| 113 | + if strcmp(ME.identifier, 'load_datx:samplerateError') |
| 114 | + msg = ['The sample rate is outside the excepted range in file:\n' ... |
| 115 | + '%s\n' ... |
| 116 | + 'Please report this to the developers at:\n' ... |
| 117 | + 'https://github.com/R-Broadley/activpal_utils-matlab/issues']; |
| 118 | + ME = MException(ME.identifier, msg, filePath); |
| 119 | + end |
| 120 | + throw(ME); |
| 121 | + end |
110 | 122 |
|
111 | 123 | % Remove invalid rows
|
112 | 124 | signals = clean(signals, 254);
|
|
263 | 275 | end
|
264 | 276 |
|
265 | 277 |
|
266 |
| -function [signals, meta] = correct_length(signals, meta, filePath) |
267 |
| - nsamples = length(signals); |
268 |
| - nexpected = seconds(meta.duration) * double(meta.hz); |
269 |
| - diffSamples = nsamples - nexpected; |
270 |
| - threshold = 5 * 60 * double(meta.hz); % 5 minutes |
271 |
| - if diffSamples < threshold && diffSamples > 0 % diff < 5 minutes && + |
272 |
| - % Shorten signals to length specified in duration |
273 |
| - signals = signals(1 : nexpected, :); |
274 |
| - elseif diffSamples > -threshold && diffSamples < 0 % diff < 5 minutes && - |
275 |
| - % Adjust duration and stoptime to match nsamples in data |
276 |
| - diffSeconds = diffSamples / 20; |
277 |
| - meta.duration = meta.duration + seconds(diffSeconds); |
278 |
| - meta.stopTime = meta.stopTime + seconds(diffSeconds); |
| 278 | +function hz = correct_hz(nsamples, hz, duration) |
| 279 | + allowedVariability = 0.005; % 0.5% |
| 280 | + maxAllowedHz = double(hz) * (1 + allowedVariability); |
| 281 | + minAllowedHz = double(hz) * (1 - allowedVariability); |
| 282 | + avgHz = nsamples / seconds(duration); |
| 283 | + |
| 284 | + if avgHz > minAllowedHz && avgHz < maxAllowedHz |
| 285 | + hz = avgHz; |
279 | 286 | else
|
280 |
| - % Raise error due to large discrepancy |
281 |
| - msgID = 'load_datx:fileError'; |
282 |
| - msgText = ['There are fewer data points than expected in file:\n' ... |
283 |
| - '%s \n' ... |
284 |
| - 'Please report this to the developers at:\n' ... |
285 |
| - 'https://github.com/R-Broadley/activpal_utils-matlab/issues']; |
286 |
| - ME = MException(msgID, msgText, filePath); |
| 287 | + ME = MException('load_datx:samplerateError', ''); |
287 | 288 | throw(ME);
|
288 | 289 | end
|
289 | 290 | end
|
|
0 commit comments