function h = ftrans2(b,t)
%FTRANS2 2-D FIR filter design via frequency transformation.
%	H = FTRANS2(b,T) designs a 2-D FIR filter H based on the 1-D FIR
%	filter b and the frequency transformation matrix T.  b must be
%	a 1-D odd-length (type I) filter such as those returned by FIR1, 
%	FIR2, or REMEZ in the SIGNAL PROCESSING TOOLBOX.  The transform
%	matrix T contains coefficients that define the frequency 
%	transformation to use.  If T is m-by-n and b has length N, then
%	H will be ((m-1)*(N-1)/2+1)-by-((n-1)*(N-1)/2+1).
%
%	H = FTRANS2(b) uses the McClellan transformation for T,
%	   T = [ 1   2   1  ;
%	         2  -4   2  ;
%	         1   2   1  ] / 8
%
%	See also FWIND1, FWIND2, FSAMP2.

%	Clay M. Thompson 1-19-93
%	Copyright (c) 1993 by The MathWorks, Inc.
%	$Revision: 1.10 $  $Date: 1993/09/07 19:07:30 $

% Reference: Jae S. Lim, "Two Dimensional Signal and Image Processing",
%            Prentice Hall, 1990, pages 213-217.

if nargin<2, t = [1 2 1;2 -4 2;1 2 1]/8; end % McClellan transformation

% Convert the 1-D filter b to SUM_n a(n) cos(wn) form
n = (length(b)-1)/2;
if floor(n)~=n, error('b must be an odd-length (type I) filter.'); end
if any(abs(b-rot90(b,2))>sqrt(eps)) | min(size(b))~=1,
  error('b must be a zero phase (symmetric) filter.');
end
b = rot90(fftshift(rot90(b,2)),2); % Inverse fftshift
a = [b(1) 2*b(2:n+1)];

inset = floor((size(t)-1)/2);

% Use Chebyshev polynomials to compute h
P0 = 1; P1 = t;
h = a(2)*P1; 
rows = inset(1)+1; cols = inset(2)+1;
h(rows,cols) = h(rows,cols)+a(1)*P0;
for i=3:n+1,
  P2 = 2*conv2(t,P1);
  rows = rows + inset(1); cols = cols + inset(2);
  P2(rows,cols) = P2(rows,cols) - P0;
  rows = inset(1) + [1:size(P1,1)];
  cols = inset(2) + [1:size(P1,2)];
  hh = h;
  h = a(i)*P2; h(rows,cols) = h(rows,cols) + hh;
  P0 = P1;
  P1 = P2;
end
h = rot90(h,2); % Rotate for use with filter2
