Skip to content
Snippets Groups Projects
tsg_moveaverage.m 2.35 KiB
Newer Older
function [] = tsg_moveaverage(hTsgGUI)
% Perform a moving average of a time series.
% The average is made over 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
% hTsgGUI ............ Handle to the main user interface
%
% No computation : NaN
%

% Get the tsg structure from the application
% ------------------------------------------
tsg = getappdata( hTsgGUI, 'tsg_data');

% Memory allocation
% -----------------
%tsg.ssps.smooth      = zeros( size(tsg.SSPS) );
%tsg.ssps.smooth.nval = zeros( size(tsg.SSPS) );
smooth = zeros( size(tsg.SSPS) );
nval   = zeros( size(tsg.SSPS) );

% Loop over the tsg.SSPS time series
% -----------------------------------
for i = 1:length(tsg.SSPS)
  
  % Select the param data over 'tsg.cst.TSG_DT_SMOOTH' time interval
  % ind1 : indices of tsg.SSPS in the 'tsg.cst.TSG_DT_SMOOTH' time interval
  % ind2 : indices of 'good' tsg.SSPS in the 'tsg.cst.TSG_DT_SMOOTH' time
  %        interval
  % --------------------------------------------------
  ind1 = find( tsg.DAYD >= tsg.DAYD(i) - tsg.cst.TSG_DT_SMOOTH/2 & ...
               tsg.DAYD <= tsg.DAYD(i) + 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.cst.TSG_STDMAX && currentStd ~= previousStd
      
      previousStd = currentStd;
      
      % Standard deviation and average over timeInterval
      % ------------------------------------------------
			currentStd = nanstd(  tsg.SSPS(ind2) );
      meanParam  = nanmean( tsg.SSPS(ind2) );
            
			% Indices of 'good' values of Param
      % ---------------------------------
      ind2 = find( tsg.SSPS(ind1) >= meanParam - currentStd & ...
                   tsg.SSPS(ind1) <= meanParam + currentStd);
      ind2 = ind1( ind2 );
    end
    
    smooth(i) = nanmean( tsg.SSPS(ind2) );
    
  else
    smooth(i) = NaN;
  end
  
  nval( i ) = length( ind2 );
end 

tsg.ssps.smooth.val  = smooth;
tsg.ssps.smooth.nval = nval;

% Update the tsg structure in the application
% --------------------------------------------
setappdata( hTsgGUI, 'tsg_data', tsg);