
%   QUANTIZE_DATA loads the true data in pupil.dat and quantizes it into 6
%   bins, to simulate sampling a signal with an A/D board with poor
%   resolution.  The signal x is the true pupil signal quantized into a
%   numBins signal ranging from minVal to maxVal

%   Copyright 2003
%   Centre for Nonlinear Dynamics in Physiology and Medicine

load pupil.dat

% Make sure the bins span the range of the signal
minVal = min(pupil)-5;
maxVal = max(pupil)+5;

numBins = 5;
bins = linspace(minVal, maxVal, numBins);

for k = [1:numBins-1]
  ind = find(pupil > bins(k) & pupil <= bins(k+1));
  x(ind) = bins(k);
end

save quantized.dat x -ascii


% Now do the same trick but add noise before quantization
% add noise with a std = 0.35 * the quantization interval
interval = bins(2) - bins(1);
sigma = 0.35*interval;
nse = sigma*randn(length(x),1); 
pupilNse = pupil + nse;

for k = [1:numBins-1]
  ind = find(pupilNse > bins(k) & pupilNse <= bins(k+1));
  xNse(ind) = bins(k);
end

save quantized_nse.dat xNse -ascii