function y = chirp(t,f0,t1,f1,method,phi)
%CHIRP  Swept-frequency cosine generator.
%   Y = CHIRP(T,F0,T1,F1) generates samples of a linear swept-frequency
%   signal at the time instances defined in array T.  The instantaneous
%   frequency at time 0 is F0 Hertz.  The instantaneous frequency F1
%   is achieved at time T1.  By default, F0=0, T1=1, and F1=100.
%
%   Y = CHIRP(T,F0,T1,F1,method) specifies alternate sweep methods.
%   Available methods are 'linear','quadratic', and 'logarithmic'; the
%   default is 'linear'.  Note that for a log-sweep, F1>F0 is required.
%
%   Y = CHIRP(T,F0,T1,F1,method, PHI) allows an initial phase PHI to
%   be specified in degrees.  By default, PHI=0.
%
%   Y = CHIRP(T,P) specifies a polynomial vector P for the
%   instantaneous frequency trajectory of the chirp.  P may not be
%   a scalar value.
%
%   Default values are substituted for empty or omitted trailing input
%   arguments.
%
%   EXAMPLE 1: Compute the spectrogram of a linear chirp.
%     t=0:0.001:2;                 % 2 secs @ 1kHz sample rate
%     y=chirp(t,0,1,150);          % Start @ DC, cross 150Hz at t=1sec 
%     specgram(y,256,1E3,256,250); % Display the spectrogram
%
%   EXAMPLE 2: Compute the spectrogram of a quadratic chirp.
%     t=-2:0.001:2;                % +/-2 secs @ 1kHz sample rate
%     y=chirp(t,100,1,200,'q');    % Start @ 100Hz, cross 200Hz at t=1sec 
%     specgram(y,128,1E3,128,120); % Display the spectrogram
%
%   EXAMPLE 3: Compute the spectrogram of a polynomial chirp.
%     t=[0 0.5 1.0 1.5 2.0]; % time breakpoints
%     f=[0 200 100 150 300];  % instantaneous frequency breakpoints
%     p=polyfit(t,f,4);      % fit 4th order polynomial over time
%     t=0:0.001:2;           % 2 secs @ 1kHz sample rate
%     y=chirp(t,p);
%     subplot(211); plot(t,polyval(p,t)); set(gca,'ylim',[0 500]);
%     subplot(212); specgram(y,128,1E3,128,120);
%
%   See also GAUSPULS, SAWTOOTH, SINC, SQUARE.

%   Author(s): D. Orofino, T. Krauss, 3/96
%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.7 $  $Date: 2000/06/09 22:03:53 $

% Parse inputs, and substitute for defaults:
error(nargchk(1,6,nargin));
if nargin<6, phi=[]; end
if nargin<5, method=[]; end
if nargin<4, f1=[]; end
if nargin<3, t1=[]; end
if nargin<2, f0=[]; end
if isempty(phi), phi=0; end
if isempty(method), method='linear'; end
if isempty(f1), f1=100; end
if isempty(t1), t1=1; end
if isempty(f0), f0=0; end

% Parse the method string:

if length(f0)>1,
   method='polynomial';
else
   % Set p=1 for linear, 2 for quadratic, 3 for logarithmic
   strs = {'linear','quadratic','logarithmic'};
   p = strmatch(lower(method),strs);
   if isempty(p),
      error('Unknown method selected.');
   elseif length(p)>1,
      error('Ambiguous method selected.');
   end
   method = strs{p};
end

switch method
case 'polynomial'
   % Polynomial chirp
   y = cos( 2*pi * polyval(polyint(f0),t) );
   
case {'linear','quadratic'},
  % Polynomial chirp: p is the polynomial order
  beta = (f1-f0).*(t1.^(-p));
  y = cos(2*pi * ( beta./(1+p).*(t.^(1+p)) + f0.*t + phi/360));
  
case 'logarithmic',
  % Logarithmic chirp:
  if f1<f0, error('F1>F0 is required for a log-sweep.'); end
  beta = log10(f1-f0)/t1;
  y = cos(2*pi * ( (10.^(beta.*t)-1)./(beta.*log(10)) + f0.*t + phi/360));
  
end

% [EOF] chirp.m