% Funzione che converte i dati in Hz in dati di allungamento adimensionale
% mediante i valori di calibrazione.
% Il risultato è la matrico DatiExtensometer

function [DatiExtensometer3D,DatiExtensometer3D_Rif,DatiNTCExtensometer3D,...
    Err3DExtensometerLink] = conv_grezzi3DEL(Extensometer3D,NTCExtensometer3D,...
    Extensometer3D_Rif,DCalEL3DTot,Nodo3DExtensometerLink,Err3DExtensometerLink,...
    r3DEL,EL3D_NTC,FileName)

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

[rA,cA] = size(Extensometer3D);
DatiExtensometer3D = zeros(rA,cA);
DatiExtensometer3D_Rif = zeros(1,cA);
DatiNTCExtensometer3D = zeros(rA,cA/3);
s = 1;
ii = 1;
for e=1:r3DEL % numero di Extensometer Link
    if strcmp(Nodo3DExtensometerLink(e,3),'Hz')
        % Spacchetto i parametri di calibrazione
        GFx = DCalEL3DTot(e,1);
        BFx = DCalEL3DTot(e,2);
        GFy = DCalEL3DTot(e,3);
        BFy = DCalEL3DTot(e,4);
        GFz = DCalEL3DTot(e,5);
        BFz = DCalEL3DTot(e,6);
        % Lettura di riferimento
        digit_Rif_x = (Extensometer3D_Rif(1,ii).^2)*0.001; % Freq. ^2 *E-3
        DatiExtensometer3D_Rif(1,ii) = GFx*BFx*digit_Rif_x; % conversione in microEpsilon
        digit_Rif_y = (Extensometer3D_Rif(1,ii+1).^2)*0.001; % Freq. ^2 *E-3
        DatiExtensometer3D_Rif(1,ii+1) = GFy*BFy*digit_Rif_y; % conversione in microEpsilon
        digit_Rif_z = (Extensometer3D_Rif(1,ii+2).^2)*0.001; % Freq. ^2 *E-3
        DatiExtensometer3D_Rif(1,ii+2) = GFz*BFz*digit_Rif_z; % conversione in microEpsilon
        % Dati successivi
        digit_x = (Extensometer3D(:,ii).^2)*0.001; % Freq. ^2 *E-3
        DatiExtensometer3D(:,ii) = GFx*BFx*digit_x(:,1); % conversione in microEpsilon
        digit_y = (Extensometer3D(:,ii+1).^2)*0.001; % Freq. ^2 *E-3
        DatiExtensometer3D(:,ii+1) = GFy*BFy*digit_y(:,1); % conversione in microEpsilon
        digit_z = (Extensometer3D(:,ii+2).^2)*0.001; % Freq. ^2 *E-3
        DatiExtensometer3D(:,ii+2) = GFz*BFz*digit_z(:,1); % conversione in microEpsilon
    elseif strcmp(Nodo3DExtensometerLink(e,3),'4-20 mA')
        % Spacchetto i parametri di calibrazione
        ax = DCalEL3DTot(e,1);
        bx = DCalEL3DTot(e,2);
        cx = DCalEL3DTot(e,3);
        ay = DCalEL3DTot(e,4);
        by = DCalEL3DTot(e,5);
        cy = DCalEL3DTot(e,6);
        az = DCalEL3DTot(e,7);
        bz = DCalEL3DTot(e,8);
        cz = DCalEL3DTot(e,9);
        % Lettura di Riferimento
        Rif_X = (str2double(cell2mat(Extensometer3D_Rif(1,ii))));
        DatiExtensometer3D_Rif(1,ii) = ax*Rif_X.^2 + bx*Rif_X+cx; % conversione in microEpsilon
        Rif_Y = (str2double(cell2mat(Extensometer3D_Rif(1,ii+1))));
        DatiExtensometer3D_Rif(1,ii+1) = ay*Rif_Y.^2 + by*Rif_Y+cy; % conversione in microEpsilon
        Rif_Z = (str2double(cell2mat(Extensometer3D_Rif(1,ii+2))));
        DatiExtensometer3D_Rif(1,ii+2) = az*Rif_Z.^2 + bz*Rif_Z+cz; % conversione in microEpsilon
        % Dati successivi
        DatiExtensometer3D(1,ii) = ax*Extensometer3D(1,ii).^2 + bx*Extensometer3D(1,ii) +cx; % conversione in microEpsilon
        DatiExtensometer3D(1,ii+1) = ay*Extensometer3D(1,ii+1).^2 + by*Extensometer3D(1,ii+1) +cy; % conversione in microEpsilon
        DatiExtensometer3D(1,ii+2) = az*Extensometer3D(1,ii+2).^2 + bz*Extensometer3D(1,ii+2) +cz; % conversione in microEpsilon
    elseif strcmp(Nodo3DExtensometerLink(e,3),'mV/V')
        gainx = DCalEL3DTot(:,1);
        intx = DCalEL3DTot(:,2);
        gainy = DCalEL3DTot(:,3);
        inty = DCalEL3DTot(:,4);
        gainz = DCalEL3DTot(:,5);
        intz = DCalEL3DTot(:,6);
        % Lettura di Riferimento
        Rif_X = (str2double(cell2mat(Extensometer3D_Rif(1,ii)))); 
        DatiExtensometer3D_Rif(1,ii) = gainx*Rif_X + intx; % conversione in microEpsilon
        Rif_Y = (str2double(cell2mat(Extensometer3D_Rif(1,ii+1))));
        DatiExtensometer3D_Rif(1,ii+1) = gainy*Rif_Y + inty; % conversione in microEpsilon
        Rif_Z = (str2double(cell2mat(Extensometer3D_Rif(1,ii+2))));
        DatiExtensometer3D_Rif(1,ii) = gainz*Rif_Z + intz; % conversione in microEpsilon
        % Dati successivi
        DatiExtensometer3D(:,ii) = gainx*Extensometer3D(:,ii) + intx; % conversione in microEpsilon
        DatiExtensometer3D(:,ii+1) = gainy*Extensometer3D(:,ii+1) + inty; % conversione in microEpsilon
        DatiExtensometer3D(:,ii+2) = gainz*Extensometer3D(:,ii+2) + intz; % conversione in microEpsilon
    end
    ii = ii+3;
    A = 1.4051*10^(-3);
    B = 2.369*10^(-4);
    C = 1.019*10^(-7);
    if EL3D_NTC(s,1)==1 % è presente il termometro
        R = NTCExtensometer3D(:,s);
        [rN,~] = size(R);
        for n = 1:rN
            if R(n) <= 0
                if n > 1
                    R(n) = R(n-1);
                else
                    R(n) = 5000;
                end
                if Err3DExtensometerLink(n,ii) == 0
                    Err3DExtensometerLink(n,ii) = 0.5;
                end
            end
        end
        L = reallog(R);
        DatiNTCExtensometer3D(:,s) = (1./(A + B*L + C*(L.^3))) -273.2; % conversione in gradi centigradi
    end
    s = s+1;
end

text = 'Calibrations convertion of 3D Extensometer Link defined correctly. conv_grezzi3DEL function closed';
fprintf(fileID,fmt,text);
fclose(fileID);

end