%IDEAL_RESPONSE_QS Design an ideal filter with quadrantal symmetry in the
%   frequency domain using the specifications given in the text file
%   "filename".
%
%   filename: the full path to the text file containing the specifications
%   for the frequency response of the ideal filter. The first two columns
%   of the text file contains the horizontal and the vertical frequency
%   components of the points located on pass band and stop band of
%   frequency response and the third column contains their respected 
%   values. NOTE: since the filter is assumed to have a quadrantal symmetry
%   in this case, the user only needs to provide the specifications for one
%   quarter of the frequency response of the ideal filter. 
%   The positive-axes quarter is recommended.
%
%   Npt: the number of points to be used in the frequency response (64 is
%   recommended).
%   Hd: the frequency response of the output filter.
%   
%   A general suggestion when designing the ideal filter is to truncate the
%   sharp corners located on the bands of the frequency response of the
%   filter. This would allow for a better solution when using
%   filter_design_qs_unc or filter_design_qs_con to design a filter based
%   on this ideal filter. The following examples make use of this
%   suggestion with a truncation amount of 0.02 on the frequency scale.
%
%   Example
%   --------
%   Design an ideal diamond-shaped lowpass filter with a pass band located 
%   at frequency 0.3 and stop band located at frequency 0.4.
%   Content of "ideal_LP.txt"
%   0.0  0.0  1.0
%   0.3  0.0  1.0
%   0.3  0.02 1.0
%   0.16 0.16 1.0
%   0.02 0.3  1.0
%   0.0  0.3  1.0
%   0.4  0.0  0.0
%   0.4  0.02 0.0
%   0.21 0.21 0.0
%   0.02 0.4  0.0
%   0.0  0.4  0.0
%   0.5  0.0  0.0
%   0.0  0.5  0.0
%   0.5  0.5  0.0
%
%   Hd_LP=ideal_response('ideal_LP.txt',64);
%
%   Similarly, design an ideal diamond-shaped highpass filter with a pass 
%   band located at frequency 0.4 and a stop band located at frequency 0.3.
%   Content of "ideal_HP.txt"
%   0.0  0.0  0.0
%   0.3  0.0  0.0
%   0.3  0.02 0.0
%   0.16 0.16 0.0
%   0.02 0.3  0.0
%   0.0  0.3  0.0
%   0.4  0.0  1.0
%   0.4  0.02 1.0
%   0.21 0.21 1.0
%   0.02 0.4  1.0
%   0.0  0.4  1.0
%   0.5  0.0  1.0
%   0.0  0.5  1.0
%   0.5  0.5  1.0
%
%   Hd_HP=ideal_response('ideal_HP.txt',64);
%
%   Similarly, design an ideal diamond-shaped band pass filter with a
%   bandwidth of 0.1 (from 0.2 to 0.3) and transition bandwidths of 0.1.
%   Content of "ideal_BP.txt"
%   0.0  0.0  0.0
%   0.1  0.0  0.0
%   0.1  0.02 0.0
%   0.06 0.06 0.0
%   0.02 0.1  0.0
%   0.0  0.1  0.0
%   0.2  0.0  1.0
%   0.2  0.02 1.0
%   0.11 0.11 1.0
%   0.02 0.2  1.0
%   0.0  0.2  1.0
%   0.3  0.0  1.0
%   0.3  0.02 1.0
%   0.16 0.16 1.0
%   0.02 0.3  1.0
%   0.0  0.3  1.0
%   0.4  0.0  0.0
%   0.4  0.02 0.0
%   0.21 0.21 0.0
%   0.02 0.4  0.0
%   0.0  0.4  0.0
%   0.5  0.0  0.0
%   0.0  0.5  0.0
%   0.5  0.5  0.0
%
%   Hd_BP=ideal_response('ideal_BP.txt',64);
function Hd = ideal_response_qs(filename,Npt)
%filename contains the (full path if not in the same work folder)
%filename of the text file.
%Npt = number of points in ideal response in each dimension.
a = textread(filename);
X = [a(:,1)' a(:,1)' -a(:,1)' -a(:,1)']*2;
Y = [a(:,2)' -a(:,2)' a(:,2)' -a(:,2)']*2;
Z = [a(:,3)' a(:,3)' a(:,3)' a(:,3)'];
[f1,f2] = freqspace(Npt);
Hd = griddata(X,Y,Z,f1,f2');
[F1,F2] = meshgrid(f1,f2);

%Perspective plot of ideal filter response
figure,mesh(0.5*F1,0.5*F2,Hd);
title('Perspective plot of ideal filter response');
xlabel('Horizontal frequency');ylabel('Vertical frequency');
zlabel('Magnitude of filter response');colormap([0 0 0]);

%Contour plot of ideal filter response
v=[0,0.05,0.1,0.9,0.95,1];
figure,[Con,hlev] = contour(0.5*F1,0.5*F2,Hd,v);
title('Contour plot of ideal filter response');
xlabel('Horizontal frequency');ylabel('Vertical frequency');
clabel(Con,hlev);colormap([0 0 0]);
axis square;
grid on;