function [floats, times] = convfloats(c, t, preCode) % function [floats, times] = convfloats(c, t, preCode) % % c The encodes in the cortex data file you would like to extract % floating point information from % t The times that the corresponding encodes were output % preCode Optional argument that causes the program to extract only the % integers which follow this encoded value. This allows you to % "mark" different integers as having different meanings in the % data file. % % Given a set of Matlab encode values and their times, % read out the floating point numbers and store them in the floats array, % along with their time of occurrence. This code is used to extract % information embedded in the data file with the routines in "extenc.h" % and "extenc.c" % % If preCode is given, then only get integers which follow % preCode in the data. Effectively, this replaces INT_MARKER % with preCode. % % Because of the way Cortex encodes things, intervening codes can be % mixed with floating point data. You must be careful to never have % two threads running which both try to encode floating point values, % as the resulting data file can intermingle the bytes of two separate % floats, making it impossible to later decode. % % This code needs access to either a global structure Encodes, or % an m-file called 'encodes.m' that creates the structure. % % Version 1.0 % For additional code, updated versions, questions or comments, please refer % to the Active Perception Lab home page % http://www.cnbc.cmu.edu/~tai/lab.html % Copyright 1998-1999 % Rick Romero % rickr+@cmu.edu % Permission to use and modify is granted, % but not to redistribute original or modified code. % Assumes that floats are coded as follows: % All floats are marked by a FLOAT_MARKER (125) before the float data % The float data is encoded using four encodes, ENCODE_FLOAT[1-4] in % increasing order of byte number. % Some encode values, just so we have them if (exist('Encodes') == 0) global Encodes evalin('caller','global Encodes;encodes'); end if (nargin < 3) preCode = Encodes.FLOAT_MARKER; end c = c(:)'; t = t(:)'; which = find(c == preCode); times = t(which); numfloats = prod(size(which)); floats = zeros(numfloats, 1); for i=1:numfloats if (i == numfloats) cndx = which(i):prod(size(c)); else cndx = which(i):(which(i+1)-1); end cc = c(cndx); cencodes = find((cc >= Encodes.ENCODE_FLOAT1) & ... (cc < Encodes.ENCODE_FLOAT4 + 256)); if (prod(size(cencodes)) ~= 4) error('Problem reading in float data--wrong number of markers'); end if ((cc(cencodes(1)) >= Encodes.ENCODE_FLOAT2) | ... (cc(cencodes(2)) >= Encodes.ENCODE_FLOAT3) | ... (cc(cencodes(3)) >= Encodes.ENCODE_FLOAT4) | ... (cc(cencodes(4)) < Encodes.ENCODE_FLOAT4) | ... (cc(cencodes(3)) < Encodes.ENCODE_FLOAT3) | ... (cc(cencodes(2)) < Encodes.ENCODE_FLOAT2)) errmsg = sprintf('Out of order float data: received following encodes (0x%X 0x%X 0x%X 0x%X)', cc(cencodes(1)), cc(cencodes(2)), cc(cencodes(3)), cc(cencodes(4))); error(errmsg); end cencodes = cc(cencodes); cencodes = fliplr(bitand(cencodes, 255)); if (cencodes(1) > 127) fsign = -1; else fsign = 1; end fexp = 2 * bitand(cencodes(1), 127) + bitget(cencodes(2), 8) - 127; if (all(cencodes == 0)) floats(i) = 0; else fmant = 1 + bitor(... bitshift(... bitor(bitshift(bitand(cencodes(2), 127), 8), ... cencodes(3)), 8), ... cencodes(4)) / bitshift(1, 23); floats(i) = fsign * fmant * (2 ^ fexp); end end