Newer
Older
function [smooth] = tsg_average(hMainFig, PARA, iTsg)
% Perform the average of a time series at the position of a WS sample
%
% The average is made for a period equal to 'tsg.cst.TSG_DT_SMOOTH'
% Data exceeding the average over that period by 'tsg.cst.TSG_STDMAX'
% are not taken into account.
%
% Input
% hMainFig ............ Handle to the main user interface
%
% No computation : NaN
%
% Get the tsg structure from the application
% ------------------------------------------
tsg = getappdata( hMainFig, 'tsg_data');
% Select the param data over 'tsg.cst.TSG_DT_SMOOTH' time interval
% ind1 : indices of tsg.PARA in the 'tsg.cst.TSG_DT_SMOOTH' time interval
% ind2 : indices of tsg.PARA not rejected by the S.D. test
% -----------------------------------------------------------------------
ind1 = find( tsg.DAYD >= tsg.DAYD(iTsg) - tsg.cst.TSG_DT_SMOOTH/2 & ...
tsg.DAYD <= tsg.DAYD(iTsg) + tsg.cst.TSG_DT_SMOOTH/2 );
ind2 = ind1;
if ~isempty(ind2)
currentStd = Inf;
previousStd = 0;
% Compare Standard Deviation to the MAX acceptable STD
% ----------------------------------------------------
while currentStd > tsg.([PARA '_STDMAX']) && currentStd ~= previousStd
previousStd = currentStd;
% Standard deviation and average over timeInterval
% ------------------------------------------------
currentStd = nanstd( tsg.(PARA)(ind2) );
meanParam = nanmean( tsg.(PARA)(ind2) );
% Indices of 'good' values of Param
% ---------------------------------
ind2 = ind1( tsg.(PARA)(ind1) >= meanParam - currentStd & ...
tsg.(PARA)(ind1) <= meanParam + currentStd );
smooth = nanmean( tsg.(PARA)(ind2) );
else
smooth = NaN;
end