function [h,p,ci,stats] = Ttest_signf_autocorr(x,m,varargin)
%TTEST  One-sample and paired-sample t-test.
%   H = TTEST(X) performs a t-test of the hypothesis that the data in the
%   vector X come from a distribution with mean zero, and returns the
%   result of the test in H.  H=0 indicates that the null hypothesis
%   ("mean is zero") cannot be rejected at the 5% significance level.  H=1
%   indicates that the null hypothesis can be rejected at the 5% level.
%   The data are assumed to come from a normal distribution with unknown
%   variance.
%
%   X can also be a matrix or an N-D array.   For matrices, TTEST performs
%   separate t-tests along each column of X, and returns a vector of
%   results.  For N-D arrays, TTEST works along the first non-singleton
%   dimension of X.
%
%   TTEST treats NaNs as missing values, and ignores them.
%
%   H = TTEST(X,M) performs a t-test of the hypothesis that the data in
%   X come from a distribution with mean M.  M must be a scalar.
%
%   H = TTEST(X,Y) performs a paired t-test of the hypothesis that two
%   matched samples, in the vectors X and Y, come from distributions with
%   equal means. The difference X-Y is assumed to come from a normal
%   distribution with unknown variance.  X and Y must have the same length.
%   X and Y can also be matrices or N-D arrays of the same size.
%
%   [H,P] = TTEST(...) returns the p-value, i.e., the probability of
%   observing the given result, or one more extreme, by chance if the null
%   hypothesis is true.  Small values of P cast doubt on the validity of
%   the null hypothesis.
%
%   [H,P,CI] = TTEST(...) returns a 100*(1-ALPHA)% confidence interval for
%   the true mean of X, or of X-Y for a paired test.
%
%   [H,P,CI,STATS] = TTEST(...) returns a structure with the following fields:
%      'tstat' -- the value of the test statistic
%      'df'    -- the degrees of freedom of the test
%      'sd'    -- the estimated population standard deviation.  For a
%                 paired test, this is the std. dev. of X-Y.
%
%   [...] = TTEST(X,Y,'PARAM1',val1,'PARAM2',val2,...) specifies one or
%   more of the following name/value pairs:
%
%       Parameter       Value
%       'alpha'         A value ALPHA between 0 and 1 specifying the
%                       significance level as (100*ALPHA)%. Default is
%                       0.05 for 5% significance.
%       'dim'           Dimension DIM to work along. For example, specifying
%                       'dim' as 1 tests the column means. Default is the
%                       first non-singleton dimension.
%       'tail'          A string specifying the alternative hypothesis:
%           'both'  -- "mean is not M" (two-tailed test)
%           'right' -- "mean is greater than M" (right-tailed test)
%           'left'  -- "mean is less than M" (left-tailed test)
%
%   See also TTEST2, ZTEST, SIGNTEST, SIGNRANK, VARTEST.

%   References:
%      [1] E. Kreyszig, "Introductory Mathematical Statistics",
%      John Wiley, 1970, page 206.

%   Copyright 1993-2012 The MathWorks, Inc.


if nargin < 2 || isempty(m)
    m = 0;
% elseif ~isscalar(m) % paired t-test %%- was original GDL
elseif ~isscalar(m) && isequal(size(m),size(x)) % paired t-test
    if ~isequal(size(m),size(x))
        error(message('stats:ttest:InputSizeMismatch'));
    end
    x = x - m;
    m = 0;
elseif ~isscalar(m)     %GDL
    multi_dim_mean = 1; %GDL
else                    %GDL
    multi_dim_mean = 0; %GDL
end

% Process remaining arguments
alpha = 0.05;
tail = 0;    % code for two-sided
dim = '';

if nargin>=3
    if isnumeric(varargin{1})
        % Old syntax
        %    TTEST(X,M,ALPHA,TAIL,DIM)
        alpha = varargin{1};
        if nargin>=4
            tail = varargin{2};
             if nargin>=5
                dim = varargin{3};
             end
        end
                
    elseif nargin==3 
            error(message('stats:ttest:BadAlpha'));
   
    else
        % Calling sequence with named arguments
%         okargs =   {'alpha' 'tail' 'dim'};
%         defaults = {0.05    'both' ''};
        okargs =   {'alpha' 'tail' 'dim' 'sample_s'}; %GDL new 25jan14
        defaults = {0.05    'both' '' NaN};
%         [alpha, tail, dim] = ...
        [alpha, tail, dim, sample_s] = ...
                         internal.stats.parseArgs(okargs,defaults,varargin{:});
    end
end

% Argument error checking
if isempty(alpha)
    alpha = 0.05;
elseif ~isscalar(alpha) || ~isnumeric(alpha) || isnan(alpha) || (alpha <= 0) || (alpha >= 1)
    error(message('stats:ttest:BadAlpha'));
end
if isempty(tail)
    tail = 0;
elseif isnumeric(tail) && isscalar(tail) && ismember(tail,[-1 0 1])
    % OK, grandfathered
else
    [~,tail] = internal.stats.getParamVal(tail,{'left','both','right'},'''tail''');
    tail = tail - 2;
end

if isempty(dim)
    dim = find(size(x) ~= 1, 1);
    if isempty(dim), dim = 1; end
end

nans = isnan(x);
if any(nans(:))
    samplesize = sum(~nans,dim);
else
    samplesize = size(x,dim); % a scalar, => a scalar call to tinv
end

% GDL - deal with multi-dimensional mean (for each dataseries) to do T-test against
% DOES NOT MATTER FOR CI, so don't bother...
if multi_dim_mean
    if ~isequal(size(m,2),size(x,2))
        error(['If testing against a given mean,'...
            'then either this mean should be scalar',...
            'or have a length that corresponds to the number of columns [HARDWIRED] in the X-array']);
    end    
end


%Adjust sample size to account for lag-1 correlation
%=======begin GDL=======================
%samplesize =  ceil(samplesize.*(1-rx*rx)./(1+rx*rx));
%sample_s = adjusted sample size (deal with autocorr);

if ~isnan(sample_s)
   df = max(sample_s - 1,0);
else
   df = max(samplesize - 1,0); 
end
%=======end GDL==========================

%df = max(samplesize - 1,0);       %GDL, 07Feb2014
xmean = nanmean(x,dim);
%sdpop = nanstd(x,[],dim);         %GDL, 05Sep2013
sdpop = nanstd(x,dim);
% ser = sdpop ./ sqrt(samplesize); %GDL, 13Oct2015 %BUGFIX
if ~isnan(sample_s)
    ser = sdpop ./ sqrt(sample_s);
else
    ser = sdpop ./ sqrt(samplesize);
end
tval = (xmean - m) ./ ser;
if nargout > 3
    stats = struct('tstat', tval, 'df', cast(df,class(tval)), 'sd', sdpop);
    if isscalar(df) && ~isscalar(tval)
        stats.df = repmat(stats.df,size(tval));
    end
end

% Compute the correct p-value for the test, and confidence intervals
% if requested.
if tail == 0 % two-tailed test
    p = 2 * tcdf(-abs(tval), df);
    if nargout > 2
        crit = tinv((1 - alpha / 2), df) .* ser;
        ci = cat(dim, xmean - crit, xmean + crit);
    end
elseif tail == 1 % right one-tailed test
    p = tcdf(-tval, df);
    if nargout > 2
        crit = tinv(1 - alpha, df) .* ser;
        ci = cat(dim, xmean - crit, Inf(size(p)));
    end
elseif tail == -1 % left one-tailed test
    p = tcdf(tval, df);
    if nargout > 2
        crit = tinv(1 - alpha, df) .* ser;
        ci = cat(dim, -Inf(size(p)), xmean + crit);
    end
end
% Determine if the actual significance exceeds the desired significance
h = cast(p <= alpha, class(p));
h(isnan(p)) = NaN; % p==NaN => neither <= alpha nor > alpha
