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

% Last Modified by GUIDE v2.5 16-Dec-2009 13:02:58

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

% Choose default command line output for ukazkaKF
handles.output = hObject;
p=inputParser;
p.addParamValue('pokyn',@(varargin)(eval('disp(''ukazka KF/pokyn:''),disp(varargin)')));
p.addParamValue('hParent',[]);
p.addParamValue('nazev','Kalmanův filtr');
p.addParamValue('nastaveni',[]);
p.addParamValue('data',[]);
p.parse(varargin{:})
handles.pokyn=p.Results.pokyn;
handles.nastaveni=p.Results.nastaveni;
handles.data=p.Results.data;
set(handles.guiNazev,'String',p.Results.nazev);

handles.legenda=1;
handles.ovladace.Q=zeros(3,3);
handles.ovladace.R=0;
handles.nastaveni.bInter=0;     %vypíná interaktivní mod
handles.hnahled=[];
handles.anahled=[];
handles.graf=handles.axes1;
% Update handles structure
guidata(hObject, handles);
updateData(hObject, eventdata, handles);

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

function prikaz(nazev,h,hodn)
%keyboard
if exist('nazev','var')
if exist(nazev)
    fce=str2func(nazev);
    if exist('hodn')
       fce(h,hodn); 
    else
        fce(h);
    end
else
    disp(['ukazkaKF/Neznamy příkaz: ' nazev]);
end,end
% --- Outputs from this function are returned to the command line.
function varargout = ukazkaKF_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 guiInteraktiv.
function guiInteraktiv_Callback(hObject, eventdata, handles)
% hObject    handle to guiInteraktiv (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 guiInteraktiv
handles.nastaveni.bInter=get(hObject,'Value');
guidata(hObject,handles);


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


% --- Executes during object creation, after setting all properties.
function guiRad_CreateFcn(hObject, eventdata, handles)
% hObject    handle to guiRad (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 guiR_Callback(hObject, eventdata, handles)
% hObject    handle to guiR (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 guiR as text
%        str2double(get(hObject,'String')) returns contents of guiR as a double
%keyboard
cislo=str2num(get(hObject,'String'));
if isempty(cislo)
set(hObject,'String',handles.nastaveni.R);
else
    handles.nastaveni.R=power(cislo,2);guidata(hObject,handles);
    updateData(hObject, eventdata, handles);
end

% --- Executes during object creation, after setting all properties.
function guiR_CreateFcn(hObject, eventdata, handles)
% hObject    handle to guiR (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 updateData(hObject, eventdata, handles)
set(handles.guiR,'String',sqrt(handles.nastaveni.R));
set(handles.guiA,'data',handles.nastaveni.A);
Q1=sqrt(handles.nastaveni.Q);
for ni=1:length(Q1),Q1(ni,ni)=Q1(ni,ni)*power(handles.nastaveni.dt,ni-1);end
set(handles.guiQ,'data',Q1);
set(handles.guiRad,'String',handles.nastaveni.radKF);
%plot(handles.data);
%keyboard
%clear s
s=handles.nastaveni;
for t=1:length(handles.data)
   s(end).z = handles.data(t,1);
   s(end+1)=kalmanf(s(end)); % perform a Kalman filter iteration
end

xsmoothv=1e3*smooth(diff(smooth(handles.data,10)),20);
xsmootha=smooth(diff(xsmoothv),10);

graf=handles.graf;
% plot measurement data:
%keyboard
meritko=axis(graf);

merx=[1:t]*handles.nastaveni.dt;
hz=plot(graf,merx,[s(1:end-1).z],'r.');
hold(graf,'on');
grid(graf,'on');

%keyboard

% plot a-posteriori state estimates:
%keyboard
d=[s(2:end).x];
hk=plot(graf,merx,d(1,:),'b-');
%ht=plot(handles.axes1,d(2,:)/5,'g-');
ht=plot(graf,merx,d(2,:),'g-');
%hs=plot(handles.axes1,xsmoothv/5,'c');
%hs=plot(graf,xsmoothv/1000,'c');%smooth rychlost
%ha=plot(handles.axes1,d(3,:)/1e3,'r');
ha=plot(graf,merx,d(3,:),'r');
%hsa=plot(graf,xsmootha/1000,'y');%smooth zrychleni
if handles.legenda
    legend(graf,...%[hz hk ht hs ha hsa],
    'Naměřené hodnoty','Kalman-output',...
    'Kalman-rychlost','klouzavý průměr-rychlost','Kalman-zrychlení',...
    'Klouzavý průměr-zrychlení',0);end
%title('Automobile Voltimeter Example')
xlabel(graf,'[s]');
%line([265 265],[0 200]);
hold(graf,'off');
for ni=1:length(s(end).K),K(ni)=s(end).K(ni)*power(handles.nastaveni.dt,ni-1);end
set(handles.textK,'String',K);
handles.nastaveni.K=s(end).K;
%keyboard
if isfield(handles,'meritko')
axis(graf,meritko);
end
handles.meritko=meritko;
guidata(hObject,handles);
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over guiR.
function guiR_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to guiR (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


%keyboard

if ~handles.ovladace.R
    value=str2num(get(handles.guiR,'String'));
handles.ovladace.R=ovladac('hParent',hObject,'pokyn',@prikaz,'nazev','Šum měření R',... 
       'limity',[0 100*value 0 value],'nazevhodnoty','nastav','promena','R');
   guidata(hObject,handles);
end

% --- Executes on button press in checkboxLegenda.
function checkboxLegenda_Callback(hObject, eventdata, handles)
% hObject    handle to checkboxLegenda (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 checkboxLegenda
handles.legenda=get(hObject,'Value');guidata(hObject,handles);
updateData(hObject, eventdata, handles);

function nastav(hObject, parametry)
handles=guidata(hObject);
%keyboard
handles.nastaveni.(parametry.promena)=power(parametry.hodnota,2);
guidata(hObject,handles);
%keyboard
updateData(hObject, [], handles);


function nastavmatici(hObject, parametry)
handles=guidata(hObject);
%keyboard
data=handles.nastaveni.(parametry.promena.nazev);
Q1=power(parametry.hodnota/power(handles.nastaveni.dt,parametry.promena.indices(2)-1),2);
data(parametry.promena.indices(1),parametry.promena.indices(2))=Q1;
%data(parametry.promena.indices(1),parametry.promena.indices(2))=parametry.hodnota;
handles.nastaveni.(parametry.promena.nazev)=data;
guidata(hObject,handles);
updateData(hObject, [], handles);
%keyboard


% --- Executes when entered data in editable cell(s) in guiQ.
function guiQ_CellEditCallback(hObject, eventdata, handles)
% hObject    handle to guiQ (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) edited
%	PreviousData: previous data for the cell(s) edited
%	EditData: string(s) entered by the user
%	NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
%	Error: error string when failed to convert EditData to appropriate value for Data
% handles    structure with handles and user data (see GUIDATA)
%keyboard

if isnan(eventdata.NewData)
    data = get(handles.guiQ,'Data');
    data(eventdata.Indices(1),eventdata.Indices(2)) = eventdata.PreviousData;
    set(handles.guiQ,'Data',data);
else
    %keyboard
    handles.nastaveni.Q(eventdata.Indices(1),eventdata.Indices(2)) = power(eventdata.NewData/power(handles.nastaveni.dt,eventdata.Indices(2)-1),2);
    guidata(hObject,handles);
    updateData(hObject, eventdata, handles);
end
 

% --- Executes when selected cell(s) is changed in guiQ.
function guiQ_CellSelectionCallback(hObject, eventdata, handles)
% hObject    handle to guiQ (see GCBO)
% eventdata  structure with the following fields (see UITABLE)
%	Indices: row and column indices of the cell(s) currently selecteds
% handles    structure with handles and user data (see GUIDATA)

%keyboard
if handles.nastaveni.bInter
if ~isempty(eventdata.Indices)
if ~handles.ovladace.Q(eventdata.Indices(1),eventdata.Indices(2))

data=get(handles.guiQ,'data');
data=data(eventdata.Indices(1),eventdata.Indices(2));
%keyboard
handles.ovladace.Q(eventdata.Indices(1),eventdata.Indices(2))=...
ovladac('hParent',hObject,'pokyn',@prikaz,'nazev',...
    ['Šum Q(' num2str(eventdata.Indices(1)) ',' num2str(eventdata.Indices(2)) ')'],... 
       'limity',[0 100*data 0 1*data],'nazevhodnoty','nastavmatici',...
       'promena',struct('indices',eventdata.Indices,'nazev','Q'));
guidata(hObject,handles);
end,end,end


function uloz(a,h)
handles=guidata(h);
%keyboard
if isstruct(a.data.promena)
handles.ovladace.(a.data.promena.nazev)(a.data.promena.indices(1),a.data.promena.indices(2))=0;
else
    handles.ovladace.(a.data.promena)=0;
end
guidata(h,handles);


% --- Executes during object deletion, before destroying properties.
function figure1_DeleteFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
K=handles.nastaveni.K;
assignin('base', 'K',K);
for ni=1:length(K),K(ni)=K(ni)*power(handles.nastaveni.dt,ni-1);end
assignin('base', 'K1s',K);

% --- Executes on button press in pushbuttonnahled.
function pushbuttonnahled_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonnahled (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
if isempty(handles.hnahled)
handles.hnahled=figure;
handles.anahled=axes;
handles.graf=handles.anahled;
set(handles.hnahled,'DeleteFcn',@vymaz,'UserData',hObject);
guidata(hObject,handles);
updateData(hObject,[],handles);
h=zoom;
set(h,'Motion','vertical');
axis(handles.graf,[-inf inf -inf inf])
end

function vymaz(src,evnt)
hObject=get(src,'UserData');
handles=guidata(hObject);
handles.hnahled=[];
handles.anahled=[];
handles.graf=handles.axes1;
guidata(hObject,handles);
updateData(hObject,[],handles);


% --- Executes on button press in pushbuttonfit.
function pushbuttonfit_Callback(hObject, eventdata, handles)
% hObject    handle to pushbuttonfit (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
axis(handles.graf,[-inf inf -inf inf])


