Files
matlab-python/Tilt/defDatiTLHRH.m

166 lines
5.3 KiB
Matlab
Executable File

% Funzione che definisce accelerazioni, dati magnetici, temperature, date e
% livello della batteria per i nodi di tipo Tilt Link
function [TimeTLHRH,angTLHRH,tempTLHRH,ErrTiltLinkHRH] = defDatiTLHRH(...
DatiTiltLinkHRH,ErrTiltLinkHRH,Ndatidespike,MEMS,rTLHRH,NodoTiltLinkHRH,...
NuovoZeroTLHRH,IDcentralina,DTcatena,FileName)
fileID = fopen(FileName,'a');
fmt = '%s \r';
text = 'defDatiTLHRH function started';
fprintf(fileID,fmt,text);
[r,c]=size(DatiTiltLinkHRH);
Ncorr = 0;
% Elimino gli eventuali Not a Number
for a = 2:r
for b = 1:c
check = isnan(DatiTiltLinkHRH(a,b));
if check == 1
DatiTiltLinkHRH(a,b) = DatiTiltLinkHRH(a-1,b);
ErrTiltLinkHRH(a,b-1) = 1;
Ncorr = Ncorr+1;
end
end
end
text = [num2str(Ncorr) ' NaN of Tilt Link HR H corrected by defDatiTLHRH function'];
fprintf(fileID,fmt,text);
TimeTLHRH = DatiTiltLinkHRH(:,1); % data
angTLHRH = zeros(r,rTLHRH*2); % dati accelerometro
controllo_TLHRH = zeros(r,rTLHRH*3);
tempTLHRH = zeros(r,rTLHRH); % temperatura del nodo per la calibrazione
ctl = 1;
p = 2; % passo
s = 1;
for i = 1:rTLHRH
angTLHRH(:,s:s+1) = DatiTiltLinkHRH(:,p:p+1);
s = s+2;
controllo_TLHRH(:,ctl:ctl+2) = DatiTiltLinkHRH(:,p+2:p+4);
ctl = ctl+3;
tempTLHRH(:,i) = DatiTiltLinkHRH(:,p+5);
p = p+6;
end
s = 1;
Num_Dati = Ndatidespike; % numero di dati per il despike
if Num_Dati > r
Num_Dati = r;
end
for i = 1:rTLHRH
% despiking
angTLHRH(:,s) = filloutliers(angTLHRH(:,s),'linear','movmedian',Num_Dati);
angTLHRH(:,s+1) = filloutliers(angTLHRH(:,s+1),'linear','movmedian',Num_Dati);
s = s+2;
end
%% !!! Controllo che le ampolle non siano fuori scala (32768)
AmpolleUpdate = ['' IDcentralina '-' DTcatena '-Ampolle.csv'];
[rC,cC] = size(angTLHRH);
if NuovoZeroTLHRH == 1
Dati = csvread(AmpolleUpdate);
Dati(:,1) = Dati(:,1) + 730000;
[rD,~] = size(Dati);
num = 1;
cont = 1;
if MEMS == 2 % Nuovi nodi
if rD ~= 0
for j = 1:rC
for ii = 1:cC
segno1 = 0;
if j == 1
for a = 2:rD
if TimeTLHRH(j,1) >= Dati(a,1)
segno1 = sign(Dati(a-1,ii+1)); % segno del valore alla data precedente
break
end
end
end
if segno1 == 0
if j == 1
else
segno1 = sign(angTLHRH(j-1,ii)); % segno del valore alla data precedente
end
end
segno2 = sign(angTLHRH(j,ii)); % segno del valore alla data successiva
if segno2 ~= segno1 % se i due segni sono diversi
if abs(angTLHRH(j,ii)) > 15000
if segno1 == 1
angTLHRH(j,ii) = 32768 + (32768 + angTLHRH(j,ii)); % Fondo scala positivo
elseif segno1 == -1
angTLHRH(j,ii) = -32768 + (-32768 + angTLHRH(j,ii)); % Fondo scala negativo
end
text = ['Tilt Link HR H was out of range on node ' mat2str(cell2mat(NodoTiltLinkHRH(num,2))) ''];
fprintf(fileID,fmt,text);
end
end
cont = cont+1;
if cont == 3
cont = 1;
num = num+1;
end
end
cont = 1;
num = 1;
end
indice = 1;
for j=1:rC
if Dati(end,1) == TimeTLHRH(j,1)
indice = j+1;
break
end
end
else
indice = 1;
end
else
indice = [];
end
else
indice = 1;
end
%% Controllo che l'ampolla non venga letta come MEMS!
index = 1;
nodo = 1;
for b = 1:rTLHRH % nodi
for a = 1:rC % date
if controllo_TLHRH(a,index)~=0 && controllo_TLHRH(a,index+1)~=0 && controllo_TLHRH(a,index+2)~=0
if a == 1
if NuovoZeroTLHRH == 1
for aa = 2:rD
if TimeTLHRH(a,1) == Dati(aa,1)
angTLHRH(a,2*nodo-1:2*nodo) = Dati(aa-1,2*nodo:2*nodo+1); % valore alla data precedente
break
end
end
end
else
angTLHRH(a,2*nodo-1:2*nodo) = angTLHRH(a-1,2*nodo-1:2*nodo);
end
ErrTiltLinkHRH(a,2*nodo-1:2*nodo) = 1;
end
end
index = index+3;
nodo = nodo+1;
end
if isempty(indice) == 0
Dati = TimeTLHRH(indice:end,1);
Dati(:,1) = Dati(:,1) - 730000;
datiHRH = [Dati(:,1) angTLHRH(indice:end,:)];
dat=isempty(datiHRH);
if dat==0
if NuovoZeroTLHRH == 1
delete(AmpolleUpdate);
end
csvwrite(AmpolleUpdate,datiHRH);
end
end
text = 'Data of Tilt Link HR H defined correctly. defDatiTLHRH function started';
fprintf(fileID,fmt,text);
fclose(fileID);
end