function demoFIRwindowDB(win, M, Wc)
% demoFIRwindowDB demonstrates window method in FIR filter design
%
% Usage:
%   demoFIRwindowDB(win, M, Wc)
%
%      win     (function_handle) function handle to
%              a window function, by default "win = @hamming",
%              see "help window" for all possible windows
%      M       window from -M <= n <= +M, window length 2M+1,
%              window order N = 2M, by default M = 5
%      Wc      cut-off frequency for LP filter, 0 < Wc < 1,
%              by default Wc = 0.35
%
% Demo function can be used in three ways, Examples #1..#3:
%
% Example #1, call without any arguments using defaults
%
%    demoFIRwindowDB
%
% Example #2, call with different windows
%
%    win = @blackman;   % type "help window" to list all possible
%    figure(1);         % open/activate Figure No 1
%    demoFIRwindowDB(win)
%
% Example #3, call with several parameters affecting
% plotting etc.
%  
%    win = @bartlett;
%    M   = 15;
%    Wc  = 0.6;    
%    figure(2); 
%    demoFIRwindowDB(win, M, Wc)
%
% Example #4
%
%    demoFIRwindowdemo  % demo that uses this m-file!
%
% Example #5, check if specifications are fulfilled.
%    win = @hamming;
%    M   = 5; % N=10
%    Wc  = 0.5;
%    Wp  = 0.4;
%    Ws  = 0.6;
%    Rp  = 3;
%    Rs  = 50;
%    figure(3); 
%    demoFIRwindowDB(win, M, Wc)
%    subplot(3,3,9);
%    speksit(Wp, Ws, Rp, Rs); % NOT fulfilled!
%    M   = 50;
%    demoFIRwindowDB(win, M, Wc)
%    subplot(3,3,9);
%    speksit(Wp, Ws, Rp, Rs); % now TOO large order!
%
%
% Fixed windows from "help window"
%    win = @rectwin    % rectangular
%    win = @hamming    % hamming
%    win = @hann       % hann
%    win = @blackman   % blackman
%    win = @bartlett   % bartlett
%    win = @triangular % triangular
%
% demoFIRwindow plots only first two columns
%
%
% See also WINDOW, WINTOOL, demoFIRwindow, speksit

% Jukka Parviainen, 14.4.2008 / 29.3.2006 / 2004

if (nargin < 2) 
  M       = 5;                     % order N=2M, length 2M+1; 
  Wc      = 0.35;
elseif (nargin == 3)
  % probably OK!
else
  error('Wrong number of arguments, see "help demoFIRwindow"');
end;
if (nargin > 0)
  winf    = win(2*M+1)';           % window function w[n]
else
  win     = @hamming;              % default window
  winf    = win(2*M+1)';           % window function w[n]
end;

wc         = Wc*pi;                 % cut-off frequency w_c
K          = 100+M;                 % dummy variable, K >> M
n          = [-K+1 : K-1];          % time axis
h_ideal    = sin(wc*n)./(pi*n);     % h_ideal[n]
h_ideal(K) = wc/pi;                 % h_ideal[0]
h_FIR      = h_ideal(K-M:K+M) .* winf;     % FIR filter

GR = 1;

clf;  % clear current figure


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Time-domain: left column

sp1 = subplot(3,3,1); 
stem(n, h_ideal); 
ylabel('IDEAL h_i[n]'); 
axis([min(n) max(n) -0.2 0.2+(wc/pi)]);
title('Time-domain'); 
if GR, grid on; end;

subplot(3,3,4); 
%stem([-M-0.5:M+0.5], winf); 
stem(n, [zeros(1,K-M-1) winf zeros(1,K-M-1)]); 
%axis([min(n) max(n) -0.05 1.05]);
axis([-M-0.5 M+0.5 -0.05 1.05]);
ylabel('WINDOW w[n]');
title(['[' func2str(win) ': ' num2str(-M) ' <= n <= ' num2str(M) ' ]']);
if GR, grid on; end;

subplot(3,3,7); 
%stem([-M:M], h_FIR); 
stem(n, [zeros(1,K-M-1) h_FIR zeros(1,K-M-1)]); 
%axis([min(n) max(n) min(h_FIR) max(h_FIR)]);
axis([-M-0.5 M+0.5 min(h_FIR) max(h_FIR)]);
xlabel('Time index n'); 
ylabel('FIR h_F[n]');
title(['Filter order N = ' num2str(2*M)]);
if GR, grid on; end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Frequency-domain: middle column

FF = [-1 : 0.001 : 1];

% 0 .. pi
%subplot(3,2,2); 
%line([0 wc/pi wc/pi 1],[1 1 0 0]); 
%axis([0 1 0 1.5]);
%title('Frequency-domain');

subplot(3,3,2); 
line([-1 -wc/pi -wc/pi wc/pi wc/pi 1],[0 0 1 1 0 0]); 
axis([-1.02 1.02 -0.1 1.1]);
title('Frequency-domain');
if GR, grid on; end;

% 0 .. pi
%subplot(3,2,4); 
%[H_R,w] = freqz(winf,1);  
%plot(w/pi, abs(H_R));
%title(['[' func2str(win) ']']);

subplot(3,3,5); 
[H_R,w] = freqz(winf,1,FF, 2);
plot(FF, abs(H_R)/max(abs(H_R)));
axis([-1.02 1.02 -0.1 1.1]);
title(['[' func2str(win) ']']);
if GR, grid on; end;

% 0 .. pi
%subplot(3,2,6); 
%[H_F,w] = freqz(h_FIR,1);   
%plot(w/pi, abs(H_F)); 
%hold on; 
%li = line([0 wc/pi wc/pi 1],[1 1 0 0]); 
%set(li,'LineStyle','-.');
%xlabel('Norm. angular frequency (\times \pi)');

subplot(3,3,8); 
%[H_F,w] = freqz(h_FIR,1, 1024, 'whole');   
[H_F] = freqz(h_FIR,1, FF, 2);
plot(FF, abs(H_F)/max(abs(H_F)));
axis([-1.02 1.02 -0.1 1.1]);
hold on; 
li = line([-1 -wc/pi -wc/pi wc/pi wc/pi 1],[0 0 1 1 0 0]); 
set(li,'LineStyle','-.');
xlabel('Norm. angular frequency (\times \pi)');
if GR, grid on; end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Frequency-domain: right column


subplot(3,3,3); 
line([-1 -wc/pi -wc/pi wc/pi wc/pi 1],[-500 -500 0 0 -500 -500]); 
axis([-1.02 1.02 -80 10]);
title('Frequency-domain');
ylabel('dB');
if GR, grid on; end;

subplot(3,3,6); 
mag_R = 20*log10(abs(H_R)/max(abs(H_R)));
plot(FF, mag_R);
axis([-1.02 1.02 -80 10]);
title(['[' func2str(win) ']']);
ylabel('dB');
if GR, grid on; end;

subplot(3,3,9); 
mag_F = 20*log10(abs(H_F)/max(abs(H_F)));
plot(FF, mag_F);
axis([-1.02 1.02 -80 80]);
hold on; 
li2 = line([-1 -wc/pi -wc/pi wc/pi wc/pi 1],[-500 -500 0 0 -500 -500]); 
set(li2,'LineStyle','-.');
xlabel('Norm. angular frequency (\times \pi)');
ylabel('dB');
axis([-1.02 1.02 -80 10]);
if GR, grid on; end;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

pos11 = get(sp1, 'Position')
[pos11(1)+0.7*pos11(3) pos11(2) 0.25*pos11(3) 0.1*pos11(4)]
myB = uicontrol;
set(myB, ... 
    'Units', 'normalized', ...
    'Position', [pos11(1)+0.6*pos11(3) pos11(2)+0.6*pos11(4) 0.35*pos11(3) 0.25*pos11(4)], ...
    'Callback', ['subplot(3,3,1); axis([-' num2str(M) '-0.5 ' num2str(M) '+0.5 ' num2str(min(h_FIR)) ' ' num2str(max(h_FIR)) ']);'], ...
    'String', 'zoom h_ideal');


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%

shg;  % pop up
