function [imptext, implatex] = imp2latex(h, N, offset)
% IMP2LATEX prints impulse response in text and LaTeX
%
%   [imptext, implatex] = imp2latex(h, N, offset)
%
%    imptext     h[n] as text
%    implatex    h[n] as LaTeX code
%    h           impulse response
%    N           number of samples in IIR h[n] shown;
%                if vector, then N = length(N), and offset = N(1)
%    offset      first nonzero element (from left) is h[offset];
%                if vector, then offset(1) is offset value above
%
% IMP2LATEX does NOT compute values of impulse response,
% that must be done in advance by using IMPZ.
%
% IMP2LATEX can be used to print any sequence with delta
% notation.
%
% The additional dots "..." are added if length(h) > N,
% i.e. having an infinite long sequence,
% probably the case when having IIR. 
% 
% Example:
%    x = randn(100,1);          % random sequence
%    imp2latex(x, 50, 6)        % 50 *first* samples starting from 6
%    imp2latex(x(1:50), 50, -2) % 50 samples starting from -2
%
% Example:
%    h = [1  -2 0 1];               % FIR
%    n = [-1  0 1 2];
%    stem(n, h);                    % plot
%    imp2latex(h, 10, -1)           % either max 10 samples ...
%    [txt, ltxt] = imp2latex(h, n)  % or using "n"
%
% Example:
%    B = [1 0 0 1];
%    A = [1 0.5];                   % B & A --> IIR
%    impz(B, A, 10);                % plot
%    [h,n] = impz(B, A, 10)         % compute values into h
%    imp2latex(h, n(1:9))
%    [txt, ltxt] = imp2latex(h, 5)
%
% Jukka Parviainen, 10.2.2006 / 5.1.2006 


%%%%%%%%%%%%%%%%%%%%%%%%
%
 
if nargin < 2
  error('Too few arguments');
elseif nargin == 2
  offset = 0;
end;

lenH = length(h);

if (prod(size(N)) > 1)
  % N is a vector, actually n = [min_index ... max_index]
  % Now N = length(N) and offset = N(1), overrides possible third arg.
  offset = N(1);  
  N = length(N);
end;
% N is a scalar
% Now offset is set by third argument, 
%   by previous setting or by default 0 
if (lenH < N)
  % N is needed in the end, so copy the value to LN
  LN = lenH;
else
  LN = N;
end;
  
if (length(size(offset)) > 2)
  error('Offset has to be a scalar or a vector');
end;
if (prod(size(offset)) > 1)
  % offset is a vector
  offset = offset(1);
end;
% offset is now a scalar

% any zeros in the beginning of sequence?
dummy = find(h ~= 0);
if (dummy(1) > 1)
  % there are zeros in the beginning
  foo = min(dummy);
  % remove all zeros and update the counters
  h = h(foo:end);
  offset = offset + foo - 1;
  LN = LN - foo + 1;
end;

% First value
ind  = offset;

% Find a correct +/- for argument of delta[]
if (ind < 0)
  markS = ['+' num2str(abs(ind))];
elseif (ind > 0)
  markS = ['-' num2str(abs(ind))];
else
  markS = '';
end;
ind = ind+1;

% Write down "h[n] =" and the first non-zero item
if (h(1) == 1)
  Hstr = ['h[n] = d[n' markS ']'];
  Lstr = ['h[n] = \delta[n' markS ']'];
elseif (h(1) == -1)
  Hstr = ['h[n] = -d[n' markS ']'];
  Lstr = ['h[n] = -\delta[n' markS ']'];
elseif (h(1) < 0)
  Hstr = ['h[n] = -' num2str(abs(h(1))) ' d[n' markS ']'];
  Lstr = ['h[n] = -' num2str(abs(h(1))) ' \delta[n' markS ']'];
elseif (h(1) > 0)
  Hstr = ['h[n] = ' num2str(h(1)) ' d[n' markS ']'];
  Lstr = ['h[n] = ' num2str(h(1)) ' \delta[n' markS ']'];
elseif (h(1) == 0)
  % should not flow here
  Hstr = ['h[n] ='];
  Lstr = ['h[n] ='];
end;

% Write down the other items
for k = [2 : LN]
  tmp   = h(k);
  if (ind < 0)
    markS = ['+' num2str(abs(ind))];
  elseif (ind > 0)
    markS = ['-' num2str(abs(ind))];
  else
    markS = '';
  end;
  ind = ind+1;
    
  if (tmp == 1)
    Hstr = [Hstr ' + d[n' markS ']'];
    Lstr = [Lstr ' + \delta[n' markS ']'];    
  elseif (tmp == -1)
    Hstr = [Hstr ' - d[n' markS ']'];
    Lstr = [Lstr ' - \delta[n' markS ']'];    
  elseif (tmp == 0)
    % skip
    % ... or alternatively tmp>=0 ...?
  elseif (tmp > 0)
    Hstr = [Hstr ' + ', num2str(tmp), ' d[n' , markS ']'];
    Lstr = [Lstr ' + ', num2str(tmp), ' \delta[n' , markS ']'];
  else
    Hstr = [Hstr ' - ', num2str(abs(tmp)), ' d[n' , markS ']'];
    Lstr = [Lstr ' - ', num2str(abs(tmp)), ' \delta[n' , markS ']'];
  end
end;

% Add dots in case of IIR
if (lenH > N)
  Hstr = [Hstr ' + ...'];
  Lstr = [Lstr ' + \ldots'];
end;

imptext  = Hstr;
implatex = ['$' Lstr '$'];
