function h = fwind1(f1,f2,hd,w1,w2)
%FWIND1	2-D FIR filter design using 1-D windows.
%	H = FWIND1(HD,w) designs a 2-D FIR filter with desired frequency
%	response HD and returns the coefficients in the matrix H.
%	FWIND1 uses the 1-D window w to create an approximately
%	circularly symmetric 2-D window for use with FWIND2. If 
%	LENGTH(w) is N, then the size of H will be N-by-N.  HD 
%	is a complex matrix containing the desired frequency response
%	sampled at equally spaced points between -1 to 1 (the Nyquist
%	frequency) along the x and y frequency axes (see FREQSPACE 
%	for more info). 
%
%	H = FWIND1(HD,w1,w2) designs a 2-D filter using the two 1-D
%	windows w1 and w2 to create a 2-D separable window. FWIND2 is 
%	then used to create H.  H will by M-by-N when LENGTH(w1) is N
%	and LENGTH(w2) is M.
%
%	Windows from the SIGNAL PROCESSING TOOLBOX (e.g., BOXCAR, 
%	HANNING, BARTLETT, BLACKMAN, KAISER, or CHEBWIN) can be used
%	to specify w, w1, or w2.
%
%	H = FWIND1(F1,F2,HD,w) or H = FWIND1(F1,F2,HD,w1,w2) designs 
%	a 2-D FIR filter using the desired frequency response defined
%	at the points (F1,F2,HD). F1 and F2 are normalized frequency 
%	matrices where +/-1.0 corresponds to the Nyquist frequency.
%
%	See also FREQSPACE, FSAMP2, FTRANS2, and FWIND2.

%	Clay M. Thompson 1-15-92
%	Copyright (c) 1993 by The MathWorks, Inc.
%	$Revision: 1.17 $  $Date: 1993/09/17 15:44:11 $

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

error(nargchk(2,5,nargin));

method1 = 'linear';
method2 = 'bilinear';

if nargin==2, % Uniform spacing case with Huang's method
  hd = f1; w1 = f2;
  n = length(w1); m = n;
elseif nargin==3, % Uniform spacing with separable window
  w2 = hd;  hd = f1; w1 = f2;
  n = length(w1); m = length(w2);
elseif nargin==4, % Non-uniform spacing with Huang's method
  n = length(w1); m = n;
else
  n = length(w1); m = length(w2);
end


%
% Create 2-D window using either Huang's method or separable method.
%
if nargin==2 | nargin==4, % Huang's method: Create 2-D circular window
  if any(abs(w1-rot90(w1,2))>sqrt(eps)), 
    error('1-D window must be symmetric to use Huang''s method.');
  end
  if length(w1)<2, error('Length of window must be greater than 1.'); end

  t = (-(n-1)/2:(n-1)/2)*(2/(n-1));
  [t1,t2] = meshgrid(t,t);
  t12 = sqrt(t1.*t1 + t2.*t2);
  d = find(t12<t(1) | t12>t(length(w1)));
  if ~isempty(d), t12(d) = zeros(size(d)); end
  w = zeros(size(t12)); w(:) = interp1(t,w1,t12(:),method1);
  if ~isempty(d), w(d) = zeros(size(d)); end

else % Create separable window
  w = w2(:)*w1(:).';

end

%
% Design filter using fsamp2 and apply window
%
if nargin<4, % Uniformly spaced data
  % Interpolate Hd to be the same size as W, if necessary
  if any([m n]~=size(hd)), 
    if any(size(hd)<[2 2]),
      error('Hd must have at least 2 rows and 2 columns.');
    end

    [f1,f2] = freqspace(size(hd));
    % Extrapolate hd so that interpolation is never out of range.
    [mh,nh] = size(hd);
    if floor(nh/2)==nh/2, % if even
      hd = [hd,hd(:,1)]; f1 = [f1 1];
    else
      hd = [zeros(mh,1) hd zeros(mh,1)]; 
      df = f1(2)-f1(1); f1 = [f1(1)-df f1 f1(nh)+df];
    end
    [mh,nh] = size(hd);
    if floor(mh/2)==mh/2, % if even
      hd = [hd;hd(1,:)]; f2 = [f2 1];
    else
      hd = [zeros(1,nh);hd;zeros(1,nh)]; 
      df = f2(2)-f2(1); f2 = [f2(1)-df f2 f2(mh)+df];
    end
    [t1,t2] = freqspace([m n],'meshgrid');
    hd = interp2(f1,f2,hd,t1,t2,method2);
    d = find(isnan(hd)); if ~isempty(d), hd(d) = zeros(size(d)); end
  end
  h = fsamp2(hd) .* w;

else % Non-uniformly spaced data
  h = fsamp2(f1,f2,hd,size(w)) .* w;

end
