function varargout = impz(f,varargin)
%IMPZ  DFILT object impulse response.
%   [H,T] = IMPZ(F) computes the impulse response of the DFILT
%   object F, and returns it in vector H.  The vector of time
%   samples at which H is evaluated is returned in vector T.
%
%   [H,T] = IMPZ(F,N) computes the first N samples of the impulse
%   response.
%
%   [H,T] = IMPZ(F,N,Fs) separates the time samples by T = 1/Fs.
%   Fs is assumed to be in Hz.
%
%   IMPZ(...) with no output arguments plots the impulse response.
%
%   See also DFILT/STEPZ, DFILT/FREQZ, DFILT/ZPLANE, DFILT/GRPDELAY.

%   Author(s): R. Losada
%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.3 $  $Date: 2000/06/13 21:21:01 $ 


switch f.connection,
case 'cas',
    % Initialize h
    h = 1;
    for n = 1:nsections(f),
        fsec = section(f,n);
        [b,a] = tf(fsec);
        
        % Find the impulse response of each section
        ht = impz(b,a,varargin{:});
                
        % Convolve impulse responses to get overall impz
        h = conv(h,ht);
        
        % Truncate the impulse response to N, is N was specified
        if nargin > 1,
            h = h(1:varargin{1});
        end
    end
    
case 'par',
    % Initialize h
    h = 0;
    for n = 1:nsections(f),
        fsec = section(f,n);
        [b,a] = tf(fsec);
        
        % Find the impulse response of each section
        ht = impz(b,a,varargin{:});
        % Pad with zeros
        h = [h;zeros(length(ht)-length(h),1)];
        ht = [ht;zeros(length(h)-length(ht),1)];

        % Add all impulse resps. to get overall impz
        h = h + ht;
    end
end

% Create sample vector
t = 0:length(h)-1;

% Initialize Fs
Fs = 1;
xlbl = 'n (samples)';
if nargin == 3,
    Fs = varargin{2};
    xlbl = 'nT (seconds)';
end

% Scale time by Fs (convert n to n*T)
t = t./Fs;

if nargout == 0,
    newplot;
    stem(t,h,'filled');
    xlabel(xlbl);
    ylabel('Impulse response');
else
    varargout = {h,t};
end

