close all;
clear all;



h1 = 0.8;
h2 = 1;
r = 10;
P = 100;


% Izotropní zářič
D = ones(1,360);

% Dipol (shift)
%D = dipol(90);

% random
%D = rand(1, 360);

% Get_random_Char
%D = get_random_Char(1)



%% Prijmaci antena - charky


 
T_both = xlsread('3142_BiConiLog_97.xls'); 
 T_ver = T_both(:,1:8);
 T_hor = T_both(:,9:16);





%% r = 3 m

r = 3;

for h2 = 1:0.05:4
    i = 1;
   
for freq = 30e6:10e6:1000e6
    
    if freq <= 400e6
        Tx_hor = db2mag(T_hor(:,1));
        Tx_ver = db2mag(T_ver(:,1));
    elseif freq <= 500e6
        Tx_hor = db2mag(T_hor(:,2));
        Tx_ver = db2mag(T_ver(:,2));
    elseif freq <= 600e6
        Tx_hor = db2mag(T_hor(:,3));
        Tx_ver = db2mag(T_ver(:,3));
    elseif freq <= 700e6
        Tx_hor = db2mag(T_hor(:,4));
        Tx_ver = db2mag(T_ver(:,4));
    elseif freq <= 800e6
        Tx_hor = db2mag(T_hor(:,5));
        Tx_ver = db2mag(T_ver(:,5));
    elseif freq <= 900e6
        Tx_hor = db2mag(T_hor(:,6));
        Tx_ver = db2mag(T_ver(:,6));
    elseif freq <= 1000e6
        Tx_hor = db2mag(T_hor(:,7));
        Tx_ver = db2mag(T_ver(:,7));
    end

 
    
r1 = sqrt((h2-h1)^2 + r^2);
r2 = sqrt((h1+h2)^2 + r^2);


del_r = r2 - r1;

lam = 3e8/freq;
k = 2*pi/lam;

alfaD = (180/pi)*asin((h2-h1)/r1);
betaD = (180/pi)*asin((h2+h1)/r2);

Up = round(alfaD);
Uo = round(360-betaD);

Upt = round(alfaD);
Uot = round(betaD);

 E1d(i) = (sqrt(30*P*D(Up))/3.0067);
 E1(i) = (sqrt(30*P*D(Up))/r1);
 E2(i) = (sqrt(30*P*D(Uo))/r2);

 Ev3(i) = abs( E1(i) + E2(i)*exp(j*k*del_r));
 Eh3(i) = abs( E1(i) - E2(i)*exp(j*k*del_r));
  
 Evt3(i) = abs( Tx_ver(Upt)*E1(i) + Tx_ver(Uot)*E2(i)*exp(j*k*del_r));
 Eht3(i) = abs( Tx_hor(Upt)*E1(i) - Tx_hor(Uot)*E2(i)*exp(j*k*del_r));
 
 Evt_Fhx(i) = Tx_ver(Upt)*E1(i)
 Eht_Fhx(i) = Tx_hor(Upt)*E1(i)
 
 
 if h2 == 1
      Evmax3F(i) = Evt_Fhx(i);
      Ehmax3F(i) = Eht_Fhx(i);
      
      Evmax3(i) = Ev3(i);
      Ehmax3(i) = Eh3(i);
      
      Evmaxt3(i) = Evt3(i);
      Ehmaxt3(i) = Eht3(i);   
 
 else
    if Evmax3(i) < Ev3(i)
       Evmax3(i) = Ev3(i);
       
    end
    
    if Evmaxt3(i) < Evt3(i)
       Ehmax3F(i) = Evt3(i);
       
    end
    
    if Evmax3F(i) < Evt_Fhx(i)
       Evmax3F(i) = Evt_Fhx(i);
       
    end
    
       
    if Ehmax3(i) < Eh3(i)
       Ehmax3(i) = Eh3(i)
    end
    
    if Ehmaxt3(i) < Eht3(i)
       Ehmaxt3(i) = Eht3(i)
    end
    
    if Ehmax3F(i) < Eht_Fhx(i)
       Ehmax3F(i) = Eht_Fhx(i);
       
    end
    
 end
 
 
i = i+1;
end

Xplot = 30:10:1000


i = 1;
end

Evmax_dB3F = 20*LOG10(Evmax3F*1e6);
Ehmax_dB3F = 20*LOG10(Ehmax3F*1e6);

Evmax_dB3 = 20*LOG10(Evmax3*1e6);
Ehmax_dB3 = 20*LOG10(Ehmax3*1e6);
Evmaxt_dB3 = 20*LOG10(Evmaxt3*1e6);
Ehmaxt_dB3 = 20*LOG10(Ehmaxt3*1e6);
E1max_dB3 = 20*LOG10(E1d*1e6);

MAX3_izo = max(Evmax_dB3, Ehmax_dB3);
MAX3_ch = max(Evmaxt_dB3, Ehmaxt_dB3);
MAX3_F = max(Evmax_dB3F, Ehmax_dB3F);


figure(2)
set(gcf, 'color', [1 1 1])
subplot(2,1,1)
plot(Xplot, Ehmax_dB3, 'r','LineWidth',2)
hold on;
plot(Xplot, Ehmaxt_dB3, 'g','LineWidth',2)
hold on;
plot(Xplot, Ehmax_dB3F, 'm','LineWidth',2)
hold on;
plot(Xplot, E1max_dB3, 'b','LineWidth',2)
hold on;
set(gca,'FontSize',20)
legend('SAR(Izo)', 'SAR(BiLog)', 'FAR')
title('Horizontal');
xlabel('Frekvence [MHz]');
ylabel('Intenzita [dBuV/m]');
set(gca,'FontSize',20)

subplot(2,1,2)
plot(Xplot, (Ehmax_dB3 - E1max_dB3), 'r','LineWidth',2)
hold on;

set(gca,'FontSize',20)
legend('Izo', 'BiLog')
title('Rozdíly intenzity SAR a FAR (3 m)');
xlabel('Frekvence [MHz]');
ylabel('\Delta E [dBuV/m]');
set(gca,'FontSize',20)



figure(3)
set(gcf, 'color', [1 1 1])
subplot(2,1,1)
plot(Xplot, Evmax_dB3, 'r','LineWidth',2)
hold on;
plot(Xplot, Evmaxt_dB3, 'g','LineWidth',2)
hold on;
plot(Xplot, Evmax_dB3F, 'm','LineWidth',2)
hold on;
plot(Xplot, E1max_dB3, 'b','LineWidth',2)
hold on;
set(gca,'FontSize',20)
legend('SAR(Izo)', 'SAR(BiLog)', 'FAR')
title('Vertical)');
xlabel('Frekvence [MHz]');
ylabel('Intenzita [dBuV/m]');
set(gca,'FontSize',20)

subplot(2,1,2)
plot(Xplot, (Evmax_dB3 - E1max_dB3), 'r','LineWidth',2)
hold on;

set(gca,'FontSize',20)
legend('Izo', 'BiLog')
title('Rozdíly intenzity SAR a FAR (3 m)');
xlabel('Frekvence [MHz]');
ylabel('\Delta E [dBuV/m]');
set(gca,'FontSize',20)




figure(4)
set(gcf, 'color', [1 1 1])
subplot(2,1,1)
plot(Xplot, MAX3_izo, 'r','LineWidth',2)
hold on;
plot(Xplot, MAX3_ch, 'g','LineWidth',2)
hold on;
plot(Xplot, MAX3_F, 'm','LineWidth',2)
hold on;
plot(Xplot, E1max_dB3, 'b','LineWidth',2)
hold on;
set(gca,'FontSize',20)
legend('SAR(Izo)', 'SAR(BiLog)', 'FAR')
title('Srovnání intenzity SAR a FAR (3 m)');
xlabel('Frekvence [MHz]');
ylabel('Intenzita [dBuV/m]');
set(gca,'FontSize',20)

subplot(2,1,2)
semilogx(Xplot, (MAX3_izo - E1max_dB3), 'r','LineWidth',2)
hold on;
semilogx(Xplot, (MAX3_ch - E1max_dB3), 'g','LineWidth',2)
set(gca,'FontSize',20)
legend('Izo', 'BiLog')
title('Rozdíly intenzity SAR a FAR (3 m)');
xlabel('Frekvence [MHz]');
ylabel('\Delta E [dBuV/m]');
set(gca,'FontSize',20)


figure(5)
set(gcf, 'color', [1 1 1])
plot(Xplot, (Ehmax_dB3 - E1max_dB3), 'b','LineWidth',2)
hold on;
plot(Xplot, (Evmax_dB3 - E1max_dB3), 'r','LineWidth',2)
hold on;
set(gca,'FontSize',25)
legend('Horizontální polarizace', 'Vertikální polarizace','southeast','Orientation','vertical')
title('Srovnání intenzity v SAR a FAR (3 m)');
xlabel('Frekvence [MHz]');
xlim([30 1000]);
ylim([-6 6]);
ylabel('Rozdíl intenzit [dB]');
set(gca,'FontSize',20)




