function varargout = cpmmodparams(mode,modeParams,L,N)
%*********************************************************************
% Function Name:    cpmmodparams
% Description:      Define the modulation pulse shapes
% Inputs:           Mode, mode specific parameters, pulse length
%                   Samples per symbol
% Return Values:    Parameter structure containing:
%                   ecode (0 = no error, 1 = error, 2 = warning)
%                   emsg 
%                   params
%********************************************************************

%   Copyright 1996-2000 The MathWorks, Inc.
%   $Revision: 1.2 $  $Date: 2000/06/26 21:56:51 $

TSym = 1;        % Notional symbol period
Ts   = TSym/N;   % Notional sample period

t = (0:Ts:L*TSym-Ts)';
%t = (0:Ts:L*TSym)';
ecode = 0;
emsg  = '';
% --- Define the phase transitions
switch upper(mode)
case 'LRC'
    g = (1/(2*L*TSym))*(1 - cos(2*pi*t/(L*TSym)));
    q = Ts*cumsum(g);
    
case 'TFM'
    t = t - TSym*(L/2); % Offset to pulse centre
    g = (g0(t-TSym,TSym) + 2*g0(t,TSym) + g0(t+TSym,TSym))/8;
    q = Ts*cumsum(g);
    g = g * 0.5/q(end); % Normalise so that the total phase transition is 0.5

case 'LSRC'
    beta = modeParams.beta;
    t = t - TSym*(L/2); % Offset to pulse centre
    t(find(t==0))=eps;
    g = (1/(L*TSym))*(sin(2*pi*t/(L*TSym))./(2*pi*t/(L*TSym))) .* cos(beta*2*pi*t/(L*TSym))./(1 - (4*beta*t/(L*TSym)).^2);
    q = Ts*cumsum(g);
    g = g * 0.5/q(end); % Normalise so that the total phase transition is 0.5  

case 'GMSK'
    Bb = modeParams.BT;
    K = 2*pi*Bb/sqrt(log(2));
    t = t - TSym*(L/2); % Offset to pulse centre
    g = (1/(2*TSym))*(qfun(K*(t-TSym/2)) - qfun(K*(t+TSym/2)));
    q = Ts*cumsum(g);
    g = g * 0.5/q(end); % Normalise so that the total phase transition is 0.5

case 'LREC'
    g = ones(size(t))/(2*L*TSym);
    q = Ts*cumsum(g);

otherwise
    emsg  = 'Unknown mode.';
    ecode = 1;
    g = [];
    q = [];
end;

% --- Assign these to a structure (scale so that sum(params.g) = Ts*sum(g) = 0.5 )
params.g = g*Ts;
params.q = q*Ts;

% --- Return the required values
varargout{1} = ecode;
varargout{2} = emsg;
varargout{3} = params;

return;

%*********************************************************************
% Function Name:    g0
% Description:      TFM fundamental frequency shape
% Inputs:           time vector, symbol period
% Return Values:    g
%********************************************************************function y=qfun(t)
function g = g0(t,TSym)
    t(find(t==0))=eps;
    arg = pi*t/TSym;
    g   = (1/TSym)*((sin(arg)./arg) - (((pi^2)./(24*arg.^2)) .* (2*sin(arg) - 2*arg.*cos(arg) - (arg.^2).*sin(arg))));
return;
    
%*********************************************************************
% Function Name:    qfun
% Description:      Gaussian tail function
% Inputs:           x
% Return Values:    q(x)
%********************************************************************function y=qfun(t)
function y=qfun(t)
    y = 0.5*(1-erf(t/sqrt(2)));
return;

