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

% Last Modified by GUIDE v2.5 13-May-2008 19:32:33

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

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


handles.zadanoX=10;
handles.zadanoY=0;
handles.klik=0;
%guidata(hObject,handles);


% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = zadavani_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)

%handles.klik= get(handles.pocet, 'Value');

handles.klik=handles.klik+1;

set(handles.pocet, 'String', handles.klik);
%set(handles.pocet, 'Value', handles.klik);

handles.f(handles.klik)=handles.zadanoX;

handles.rus(handles.klik)=handles.zadanoY;
handles.poradi(handles.klik)=handles.klik;

    set(handles.activex1,'Col',0);
    set(handles.activex1,'Row',handles.klik-1);
    set(handles.activex1,'Text',num2str(handles.f(handles.klik)));
    
    set(handles.activex1,'Col',1);
    set(handles.activex1,'Row',handles.klik-1);
    set(handles.activex1,'Text',num2str(handles.rus(handles.klik)));
    

%set(handles.text1, 'String', handles.f);
%set(handles.text4, 'String', handles.rus);
%set(handles.index, 'String', handles.poradi);

%set(handles.posuvnik, 'String', matice(:,2));

plot(handles.f, handles.rus );
hold on;
title('PSD maska')
xlabel('f [kHz]')
ylabel('PSD [dBm/Hz]')
hold off;


guidata(hObject, handles);



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

handles.klik=0;
set(handles.pocet, 'String', handles.klik);
set(handles.pocet, 'Value', handles.klik);




set(handles.index, 'String', ' ');



for j=1 : 1 : length(handles.f)
    set(handles.activex1,'Col',0);
    set(handles.activex1,'Row',j-1);
    set(handles.activex1,'Text',' ');
    
    
    set(handles.activex1,'Col',1);
    set(handles.activex1,'Row',j-1);
    set(handles.activex1,'Text',' ');
    
    
end
handles.f=10;;

handles.rus=0;
handles.poradi=0;


guidata(hObject, handles);


function zadanoX_Callback(hObject, eventdata, handles)
% hObject    handle to zadanoX (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 zadanoX as text
%        str2double(get(hObject,'String')) returns contents of zadanoX as a double
zadanoX = str2double(get(hObject, 'String'));
if isnan(zadanoX)
    set(hObject, 'String', 10);
    errordlg('Musí být zadána hodnota od 10 do 2000 ','Error');
end

% Save the new volume value
handles.zadanoX = zadanoX;
guidata(hObject,handles);



% --- Executes during object creation, after setting all properties.
function zadanoX_CreateFcn(hObject, eventdata, handles)
% hObject    handle to zadanoX (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 zadanoY_Callback(hObject, eventdata, handles)
% hObject    handle to zadanoY (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 zadanoY as text
%        str2double(get(hObject,'String')) returns contents of zadanoY as a double
zadanoY = str2double(get(hObject, 'String'));
if isnan(zadanoY)
    set(hObject, 'String', 0);
    errordlg('Musí být zadána hodnota  ','Error');
end

% Save the new volume value
handles.zadanoY = zadanoY;
guidata(hObject,handles);






% --- Executes during object creation, after setting all properties.
function zadanoY_CreateFcn(hObject, eventdata, handles)
% hObject    handle to zadanoY (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





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

handles.f=handles.f.*1000;
pocitani(hObject,handles);
guidata(hObject,handles);



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

close zadavani;




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

hlaseni;
handles.f=load('vlastni_f.txt');
handles.rus=load('vlastni_PSD.txt');

handles.f=handles.f';
handles.rus=handles.rus';

    pocitani(hObject,handles);

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;

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;

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

handles.vysled=a;
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




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

for k=1 : 1 : str2double(get(handles.pocet, 'String'))
    
    set(handles.activex1,'Col',0);
    set(handles.activex1,'Row',k-1);
    
    aaa= get(handles.activex1,'Text');
    
    handles.f(k)=str2double(aaa);
    
    set(handles.activex1,'Col',1);
    set(handles.activex1,'Row',k-1);
    
    bbb=get(handles.activex1,'Text');
    handles.rus(k)=str2double(bbb);
end


plot(handles.f, handles.rus );
hold on;
title('PSD maska')
xlabel('f [kHz]')
ylabel('PSD [dBm/Hz]')
hold off;

guidata(hObject, handles);