function animate_alias_rotors(ball_radius, rotor_center, scale, ...
                          speed_rev, speed_ratio, step_rev);

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

  t=0;

  ball_center_1 = rotor_center + [cos(t) sin(t)] .* scale; 
  ball_center_2 = rotor_center + ...
                  [cos(t*speed_ratio) sin(t*speed_ratio)] .* scale; 
  ball_center_3 = rotor_center + ...
                  [cos(t*(2*speed_ratio-1)) sin(t*(2*speed_ratio-1))] .* scale; 

  draw_ball(ball_center_1, ball_radius, 2);
  draw_ball(ball_center_2, ball_radius, 5);
  draw_ball(ball_center_3, ball_radius, 15);

  while %T do

      t = t + step_rad;

      old_ball_center_1 = ball_center_1;
      old_ball_center_2 = ball_center_2;
      old_ball_center_3 = ball_center_3;

      ball_center_1 = rotor_center + [cos(t) sin(t)] .* scale; 
      ball_center_2 = rotor_center + ...
                      [cos(t*speed_ratio) sin(t*speed_ratio)] .* scale; 
      ball_center_3 = rotor_center + ...
                  [cos(t*(2*speed_ratio-1)) sin(t*(2*speed_ratio-1))] .* scale; 
      xpause(anim_frame_delay);

      clear_ball(old_ball_center_1, ball_radius);
      clear_ball(old_ball_center_2, ball_radius);
      clear_ball(old_ball_center_3, ball_radius);

      draw_ball(ball_center_1, ball_radius, 2);
      draw_ball(ball_center_2, ball_radius, 5);
      draw_ball(ball_center_3, ball_radius, 15);

  end //while %T

//end animate_alias_rotors

function animate_inc_freq_sin(n_frames, max_st, step, ...
                              color, initial_freq, final_freq, phase);

  kp = xget("pixmap"); xset("pixmap", 1);

  plot2d(0, 0, [0], "012", " ", [0, -1, 5, 1]);

  freq_inc = (final_freq - initial_freq) / n_frames;

  st = 0:step:max_st;

  for fr = initial_freq:freq_inc:final_freq do

    sw = sin(2*%pi*fr*st+phase);

    xset("wwpc");

    plot2d(st, sw, [color], "001", " ");

    xset("wshow");

  end //for

  xset("pixmap", kp);

//end animate_inc_freq_sin

function animate_inc_freq_sin_ln_pts(n_frames, max_st, step, ...
                              line_color, point_color, ...
                              initial_freq, final_freq, phase);

  kp = xget("pixmap"); xset("pixmap", 1);

  plot2d(0, 0, [1],   "012", " ", [0, -1, 5, 1]);

  freq_inc = (final_freq - initial_freq) / n_frames;

  st = 0:step:max_st;

  for fr = initial_freq:freq_inc:final_freq do

    sw = sin(2*%pi*fr*st+phase);

    xset("wwpc");

    plot2d(st, sw, [line_color],   "001", " ");
    plot2d(st, sw, [-point_color], "001", " ");

    xset("wshow");

  end //for

  xset("pixmap", kp);

//end animate_inc_freq_sin_ln_pts

function animate_inc_freq_sin_pts(n_frames, max_st, step, ...
                              point_color, ...
                              initial_freq, final_freq, phase);

  kp = xget("pixmap"); xset("pixmap", 1);

  plot2d(0, 0, [1],   "012", " ", [0, -1, 5, 1]);

  freq_inc = (final_freq - initial_freq) / n_frames;

  st = 0:step:max_st;

  for fr = initial_freq:freq_inc:final_freq do

    sw = sin(2*%pi*fr*st+phase);

    xset("wwpc");

    plot2d(st, sw, [-point_color], "001", " ");

    xset("wshow");

  end //for

  xset("pixmap", kp);

//end animate_inc_freq_sin_pts

function animate_2_inc_freq_sin(n_frames, max_st, step_1, step_2, ...
                                color_1, color_2, point_color, ...
                                initial_freq, final_freq, phase);

  kp = xget("pixmap"); xset("pixmap", 1);

  plot2d(0, 0, [0], "012", " ", [0, -1, 5, 1]);

  freq_inc = (final_freq - initial_freq) / n_frames;

  st_1 = 0:step_1:max_st;

  st_2 = 0:step_2:max_st;

  for fr = initial_freq:freq_inc:final_freq do

    sw_1 = sin(2*%pi*fr*st_1+phase);
    sw_2 = sin(2*%pi*fr*st_2+phase);

    xset("wwpc");

    plot2d(st_1, sw_1, [color_1],      "001", " ");
    plot2d(st_2, sw_2, [color_2],      "001", " ");
    plot2d(st_2, sw_2, [-point_color], "001", " ");

    xset("wshow");

  end //for

  xset("pixmap", kp);

//end animate_2_inc_freq_sin

function animate_2_inc_freq_sin_p(n_frames, max_st, step_1, step_2, ...
                                color_1, point_color, ...
                                initial_freq, final_freq, phase);

  kp = xget("pixmap"); xset("pixmap", 1);

  plot2d(0, 0, [0], "012", " ", [0, -1, 5, 1]);

  freq_inc = (final_freq - initial_freq) / n_frames;

  st_1 = 0:step_1:max_st;

  st_2 = 0:step_2:max_st;

  for fr = initial_freq:freq_inc:final_freq do

    sw_1 = sin(2*%pi*fr*st_1+phase);
    sw_2 = sin(2*%pi*fr*st_2+phase);

    xset("wwpc");

    plot2d(st_1, sw_1, [color_1],      "001", " ");
    plot2d(st_2, sw_2, [-point_color], "001", " ");

    xset("wshow");

  end //for

  xset("pixmap", kp);

//end animate_2_inc_freq_sin_p

function inc_freq_sin_demo;

  animate_inc_freq_sin(100, 5, 0.01, 5, 0, 5, 0.1);

//end inc_freq_sin_demo

function inc_freq_samp_sin_lp_demo;

  animate_inc_freq_sin_ln_pts(500, 5, 0.1, 2, 15, 0, 10, 0.1);

//end inc_freq_samp_sin_lp_demo

function inc_freq_samp_sin_p_demo;

  animate_inc_freq_sin_pts(500, 5, 0.1, 15, 0, 10, 0.1);

//end inc_freq_samp_sin_p_demo

function inc_freq_2_sin_demo;

  animate_2_inc_freq_sin(500, 5, 0.01, 0.1, 5, 2, 15, 0, 10, 0.1);

//end inc_freq_2_sin_demo

function inc_freq_2_sin_p_demo;

  animate_2_inc_freq_sin_p(500, 5, 0.01, 0.1, 5, 15, 0, 10, 0.1);

//end inc_freq_2_sin_p_demo

function alias_helix_demo

  xselect(); xbasc();

  t            = (0:0.1:3.9*%pi)';

  helix_1      = helix(1., [1. 1.], [0. 3.9*%pi], 0.1);
  helix_tan_1  = helix_tan(1., [1. 1.], [0. 3.9*%pi], 0.1);
  helix_rad_1  = helix_rad(1., [1. 1.], [0. 3.9*%pi], 0.1)/15.;
  helix_perp_1 = normalize_rows(cross_product(helix_rad_1,helix_tan_1))/15.;

  helix_2      = helix(2.7, [1. 1.], [0. 3.9*%pi], 0.1);
  helix_tan_2  = helix_tan(2.7, [1. 1.], [0. 3.9*%pi], 0.1);
  helix_rad_2  = helix_rad(2.7, [1. 1.], [0. 3.9*%pi], 0.1)/15.;
  helix_perp_2 = normalize_rows(cross_product(helix_rad_1,helix_tan_1))/15.;

  tube_plot([helix_1 helix_2], [helix_rad_1 helix_rad_2], ...
	    [helix_perp_1 helix_perp_2], 8, [28 29 30 31 31 30 29 28]);

//end alias_helix_demo

function alias_rotor_demo

  expand_frame = 1.2;

  xset("window",1); xselect(); xclear();
  xstring(0., 0., ...
  [ ...
    'Animation of three circular rotors' ;
    ' '                                                ;
    'Notice how the three rotors hit the same point at' ;
    'regular intervals. If we sample at precisely those';
    'intervals, we cannot distinguish between these'    ;
    'rotors. The ambiguity of sampled signals is called';
    '""aliasing""'                                     ;
    ' '                                                ;
    'To control the animation, use the ""Stop"" and ""Resume""';
    'commands in the ""Control"" menu on the main Scilab';
    'window. Do not use ""Restart""---it will restart your';
    'whole Scilab session. To exit the animation, first';
    'stop it, then type ""quit"" to the main Scilab menu.'
  ]);

  xset("window",0); xselect(); xbasc();
  xtitle(['Animation of three circular rotors' ;
          '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_alias_rotors(0.05, [0 0], [1 1], 0.075, 7.3, 0.002);

//end alias_rotor_demo
