% Simulation of digital communication system with discrete-amplitude noise:
% Sample code to get you started.
% 
% Rich Kozick, Spring 1999

% Generate signal

  z = unifrnd(0, 1);
  if (z < 0.5)
    s = -1;
  else
    s = +1;
  end

% Generate noise

  z = unifrnd(0, 1);
  if (z < 0.1)
    n = -2;
  elseif (z < 0.3)
    n = -1;
  elseif (z < 0.7)
    n = 0;
  elseif (z < 0.9)
    n = +1;
  else
    n = +2;
  end

% Generate observations

  x = s + n;

% Decision algorithm for each bit:
% based on choosing +1 when x = 0.

  if (x <= -1)
    shat = -1;
  else
    shat = +1;
  end

if (shat_all ~= s)
  fprintf('Error\n')
else
  fprintf('Correct\n');
end

