% Filtro che considera N deviazioni standard in una finestra centrata di
% input dati (una in avanti e una all'indietro) e mette a zero lo
% spostamento se è inferiore a tale valore
% E' previsto un secondo filtro forfettario se lo spostamento è minore di
% un determinato valore

function [dX,dY,dZ,dXstar,dYstar,dZstar,text4] = filtro_TuL(dX,dY,dZ,dXstar,...
    dYstar,dZstar,Ndevst,Wdevst)

dX = dX';
dXstar = dXstar';
dY = dY';
dYstar = dYstar';
dZ = dZ';
dZstar = dZstar';
calcoloX = dX;
calcoloXstar = dXstar;
calcoloY = dY;
calcoloYstar = dYstar;
calcoloZ = dZ;
calcoloZstar = dZstar;

[r,c] = size(dX); % riga date, colonna nodi
filtroX = zeros(r,c);
filtroXstar = zeros(r,c);
filtroY = zeros(r,c);
filtroYstar = zeros(r,c);
filtroZ = zeros(r,c);
filtroZstar = zeros(r,c);
N_dev_st = Ndevst;
finestra = Wdevst;
contX = 0;
contY = 0;
contZ = 0;
contXstar = 0;
contYstar = 0;
contZstar = 0;

for j = 2:c % nodi
    for i = round((finestra/2+0.5)):round((r-(finestra/2-0.5))) % date
        % X
        var = calcoloX((i-(finestra/2-0.5)):(i+(finestra/2-0.5)),j);
        filtroX(i,j) = N_dev_st*std(var);
        if abs(dX(i,j))<filtroX(i,j)
           dX(i,j) = 0;
           contX = contX+1;
        end
        
        % X star
        var = calcoloXstar((i-(finestra/2-0.5)):(i+(finestra/2-0.5)),j);
        filtroXstar(i,j) = N_dev_st*std(var);
        if abs(dX(i,j))<filtroXstar(i,j)
           dXstar(i,j) = 0;
           contXstar = contXstar+1;
        end
        
        % Y
        var = calcoloY((i-(finestra/2-0.5)):(i+(finestra/2-0.5)),j);
        filtroY(i,j) = N_dev_st*std(var);
        if abs(dY(i,j))<filtroY(i,j)
           dY(i,j) = 0;
           contY = contY+1;
        end
        
        % Y star
        var = calcoloYstar((i-(finestra/2-0.5)):(i+(finestra/2-0.5)),j);
        filtroYstar(i,j) = N_dev_st*std(var);
        if abs(dYstar(i,j))<filtroYstar(i,j)
           dYstar(i,j) = 0;
           contYstar = contYstar+1;
        end
        
        % Z
        var = calcoloZ((i-(finestra/2-0.5)):(i+(finestra/2-0.5)),j);
        filtroZ(i,j) = N_dev_st*std(var);
        if abs(dZ(i,j))<filtroZ(i,j)
           dZ(i,j) = 0;
           contZ = contZ+1;
        end
        
        % Z star
        var = calcoloZstar((i-(finestra/2-0.5)):(i+(finestra/2-0.5)),j);
        filtroZstar(i,j) = N_dev_st*std(var);
        if abs(dZstar(i,j))<filtroZstar(i,j)
           dZstar(i,j) = 0;
           contZstar = contZstar+1;
        end
    end
    
    for i = (r-(finestra/2-1.5)):r % date
        var = zeros(finestra-1,1); % Prolungo l'ultimo dato per il trend che non ho
        delta = r-i+1;
        
        % X
        var(1:delta,1) = calcoloX(i:end,j);
        var(delta+1:end-1) = calcoloX(end,j);
        filtroX(i,j) = N_dev_st*std(var);
        filtroX(end,j) = filtroX(end-1,j);
        if abs(dX(i,j))<filtroX(i,j)
           dX(i,j) = 0;
           contX = contX+1;
        end
        
         % X star
        var(1:delta,1) = calcoloXstar(i:end,j);
        var(delta+1:end-1) = calcoloXstar(end,j);
        filtroXstar(i,j) = N_dev_st*std(var);
        filtroXstar(end,j) = filtroXstar(end-1,j);
        if abs(dXstar(i,j))<filtroXstar(i,j)
           dXstar(i,j) = 0;
           contXstar = contXstar+1;
        end
        
        % Y
        var(1:delta,1) = calcoloY(i:end,j);
        var(delta+1:end-1) = calcoloY(end,j);
        filtroY(i,j) = N_dev_st*std(var);
        filtroY(end,j) = filtroY(end-1,j);
        if abs(dY(i,j))<filtroY(i,j)
           dY(i,j) = 0;
           contY = contY+1;
        end
        
        % Y star
        var(1:delta,1) = calcoloYstar(i:end,j);
        var(delta+1:end-1) = calcoloYstar(end,j);
        filtroYstar(i,j) = N_dev_st*std(var);
        filtroYstar(end,j) = filtroYstar(end-1,j);
        if abs(dYstar(i,j))<filtroYstar(i,j)
           dYstar(i,j) = 0;
           contYstar = contYstar+1;
        end
        
        % Z
        var(1:delta,1) = calcoloZ(i:end,j);
        var(delta+1:end-1) = calcoloZ(end,j);
        filtroZ(i,j) = N_dev_st*std(var);
        filtroZ(end,j) = filtroZ(end-1,j);
        if abs(dZ(i,j))<filtroZ(i,j)
           dZ(i,j) = 0;
           contZ = contZ+1;
        end
        
        % Z star
        var(1:delta,1) = calcoloZstar(i:end,j);
        var(delta+1:end-1) = calcoloZstar(end,j);
        filtroZstar(i,j) = N_dev_st*std(var);
        filtroZstar(end,j) = filtroZstar(end-1,j);
        if abs(dZstar(i,j))<filtroZstar(i,j)
           dZstar(i,j) = 0;
           contZstar = contZstar+1;
        end
    end 
end

dX = dX';
dXstar = dXstar';
dY = dY';
dYstar = dYstar';
dZ = dZ';
dZstar = dZstar';

text4 = ['' num2str(contX) ' X data, ' num2str(contY) ' Y data, ' num2str(contZ) ...
    ' Z data, ' num2str(contXstar) ' Xstar data, ' num2str(contYstar) ' Ystar data, ' num2str(contZstar) ...
    ' Zstar data filtered for Tunnel Link - Statistic filter!'];
end