function varargout = Rez_spoje(varargin)
% REZ_SPOJE M-file for Rez_spoje.fig
%      REZ_SPOJE, by itself, creates a new REZ_SPOJE or raises the existing
%      singleton*.
%
%      H = REZ_SPOJE returns the handle to a new REZ_SPOJE or the handle to
%      the existing singleton*.
%
%      REZ_SPOJE('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in REZ_SPOJE.M with the given input arguments.
%
%      REZ_SPOJE('Property','Value',...) creates a new REZ_SPOJE or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Rez_spoje_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Rez_spoje_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Rez_spoje

% Last Modified by GUIDE v2.5 22-May-2013 06:09:11

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Rez_spoje_OpeningFcn, ...
                   'gui_OutputFcn',  @Rez_spoje_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before Rez_spoje is made visible.
function Rez_spoje_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Rez_spoje (see VARARGIN)

% Choose default command line output for Rez_spoje
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% This sets up the initial plot - only do when we are invisible
% so window can get raised using Rez_spoje.
if strcmp(get(hObject,'Visible'),'off')
    
end

% UIWAIT makes Rez_spoje wait for user response (see UIRESUME)
% uiwait(handles.figure1);
nacti(hObject, eventdata, handles);
UpdateButton_Callback(hObject, eventdata, handles);


% --- Outputs from this function are returned to the command line.
function varargout = Rez_spoje_OutputFcn(hObject, eventdata, handles)
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;

% --- Executes on button press in UpdateButton.
function UpdateButton_Callback(hObject, eventdata, handles)
% hObject    handle to UpdateButton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
GrafVypocet(hObject, eventdata, handles);

axes(handles.Graf1);
cla;
Graf1Vykresli(hObject, eventdata, handles);

axes(handles.Graf2);
cla;
Graf2Vykresli(hObject, eventdata, handles);



% --------------------------------------------------------------------
function FileMenu_Callback(hObject, eventdata, handles)
% hObject    handle to FileMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function OpenMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to OpenMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
file = uigetfile('*.fig');
if ~isequal(file, 0)
    open(file);
end

% --------------------------------------------------------------------
function PrintMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to PrintMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
printdlg(handles.figure1)

% --------------------------------------------------------------------
function CloseMenuItem_Callback(hObject, eventdata, handles)
% hObject    handle to CloseMenuItem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
selection = questdlg(['Close ' get(handles.figure1,'Name') '?'],...
                     ['Close ' get(handles.figure1,'Name') '...'],...
                     'Yes','No','Yes');
if strcmp(selection,'No')
    return;
end

delete(handles.figure1)


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1


% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
     set(hObject,'BackgroundColor','white');
end

set(hObject, 'String', {'plot(rand(5))', 'plot(sin(1:0.01:25))', 'bar(1:.5:10)', 'plot(membrane)', 'surf(peaks)'});



function eL12_Callback(hObject, eventdata, handles)
% hObject    handle to eL12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of eL12 as text
%        str2double(get(hObject,'String')) returns contents of eL12 as a double
nacti(hObject, eventdata, handles);

% --- Executes during object creation, after setting all properties.
function eL12_CreateFcn(hObject, eventdata, handles)
% hObject    handle to eL12 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function eM1_Callback(hObject, eventdata, handles)
% hObject    handle to eM1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of eM1 as text
%        str2double(get(hObject,'String')) returns contents of eM1 as a double
nacti(hObject, eventdata, handles);

% --- Executes during object creation, after setting all properties.
function eM1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to eM1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function ePun_Callback(hObject, eventdata, handles)
% hObject    handle to ePun (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ePun as text
%        str2double(get(hObject,'String')) returns contents of ePun as a double
nacti(hObject, eventdata, handles);

% --- Executes during object creation, after setting all properties.
function ePun_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ePun (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function nacti(hObject, eventdata, handles)    
    %nacteni z okna pro ovladani
    L12 = str2double(get(handles.eL12,'String'));
    M1 = str2double(get(handles.eM1,'String'));
    Pun = str2double(get(handles.ePun,'String'));
    
    %ulozeni ve workspace
    assignin('base','L12',L12);
    assignin('base','M1',M1);
    assignin('base','Pun',Pun);
    
    
function GrafVypocet(hObject, eventdata, handles)
    %nacteni z workspace
    L12 = evalin('base','L12');
    M1 = evalin('base','M1');
    PLD = evalin('base','PLD');
    alfaLD= evalin('base','alfaLD');
    alfaTXA= evalin('base','alfaTXA');
    alfaWT= evalin('base','alfaWT');
    alfaAL= evalin('base','alfaAL');
    Dekv= evalin('base','Dekv');
    fiT= evalin('base','fiT');
    DRXA= evalin('base','DRXA');
    alfaWR= evalin('base','alfaWR');
    alfaRXA= evalin('base','alfaRXA');
    alfaIF= evalin('base','alfaIF');
    alfaPD= evalin('base','alfaPD');
    alfa1cast= evalin('base','alfa1cast');
    SNR0= evalin('base','SNR0');
    NEP= evalin('base','NEP');
    delta= evalin('base','delta');
    lok_str= evalin('base','lok_str');
    lok_val= evalin('base','lok_val');
    Cn2= evalin('base','Cn2');
    lambda= evalin('base','lambda');
    
    %vypocet koherence svazku
    Koh_sv = (0.55*Cn2*(((2*pi)/lambda)^(2))*L12)^(-3/5)*10^3;
    assignin('base','Koh_sv',Koh_sv);
    
    %vypocet turbulence
    Ryt_var = (0.5*Cn2*(((2*pi)/lambda)^(7/6))*(L12^(11/6)))^(1/2);
    d2 = (2*pi*(DRXA/1000)^2)/(lambda*4*L12);
    sigma12D = exp(((0.51*Ryt_var^2*(1+(0.69*Ryt_var^(12/5)))^(-5/6))/((1+0.62*Ryt_var^(12/5))*d2+(0.9*d2)))+((0.49*Ryt_var^2)/((0.56*Ryt_var^(12/5)+1+(0.18*d2))^(7/6))))-1
    alfaTurb = 10*Log10(1-(sigma12D)^(1/2));
    assignin('base','Ryt_var',Ryt_var);
    assignin('base','d2',d2);
    assignin('base','sigma12D',sigma12D);
    assignin('base','alfaTurb',alfaTurb);
    
    %vypocet bilance
    PmLD = 10*log10(PLD);
    L0 = Dekv/fiT;
    alfa12 = 20*log10(L0/(L0+L12));
    gamaTOT = 3.67+10*log10((DRXA/Dekv)^2);
    alfaCast = alfa1cast*L12*10^-3;
    alfaATM = alfaCast+alfaTurb;
    alfaTS = alfaLD+alfaTXA+alfaWT;
    alfaRS = alfaWR+alfaRXA+alfaIF+alfaPD;
    alfaTOT = alfaTS+alfaRS+alfaAL;
    assignin('base','alfaATM',alfaATM);
    
    P1 = PmLD;
    P2 = P1+alfaLD;
    P3 = P2+alfaTXA+alfaWT;
    P4 = P3+alfaAL;
    P5 = P4+alfa12;
    P6 = P5+gamaTOT;
    P7 = P6+alfaATM;
    P8 = P7+alfaRXA+alfaWR;
    P9 = P8+alfaIF+alfaPD;
    P10 = NEP+SNR0;
    
    P = [P1,P2,P3,P4,P5,P6,P7,P8,P9,P10];
    assignin('base','P',P);

    PTXA = PmLD+alfaLD+alfaTXA+alfaWT+alfaAL;
    PRXA = P7;
    P0RXA = NEP+SNR0-alfaRS;
    
    MS = PTXA-P0RXA+20*log10(DRXA/fiT);
    assignin('base','MS',MS);
    
    Psat = P10+delta;
    Pmin = P10;
	
    PmPD = P9;
    PPD = 10^(0.1*PmPD);

    %vypocet M1 pro zadanou L12
    M = P9-P10;
    tM1 = M/(L12*10^-3);
    set(handles.tM1,'String',tM1);  %zapis do objektu tM1 ve vizu
    
    %vypocty u vykonu bilance spoje
    set(handles.tP1,'String',P1);
    set(handles.tP2,'String',P2);
    set(handles.tP3,'String',P3);
    set(handles.tP4,'String',P4);
    set(handles.tP5,'String',P5);
    set(handles.tP6,'String',P6);
    set(handles.tP7,'String',P7);
    set(handles.tP8,'String',P8);
    set(handles.tP9,'String',P9);
    set(handles.tPmin,'String',Pmin);
    set(handles.tPsat,'String',Psat);
    set(handles.tNEP,'String',NEP);
    set(handles.tM,'String',M);
    set(handles.tMS,'String',MS);
    
    %vypocet delky L12 podle M1
    tL12 = M/(M1*10^-3);
    set(handles.tL12,'String',tL12);
    
    %vypocitani zavislosti
    G2_L12min = 0;
    G2_L12max = 1;
    G2_krok = 0.01;
    G2_L12 = (G2_L12min:G2_krok:G2_L12max);
    G2_prvku =length(G2_L12);
    G2_M = M*ones(1,G2_prvku);
    
    assignin('base','G2_M',G2_M);
    assignin('base','G2_L12',G2_L12);
    assignin('base','G2_M',G2_M);
    G2_M1 = G2_M./G2_L12;
    assignin('base','G2_M1',G2_M1);
    
    %zadani pravdepodobnosti Pun
    switch lok_str{lok_val};
    case 'Lokalita Brno'    
        G2_PunX = [10,11,12,13,14,15,16,18,19,21,23,24,26,29,34,38,42,47,58,68,85,113,127,170];
        G2_PunY = [6.3,5.3,4.7,4.2,3.6,3.3,3.1,2.6,2.5,2.3,2.1,1.8,1.6,1.45,1.3,1.25,1.1,0.94,0.66,0.49,0.3,0.15,0.1,0.03];
    case 'Lokalita Náměšt'    
        G2_PunX = [10,11,12,13,14,15,16,18,19,21,23,24,26,29,34,38,42,47,58,68,85,113,127,170];
        G2_PunY = [13.5,12,11.5,11,10.5,10,9.8,9.5,9,8,7.3,7,6.5,6,5.6,5.4,5.2,5,4.5,4,3.3,2.2,1.8,1];
    case 'Lokalita Přerov'    
        G2_PunX = [10,11,12,13,14,15,16,18,19,21,23,24,26,29,34,38,42,47,58,68,85,113,127,170];
        G2_PunY = [10,8.2,7.4,6.6,5.8,5.2,4.7,4,3.7,3.3,3,2.85,2.6,2.4,2.15,2.05,1.95,1.85,1.7,1.55,1.35,1.1,1,0.8];
    case 'Lokalita Milešovka'    
        G2_PunX = [10,11,12,13,14,15,16,18,19,21,23,24,26,29,34,38,42,47,58,68,85,113,127,170];
        G2_PunY = [26,25,23.9,22.9,22.6,22.2,21.8,21.5,21.3,20.8,20.7,20.5,20.3,20,19.6,19.4,19,18.5,17.6,16.8,15.8,14.2,13.3,11];
    case 'Lokalita Kopisty'    
        G2_PunX = [10,11,12,13,14,15,16,18,19,21,23,24,26,29,34,38,42,47,58,68,85,113,127,170];
        G2_PunY = [10,7.5,6.5,5.5,4.8,4.2,3.8,3.2,3,2.6,2.3,2.15,1.95,1.7,1.5,1.43,1.38,1.3,1.2,1.12,1,0.85,0.78,0.6];
    end
            
    assignin('base','G2_PunX',G2_PunX);
    assignin('base','G2_PunY',G2_PunY);
    
    

function Graf1Vykresli(hObject, eventdata, handles) 
    %vykresleni grafu1
    G1_P = evalin('base', 'P');
    NEP = str2double(get(handles.tNEP,'String'));
    NEP_graf = NEP*ones(1,10);
    Psat = str2double(get(handles.tPsat,'String'));
    Psat_graf = Psat*ones(1,10);
    
    plot(G1_P);   
    hold on;
    plot(Psat_graf,'--g'); 
    plot(NEP_graf,'--r');
    xlabel('Výkonový bilanční diagram');
    ylabel('Výkon [dBm]');
    title('Stacionární model spoje');
    grid on;
    ylim([-85 20]);
    legend('Pi','Psat','NEP');
    hold off;

    
function Graf2Vykresli(hObject, eventdata, handles) 
    %vykresleni grafu2
    G2_M1 = evalin('base', 'G2_M1');
    G2_L12 = evalin('base', 'G2_L12');
    
    G2_PunX = evalin('base', 'G2_PunX');
    G2_PunY = evalin('base', 'G2_PunY');
    
    lok_str= evalin('base','lok_str');
    lok_val= evalin('base','lok_val');
    
    [haxes,hline1,hline2] = plotyy(G2_M1,G2_L12,G2_PunX,G2_PunY,'plot','semilogy')
    
    grid on;
    axes(haxes(1)); %nastaveni lin. grafu
    ylabel('Délka spoje L12 [km]');
    axis([0 170 0 1]);
    set(gca,'YTick',[0, 0.2, 0.4, 0.6, 0.8, 1]);
    
    axes(haxes(2)); %nastaveni log. grafu
    ylabel('Nedostupnost spoje Pun [%]');
    axis ij;
    
    
     switch lok_str{lok_val};
     case 'Lokalita Brno'    
         xlim([0 170]);
         ylim([0.01 10]);
         set(gca,'YTick',[0.01, 0.1, 1, 10]);
     case 'Lokalita Náměšt'    
         xlim([0 170]);
         ylim([0.5 20]);
         set(gca,'YTick',[0.5, 1, 2, 3, 5, 10, 20]);
     case 'Lokalita Přerov'    
         xlim([0 170]);
         ylim([0.5 20]);
     case 'Lokalita Milešovka'    
         xlim([0 170]);
         ylim([10 30]);
         set(gca,'YTick',[10, 12, 15, 20, 30]);
     case 'Lokalita Kopisty'    
         xlim([0 170]);
         ylim([0.5 20]);
     end 
    
    xlabel('Normovaná výkonová rezerva spoje M1 [dB/km]');
    title('Statistický model spoje');
    
    
    %lokalita
    set(handles.tLok,'String',lok_str(lok_val));
    
    %aproximace Pun ze zad. hodnot L12
    M1 = str2double(get(handles.tM1,'String'));
    tPun_L12 = interp1(G2_PunX,G2_PunY,M1,'spline');
    set(handles.tPun_L12,'String',tPun_L12);
    
    %aproximace Pun ze zad. hodnot M1
    M1 = evalin('base', 'M1');
    tPun_M1 = interp1(G2_PunX,G2_PunY,M1,'spline');
    set(handles.tPun_M1,'String',tPun_M1);
    
    Pun = evalin('base', 'Pun');
    tM1_Pun = interp1(G2_PunY,G2_PunX,Pun,'spline');
    set(handles.tM1_Pun,'String',tM1_Pun);
    M = str2double(get(handles.tM,'String'));
    tL12_Pun = M/(tM1_Pun*10^-3);
    set(handles.tL12_Pun,'String',tL12_Pun);
    
% --- Executes on button press in UpdateButton1.
function UpdateButton1_Callback(hObject, eventdata, handles)
% hObject    handle to UpdateButton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes during object creation, after setting all properties.
function UpdateButton1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to UpdateButton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
