close all;
clear all;



h1 = 0.8;
h2 = 1;
r = 3;
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 = 3 m

r = 3;
 i = 1;

%for h2 = 1:0.1:4
   
   for h2 = 1:0.05:4
    
r1 = sqrt((h2-h1)^2 + r^2);
r2 = sqrt((h1+h2)^2 + r^2);

del_r = r2 - r1;

lam_30 = 3e8/(30e6);
k_30 = 2*pi/lam_30;

lam_500 = 3e8/(500e6);
k_500 = 2*pi/lam_500;

lam_1000 = 3e8/(1000e6);
k_1000 = 2*pi/lam_1000;


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_30(i) = abs( E1(i) + E2(i)*exp(j*k_30*del_r));
 Eh_30(i) = abs( E1(i) - E2(i)*exp(j*k_30*del_r));
 
 Ev_500(i) = abs( E1(i) + E2(i)*exp(j*k_500*del_r));
 Eh_500(i) = abs( E1(i) - E2(i)*exp(j*k_500*del_r));
 
 Ev_1000(i) = abs( E1(i) + E2(i)*exp(j*k_1000*del_r));
 Eh_1000(i) = abs( E1(i) - E2(i)*exp(j*k_1000*del_r));
  
 i = i+1;
end

 
Xplot = 1:0.05:4;
 
figure(1)

subplot(2,1,1)
plot(Xplot, Eh_30, 'g', 'LineWidth',2)
hold on;
plot(Xplot, Eh_500, 'r', 'LineWidth',2)
hold on;
plot(Xplot, Eh_1000, 'b', 'LineWidth',2)
hold on;
set(gcf, 'color', [1 1 1])
set(gca,'FontSize',20)
legend('30 MHz', '500 MHz', '1000 MHz')
title('Horizontální polarizace (3 m) - simulace');
xlabel('Výška antény');
ylabel('Intenzita [uV]');

hold on;
subplot(2,1,2)
plot(Xplot, Ev_30, 'g','LineWidth',2)
hold on;
plot(Xplot, Ev_500, 'r','LineWidth',2)
hold on;
plot(Xplot, Ev_1000, 'b','LineWidth',2)
hold on;
set(gcf, 'color', [1 1 1])
set(gca,'FontSize',20)
title('Vertikální polarizace (3 m) - simulace');
legend('30 MHz', '500 MHz', '1000 MHz')
xlabel('Výška antény');
ylabel('Intenzita [uV]');

%% r = 3 m + uhly

filename = 'Bilog_char.xlsx';
sheet = 1;

%ver
VER_500_dB  = xlsread(filename, sheet, 'A2:A63');
VER_1000_dB  = xlsread(filename, sheet, 'B3:B63');

%hor
HOR_500_dB  = xlsread(filename, sheet, 'C2:C63');
HOR_1000_dB  = xlsread(filename, sheet, 'D3:D63');

VER_500 = db2mag(VER_500_dB);
VER_1000 = db2mag(VER_1000_dB);

HOR_500 = db2mag(HOR_500_dB);
HOR_1000 = db2mag(HOR_1000_dB);

r = 3;
 i = 1;

for h2 = 1:0.05:4

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

del_r = r2 - r1;

lam_30 = 3e8/(30e6);
k_30 = 2*pi/lam_30;

lam_500 = 3e8/(500e6);
k_500 = 2*pi/lam_500;

lam_1000 = 3e8/(1000e6);
k_1000 = 2*pi/lam_1000;


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

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



  E1(i) = (sqrt(30*P)/r1);
  E2(i) = (sqrt(30*P)/r2);
 
 Ev_30_C(i) = abs( E1(i) + E2(i)*exp(j*k_30*del_r));
 Eh_30_C(i) = abs( E1(i) - E2(i)*exp(j*k_30*del_r));
 
 
  E1_V_500(i) = (sqrt(30*P*VER_500(Up))/r1);
  E1_H_500(i) = (sqrt(30*P*HOR_500(Up))/r1);
  
  E2_V_500(i) = (sqrt(30*P*VER_500(Up))/r2);
  E2_H_500(i) = (sqrt(30*P*HOR_500(Up))/r2);
    
 Ev_500_C(i) = abs( E1_V_500(i) + E2_V_500(i)*exp(j*k_500*del_r));
 Eh_500_C(i) = abs( E1_H_500(i) - E2_H_500(i)*exp(j*k_500*del_r));
 
 
  E1_V_1000(i) = (sqrt(30*P*VER_1000(Up))/r1);
  E1_H_1000(i) = (sqrt(30*P*HOR_1000(Up))/r1);
  
  E2_V_1000(i) = (sqrt(30*P*VER_1000(Up))/r2);
  E2_H_1000(i) = (sqrt(30*P*HOR_1000(Up))/r2);

 Ev_1000_C(i) = abs( E1_V_1000(i) + E2_V_1000(i)*exp(j*k_1000*del_r));
 Eh_1000_C(i) = abs( E1_H_1000(i) - E2_H_1000(i)*exp(j*k_1000*del_r));
 i = i+1;
end

 
 
figure(2)

subplot(2,1,1)
plot(Xplot, 3*Eh_30_C, 'g', 'LineWidth',2)
hold on;
plot(Xplot, 3*Eh_500_C, 'r', 'LineWidth',2)
hold on;
plot(Xplot, 3*Eh_1000_C, 'b', 'LineWidth',2)
hold on;
set(gcf, 'color', [1 1 1])
set(gca,'FontSize',20)
legend('30 MHz', '500 MHz', '1000 MHz')
title('Horizontální polarizace (3 m) - S');
xlabel('Výška antény');
ylabel('Intenzita [uV]');

hold on;
subplot(2,1,2)
plot(Xplot, 3*Ev_30_C, 'g','LineWidth',2)
hold on;
plot(Xplot, 3*Ev_500_C, 'r','LineWidth',2)
hold on;
plot(Xplot, 3*Ev_1000_C, 'b','LineWidth',2)
hold on;
set(gcf, 'color', [1 1 1])
set(gca,'FontSize',20)
title('Vertikální polarizace (3 m) - S');
legend('30 MHz', '500 MHz', '1000 MHz')
xlabel('Výška antény');
ylabel('Intenzita [uV]');


%% Measured


filename = 'SIN_to_matlab.xlsx';
sheet = 1;

%% BiLog

%ver
VER_30    = xlsread(filename, sheet, 'B2:BJ2');
VER_500   = xlsread(filename, sheet, 'B3:BJ3');
VER_1000  = xlsread(filename, sheet, 'B4:BJ4');

%hor
HOR_30    = xlsread(filename, sheet, 'B8:BJ8');
HOR_500   = xlsread(filename, sheet, 'B9:BJ9');
HOR_1000  = xlsread(filename, sheet, 'B10:BJ10');



figure(3)

subplot(2,1,1)
plot(Xplot, 3*HOR_30, '--g', 'LineWidth',2)
hold on;
plot(Xplot, 3*HOR_500, '--r', 'LineWidth',2)
hold on;
plot(Xplot, 3*HOR_1000, '--b', 'LineWidth',2)
hold on;
set(gcf, 'color', [1 1 1])
set(gca,'FontSize',20)
legend('30 MHz', '500 MHz', '1000 MHz')
title('Horizontální polarizace (3 m) - namereno');
xlabel('Výška antény');
ylabel('Intenzita [uV]');

hold on;
subplot(2,1,2)
plot(Xplot, 3*VER_30, '--g','LineWidth',2)
hold on;
plot(Xplot, 3*VER_500, '--r','LineWidth',2)
hold on;
plot(Xplot, 3*VER_1000, '--b','LineWidth',2)
hold on;
set(gcf, 'color', [1 1 1])
set(gca,'FontSize',20)
title('Vertikální polarizace (3 m) - namereno');
legend('30 MHz', '500 MHz', '1000 MHz')
xlabel('Výška antény');
ylabel('Intenzita [uV]');

figure(4)

subplot(2,1,1)
plot(Xplot, Eh_30_C, 'g','LineWidth',2)
hold on;
plot(Xplot, Eh_500_C, 'r','LineWidth',2)
hold on;
plot(Xplot, Eh_1000_C, 'b','LineWidth',2)
hold on;
plot(Xplot, HOR_30, '--g', 'LineWidth',2)
hold on;
plot(Xplot, HOR_500, '--r', 'LineWidth',2)
hold on;
plot(Xplot, HOR_1000, '--b', 'LineWidth',2)
hold on;
set(gcf, 'color', [1 1 1])
set(gca,'FontSize',20)
legend('30 MHz', '500 MHz', '1000 MHz')
title('Horizontální polarizace (3 m)');
xlabel('Výška antény');
ylabel('Intenzita [V/m]');

hold on;
subplot(2,1,2)
plot(Xplot, Ev_30_C, 'g', 'LineWidth',2)
hold on;
plot(Xplot, Ev_500_C, 'r', 'LineWidth',2)
hold on;
plot(Xplot, Ev_1000_C, 'b', 'LineWidth',2)
hold on;
plot(Xplot, VER_30, '--g','LineWidth',2)
hold on;
plot(Xplot, VER_500, '--r','LineWidth',2)
hold on;
plot(Xplot, VER_1000, '--b','LineWidth',2)
hold on;
set(gcf, 'color', [1 1 1])
set(gca,'FontSize',20)
title('Vertikální polarizace (3 m)');
legend('30 MHz', '500 MHz', '1000 MHz')
xlabel('Výška antény');
ylabel('Intenzita [V/m]');

%% polynoms

pH30 = polyfit(Xplot,HOR_30,18);
pH500 = polyfit(Xplot,HOR_500,18);
pH1000 = polyfit(Xplot,HOR_1000,18);

pV30 = polyfit(Xplot,VER_30,18);
pV500 = polyfit(Xplot,VER_500,18);
pV1000 = polyfit(Xplot,VER_1000,18);

HOR_30p = polyval(pH30,Xplot);
HOR_500p = polyval(pH500,Xplot);
HOR_1000p = polyval(pH1000,Xplot);

VER_30p = polyval(pV30,Xplot);
VER_500p = polyval(pV500,Xplot);
VER_1000p = polyval(pV1000,Xplot);

figure(5)

subplot(2,1,1)

plot(Xplot, 3*HOR_30p, 'g', 'LineWidth',2)
hold on;
plot(Xplot, 3*HOR_500p, 'r', 'LineWidth',2)
hold on;
plot(Xplot, 3*HOR_1000p, 'b', 'LineWidth',2)
hold on;
set(gcf, 'color', [1 1 1])
set(gca,'FontSize',20)
legend('30 MHz', '500 MHz', '1000 MHz')
title('Horizontální polarizace (3 m) - M');
xlabel('Výška antény');
ylabel('Intenzita [uV]');

hold on;
subplot(2,1,2)
plot(Xplot, 3*VER_30p, 'g','LineWidth',2)
hold on;
plot(Xplot, 3*VER_500p, 'r','LineWidth',2)
hold on;
plot(Xplot, 3*VER_1000p, 'b','LineWidth',2)
hold on;
set(gcf, 'color', [1 1 1])
set(gca,'FontSize',20)
title('Vertikální polarizace (3 m) - M');
legend('30 MHz', '500 MHz', '1000 MHz')
xlabel('Výška antény');
ylabel('Intenzita [uV]');
