close all;
clear all;



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


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

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

% 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 = 10 m

r = 10;

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);



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

 Ev(i) = abs( E1(i) + E2(i)*exp(j*k*del_r));
 Eh(i) = abs( E1(i) - E2(i)*exp(j*k*del_r));
  
 Evt(i) = abs( Tx_ver(Upt)*E1(i) + Tx_ver(Uot)*E2(i)*exp(j*k*del_r));
 Eht(i) = abs( Tx_hor(Upt)*E1(i) - Tx_hor(Uot)*E2(i)*exp(j*k*del_r));
 
 if h2 == 1
      Evmax(i) = Ev(i);
      Ehmax(i) = Eh(i);
      
      
      Evmaxt(i) = Evt(i);
      Ehmaxt(i) = Eht(i);   
 
 else
    if Evmax(i) < Ev(i)
       Evmax(i) = Ev(i);
       
    end
    
    if Evmaxt(i) < Evt(i)
       Evmaxt(i) = Evt(i);
       
    end

    if Ehmax(i) < Eh(i)
       Ehmax(i) = Eh(i)
    end
    
    if Ehmaxt(i) < Eht(i)
       Ehmaxt(i) = Eht(i)
    end
    
 end
 
 
i = i+1;
end

Xplot = 30e6:10e6:1000e6

figure(2)

subplot(2,1,1)
semilogx(Xplot, Ev, 'r')
hold on;
semilogx(Xplot, Evt, '--g')
title('Vertikální polarizace (10 m)');
%legend('0.05m', 'Obálka')
xlabel('Frekvence');
ylabel('Intenzita [V/m]');

subplot(2,1,2)
semilogx(Xplot, Eh, 'r')
hold on;
semilogx(Xplot, Eht, '--g')
title('Horizontální polarizace (10 m)');
%legend('0.05m', 'Obálka')
xlabel('Frekvence');
ylabel('Intenzita [V/m]');

i = 1;
end

Evmax_dB = 20*LOG10(Evmax*1e6);
Ehmax_dB = 20*LOG10(Ehmax*1e6);
Evmaxt_dB = 20*LOG10(Evmaxt*1e6);
Ehmaxt_dB = 20*LOG10(Ehmaxt*1e6);


%% 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);

E_far(i) = (sqrt(30*P)/r1);

 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));
 
 if h2 == 1
      Evmax3(i) = Ev3(i);
      Ehmax3(i) = Eh3(i);
      E_farM(i) = E_far(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)
       Evmaxt3(i) = Evt3(i);
       
    end

    if Ehmax3(i) < Eh3(i)
       Ehmax3(i) = Eh3(i)
    end
    
    if Ehmaxt3(i) < Eht3(i)
       Ehmaxt3(i) = Eht3(i)
    end
    
 end
 
 
i = i+1;
end

Xplot = 30:10:1000


i = 1;
end

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




figure(5)
set(gcf, 'color', [1 1 1])
subplot(2,1,1)

semilogx(Xplot, Evmaxt_dB3, 'r','LineWidth',2)
hold on;
semilogx(Xplot, E_far_db3, 'b','LineWidth',2)
hold on;
semilogx(Xplot, Evmaxt_dB, 'g','LineWidth',2)
set(gca,'FontSize',25)
legend('SAR (3 m)', 'FAR (3 m)', 'SAR (10 m)', 'Location','southeast','Orientation','horizontal')
title('Vertikální polarizace');
xlim([30 1000]);
xlabel('Frekvence [MHz]');
ylabel('Intenzita [dBuV/m]');
set(gca,'FontSize',20)

subplot(2,1,2)
semilogx(Xplot, Evmaxt_dB3 - Evmaxt_dB, 'r','LineWidth',2)
hold on;
semilogx(Xplot, E_far_db3 - Evmaxt_dB, 'b','LineWidth',2)
set(gca,'FontSize',25)
xlim([30 1000]);
ylim([0 12])
legend('SAR (3 m) vs SAR (10 m)', 'FAR (3 m) vs SAR (10 m)', 'Location','southeast','Orientation','horizontal')
title('Vertikální polarizace');
xlabel('Frekvence [MHz]');
ylabel('Rozdíl [dB]');
set(gca,'FontSize',20)


figure(6)
set(gcf, 'color', [1 1 1])
subplot(2,1,1)
semilogx(Xplot, Ehmaxt_dB3, 'r','LineWidth',2)
hold on;
semilogx(Xplot, E_far_db3, 'b','LineWidth',2)
hold on;
semilogx(Xplot, Ehmaxt_dB, 'g','LineWidth',2)
set(gca,'FontSize',25)
legend('SAR (3 m)', 'FAR (3 m)', 'SAR (10 m)', 'Location','southeast','Orientation','horizontal')
title('Horizontální polarizace');
xlim([30 1000]);
xlabel('Frekvence [MHz]');
ylabel('Intenzita [dBuV/m]');
set(gca,'FontSize',20)

subplot(2,1,2)
semilogx(Xplot, Ehmaxt_dB3 - Ehmaxt_dB, 'r','LineWidth',2)
hold on;
semilogx(Xplot, E_far_db3 - Ehmaxt_dB, 'b','LineWidth',2)
set(gca,'FontSize',25)
legend('SAR (3 m) vs SAR (10 m)', 'FAR (3 m) vs SAR (10 m)', 'Location','southeast','Orientation','horizontal')

title('Horizontální polarizace');
xlim([30 1000]);
ylim([0 22])
xlabel('Frekvence [MHz]');
ylabel('Rozdíl [dB]');
set(gca,'FontSize',20)

% MAX3_izo = max(Evmax_dB3, Ehmax_dB3);
% MAX3_ch = max(Evmaxt_dB3, Ehmaxt_dB3);
% MAX10_izo = max(Evmax_dB, Ehmax_dB);
% MAX10_ch = max(Evmaxt_dB, Ehmaxt_dB);
% 
% figure(7)
% set(gcf, 'color', [1 1 1])
% subplot(2,1,1)
% semilogx(Xplot, MAX3_izo, 'r','LineWidth',2)
% hold on;
% semilogx(Xplot, MAX10_izo, '--r','LineWidth',2)
% hold on;
% semilogx(Xplot, MAX3_ch, 'g','LineWidth',2)
% hold on;
% semilogx(Xplot, MAX10_ch, '--g','LineWidth',2)
% set(gca,'FontSize',25)
% legend('3 m (Izo)', '10 m (Izo)','3 m (BiLog)', '10 m (BiLog)', 'Location','northwest','Orientation','vertical')
% title('Kombinace vertikální a horizontální polarizace');
% xlabel('Frekvence [MHz]');
% ylabel('Intenzita [dBuV/m]');
% set(gca,'FontSize',20)
% 
% subplot(2,1,2)
% semilogx(Xplot, (MAX3_izo - MAX10_izo), 'r','LineWidth',2)
% hold on;
% semilogx(Xplot, (MAX3_ch - MAX10_ch), 'g','LineWidth',2)
% 
% set(gca,'FontSize',25)
% legend('S náklonem', 'Bez náklonu', 'Location','northwest','Orientation','vertical')
% title('Kombinace vertikální a horizontální polarizace')
% xlabel('Frekvence [MHz]');
% ylabel('Rozdíl [dB]');
% set(gca,'FontSize',20)
% 
% 
% 
