function [win_fun, win_fun_noise_bw] = max8_window_fn(window_type, npts)
%-------------------------------------------------------------------------------
% $Id: max8_window_fn.m,v 1.13 2023/07/26 21:49:41 ddelore Exp $
%
%  David S. De Lorenzo
%  12 December 2019
%
%  Window specification function:  define the time-domain window & its noise BW
%
%-------------------------------------------------------------------------------
%
%  We seek a time-domain window function to reduce spectral leakage.
%  Recall, leakage is the result of processing finite-duration records
%  that contain signal components which are not periodic in the observation
%  window.  While window functions primarily are used in signal detection
%  and estimation, here we are most interested in identifying and isolating
%  just those FFT bins which contain harmful interference and jamming, for
%  the purposes of nulling/excision (i.e., from a window function perspective,
%  we seek to limit the number of frequency components of the discrete Fourier
%  transform that are tagged as "jammed").  This is why it is appropriate for
%  us to prioritize the bandwidth of the window function (i.e., across how
%  many frequency bins does it spread a periodic signal) more heavily than its
%  highest sidelobe level (which is more useful when resolving closely-spaced
%  signals of very different amplitudes).
%
%  The definition of the window functions contained within this file and
%  the theory of their selection and use are mainly explained within the
%  following three references:
%    - Fredric J. Harris, 1978, "On the Use of Windows for Harmonic
%      Analysis with the Discrete Fourier Tranform", Proc. IEEE, vol.
%      66, no. 1, pgs. 51-83.
%        --> Table 1 on pg. 55:  "Windows and Figures of Merit" contains
%            data on highest sidelobe level & equivalent noise bandwidth
%    - Howard A. Gaberson, 2006, "A Comprehensive Windows Tutorial",
%      Sound and Vibration, vol. 40, no. 3, pgs. 14-23.
%    - Julius O. Smith III, 2011, "Spectral Audio Signal Processing",
%      W3K Publishing.
%        --> pg. 50:  "In practical applications, it is safest to write
%            your own window functions ... in order to ensure portability
%            and consistency."
%
%  Notes:
%    1. At the bottom of this function we do a re-scaling of the window
%       in order to preserve time-domain amplitude.
%        --> win_fun = (win_fun / mean(win_fun));
%    2. We don't use the native MATLAB function WINDOW() since (1) we
%       want explicit control over the window, (2) we anticipate an
%       eventual port of this code to C/C++, (3) we may desire window
%       functions that aren't valid arguments in the call to WINDOW(),
%       and (4) we need to return (via look-up) the equivalent noise
%       bandwidth for our choice of window for later use in estimating
%       the white noise power.
%
%-------------------------------------------------------------------------------

switch window_type
    case 'rectangular'
        % highest sidelobe level = -13 dB
        win_fun = ones(1,npts);
        win_fun_noise_bw = 1.00;
    case 'hanning'
        % highest sidelobe level = -32 dB
        win_fun = 0.5 - 0.5*cos(2*pi*(0:npts-1)/npts);
        win_fun_noise_bw = 1.50;
    case 'hamming'
        % highest sidelobe level = -43 dB
        win_fun = 0.54 - 0.46*cos(2*pi*(0:npts-1)/npts);
        win_fun_noise_bw = 1.36;
    case 'blackman'
        % highest sidelobe level = -58 dB
        win_fun = 0.42 - 0.50*cos(2*pi*(0:npts-1)/npts) ...
                       + 0.08*cos(4*pi*(0:npts-1)/npts);
        win_fun_noise_bw = 1.73;
    case '3_term_blackman'
        % highest sidelobe level = -61 dB
        win_fun = 0.44959 - 0.49364*cos(2*pi*(0:npts-1)/npts) ...
                          + 0.05677*cos(4*pi*(0:npts-1)/npts);
        win_fun_noise_bw = 1.61;
    case 'minimum_3_term_blackman'
        % highest sidelobe level = -67 dB
        win_fun = 0.42323 - 0.49755*cos(2*pi*(0:npts-1)/npts) ...
                          + 0.07922*cos(4*pi*(0:npts-1)/npts);
        win_fun_noise_bw = 1.71;
    case '4_term_blackman'
        % highest sidelobe level = -74 dB
        win_fun = 0.40217 - 0.49703*cos(2*pi*(0:npts-1)/npts) ...
                          + 0.09392*cos(4*pi*(0:npts-1)/npts) ...
                          - 0.00183*cos(6*pi*(0:npts-1)/npts);
        win_fun_noise_bw = 1.79;
    case 'minimum_4_term_blackman'
        % highest sidelobe level = -92 dB
        win_fun = 0.35875 - 0.48829*cos(2*pi*(0:npts-1)/npts) ...
                          + 0.14128*cos(4*pi*(0:npts-1)/npts) ...
                          - 0.01168*cos(6*pi*(0:npts-1)/npts);
        win_fun_noise_bw = 2.00;
    otherwise
        error('Invalid window function!!');
end

% Re-scale the window to preserve time-domain amplitude
win_fun = (win_fun / mean(win_fun));

return
%===============================================================================