-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtractMeasurements.m
37 lines (33 loc) · 1.18 KB
/
ExtractMeasurements.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
% Data pre-processing.
% Transforms the integer value raw data of the magnetic sensor into a
% vector of binary bits. One bit with value noMagnetDetected is added
% to the left and to the right of the bit vector so that presence of a
% magnet always results in two transitions from noMagnetDetected to
% magnetDetected and vice versa.
% The measurements are the values located mid-way between the two
% transitions.
function measurements = ExtractMeasurements( ...
rawSensorData, nbReedSensors, magnetDetected )
bitVector = [ not(magnetDetected) bitget(rawSensorData,1:nbReedSensors) not(magnetDetected)] ;
vectSize = nbReedSensors + 2 ;
% Look for first transition in bit vector, if any
i = 2 ;
while (i<=vectSize) && (bitVector(i)==not(magnetDetected))
i = i+1 ;
end
nbMeasurements = 0 ;
measurements = zeros(1,2) ;
while i < vectSize
indBegin = i ;
while bitVector(i)==magnetDetected
i = i+1 ;
end
indEnd = i-1 ;
nbMeasurements = nbMeasurements + 1 ;
measurements(nbMeasurements) = (indBegin+indEnd)/2 - 1 ;
while (i<vectSize) && (bitVector(i)==not(magnetDetected))
i = i+1 ;
end
end
measurements = measurements(1:nbMeasurements) ;
return