function timestampDecoded = timestampDecoder(timeStamps)
% converts metadata timestamps recorded in bonsai from point grey camera into seconds
%
% input: timeStamps: metadata timestamps recorded in bonsai
% output: times: seconds, normalized such that first time is 0
timeStamps(isnan(timeStamps(:,1)))=0;
% extract first cycle (first 7 bits)
cycle1 = bitshift(timeStamps, -25);
% extract second cycle (following 13 bits)
cycle2 = bitand(bitshift(timeStamps, -12), 8191) / 8000; % 8191 masks all but but last 13 bits
% account for overflows in counter
timestampDecoded = cycle1 + cycle2;
overflows = [0; diff(timestampDecoded) < 0];
timestampDecoded = timestampDecoded + (cumsum(overflows) * 128);
% offset such that first time is zero
timestampDecoded = timestampDecoded - min(timestampDecoded);
end

--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/142f3a98-eb60-41bb-8ed2-d606beaf57c9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/fce9c616-1f2d-4d53-9f3c-334d90716f22%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/90667b12-86c4-496e-8b0c-30edaa449696%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/9c7a3262-d5e7-44a5-8e8a-dbd53db1077f%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/1168b5d2-40b0-4a50-9cd4-39f34944f9e1%40googlegroups.com.