% On ...
clear all;
close all;
whitebg('w');


%Constante de choix du PCB : PCB droite,PCB GAUCHE, PCB COMPLET
PCB_Droite = 0;
PCB_Gauche = 0;
PCB_Complet = 1;

% Constante de comparaison des gains Gain_1 = 0 et Gain_10 = 1
Gain = 0;
       
if Gain == 0
    Pente = 2; 
    V_DAC_EXT   = 1.0;
else
    Pente = 1.3;%1.5;
    V_DAC_EXT   = 0.1;
end

%Nom du fichier texte à ouvrir

fileInName          = '308007-04-31.014.acq_moy_pcb1';
fileOutName         = 'tmp.dat';

fileIn              = fopen(fileInName, 'rt');
fileOut             = fopen(fileOutName, 'wt');

i=0;
while feof(fileIn) == 0
    % Lecture de la ligne du fichier jusqu'a // EOH
    tline = fgets(fileIn);
    while (strncmpi(tline, '// EOH', 6) == 0)
       tline = fgets(fileIn);
       i = i+1;
    end
j =  0;    
    % Lecture des donnees + Ecriture dans fichier temporaire
    while (strncmpi(tline, '// EOF', 6) == 0)
       tline = fgets(fileIn);
       j = j + 1;
       if (strncmpi(tline, '// EOF', 6) == 0)
           fprintf(fileOut, '%s', tline);
       end
    end
end


% Fermeture des fichiers
fclose(fileIn);
fclose(fileOut);

% Déclaration des variables globales
Nbr_Pads    = 216;
Nbr_Enable  = 6;
Nbr_DAC     = 10; %4;
%V_DAC_EXT   = 1;%1.25;



% Ouverture du fichier temporaire pour analyse
% Lecture du fichier texte et formatage  
formatStr = '%d %f %f %f %f %d' ;
formatdata = '[NumEvt Date X_Pos Y_Pos Tg_Alpha DAC';
for Count_Pads=1:(Nbr_Pads)
    formatStr = [formatStr,'%f '];
    formatdata = [formatdata,',Data(:,',num2str(Count_Pads),')'];
end

% Evaluation du contenu du fichier texte
formatdata = [formatdata, ']=textread(fileOutName,formatStr);'];
eval(formatdata);
NbEvt = size(Data,1);

% Mise en mémoire tampon de la ligne de base
Baseline = zeros(Nbr_DAC,Nbr_Pads);
Baseline = Data(1:Nbr_DAC,1:Nbr_Pads);
Mean_Baseline = mean(Baseline);
Ecart_type_Base = std(Baseline);

figure(10)
subplot(1,2,1);
bar(Ecart_type_Base(1:(Nbr_Pads/2)));
title 'PCB1 Wafer 1-6';
xlabel('Channel');
ylabel('ADC Level (V)');

subplot(1,2,2);
bar(Ecart_type_Base((Nbr_Pads/2):Nbr_Pads));
title 'PCB1 Wafer 4-6';
xlabel('Channel');
ylabel('ADC Level (V)');

% Format des Enables et declaration des matrices contenant chacune une ligne de baseline en vue
% du tracé des differents graphe

for Count_Enable = 1 : Nbr_Enable,
  Enable(Count_Enable).DAC = zeros(Nbr_DAC,Nbr_Pads);
  Mat_Plot(Count_Enable).Row = zeros((Nbr_DAC + 1),Nbr_Pads);
  Swing(Count_Enable).Value = zeros(Nbr_DAC,18);
  Result(Count_Enable).Value = zeros(Nbr_DAC,18);
end

% Construction des Matrices de données à analyser
Nbr_Row = Nbr_DAC;
for  Count_Enable = 1 : Nbr_Enable,
%Nbr_Row = 1;        
    for Count_DAC = 1 : Nbr_DAC,
       Nbr_Row = Nbr_Row + 1;        
       for Count_Pixels = 1 : Nbr_Pads
        Enable(Count_Enable).DAC(Count_DAC,Count_Pixels) = (Data(Nbr_Row,Count_Pixels)- Mean_Baseline(Count_Pixels));
       end
   end
end


% Configuration de la matrice Mat_Plot(rajout de la ligne représentant la
% baseline)
for Count_Enable = 1 : Nbr_Enable,
    Mat_Plot(Count_Enable).Row(1,:) = 0;%Mean_Baseline;
    for Count_Row = 2 : (Nbr_DAC + 1),
       for Count_Pixels = 1 : Nbr_Pads
        Mat_Plot(Count_Enable).Row(Count_Row,Count_Pixels) = Enable(Count_Enable).DAC(Count_Row-1,Count_Pixels);
        end
    end
end

% Déclaration pour les plots
% A revoir pour simplification et généricité.....
Mv_DAC = zeros(NbEvt,1);
for i = 1 : NbEvt,
    %Mv_DAC(i,1) = (DAC(i,1)*0.000445453)-0.001310782;
    Mv_DAC(i,1) = (DAC(i,1)*0.000445453*V_DAC_EXT/1)-0.001310782;
end
[X_DAC] = [ Mv_DAC(10) Mv_DAC(11) Mv_DAC(12) Mv_DAC(13) Mv_DAC(14) Mv_DAC(15) Mv_DAC(16) Mv_DAC(17) Mv_DAC(18) Mv_DAC(19) Mv_DAC(20)];
figure(1);
plot(X_DAC,Mat_Plot(1).Row(:,192),'g',X_DAC,Mat_Plot(1).Row(:,193))%,X_DAC,Mat_Plot(3).Row(:,7),X_DAC,Mat_Plot(4).Row(:,2),X_DAC,Mat_Plot(5).Row(:,5),X_DAC,Mat_Plot(6).Row(:,8));
grid on;

% figure(2);
% title 'Enable VI';
% subplot(3,2,1);plot(X_DAC,Mat_Plot(6).Row(:,8));
% grid on;
% %
% subplot(3,2,3);plot(X_DAC,Mat_Plot(6).Row(:,17:18));
% grid on;
% %26
% subplot(3,2,5);plot(X_DAC,Mat_Plot(6).Row(:,25));
% grid on;
% %
% subplot(3,2,2);plot(X_DAC,Mat_Plot(6).Row(:,27));
% grid on;
% %
% subplot(3,2,4);plot(X_DAC,Mat_Plot(6).Row(:,34));
% grid on;
% %
% subplot(3,2,6);plot(X_DAC,Mat_Plot(6).Row(:,36));
% grid on;
% 
% figure (3);
% title 'Enable II';
% subplot(3,2,1);plot(X_DAC,Mat_Plot(3).Row(:,7));
% grid on;
% %
% subplot(3,2,3);plot(X_DAC,Mat_Plot(3).Row(:,9));
% grid on;
% %26
% subplot(3,2,5);plot(X_DAC,Mat_Plot(3).Row(:,16));
% grid on;
% %
% subplot(3,2,2);plot(X_DAC,Mat_Plot(3).Row(:,17:18));
% grid on;
% %
% subplot(3,2,4);plot(X_DAC,Mat_Plot(3).Row(:,26));
% grid on;
% %
% subplot(3,2,6);plot(X_DAC,Mat_Plot(3).Row(:,35));
% grid on;
% 
% figure(4);
% subplot(3,2,1);plot(X_DAC,Mat_Plot(1).Row(:,1:108));
% grid on;
% %
% subplot(3,2,2);plot(X_DAC,Mat_Plot(4).Row(:,1:108));
% grid on;
% %
% subplot(3,2,3);plot(X_DAC,Mat_Plot(2).Row(:,1:108));
% grid on;
% %
% subplot(3,2,4);plot(X_DAC,Mat_Plot(5).Row(:,1:108));
% grid on;
% %
% subplot(3,2,5);plot(X_DAC,Mat_Plot(3).Row(:,1:108));
% grid on;
% %
% subplot(3,2,6);plot(X_DAC,Mat_Plot(6).Row(:,1:108));
% grid on;
% 
% figure(5);
% subplot(3,2,1);plot(X_DAC,Mat_Plot(1).Row(:,109:216));
% grid on;
% %
% subplot(3,2,2);plot(X_DAC,Mat_Plot(4).Row(:,109:216));
% grid on;
% %
% subplot(3,2,3);plot(X_DAC,Mat_Plot(2).Row(:,109:216));
% grid on;
% %
% subplot(3,2,4);plot(X_DAC,Mat_Plot(5).Row(:,109:216));
% grid on;
% %
% subplot(3,2,5);plot(X_DAC,Mat_Plot(3).Row(:,109:216));
% grid on;
% %
% subplot(3,2,6);plot(X_DAC,Mat_Plot(6).Row(:,109:216));
% grid on;

figure(6);
subplot(3,6,1);plot(X_DAC,Mat_Plot(1).Row(:,1:36));
grid on;
title 'Enable I';

%
subplot(3,6,7);plot(X_DAC,Mat_Plot(1).Row(:,37:72));
grid on;
%
subplot(3,6,13);plot(X_DAC,Mat_Plot(1).Row(:,73:108));
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,6,2);plot(X_DAC,Mat_Plot(2).Row(:,1:36));
grid on;
title 'Enable II';
%
subplot(3,6,8);plot(X_DAC,Mat_Plot(2).Row(:,37:72));
grid on;
%
subplot(3,6,14);plot(X_DAC,Mat_Plot(2).Row(:,73:108));
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,6,3);plot(X_DAC,Mat_Plot(3).Row(:,1:36));
grid on;
title 'Enable III';
%
subplot(3,6,9);plot(X_DAC,Mat_Plot(3).Row(:,37:72));
grid on;
%
subplot(3,6,15);plot(X_DAC,Mat_Plot(3).Row(:,73:108));
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,6,4);plot(X_DAC,Mat_Plot(4).Row(:,1:36));
grid on;
title 'Enable IV';
%
subplot(3,6,10);plot(X_DAC,Mat_Plot(4).Row(:,37:72));
grid on;
%
subplot(3,6,16);plot(X_DAC,Mat_Plot(4).Row(:,73:108));
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,6,5);plot(X_DAC,Mat_Plot(5).Row(:,1:36));
grid on;
title 'Enable V';
%
subplot(3,6,11);plot(X_DAC,Mat_Plot(5).Row(:,37:72));
grid on;
%
subplot(3,6,17);plot(X_DAC,Mat_Plot(5).Row(:,73:108));
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,6,6);plot(X_DAC,Mat_Plot(6).Row(:,1:36));
grid on;
title 'Enable VI';
%
subplot(3,6,12);plot(X_DAC,Mat_Plot(6).Row(:,37:72));
grid on;
%
subplot(3,6,18);plot(X_DAC,Mat_Plot(6).Row(:,73:108));
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure(7);
subplot(3,6,1);plot(X_DAC,Mat_Plot(1).Row(:,109:144));
grid on;
title 'Enable VII';
%
subplot(3,6,7);plot(X_DAC,Mat_Plot(1).Row(:,145:180));
grid on;
%
subplot(3,6,13);plot(X_DAC,Mat_Plot(1).Row(:,181:216));
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,6,2);plot(X_DAC,Mat_Plot(2).Row(:,109:144));
grid on;
title 'Enable VIII';
%
subplot(3,6,8);plot(X_DAC,Mat_Plot(2).Row(:,145:180));
grid on;
%
subplot(3,6,14);plot(X_DAC,Mat_Plot(2).Row(:,181:216));
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,6,3);plot(X_DAC,Mat_Plot(3).Row(:,109:144));
grid on;
title 'Enable IX';
%
subplot(3,6,9);plot(X_DAC,Mat_Plot(3).Row(:,145:180));
grid on;
%
subplot(3,6,15);plot(X_DAC,Mat_Plot(3).Row(:,181:216));
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,6,4);plot(X_DAC,Mat_Plot(4).Row(:,109:144));
grid on;
title 'Enable X';
%
subplot(3,6,10);plot(X_DAC,Mat_Plot(4).Row(:,145:180));
grid on;
%
subplot(3,6,16);plot(X_DAC,Mat_Plot(4).Row(:,181:216));
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,6,5);plot(X_DAC,Mat_Plot(5).Row(:,109:144));
grid on;
title 'Enable XI';
%
subplot(3,6,11);plot(X_DAC,Mat_Plot(5).Row(:,145:180));
grid on;
%
subplot(3,6,17);plot(X_DAC,Mat_Plot(5).Row(:,181:216));
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
subplot(3,6,6);plot(X_DAC,Mat_Plot(6).Row(:,109:144));
grid on;
title 'Enable XII';
%
subplot(3,6,12);plot(X_DAC,Mat_Plot(6).Row(:,145:180));
grid on;
%
subplot(3,6,18);plot(X_DAC,Mat_Plot(6).Row(:,181:216));
grid on;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% figure(8);
% subplot(2,1,1);plot(X_DAC,Mat_Plot(3).Row(:,16),'m');
% grid on;
% title 'Enable III';
% hold on;
% gtext ('voie 16');
% %
% subplot(2,1,1);plot(X_DAC,Mat_Plot(3).Row(:,17),'r');
% grid on;
% hold on;
% gtext ('voie 17');
% %
% subplot(2,1,1);plot(X_DAC,Mat_Plot(3).Row(:,18),'g');
% grid on;
% hold on;
% gtext ('voie 18');
% %
% subplot(2,1,1);plot(X_DAC,Mat_Plot(3).Row(:,34),'b');
% grid on;
% hold on;
% gtext ('voie 34');
% %axis ([0 0.3 0 2.5]);
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%ù
% subplot(2,1,2);plot(X_DAC,Mat_Plot(6).Row(:,16),'m');
% grid on;
% title 'Enable VI';
% hold on;
% gtext ('voie 16');
% %
% subplot(2,1,2);plot(X_DAC,Mat_Plot(6).Row(:,17),'r');
% grid on;
% hold on;
% gtext ('voie 17');
% %
% subplot(2,1,2);plot(X_DAC,Mat_Plot(6).Row(:,18),'g');
% grid on;
% hold on;
% gtext ('voie 18');
% %
% subplot(2,1,2);plot(X_DAC,Mat_Plot(6).Row(:,34),'b');
% grid on;
% gtext ('voie 34');
% 
%axis ([0 0.1 0 2.5]);


% Declaration de variables pour un fit
voie16_3 = [Mat_Plot(3).Row(:,16)];
voie17_3 = [Mat_Plot(3).Row(:,17)];
voie18_3 = [Mat_Plot(3).Row(:,18)];
voie34_3 = [Mat_Plot(3).Row(:,34)];

voie16_6 = [Mat_Plot(6).Row(:,16)];
voie17_6 = [Mat_Plot(6).Row(:,17)];
voie18_6 = [Mat_Plot(6).Row(:,18)];
voie34_6 = [Mat_Plot(6).Row(:,34)];

%fiiting(X_DAC,voie16_3,voie17_3,voie16_6,voie17_6);

outliers = excludedata(X_DAC',voie16_3,'indices',[5:11]);
voie16_En3   = fit(X_DAC',voie16_3,'poly1','Exclude',outliers);
Confidence = confint (voie16_En3,0.97);
figure(9);
plot (voie16_En3,X_DAC,voie16_3);

outliers1 = excludedata(X_DAC',voie17_6,'indices',[3:11]);
voie17_En6   = fit(X_DAC',voie17_6,'poly1','Exclude',outliers1);

%%%%============ POUR MON ESSAI=========%%%
% Tableau de correspondances pad calibration
for Id_enable = 1:12,
    Enable(Id_enable).Pads = zeros(1,18);
end
Enable(1).Pads  = [1 3 10 12 20 29 37 39 46 48 56 65 73 75 82 84 92 101];
Enable(2).Pads  = [4 6 13 15 23 32 40 42 49 51 59 68 76 78 85 87 95 104];
Enable(3).Pads  = [7 9 16 18 26 35 43 45 52 54 62 71 79 81 88 90 98 107];
Enable(4).Pads  = [2 11 19 21 28 30 38 47 55 57 64 66 74 83 91 93 100 102];
Enable(5).Pads  = [5 14 22 24 31 33 41 50 58 60 67 69 77 86 94 96 103 105];
Enable(6).Pads  = [8 17 25 27 34 36 44 53 61 63 70 72 80 89 97 99 106 108];
Enable(7).Pads  = [109 111 118 120 128 137 145 147 154 156 164 173 181 183 190 192 200 209];
Enable(8).Pads  = [112 114 121 123 131 140 148 150 157 159 167 176 184 186 193 195 203 212];
Enable(9).Pads  = [115 117 124 126 134 143 151 153 160 162 170 179 187 189 196 198 206 215];
Enable(10).Pads = [110 119 127 129 136 138 146 155 163 165 172 174 182 191 199 201 208 210];
Enable(11).Pads = [113 122 130 132 139 141 149 158 166 168 175 177 185 194 202 204 211 213];
Enable(12).Pads = [116 125 133 135 142 144 152 161 169 171 178 180 188 197 205 207 214 216];

%%%%% inter calib - essai %%%%%%%%%%%%%%%%%
for  Count_Enable = 1 : Nbr_Enable,
%Nbr_Row = 1;        
    for Count_DAC = 1 : Nbr_DAC,
       for Id_pads = 1 : 18
        num_voie   = Enable(Count_Enable).Pads(Id_pads);
        Swing(Count_Enable).Value(Count_DAC,Id_pads)= Enable(Count_Enable).DAC(Count_DAC,num_voie) ;
        Result(Count_Enable).Value(Count_DAC,Id_pads) = Swing(Count_Enable).Value(Count_DAC,Id_pads) - Swing(Count_Enable).Value(Count_DAC,1);
    end 
end 
end
figure (15);
plot (X_DAC(2:11),Result(1).Value(:,:));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
PCB = zeros(3,216);
i= 0;
for Num_Enable = 1 : 6,
for Id_pads = 1 : 18,
  num_voie   = Enable(Num_Enable).Pads(Id_pads);
  voie     = [Mat_Plot(Num_Enable).Row(:,num_voie)];
  outliers = excludedata(X_DAC',voie,'indices',[5:11]);
  voies    = fit(X_DAC',voie,'poly1','Exclude',outliers) ;
  ci =confint(voies,0.01);
  i= i +1;
  PCB(1,i) = num_voie;
  PCB(2,i) =ci(1,1);
  if (PCB(2,i)) < Pente
      PCB(3,i) = (1);
      pad_defaillant = num_voie;
  else
      PCB(3,i) = (0);
  end
end
end   

for Num_Enable = 7 : 12,
for Id_pads = 1 : 18,
  num_voie   = Enable(Num_Enable).Pads(Id_pads);
  voie     = [Mat_Plot((Num_Enable-6)).Row(:,num_voie)];
  outliers = excludedata(X_DAC',voie,'indices',[5:11]);
  voies    = fit(X_DAC',voie,'poly1','Exclude',outliers) ;
  ci =confint(voies,0.01);
  i= i +1;
  PCB(1,i) = num_voie;
  PCB(2,i) =ci(1,1);
  if (PCB(2,i)) < Pente
      PCB(3,i) = (1);
       pad_defaillant = num_voie;
  else
      PCB(3,i) = (0);
  end
end
end   
% figure(12);
% plot(X_DAC,Mat_Plot(1).Row(:,1:36));
% grid on;
% title 'Enable I';


disp (' Pad_defaillant')
if PCB_Complet ==1
 for j = 1 : 216
    if (PCB(3,j)) == 1
        disp(PCB(1,j))
        format compact
    end
  end
elseif PCB_Gauche ==1
  for j = 1 : 108
    if (PCB(3,j)) == 1
        disp(PCB(1,j))
        format compact
    end
 end
elseif PCB_Droite ==1
  for j = 109 : 216
    if (PCB(3,j)) == 1
        disp(PCB(1,j))
        format compact
    end
 end
end











