function phasegroupdelay(speed)
% phasegroupdelay(speed)   Visualize difference between phase and group delay
% 2004-10-21 Dan Ellis dpwe@ee.columbia.edu

if nargin < 1
  speed = 1;
end

% Baseline carrier and modulator
wc = 2*pi/10;
wm = 2*pi/200;
% initial phases
pc = 0;
pm = 0;

% Time base
nn = 0:400;
% total number of cycles of carrier to shift by
ncyc = 10;

% envelope
e = cos(wm*nn+pm);
% carrier modulated by envelope
x = e .* cos(wc*nn+pc);

subplot(211)
plot(nn,x,nn,e,'--r')
grid

% Make sure double buffering is on for this figure
set(gcf, 'DoubleBuffer', 'On');

% First, shift envelope
subplot(212)
pause

maxp = 2*pi*ncyc*(wm/wc);
for dp = 0:maxp/speed:maxp

  pm = -dp;
  % envelope
  e = cos(wm*nn+pm);
  % carrier modulated by envelope
  x = e .* cos(wc*nn+pc);

  plot(nn,x,nn,e,'--r')
  grid on
  drawnow;

end

% Then shift carrier
subplot(212)
pause

maxp = 2*pi*ncyc;
for dp = 0:maxp/speed:maxp

  pc = -dp;
  % envelope
  e = cos(wm*nn+pm);
  % carrier modulated by envelope
  x = e .* cos(wc*nn+pc);

  plot(nn,x,nn,e,'--r')
  grid on
  drawnow;

end

