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

% Last Modified by GUIDE v2.5 17-May-2014 12:26:35

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

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

%načtení souborů do popupmenu1
listing = dir(fullfile(pwd, 'data', '*.mat'));
set(handles.popupmenu1, 'string', {listing.name})

load(['data/' listing(1,1).name]);
x = data(:,2);
osa_x = data(:,1);

fvz = round(1/(osa_x(2)-osa_x(1)));

osa_x =(0+(1/fvz):(1/fvz):(length(x)/fvz))';

axes(handles.axes1)
plot(osa_x,x)
legend('zarušený signál')

axes(handles.axes2)
plot(osa_x(40000:50000), x(40000:50000))
legend('zarušený signál')

setappdata(handles.axes1, 'x', x)
setappdata(handles.axes1, 'osa_x', osa_x)
setappdata(handles.axes1, 'fvz', fvz)
setappdata(handles.axes1, 'snr_pos', 1)

% Update handles structure
guidata(hObject, handles);


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


% --- Outputs from this function are returned to the command line.
function varargout = index_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 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)
contents = cellstr(get(hObject,'String'));
nazev = contents{get(hObject,'Value')};
load(['data/' nazev]);
x = data(:,2);
osa_x = data(:,1);

fvz = round(1/(osa_x(2)-osa_x(1)));

osa_x =(0+(1/fvz):(1/fvz):(length(x)/fvz))';

axes(handles.axes1)
plot(osa_x,x)
legend('zarušený signál')

axes(handles.axes2)
plot(osa_x(40000:50000), x(40000:50000))
legend('zarušený signál')

setappdata(handles.axes1, 'x', x)
setappdata(handles.axes1, 'osa_x', osa_x)
setappdata(handles.axes1, 'fvz', fvz)

set(handles.checkbox3,'Value',0)
set(handles.checkbox4,'Value',0)

setappdata(handles.axes1, 'snr_pos', 1)


% Hints: contents = cellstr(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


% --- 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)
[filename, pathname] = uigetfile('.mat', 'Volba signálu');

if filename ~= 0
    load([pathname filename])
    x = data(:,2);
    osa_x = data(:,1);

    fvz = round(1/(osa_x(2)-osa_x(1)));
    osa_x = (0+(1/fvz):(1/fvz):(length(x)/fvz))';    
    
    x = x(1:60*fvz);
    osa_x = osa_x(1:60*fvz);
    
axes(handles.axes1)
plot(osa_x,x)
legend('zarušený signál')
    
    if osa_x(end) >= 50
        axes(handles.axes2)
        plot(osa_x(40*fvz:50*fvz), x(40*fvz:50*fvz))
        legend('zarušený signál')
    end
    
    setappdata(handles.axes1, 'x', x)
    setappdata(handles.axes1, 'osa_x', osa_x)
    setappdata(handles.axes1, 'fvz', fvz)
    
    set(handles.checkbox3,'Value',0)
    set(handles.checkbox4,'Value',0)
end

setappdata(handles.axes1, 'snr_pos', 0)



% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
contents = get(hObject,'String');
value = contents{get(hObject, 'Value')}
fvz = getappdata(handles.axes1, 'fvz');
osa_x = getappdata(handles.axes1, 'osa_x');
puvodni = getappdata(handles.axes1, 'x');

if get(handles.checkbox3, 'Value') == 1 && get(handles.checkbox4, 'Value') == 1
    x = getappdata(handles.axes1, 'x_drum');
    
elseif get(handles.checkbox3, 'Value') == 1 && get(handles.checkbox4, 'Value') == 0
    x = getappdata(handles.axes1, 'x_drift');
    
elseif get(handles.checkbox3, 'Value') == 0 && get(handles.checkbox4, 'Value') == 1
    x = getappdata(handles.axes1, 'x_brum');

elseif get(handles.checkbox3, 'Value') == 0 && get(handles.checkbox4, 'Value') == 0
    x = getappdata(handles.axes1, 'x');
end

switch value
    case ' '
        prompt = {'Amplituda pomocného signálu v mV:' , ...
            'Frekvence pomocného signálu v Hz:'};
        dlg_title = 'Input';
        num_lines = 1;
        def = {'80' , '50'};
        parametry = inputdlg(prompt, dlg_title, num_lines, def);
        amplituda = str2num(parametry{1});
        frekvence = str2num(parametry{2});
        y = adaptivni(x, fvz, frekvence, amplituda)';
        set(hObject, 'Value', 2)
        puvodni = puvodni(fvz*6:end);
        y = y(fvz*6:end);
    case 'Adaptivní filtr'
        prompt = {'Amplituda pomocného signálu v mV:' , ...
            'Frekvence pomocného signálu v Hz:'};
        dlg_title = 'Input';
        num_lines = 1;
        def = {'80' , '50'};
        parametry = inputdlg(prompt, dlg_title, num_lines, def);
        amplituda = str2num(parametry{1});
        frekvence = str2num(parametry{2});
        y = adaptivni(x, fvz, frekvence, amplituda)';
    case 'IIR filtr'
        prompt = {'Umístění pólů v jednotkové kružnici (volte v rozmezí 0-1, kde 0 = póly leží v počátku soustavy souřadnic a 1 = póly leží na jednotkové kružnici):'};
        dlg_title = 'Input';
        num_lines = 1;
        def = {'0.95'};
        parametry = inputdlg(prompt, dlg_title, num_lines, def);
        r = str2num(parametry{1});
        y = iir(x, fvz, r);
    case 'Interpolace'
        y = interpolace(x, fvz);
    case 'Lynnův filtr 1'
        prompt = {'Mezní frekvence filtru:'};
        dlg_title = 'Input';
        num_lines = 1;
        def = {'1.67'};
        parametry = inputdlg(prompt, dlg_title, num_lines, def);
        fmez = str2num(parametry{1});
        [y, K] = lynn_filtr1(x, fvz, fmez);
        osa_x = osa_x(1:end-K+2);
        x = x(K-1:end);
        puvodni = puvodni(K-1:end);
    case 'Lynnův filtr 2'
        prompt = {'Mezní frekvence filtru:'};
        dlg_title = 'Input';
        num_lines = 1;
        def = {'1.67'};
        parametry = inputdlg(prompt, dlg_title, num_lines, def);
        fmez = str2num(parametry{1});
        y = lynn_filtr2(x, fvz, fmez)';
    case 'Nulování spektrálních čar'
        prompt = {'Mezní frekvence filtru:'};
        dlg_title = 'Input';
        num_lines = 1;
        def = {'1.5'};
        parametry = inputdlg(prompt, dlg_title, num_lines, def);
        fmez = str2num(parametry{1});
        y = nulovani(x, fvz, fmez);
end

axes(handles.axes1)
plot(osa_x, x)
hold on
plot(osa_x, y, 'r')
hold off
legend('zarušený signál', 'vyfiltrovaný signál')

if osa_x(end) >= 50
    axes(handles.axes2)
    plot(osa_x(40*fvz:50*fvz), x(40*fvz:50*fvz))
    hold on
    plot(osa_x(40000:50000), y(40000:50000), 'r')
    hold off
    legend('zarušený signál', 'vyfiltrovaný signál')
end

switch value
    case ' '
        puvodni = puvodni(fvz*6:end);
        osa_x = osa_x(1:end-fvz*6+1);
        x = x(fvz*6:end);
        y = y(fvz*6:end);
    case 'Adaptivní filtr'
        puvodni = puvodni(fvz*6:end);
        osa_x = osa_x(1:end-fvz*6+1);
        x = x(fvz*6:end);
        y = y(fvz*6:end);
end

if getappdata(handles.axes1, 'snr_pos')==1
    pomer = SNR(puvodni, y);
    set(handles.text, 'String', pomer)
else
    set(handles.text, 'String', 'neznámé')
end
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3


% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (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 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)
x = get(handles.popupmenu3, 'Value');

switch x
    case 2
        help_adaptivni
    case 3
        help_iir
    case 4
        help_interpolace
    case 5
        help_lynn
    case 6
        help_lynn
    case 7
        help_nulovani
end



% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fvz = getappdata(handles.axes1, 'fvz');
osa_x = getappdata(handles.axes1, 'osa_x');

xx = getappdata(handles.axes1, 'x');

if get(hObject,'Value') == 1
    xx = zaruseni_drift(xx, fvz);
    setappdata(handles.axes1, 'x_drift', xx)
    
    if get(handles.checkbox4, 'Value') == 1
        x = getappdata(handles.axes1, 'x_brum');
        x = zaruseni_drift(x, fvz);
        setappdata(handles.axes1, 'x_drum', x) 
    else
        x = xx;
    end

else
    if get(handles.checkbox4, 'Value') == 1
        x = getappdata(handles.axes1, 'x_brum'); 
    else
        x = xx;
    end
end

axes(handles.axes1)
plot(osa_x, x)
legend('zarušený signál')

if osa_x(end) >= 50
    axes(handles.axes2)
    plot(osa_x(40*fvz:50*fvz), x(40*fvz:50*fvz))
    legend('zarušený signál')
end

set(handles.popupmenu3, 'Value', 1)
% Hint: get(hObject,'Value') returns toggle state of checkbox3


% --- Executes on button press in checkbox4.
function checkbox4_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fvz = getappdata(handles.axes1, 'fvz');
osa_x = getappdata(handles.axes1, 'osa_x');

xx = getappdata(handles.axes1, 'x');

if get(hObject,'Value') == 1
    prompt = {'Amplituda rušivého signálu v mV:'};
    dlg_title = 'Input';
    num_lines = 1;
    def = {'0.1'};
    amplituda = inputdlg(prompt, dlg_title, num_lines, def);
    amplituda = str2num(amplituda{:});
    
    xx = zaruseni_brum(xx, amplituda, fvz);
    setappdata(handles.axes1, 'x_brum', xx)
    
    if get(handles.checkbox3, 'Value') == 1
        x = getappdata(handles.axes1, 'x_drift');
        x = zaruseni_brum(x, amplituda, fvz);
        setappdata(handles.axes1, 'x_drum', x) 
    else
        x = xx;
    end

else
    if get(handles.checkbox3, 'Value') == 1
        x = getappdata(handles.axes1, 'x_drift');
    else
        x = xx;
    end
end

axes(handles.axes1)
plot(osa_x, x)
legend('zarušený signál')

if osa_x(end) >= 50
    axes(handles.axes2)
    plot(osa_x(40*fvz:50*fvz), x(40*fvz:50*fvz))
    legend('zarušený signál')
end

set(handles.popupmenu3, 'Value', 1)
% Hint: get(hObject,'Value') returns toggle state of checkbox4
