function disc_x = discretize(x, tolerance);

  disc_x = round(x/tolerance)*tolerance;

//end discretize

function disc_x = discretize_log(x, tolerance);

  disc_x = zeros(x);

  for i = 1:size(x,1), for j = 1:size(x,2),

    if (abs(x(i,j)) > exp(-1000000000)) then
    
      disc_x(i,j) = ...
        sign(x(i,j))*exp(round(log(abs(x(i,j)))/tolerance)*tolerance);

    end, //if

  end, end //for i for j

//end discretize_log

function animate_rounded_rotor(ball_radius, rotor_center, scale, ...
			       speed_rev, step_rev, tolerance)

  anim_frame_delay = step_rev/speed_rev * 1000000; // microseconds
  step_rad  = step_rev*2*%pi;                      // radians per frame

  t=0;

  ball_center = rotor_center + ...
                discretize([cos(t) sin(t)], tolerance) .* scale; 

  draw_ball(ball_center, ball_radius, 2);

  while %T do

      t = t + step_rad;

      old_ball_center = ball_center;

      ball_center = rotor_center + ...
                    discretize([cos(t) sin(t)], tolerance) . *scale; 

      xpause(anim_frame_delay);

      clear_ball(old_ball_center, ball_radius);

      draw_ball(ball_center, ball_radius, 2);

  end //while %T

//end animate_rounded_rotor

function round_sin_demo;

  xselect(); xbasc();

  t = 0:0.001:2*%pi;

  rs = discretize(sin(t), 0.1);

  plot2d(t, rs, 2, "011", " ", [0 -1.2 2*%pi 1.2]);

//end round_sin_demo

function round_vs_sin_demo;

  xselect(); xbasc();

  t = 0:0.001:2*%pi;

  rs = discretize(sin(t), 0.1);
  s  = sin(t);

  plot2d(t, rs, 2, "011", " ", [0 -1.2 2*%pi 1.2]);
  plot2d(t, s, 15, "011", " ", [0 -1.2 2*%pi 1.2]);

//end round_vs_sin_demo

function round_vs_sin_vs_err_demo;

  xselect(); xbasc();

  t = 0:0.001:2*%pi;

  rs = discretize(sin(t), 0.1);
  s  = sin(t);
  es = rs - s;

  plot2d(t, rs, 2, "011", " ", [0 -1.2 2*%pi 1.2]);
  plot2d(t, s, 15, "001", " ");
  plot2d(t, es, 5, "001", " ");

//end round_vs_sin_vs_errdemo

function dither_demo;

  xselect(); xbasc();

  tol = 0.1;

  t = 0:0.01:2*%pi;

  di = rand(t,'normal')*tol/2;
//  di = (rand(t,'uniform')-0.5)*tol;

  plot2d(t, di, 2, "011", " ", [0 -1.2 2*%pi 1.2]);

//end dither_demo;

function round_sin_dither_demo;

  xselect(); xbasc();

  tol = 0.1;

  t = 0:0.01:2*%pi;

  di = rand(t,'normal')*tol/2;
//  di = (rand(t,'uniform')-0.5)*tol;
  ds = discretize(sin(t)+di, tol);

  plot2d(t, ds, 2, "011", " ", [0 -1.2 2*%pi 1.2]);

//end round_sin_dither_demo

function round_sin_dither_vs_err_demo;

  xselect(); xbasc();

  tol = 0.1;

  t = 0:0.01:2*%pi;

  di = rand(t,'normal')*tol/2;
//  di = (rand(t,'uniform')-0.5)*tol;
  ds = discretize(sin(t)+di, tol);
  s  = sin(t);
  es = ds-s;

  plot2d(t, ds, 2, "011", " ", [0 -1.2 2*%pi 1.2]);
  plot2d(t, es, 5, "011", " ");
  plot2d(t, s, 15, "011", " ");

//end round_sin_dither_vs_err_demo

function silent_sin_dither_demo;

  xselect(); xbasc();

  tol = 0.1;

  t = 0:0.01:2*%pi;

  di = rand(t,'normal')*tol/2;
//  di = (rand(t,'uniform')-0.5)*tol;
  s  = sin(t)*tol/2;
  ds = discretize(s+di, tol);

  plot2d(t, ds, 2, "011", " ", [0 -0.25 2*%pi 0.25]);
  plot2d(t, s, 15, "011", " ");
  plot2d(t, discretize(s, tol), 5, "011", " ");

//end silent_sin_dither_demo

function longer_silent_sin_dither_demo;

  xselect(); xbasc();

  tol = 0.1;

  t = 0:0.02:7*%pi;

  di = rand(t,'normal')*tol/2;
  s  = sin(t)*tol/2;
  ds = discretize(s+di, tol);

  plot2d(t, ds, 2, "011", " ", [0 -0.25 7*%pi 0.25]);
  plot2d(t, s, 15, "011", " ");
  plot2d(t, discretize(s, tol), 5, "011", " ");

//end longer_silent_sin_dither_demo

function round_log_sin_demo;

  xselect(); xbasc();

  t = 0:0.001:2*%pi;

  rs = discretize_log(sin(t), 0.3);

  plot2d(t, rs, 2, "011", " ", [0 -1.2 2*%pi 1.2]);

//end round_log_sin_demo

function round_log_vs_sin_demo;

  xselect(); xbasc();

  t = 0:0.001:2*%pi;

  rs = discretize_log(sin(t), 0.3);
  s  = sin(t);

  plot2d(t, rs, 2, "011", " ", [0 -1.2 2*%pi 1.2]);
  plot2d(t, s, 15, "011", " ", [0 -1.2 2*%pi 1.2]);

//end round_log_vs_sin_demo

function round_log_vs_sin_vs_err_demo;

  xselect(); xbasc();

  t = 0:0.001:2*%pi;

  rs = discretize_log(sin(t), 0.3);
  s  = sin(t);
  es = rs - s;

  plot2d(t, rs, 2, "011", " ", [0 -1.2 2*%pi 1.2]);
  plot2d(t, s, 15, "001", " ");
  plot2d(t, es, 5, "001", " ");

//end round_log_vs_sin_vs_errdemo

function small_round_log_sin_demo;

  xselect(); xbasc();

  t = 0:0.001:2*%pi;

  rs = discretize_log(sin(t*10)*0.05, 0.3);
  s  = sin(t*10)*0.05;

  plot2d(t, rs, 2, "011", " ", [0 -1.2 2*%pi 1.2]);
  plot2d(t, s, 15, "001", " ");

//end small_round_log_sin_demo;

function round_log_2_sin_demo;

  xselect(); xbasc();

  t = 0:0.001:2*%pi;

  rs = discretize_log(sin(t*10)*0.05 + sin(t/2), 0.3);
  s  = sin(t*10)*0.05 + sin(t/2);
  es = rs - s;

  plot2d(t, rs, 2, "011", " ", [0 -1.2 2*%pi 1.2]);
  plot2d(t, s, 15, "001", " ");
  plot2d(t, es, 5, "001", " ");

//end round_log_2_sin_demo;

function rounded_rotor_demo;

  expand_frame = 1.2;

  xset("window",0); xselect(); xbasc();
  xtitle(['Animation of rounded rotor' ;
          'Use stop and resume on the Control menu' ;
          'in the main Scilab window to pause.' ;
          'When stopped, type quit to finish.']);
  square(-1,1,-1,1);
  plot2d(0,0,-1,"031"," ",[-1 -1 1 1]*expand_frame);

  animate_rounded_rotor(0.05, [0 0], [1 1], 0.05, 0.01, 0.1);

//end rounded_rotor_demo
