function [BaroDef,BaroDefT,ARRAYdateBL,ErrBaroLink] = Baro(BaroDef,BaroDefT,...
    ARRAYdateBL,NuovoZeroBL,NdatiMediaP,Ndatidespike,ErrBaroLink,rBL,marginePiezo,FileName)

text = 'Baro function started';
fileID = fopen(FileName,'a');
fmt = '%s \r';
fprintf(fileID,fmt,text);

if NuovoZeroBL == 1
    if NdatiMediaP > Ndatidespike
        Ndati = NdatiMediaP;
    else
        Ndati = Ndatidespike;
    end
    ini = round(Ndati/2)+1;
    if rem(Ndati,2) == 0
        ini = ini+1;
    end
    clear NDati
    ini = ini + marginePiezo;
    if ini < 6
        ini = 6;
    end
    ErrBaroLink = ErrBaroLink(ini:end,:);
    BaroDef = BaroDef(ini:end,:);
    BaroDefT = BaroDefT(ini:end,:);
    ARRAYdateBL = ARRAYdateBL(ini:end,1); 
end

[r,~] = size(ErrBaroLink);
Matrice_err = zeros(r,rBL);
for i = 1:r % date
    d = 1;
    for n = 1:rBL % nodi
        j = 1;
        err = ErrBaroLink(i,d:d+1);
        while j <= 2
            if err(1,j) == 1
                Matrice_err(i,n) = 1;
            end
            j = j+1;
        end
        d = d+2;
    end
end
ErrBaroLink = Matrice_err';
    
text = 'Baro Link elaborated correctly. Baro function closed';
fprintf(fileID,fmt,text);
fclose(fileID);

end