Skip to content

Commit 13e4433

Browse files
committed
Improved handling of activPAL time inaccuracy
Fixes #2
1 parent ad962bd commit 13e4433

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

load_datx.m

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,20 @@
105105
signals = extract_accdata( fileContents(fbodyInd), firmware, ...
106106
compression, Data.meta.axes );
107107

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
110122

111123
% Remove invalid rows
112124
signals = clean(signals, 254);
@@ -263,27 +275,16 @@
263275
end
264276

265277

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;
279286
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', '');
287288
throw(ME);
288289
end
289290
end

0 commit comments

Comments
 (0)