function [Y,MM,DeltaT] = AutoCorrWeights(lambda2TimeStep,corshape)
%AutoCorrWeights Provide sequential  weights to convolve with white noise 
%   Usage: [Y,MM,DeltaT] = AutoCorrWeights(lambda2TimeStep,corshape)
%   Y = AutoCorrWeights returns the column of values (only positive ones) providing 0.001
%   correlation precision for the stochastic integral.
%   Input argument corshape  must be either "exponent" or "gaussian" that means
%   exp(-|t|/lambda) or exp(-0.5*(t/lambda)^2) autocorrelation function
%   MM is the step corresponding to TimeStep in discrete units
%   Internal integration step DeltaT is optional for the cross-check
%   
%   author: Andrey Khokhlov. 
%   $Revision: 0.25 $  $Date: 2011/10/05 08:15:33 $

ModSeg=8;
ModStep=25;

if  size(lambda2TimeStep,1) ~=  1  || size(lambda2TimeStep,2) ~=  1 || lambda2TimeStep < 0.0
    error('MATLAB:AutoCorrWeight:InvalidInput', 'First argument needs to be nonnegative scalar.')
end
if lambda2TimeStep < 0.125  
    Y=1;
    DeltaT=1;
    MM=1;
    return
end
% find the bopundaries  and DeltaT for the corresponding integration:
PreNN = round(ModSeg*lambda2TimeStep) +1;
MM=round(ModStep/(lambda2TimeStep)) +1;
NN=MM*PreNN;
Y=zeros(2*NN+1,1);
DeltaT=1./(MM*lambda2TimeStep);
Sqrt_tau=sqrt(DeltaT);

switch lower(corshape)
    case 'exponent'
        Cff=sqrt(2)/pi;
        %%Y(NN+1)=0.5*Cff*pi*Sqrt_tau*(besselk(0,DeltaT/2)*struvem(-1,DeltaT/2)+besselk(1,DeltaT/2)*struvem(0,DeltaT/2));
        for ii=1:NN
            Y(NN+1-ii)=Cff*besselk(0,ii*DeltaT);
            Y(NN+1+ii)=Y(NN+1-ii);
        end
        WeightDefect=1.0-Y'*Y*DeltaT;
        if  WeightDefect  < 0.0
            error('MATLAB:AutoCorrWeight:Invalid WeightDefect')
        end
        
        Y(NN+1)=sqrt(WeightDefect/DeltaT);
        Y=Y*Sqrt_tau;
    case 'gaussian'
        Cff=sqrt(sqrt(2./pi));
        for ii=1:NN
            Y(NN+1-ii)=Cff*exp(-(ii)*(ii)*DeltaT*DeltaT);
            Y(NN+1+ii)=Y(NN+1-ii);
        end
        Y(NN+1)=Cff;
        Y=Y*Sqrt_tau;
    otherwise
        error('MATLAB:AutoCorrWeight:InvalidInput', 'Input argument corshape  must be either "exponent" or "gaussian".')
end
return
            
        
        
        