function [w,msg] = gencoswin(varargin)
%GENCOSWIN   Returns one of the generalized cosine windows.
%   GENCOSWIN returns the generalized cosine window specified by the 
%   first string argument. Its inputs can be
%     Window name    - a string, any of 'hamming', 'hann', 'blackman'.
%     N              - length of the window desired.
%     Sampling flag  - optional string, one of 'symmetric', 'periodic'. 

%   Copyright 1988-2000 The MathWorks, Inc.
%   $Revision: 1.4 $  $Date: 2000/06/09 20:50:48 $ 

% amended by David Gibson, 16 June 2002
% fourth argument is matrix of coeffs for Blackman-Harris window
% This M-file should only be called fron BlackmanHarris, because
% for other windows, the system copy in toolbox/signal/signal/private
% will be used instead.


% Parse the inputs
window = varargin{1};
n = varargin{2};
msg = '';

% Check for trivial orders - dont do this in this version of GENCOSWIN
% [n,w,trivialwin] = check_order(n);
% if trivialwin, return, end;

% Select the sampling option
if nargin == 2, % no sampling flag specified, use default. 
   sflag = 'symmetric';
else
   sflag = lower(varargin{3});
end

% get coeffs for BlackmanHarris
if nargin == 3,  
   a =[];
else
   a = (varargin{4});
end


% Allow partial strings for sampling options
allsflags = {'symmetric','periodic'};
sflagindex = strmatch(sflag, allsflags);
if length(sflagindex)~=1         % catch 0 or 2 matches
   msg = 'Sampling flag must be either ''symmetric'' or ''periodic''.';
   return;
else	
   sflag = allsflags{sflagindex};
end

% Evaluate the window
switch sflag
case 'periodic'
   w = sym_window(n+1,window,a);
   w(end) = [];
case 'symmetric'
   w = sym_window(n,window,a);
end

%---------------------------------------------------------------------
function w = sym_window(n,window,a)
%SYM_WINDOW   Symmetric generalized cosine window.
%   SYM_WINDOW Returns an exactly symmetric N point generalized cosine 
%   window by evaluating the first half and then flipping the same samples
%   over the other half.

if ~rem(n,2)
   % Even length window
   half = n/2;
   w = calc_window(half,n,window,a);
   w = [w; w(end:-1:1)];
else
   % Odd length window
   half = (n+1)/2;
   w = calc_window(half,n,window,a);
   w = [w; w(end-1:-1:1)];
end

%---------------------------------------------------------------------
function w = calc_window(m,n,window,a)
%CALC_WINDOW   Calculate the generalized cosine window samples.
%   CALC_WINDOW Calculates and returns the first M points of an N point
%   generalized cosine window determined by the 'window' string.

% For the hamming and blackman windows we force rounding in order to achieve
% better numerical properties.  For example, the end points of the hamming 
% window should be exactly 0.08.

switch window
case 'hann'
   % Hann window
   w = 0.5 * (1 - cos(2*pi*(0:m-1)'/(n-1))); 
case 'hamming'
   % Hamming window
   w = (54 - 46*cos(2*pi*(0:m-1)'/(n-1)))/100;
case 'blackman'
   % Blackman window
   w = (42 - 50*cos(2*pi*(0:m-1)/(n-1)) + 8*cos(4*pi*(0:m-1)/(n-1)))'/100;
case 'cosinewindow'
   % Blackman-Harris window
   scale = 1E5; a = a * scale;
   w = (a(1) - a(2)*cos(2*pi*(0:m-1)/(n-1)) + a(3)*cos(4*pi*(0:m-1)/(n-1)) - a(4)*cos(6*pi*(0:m-1)/(n-1)) )'/scale;
end

% [EOF] gencoswin.m
