function varargout = uvodni(varargin)
% UVODNI M-file for uvodni.fig
%      UVODNI, by itself, creates a new UVODNI or raises the existing
%      singleton*.
%
%      H = UVODNI returns the handle to a new UVODNI or the handle to
%      the existing singleton*.
%
%      UVODNI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UVODNI.M with the given input arguments.
%
%      UVODNI('Property','Value',...) creates a new UVODNI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before uvodni_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to uvodni_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 uvodni

% Last Modified by GUIDE v2.5 03-May-2008 22:08:26

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @uvodni_OpeningFcn, ...
                   'gui_OutputFcn',  @uvodni_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 uvodni is made visible.
function uvodni_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 uvodni (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes uvodni wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = uvodni_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 pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

zadavani




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

popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
    case 1
        
    case 2
         handles.f=load('down_f.txt');
        handles.rus=load('down_PSD.txt');
        plot(handles.f,handles.rus)
        
    case 3
        handles.f=load('up_f.txt');
        handles.rus=load('up_PSD.txt');
        plot(handles.f,handles.rus)
        
    case 4
        k=1;
        for(i=10000 : 1000 : 2e6)
            handles.f(k)=i;
            handles.rus(k)=-140;
            k=k+1;
        end

        plot(handles.f,handles.rus);
        hold on;
        title('Bily sum')
        xlabel('f [Hz]')
        ylabel('PSD [dBm/Hz]')
        hold off;
        
    case 5
        handles.impulzni1=load('impulzni.txt');
        plot(handles.impulzni1)
        hold on;
        title('Impulzni ruseni')
        xlabel('t [ms]')
        ylabel('U [mV]')
        hold off;
    case 6
        handles.impulzni2=load('impulzni2.txt');
        plot(handles.impulzni2)
        hold on;
        title('Impulzni ruseni')
        xlabel('t [ms]')
        ylabel('U [mV]')
        hold off;
        
    case 7
        handles.f=load('f_vysoko.txt');
        handles.rus=load('rus_vysoko.txt');
        plot(handles.f,handles.rus);
        hold on;
        title('Vysokofrekvencni ruseni')
        xlabel('f [kHz]')
        ylabel('PSD [dBm]')
        hold off;
        handles.f=handles.f.*1000;
end

guidata(hObject, handles);

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

% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (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 listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1


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

% Hint: listbox 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


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



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

popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
    
    case 2
        down=load('down.txt');
        handles.vysled=down.*0.048486;
         vys(hObject,handles);
        figure(1)
        plot(handles.f,handles.rus)
        title('Rušící PSD maska');

        xlabel('f [Hz]')
        ylabel('PSD [dBm/Hz]')
        
        f=handles.f;
        a=handles.vysled;
        t= 0:  (1/(f(1)))/(length(a)-1) :1/f(1);
        t=t.*1000;
        grid on;
        vysledek=handles.vysled;
        save vysledek.txt vysledek -ascii -tabs
        
        
        figure(2)
        plot (t,a)
        title('Rušící PSD maska převedená do časové oblasti');
        xlabel('t [ms]')
        ylabel('uroven [V]')
        grid on;
        
        SS=1000*(max(handles.vysled)-min(handles.vysled));
        set(handles.SS, 'String', SS);
        frekcas=1/handles.f(1)+1/(2*handles.f(length(handles.f)));
        set(handles.genf, 'String', 1/(frekcas*1000));

    
    case 3
        up=load('up.txt');
        handles.vysled=up.*0.11291;
        vys(hObject,handles);
        
        figure(1)
        plot(handles.f,handles.rus)
        title('Rušící PSD maska');

        xlabel('f [Hz]')
        ylabel('PSD [dBm/Hz]')
        f=handles.f;
        a=handles.vysled;
        t= 0:  (1/(f(1)))/(length(a)-1) :1/f(1);
        t=t.*1000;
        grid on;
        
        figure(2)
        plot (t,a)
        title('Rušící PSD maska převedená do časové oblasti');
        xlabel('t [ms]')
        ylabel('uroven [V]')
        grid on;
        
        vysledek=handles.vysled;
        save vysledek.txt vysledek -ascii -tabs

        
        SS=1000*(max(handles.vysled)-min(handles.vysled));
        set(handles.SS, 'String', SS);
        frekcas=1/handles.f(1)+1/(2*handles.f(length(handles.f)));
        set(handles.genf, 'String', 1/(frekcas*1000));

    case 4
        
        pocitani(hObject, handles);
                      
    case 5
       
        vysledek=handles.impulzni1/1000;
        save vysledek.txt vysledek -ascii -tabs
        SS=1000*(max(vysledek)-min(vysledek));
        set(handles.SS, 'String', SS);
        gen=1/(1000*25e-6);
        set(handles.genf, 'String', gen);
        
        figure(1)
        
        plot(handles.impulzni1)
        hold on;
        title('Impulzni ruseni')
        xlabel('t [ms]')
        ylabel('U [mV]')
        grid on;
        hold off;
        
   
    case 6
       
        vysledek=handles.impulzni2/1000;
        save vysledek.txt vysledek -ascii -tabs
        SS=1000*(max(vysledek)-min(vysledek));
        set(handles.SS, 'String', SS);
        gen=1/(1000*75e-6);
        set(handles.genf, 'String', gen);
        
        figure(1)
        plot(handles.impulzni2)
        hold on;
        title('Impulzni ruseni')
        xlabel('t [ms]')
        ylabel('U [mV]')
        grid on;
        hold off;
        
    case 7
        pocitani(hObject, handles);
        vys(hObject,  handles);
end

guidata(hObject, handles);

function pocitani(hObject, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%    prolozeni
f=handles.f;

P=handles.rus;
j=1;
for (i=1: 1: length(f)-1)      %%%%%%%%%%%%%%vypocet utlumu na cele pasmo

    a=(P(i+1)-P(i))/(f(i+1)-f(i));
    
    b= P(i)-a*f(i);
    
  %fvyp=(f(i+1)-f(i))/100  ;
  for (nf=f(i): 1000: f(i+1))
      
      K(j)=a*nf+b;
    j=j+1;
   %pokus(j)=ft
   
end
end

rus=K;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%   prevod dBm/Hz na V
rus=rus-30;


BdB=10*log10(f(length(f))-f(1));

rus=rus+BdB;
popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
    
    case 7
    
        rus=K;
        rus=rus-30;

        
end

rus=10.^(rus./10);
rus=rus.*135;
rus=rus.^0.5;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%     ifft(gen. faze, oboustrane spekrum)


gen = pi*randn(1,length(rus));

for (i=1: 1: length(rus))
  obou(i+length(rus))= rus((length(rus))-i+1) ;
  obou(i)=rus(i);
end

for (i=1: 1: length(gen))
  obougen(i+length(gen))= gen((length(gen))-i+1); 
  obougen(i)=gen(i);
end;

com=complex(obou.*cos(obougen), obou.*sin(obougen));

a = ifft(com, 'symmetric');
a=a*30;

handles.vysled=a;

guidata(hObject, handles);

figure(1)
plot(f,P)
%title('Rušící PSD maska');

xlabel('f [Hz]')
ylabel('PSD [dBm/Hz]')


t= 0:  (1/(f(1)))/(length(a)-1) :1/f(1);
t=t.*1000;
popup_sel_index = get(handles.popupmenu1, 'Value');
switch popup_sel_index
    case 7
    ylabel('PSD [dBm]')    
end
grid on;

figure(2)
plot (t,a)
title('Rušící PSD maska převedená do časové oblasti');
xlabel('t [ms]')
ylabel('uroven [V]')
grid on;

SS=1000*(max(handles.vysled)-min(handles.vysled));
set(handles.SS, 'String', SS);
frekcas=1/handles.f(1)+1/(2*handles.f(length(handles.f)));
set(handles.genf, 'String', 1/(frekcas*1000));

vysledek=handles.vysled';
save vysledek.txt vysledek -ascii -tabs


function vys(hObject, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

