% FUNCTION separateData
%
% PURPOSE: convert data vector into two synchronized vectors of RSS
%    measurements at the two nodes.  Deletes 'extra' measurements.
%
% INPUT: 'data' 
%    Column 1: node id measuring RSS
%    Column 2: the measured RSS
%    Order: data is ordered as received from the nodes.  Typically node ids will alternate
%        but once in a while a dropped packet will cause two RSS measurements at
%        the same id in a row, one is an 'extra' measurement.
%
% OUTPUTS: 
%     rssa: RSS at node a, the first node to report RSS
%     rssb: RSS at node b, the second node to report RSS
%
% AUTHOR: Neal Patwari, npatwari at ece dot utah dot edu
%

function [rssa, rssb] = separateData(data)

countera = 1;  % Row in the rssa vector
counterb = 1;  % Row in the rssb vector
ind      = 1;  % row in the data matrix
currID   = 0;  % most current id number
len      = length(data);

% Preallocate memory for the outputs.  Probably allocating too many rows.
approxRSSLen = ceil(len/2);
rssa = zeros(1,approxRSSLen);
rssb = zeros(1,approxRSSLen);

% Read line by line through 'data'.
while ind <= len;
    
    % Skip any repeat RSS readings from the same node.  Happens only infrequently
    while (ind <= len) && (data(ind,1) == currID),
        ind = ind + 1;
    end
    
    % Save the data into the appropriate of the two output vectors.
    currID = data(ind,1);
    if currID == 1,
        rssa(countera) = data(ind,2);
        countera       = countera + 1;
    else
        rssb(counterb) = data(ind,2);
        counterb       = counterb + 1;
    end
    ind = ind + 1;
end

% Cut out the extra rows at the end of the rssa and rssb vectors.
minRSSLen = min(countera, counterb)-1;
rssa      = rssa(1:minRSSLen-1);
rssb      = rssb(1:minRSSLen-1);

% flip the two if needed so that node a is the first to report rss and node
% b is the second.
if data(1,1) == 2,
    temp = rssb;
    rssb = rssa;
    rssa = temp;
end