function [m,SE] = smoothing_AI(ind,k,D,EF)
%% loop for mov mean and mov std of a matrix
m = NaN(1,length(D));
SE = NaN(1,length(D));
for i = ind(ceil(0.25*k):end-ceil(0.25*k))
    ind_window= i-ceil(k/2):i+floor(k/2);
    % the window
    if sum(ind_window<=0| ind_window>=ind(end))
        % if the window falls out of trials:
        too_big = sum(ind_window<=0| ind_window>=ind(end));
        if sum(ind_window<=0)
            % at the beginning: shrink at both sides (to show learning)
            ind_window([1:too_big]) =[];
            ind_window([(end-too_big):end]) =[];
        elseif sum(ind_window>=ind(end))
            % at the end: shrink at both sides
            ind_window([1:too_big]) =[];
            ind_window([(end-too_big):end]) =[];
        end
    end
    if EF
        % weight over window to make smooth even further:
        
        weight = hamming(length(ind_window));
        if ~isempty(ind_window)
            if sum(~isnan(D(:,ind_window)'),'all')>0
                % mean
                m(i) = nansum(D(:,ind_window)'.*weight,'all')/sum(~isnan(D(:,ind_window)').*weight,'all');
                % adapted SE
                SE(i) = sqrt(nansum(((m(i)-D(:,ind_window)').^2).*weight,'all')/sum(~isnan(D(:,ind_window)').*weight,'all'))./sqrt(sum(~isnan(D(:,ind_window)').*weight,'all'));
            end
        end
    else
        if sum(~isnan(D(ind_window,:,:)),'all')>0
            m(i) = nanmean(D(ind_window,:,:),'all');
            SE(i) = nanstd(nanmean(D(ind_window,:,:),[1,3]))/sqrt(sum(~isnan(nanmean(D(ind_window,:,:),[1,3])))); %% SE
        end
    end
end