function varargout = rozhrani(varargin)

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @rozhrani_OpeningFcn, ...
                   'gui_OutputFcn',  @rozhrani_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
global prom horiz2 vert2 cas1 check1 check2


% --- Executes just before rozhrani is made visible.
function rozhrani_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 rozhrani (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


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





function edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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 nacteni.
function nacteni_Callback(hObject, eventdata, handles)
% hObject    handle to nacteni (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Graficka nabidka Load

global prom check1 check2;
[filename, pathname, filterindex] = uigetfile( ...
{'*.m;*.fig;*.mat;*.mdl', 'All MATLAB Files (*.fig, *.mat)';
'*.mat','MAT-files (*.mat)'; ...
'*.*', 'All Files (*.*)'}, ...
'Pick a file');

% Urceni typu souboru
adresar = num2str(pathname);
jmeno = num2str(filename);
cil=[adresar, jmeno];
info = whos('-file',[adresar jmeno]);

pom  = (filename);
pom1 = length(pom);
pom2 = pom1-2;

% Nalezeni koncovky
for i = pom2:pom1
    koncovka(i-pom2+1)=pom(i);
end


% Zobrazeni prislusneho souboru
if koncovka == 'mat'
   load(cil)
   graf1 = eval(info.name);
   axes(handles.axes1)
   plot(graf1(:,1),graf1(:,2))
   xlabel('Čas [s]');
   ylabel('Amplituda [mV]');

   axes(handles.axes2)
   plot(graf1(:,1),graf1(:,3))
   xlabel('Čas [s]');
   ylabel('Amplituda [mV]');

   set(handles.filtrace,'Visible','On')
%    set(handles.orez,'Visible','On')
   set(handles.ulozit,'Visible','On')
   set(handles.uipanel1,'Visible','On')
   set(handles.uipanel2,'Visible','On')
   set(handles.uipanel4,'Visible','On')
   
   check1=0;
   set(handles.checkbox1,'Value',0);
   check2=0;
   set(handles.checkbox2,'Value',0);
   axes(handles.axes3)
   plot(0)
   axes(handles.axes4)
   plot(0)
   set(handles.text9,'String','Signál se načetl, vyberte filtraci.')
else 
     disp('neznama koncovka');

end
    
prom = eval(info.name);

% --- Executes on button press in filtrace.
function filtrace_Callback(hObject, eventdata, handles)

global  prom horiz2 vert2 cas1 check1 check2
% hObject    handle to filtrace (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if check1 == 0 && check2 == 0
axes(handles.axes3)
plot(0)

axes(handles.axes4)
plot(0)

set(handles.text9,'String','Nebyla vybrána žádná filtrace.')
end


if check1 == 1 && check2 == 1
axes(handles.axes3)
plot(0)

axes(handles.axes4)
plot(0)

set(handles.text9,'String','Nelze použít oba filtry současně.')
return
end


if check1 == 1

hmez = get(handles.edit2,'String');
hmez = str2num(hmez);
dmez = get(handles.edit3,'String');
dmez = str2num(dmez);
if hmez > 100 || dmez<0
   set(handles.text9,'String','Hodnoty jsou mimo rozsah filtru.')
   axes(handles.axes3)
   plot(0)
   axes(handles.axes4)
   plot(0)
   return
end

cas1 = prom(:,1);
horiz1 = prom(:,2);
vert1 = prom(:,3);

F = [0:0.01:1];
A=zeros(1,101);
A((dmez+1):(hmez+1))=1;
B = fir2(100,F,A);          % vypocet impulsni charaktireistiky pasmove propusti

% H = FREQZ(B,1,100,'whole');  %prevod impulsni charakteristiky do frekvencni charakteristiky
% figure(1)
% plot(abs(H));
% 
horiz2 = filter(B,1,horiz1);
horiz2=horiz2(50:(end-18));
vert2 = filter(B,1,vert1);
vert2=vert2(68:end);
cas1=cas1(1:(end-67));

axes(handles.axes3)
plot(cas1,horiz2)
xlabel('Čas [s]');
ylabel('Amplituda [mV]');

axes(handles.axes4)
plot(cas1,vert2)
xlabel('Čas [s]');
ylabel('Amplituda [mV]');

set(handles.text9,'String','Filtrace pásmovou propustí byla úspěšná.')
end

if check2 == 1
mez = get(handles.edit4,'String');
mez = str2num(mez);
if mez == 100
    mez = 98;
end

cas1 = prom(:,1);
horiz1 = prom(:,2);
vert1 = prom(:,3);
mez=(mez/100);
if mez > 1
    set(handles.text9,'String','Překročili jste rozsah filtru.')
    axes(handles.axes3)
    plot(0)
    axes(handles.axes4)
    plot(0)
    return
end
B = fir1(100,mez);

horiz2 = filter(B,1,horiz1);
horiz2 = horiz2(50:(end-18));
vert2 = filter(B,1,vert1);
vert2=vert2(68:end);
cas1=cas1(1:(end-67));

axes(handles.axes3)
plot(cas1,horiz2)
xlabel('Čas [s]');
ylabel('Amplituda [mV]');

axes(handles.axes4)
plot(cas1,vert2)
xlabel('Čas[s]');
ylabel('Amplituda [mV]');

set(handles.text9,'String','Filtrace dolní propustí byla úspěšná.')
end

% --- Executes on button press in orez.
function orez_Callback(hObject, eventdata, handles)
global cas1 horiz2 vert2
% hObject    handle to orez (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% horiz=horiz2;
% vert=vert2;

velikost_okna = get(handles.edit5,'String');
velikost_okna = str2num(velikost_okna);

derivace=diff(horiz2);
pozice=[0];

for i=1:length(derivace)
    if derivace(i)> 0.01 || derivace(i)< -0.01
    pozice=[pozice i];
    end
end
pozice2=[];
for k=1:(length(pozice)-1)
    if (pozice(1+k)-pozice(k))~=1
    pozice2=[pozice2 pozice(k) pozice(k+1)];
        
    end

end

for j=1:((length(pozice2)/2)-1)
prekmit=horiz2((pozice2(2*j)-3):(pozice2((2*j)+1)+velikost_okna));
prekmit=prekmit';
delkaprekmitu=(pozice2((2*j)+1)-pozice2(2*j))+3+velikost_okna;
orez=prekmit(1)*ones(1,delkaprekmitu);
horiz2(pozice2(2*j):(pozice2(2*j)+delkaprekmitu)-1)=orez;
end

derivace2=diff(vert2);
pozice3=[0];

for l=1:length(derivace2)
    if derivace2(l)> 0.01 || derivace2(l)< -0.01
    pozice3=[pozice3 l];
    end
end

pozice4=[];
for m=1:(length(pozice3)-1)
    if (pozice3(1+m)-pozice3(m))~=1
    pozice4=[pozice4 pozice3(m) pozice3(m+1)];
        
    end

end

for n=1:((length(pozice4)/2)-1)
prekmit=vert2((pozice4(2*n)-3):(pozice4((2*n)+1)+velikost_okna));
delkaprekmitu=(pozice4((2*n)+1)-pozice4(2*n))+3+velikost_okna;
orez=prekmit(1)*ones(1,delkaprekmitu);
vert2(pozice4(2*n):(pozice4(2*n)+delkaprekmitu)-1)=orez;
end

axes(handles.axes3)
plot(cas1,horiz2)
xlabel('Čas [s]');
ylabel('Amplituda [mV]');

axes(handles.axes4)
plot(cas1,vert2)
xlabel('Čas[s]');
ylabel('Amplituda [mV]');

set(handles.text9,'String','Ořezávám špičky.')
% --- Executes on button press in ulozit.
function ulozit_Callback(hObject, eventdata, handles)
global horiz2 vert2 cas1
% hObject    handle to ulozit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
eog=[cas1 horiz2 vert2];
ulozit = ('eog');
UISAVE(ulozit)


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

% Hint: get(hObject,'Value') returns toggle state of checkbox1
global check1
check1=0;
check1=get(hObject,'Value');

function edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (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 edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (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 checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
global check2
% hObject    handle to checkbox2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of checkbox2
check2=0;
check2=get(hObject,'Value');





function edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (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 edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double


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


