close all;
clear all;

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

D = ones(1,360);

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


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

 Ev3(i) = abs( Tx_ver(1)*E1(i) + Tx_ver(1)*E2(i)*exp(j*k*del_r));
 Eh3(i) = abs( Tx_hor(1)*E1(i) - Tx_hor(1)*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);
      
      
      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



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



%% Naklon

filename = 'matlab_data.xlsx';
sheet = 9;

FREQ = xlsread(filename, sheet, 'A2:A65');


NAKLON_HOR = xlsread(filename, sheet, 'B2:B65');
NAKLON_VER = xlsread(filename, sheet, 'C2:C65');


 O = 1;
for Q = 1:1:length(FREQ)
      for i = 1:1:7
        FREQs(O) = FREQ(Q);
        
        NAKLON_HORs(O) = NAKLON_HOR(Q);
        NAKLON_VERs(O) = NAKLON_VER(Q);
                
            
       O = O+1;
    end
end
   W = window(@hann, 10);
   FREQn = filtfilt(W, 4.5, FREQs);
   
   NAKLON_HORn = filtfilt(W, 4.5, NAKLON_HORs);
   NAKLON_VERn = filtfilt(W, 4.5, NAKLON_VERs);

 
%%
figure(1)
set(gcf, 'color', [1 1 1])

subplot(2,1,1)
plot(FREQn, NAKLON_HORn, 'g', 'LineWidth',2)
hold on;
plot(FREQn, NAKLON_VERn, 'r', 'LineWidth',2)
hold on;

% plot(FREQ, NAKLON_HOR, '--g', 'LineWidth',2)
% hold on;
% plot(FREQ, NAKLON_VER, '--r', 'LineWidth',2)
% hold on;

set(gca,'FontSize',25)
title('Vertikální polarizace');
legend( 'HOR', 'VER', 'Location','northwest','Orientation','vertical')
xlabel('Frekvence [MHz]');;
ylabel('Rozdíl [dB]');
 xlim([30 1000]);
%ylim([30 130]);
set(gca,'FontSize',20)

subplot(2,1,2)
semilogx(FREQn, NAKLON_HORn, 'g', 'LineWidth',2)
hold on;
semilogx(FREQn, NAKLON_VERn, 'r', 'LineWidth',2)
hold on;
% semilogx(FREQ, NAKLON_HOR, '--g', 'LineWidth',2)
% hold on;
% semilogx(FREQ, NAKLON_VER, '--r', 'LineWidth',2)
% hold on;
set(gca,'FontSize',25)
title('Horizontální polarizace');
legend( 'HOR', 'VER', 'Location','northwest','Orientation','vertical')
xlabel('Frekvence [MHz]');
ylabel('Rozdíl [dB]');
 xlim([30 1000]);
%ylim([5 30]);
set(gca,'FontSize',20)

%%

Xplot = 30:10:1000

%%
figure(9)
set(gcf, 'color', [1 1 1])
subplot(2,1,1)
semilogx(FREQn, NAKLON_VERn, 'r', 'LineWidth',2)
hold on;
semilogx(Xplot, Evmax_dB3 - Evmaxt_dB3, '--g','LineWidth',1.5)
hold on;
set(gca,'FontSize',25)
legend('Mereno', 'Simulace ', 'Location','northwest','Orientation','vertical')

xlim([30 1000])
ylim([-1 4]);
title('Vertikální polarizace');
xlabel('Frekvence [MHz]');
ylabel('Rozdíl [dB]');
set(gca,'FontSize',20)

subplot(2,1,2)
semilogx(FREQn, NAKLON_HORn, 'r', 'LineWidth',2)
hold on;
semilogx(Xplot, Ehmax_dB3 - Ehmaxt_dB3, '--g','LineWidth',1.5)
hold on;
set(gca,'FontSize',25)
legend('Mereno', 'Simulace ', 'Location','northwest','Orientation','vertical')
xlim([30 1000])
ylim([-1 4]);
title('Horizontální polarizace');
xlabel('Frekvence [MHz]');
ylabel('Rozdíl [dB]');
set(gca,'FontSize',20)




 
%% SAR vs FAR

filename = 'matlab_data.xlsx';
sheet = 10;

FREQ = xlsread(filename, sheet, 'A3:B66');


SAR_HOR = xlsread(filename, sheet, 'E3:E66');
SAR_VER = xlsread(filename, sheet, 'B3:B66');

FAR_HOR = xlsread(filename, sheet, 'F3:F66');
FAR_VER = xlsread(filename, sheet, 'C3:C66');

FAR2_HOR = xlsread(filename, sheet, 'G3:G66');
FAR2_VER = xlsread(filename, sheet, 'D3:D66');


 O = 1;
for Q = 1:1:length(FREQ)
      for i = 1:1:7
        FREQs(O) = FREQ(Q);
        
        SAR_HORs(O) = SAR_HOR(Q);
        SAR_VERs(O) = SAR_VER(Q);

        FAR_HORs(O) = FAR_HOR(Q);
        FAR_VERs(O) = FAR_VER(Q);
        
        FAR2_HORs(O) = FAR2_HOR(Q);
        FAR2_VERs(O) = FAR2_VER(Q);
            
       O = O+1;
    end
end
   W = window(@hann, 10);
   FREQn = filtfilt(W, 4.5, FREQs);
   
   SAR_HORn = filtfilt(W, 4.5, SAR_HORs);
   SAR_VERn = filtfilt(W, 4.5, SAR_VERs);
   
  FAR_HORn = filtfilt(W, 4.5, FAR_HORs);
   FAR_VERn = filtfilt(W, 4.5, FAR_VERs);
   
   FAR2_HORn = filtfilt(W, 4.5, FAR2_HORs);
   FAR2_VERn = filtfilt(W, 4.5, FAR2_VERs);  
   
   
   figure(10)
set(gcf, 'color', [1 1 1])
subplot(2,1,1)
plot(FREQn, SAR_VERn, 'g', 'LineWidth',2)
hold on;
plot(FREQn, FAR_VERn, 'r', 'LineWidth',2)
hold on;
plot(FREQn, FAR2_VERn, 'b', 'LineWidth',2)
hold on;

set(gca,'FontSize',25)
legend('SAR', 'FAR (sken) ','FAR (2 m) ', 'Location','northwest','Orientation','vertical')

% xlim([30 1000])
% ylim([-1 4]);
title('Vertikální polarizace');
xlabel('Frekvence [MHz]');
ylabel('Rozdíl [dB]');
set(gca,'FontSize',20)

subplot(2,1,2)
plot(FREQn, SAR_VERn - FAR_VERn, 'r', 'LineWidth',2)
hold on;
plot(FREQn, SAR_VERn - FAR2_VERn, 'b', 'LineWidth',2)
hold on;
set(gca,'FontSize',25)
legend('FAR (sken) ','FAR (2 m) ','Location','northwest','Orientation','vertical')
% xlim([30 1000])
% ylim([-1 4]);
title('Horizontální polarizace');
xlabel('Frekvence [MHz]');
ylabel('Rozdíl [dB]');
set(gca,'FontSize',20)

   figure(11)
set(gcf, 'color', [1 1 1])
subplot(2,1,1)
plot(FREQn, FAR2_HORn, 'b', 'LineWidth',2)
hold on;
plot(FREQn, FAR_HORn, 'r', 'LineWidth',2)
hold on;

plot(FREQn, SAR_HORn, 'g', 'LineWidth',2)
hold on;

set(gca,'FontSize',25)
legend('FAR (2 m) ', 'FAR (sken) ','SAR (sken)', 'Location','southeast','Orientation','horizontal')

xlim([30 1000])
% ylim([-1 4]);
title('Horizontální polarizace');
xlabel('Frekvence [MHz]');
ylabel('Intenzita [dBuV/m]');
set(gca,'FontSize',20)

subplot(2,1,2)
plot(FREQn, SAR_HORn - FAR2_HORn, 'b', 'LineWidth',2)
hold on;
plot(FREQn, SAR_HORn - FAR_HORn, 'r', 'LineWidth',2)
hold on;
set(gca,'FontSize',25)
legend('FAR (2 m) ', 'FAR (sken) ','Location','southeast','Orientation','horizontal')
xlim([30 1000])
ylim([-5 11]);
xlabel('Frekvence [MHz]');
ylabel('Odchylka [dB]');
set(gca,'FontSize',20)
     
   