function x = dtmf(varargin)
%x = dtmf(il,ih,td,tp,Fs,var)
%x = dtmf(il,ih,td,tp,Fs)
%x = dtmf(il,ih)
%x = dtmf(il,ih,td)
%x = dtmf(il,ih,td,tp)
%generate a dtmf waveform
%Inputs:
%il -> index for low frequency  [1-4]
%ih -> index for high frequency [1-4]
%td -> desired time for dual-tone (seconds)
%      (default = min=.04 seconds)
%tp -> desired time for pause after tone (seconds)
%      (default = min=.04 seconds)
%Fs -> Sampling Frequency Hz (default is 8000)
%var -> Vary frequency {0 -> don't ,1 -> uniform +/- 1.5 %,
%       var < 0 -> |var| percent}
%dtmf.m
%author: Nicholas Kottenstette
%date:   11/17/2004
  switch(nargin)
   case 2
    x = dtmf(varargin{:},.04,.04,8000,0);
    return;
   case 3
    x = dtmf(varargin{:},.04,8000,0);
    return;
   case 4
    x = dtmf(varargin{:},8000,0);
    return;
   case 5
    x = dtmf(varargin{:},0);
    return;
   case 6
    [il,ih,td,tp,Fs,var] = varargin{:};
    if td < .04
      td = .04;
    end
    if tp < .04
      tp = .04;
    end
    if Fs < 4000
      Fs = 8000;
    end
    if (il<1) | (il>4)
      il = 1;
    end
    if (ih<1) | (ih>4)
      ih = 1;
    end
   otherwise
    error('dtmf:invalid_input','%s',help('dtmf'));
  end
  if var == 0
    perl = 0;
    perh = 0;
  elseif var == 1
    perl = random('uniform',-.015,.015);
    perh = random('uniform',-.015,.015);
  elseif var < 0
    perl = .01*var;
    perh = perl;
  else
    perl = 0;
    perh = 0;
  end
  fl = (1+perl)*[697 770 852 941];
  fh = (1+perh)*[1209 1336 1477 1633];
  wl = 2*pi*fl(il)/Fs;
  wh = 2*pi*fh(ih)/Fs;
  Nd = ceil(Fs*td);
  Np = ceil(Fs*tp);
  x = zeros(Nd+Np,1);
  xl = zeros(Nd,1);
  xl(2,1) = sin(wl);
  xh = zeros(Nd,1);
  xh(2,1) = sin(wh);
  hl = [-1 2*cos(wl)];
  hh = [-1 2*cos(wh)];
  for i = 3:Nd
    xl(i) = hl*xl(i-2:i-1,1);
    xh(i) = hh*xh(i-2:i-1,1);
  end  
  x(1:Nd,1) = (xl + xh*(10^(2/20)));
  x = x*2/(max(x)-min(x));