function varargout = AVG_IMEX(varargin)
% Last Modified by GUIDE v2.5 30-Oct-2007 16:07:47

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @AVG_IMEX_OpeningFcn, ...
                   'gui_OutputFcn',  @AVG_IMEX_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 AVG_IMEX is made visible.
function AVG_IMEX_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.

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

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes AVG_IMEX wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% global FileName PathName;
% FileName = ' '; PathName = ' ';

% --- Outputs from this function are returned to the command line.
function varargout = AVG_IMEX_OutputFcn(hObject, eventdata, handles) 
varargout{1} = handles.output;

% --- Executes on button press in Open_file_button.
function Open_file_button_Callback(hObject, eventdata, handles)
    global data FileName PathName rozsah AVG_IMEX f_sondy cas_osa predchozi_pozice ...
            korekce text

[FileName, PathName] = uigetfile( ...
    { '*.DOP', 'IMEX Doppler file (*.DOP)'; ...
      '*.*', 'All files (*.*)'}, ...
      'Select a DOP-file');
if FileName(end-2:end) ~= 'DOP' %pokud je vybran jiny typ souboru
    % Get the Handle for the TFinal Tag
    HH = findobj(gcf,'Tag','hlaseni');
    % Set the string for the object TFinal
    set(HH,'String','Zvolili jste jiny soubor nez *.DOP, opakujte vyber.')
    set(handles.zmenit_rozsah_button,'Enable','Off');
%     set(handles.pushbutton_ulozit, 'Enable', 'Off');

else %Pokud je vybran spravny typ souboru
    HH = findobj(gcf,'Tag','hlaseni');
    % Set the string for the object TFinal
    set(HH,'String',['Nacten soubor ', PathName, FileName]) 
    %save lastUsedDir.mat FileName PathName lastDirMat;
    fid = fopen([PathName, FileName], 'r'); %nacte file id
    c = fread(fid); %nacte soubor v dec po adresach do jednoho sloupce
    
    %od adresy 1C8 AVG_IMEX (1C9 ...korekce adres pro matlab) => data
    data = c(hex2dec('1C9'):end-2,:); 
    data = dec2hex(data,2); 

    %======Vytazeni textu ze souboru=======
%prozatim na pevno s adresama podle struktury *.DOP souboru
text = char((c(2:hex2dec('1C8'),:))); %Matlab indexuje od 1+ ... korekce adres

    %ID pacienta na adr. 18h-23h
%ID_pacienta = (['ID pacienta - ', (text(hex2dec('18'):hex2dec('23')))']); 
HH = findobj(gcf,'Tag','ID_pacienta');
set(HH,'String',(text(hex2dec('18'):hex2dec('23')))')
    %jmeno pacienta - 25h-36h
HH = findobj(gcf,'Tag','pacient');
set(HH,'String',(text(hex2dec('25'):hex2dec('36')))')
     %Datum narozeni na adr den (3Bh-3Ch), mesic (38h-39h), rok (3Eh-3Fh)
HH = findobj(gcf,'Tag','narozen');
set(HH,'String',[text(hex2dec('3B'):hex2dec('3C'))','.',text(hex2dec('38'):hex2dec('39'))','.',text(hex2dec('3E'):hex2dec('3F'))'])
     %vek - 41h-43h
HH = findobj(gcf,'Tag','vek');
set(HH,'String',text(hex2dec('41'):hex2dec('43'))')
    %vyska - 45h-47h
HH = findobj(gcf,'Tag','vyska');
set(HH,'String',text(hex2dec('45'):hex2dec('47'))')
    %vaha - 49h-4Bh
HH = findobj(gcf,'Tag','vaha');
set(HH,'String',text(hex2dec('49'):hex2dec('4B'))')
    %pohlavi - (4Dh)
    switch lower(text(hex2dec('4D')))
        case 'm'
            pohlavi = 'Muz';
        case 'f' 
            pohlavi = 'Zena';
        otherwise
            pohlavi = 'Nezadano';
    end
HH = findobj(gcf,'Tag','pohlavi');
set(HH,'String',pohlavi)
    %rasa - (4Eh)
    switch lower(text(hex2dec('4E')))
        case 'b' 
            rasa = 'Afro-americka';
        case 'w' 
            rasa = 'Evropska';
        case 'y' 
            rasa = 'Asijska';
        otherwise
            rasa = 'Nezadano';
    end
HH = findobj(gcf,'Tag','rasa');
set(HH,'String',rasa)
    %datum vysetreni na adr 0Bh-10h - 031607
text_p = text(hex2dec('0B'):hex2dec('10'));
HH = findobj(gcf,'Tag','datum');
set(HH,'String',([(text_p(3:4))','.',(text_p(1:2))','.',(text_p(5:6))']));
    %cas mereni na adr 11h-16h (v inverznim zapisu) - 102858
text_p = text(hex2dec('11'):hex2dec('16'));
HH = findobj(gcf,'Tag','cas');
set(HH,'String',([(text_p(1:2))',':',(text_p(3:4))',':',(text_p(5:6))']));   
    %jmeno doktora - 24pismen 4Fh-66h
HH = findobj(gcf,'Tag','doktor');
set(HH,'String',((text(hex2dec('4F'):hex2dec('66')))'));
    %jmeno vysetrujiciho - 24pismen 68h-7Fh
HH = findobj(gcf,'Tag','vysetrujici');
set(HH,'String',(text(hex2dec('68'):hex2dec('7F')))');
    %f sondy 90h-95h
HH = findobj(gcf,'Tag','sonda');
set(HH,'String',((text(hex2dec('90'):hex2dec('95')))'));
f_sondy = str2double(text(hex2dec('91')));
    %jednotky stupnice 9Ah-9Dh
HH = findobj(gcf,'Tag','rozsah');
set(HH,'String',(text(hex2dec('9A'):hex2dec('9D')))');
    %rozsah stupnice
rozsah = str2double((text(hex2dec('150'):hex2dec('153')))');
    %rychlost zapisu 9Fh-A6h
HH = findobj(gcf,'Tag','v_zapisu');
set(HH,'String',(text(hex2dec('9F'):hex2dec('A6')))');
clear text_p;

%======Konec vypisu informaci o vysetreni======
HH = findobj(gcf,'Tag','zmenit_rozsah_button');
set(HH,'Enable','On');

% set(handles.pushbutton_ulozit, 'Enable', 'On');

%======dekodovani dat a jejich zobrazeni======
znamenko = data(1:2:end-1,1); %ulozi znamenko signalu 1B ze 4B
AVG_IMEX = dec2hex(zeros((length(data))/2,1),3);
AVG_IMEX(:,1) = data(1:2:end,2); 
AVG_IMEX(:,2:3) = data(2:2:end,:);
AVG_IMEX = hex2dec(AVG_IMEX); %ulozi AVG_IMEX 2-4B ze 4B

%dekodovani zapornych hodnot signalu
for index1 = 1:length(AVG_IMEX)
    if znamenko(index1) == 'F'
        AVG_IMEX(index1) = -1 * (4096 - AVG_IMEX(index1));
    end
end
AVG_IMEX = 3 * AVG_IMEX; %zesileni po AD prevodniku IMEXU 3x

switch get(handles.rozsah, 'String')
    case 'KHz '
        rozmer = 'f [kHz]';
        set(handles.zmenit_rozsah_button, 'String', 'kHz -> cm/s');
        korekce = f_sondy*1e6*0.3/1500; 
    case 'cm/s'
        rozmer = 'v [cm/s]';
        set(handles.zmenit_rozsah_button, 'String', 'cm/s -> kHz');
        korekce = 2*f_sondy/(1000*1500/(f_sondy*1e6));
end
cas_osa = (1:5:length(AVG_IMEX)*5)-1;
plot(cas_osa,(AVG_IMEX / korekce)-0.4);
title(['Signal AVG - ',FileName]); xlabel('cas [ms]'); ylabel(rozmer);
axis([0 length(cas_osa)*5 -rozsah rozsah]);
set(handles.plot_AVG, 'XGrid', 'On', 'YGrid', 'On');
% nastaveni slideru grafu
predchozi_pozice = 0;
set(handles.slider2, 'Enable', 'On');
set(handles.slider2, 'Value', 0);
% aktivace tlacitek pro nastaveni grafu
set(handles.pushbutton_auto, 'Enable', 'On');
set(handles.pushbutton_original, 'Enable', 'On');
set(handles.pushbutton_zoom, 'Enable', 'On');

end
% --- Executes on button press in zmenit_rozsah_button.
function zmenit_rozsah_button_Callback(hObject, eventdata, handles)
    global  FileName korekce AVG_IMEX f_sondy cas_osa

%zobrazeni signalu AVG s automatickym rozsahem
%axes(handles.plot_AVG)

text = get(handles.zmenit_rozsah_button, 'String');


switch strcmp(get(handles.zmenit_rozsah_button, 'String'),'kHz -> cm/s')
    case 0 % prepocet z cm/s na khz
        rozsah_osy_y = get(handles.plot_AVG, 'YLim');
        rozsah_osy_y = ((rozsah_osy_y*2*f_sondy*1e6*0.5)/(100*1500))/1000; 
        %*100 nahore bo je rychlost v cm, doppler
        korekce = f_sondy*1e6*0.3/1500;
        rozmer = 'f [kHz]';
        set(handles.zmenit_rozsah_button, 'String', 'kHz -> cm/s');
        set(handles.rozsah, 'String', 'kHz')
    case 1 %prepocet z khz na cm/s
        rozsah_osy_y = get(handles.plot_AVG, 'YLim');
        rozsah_osy_y = (rozsah_osy_y*1000*150000)/(2*f_sondy*1e6*0.5); 
        %*100 dole aby to bylo v cm, doppler
        clc
        korekce = 2*f_sondy/(1000*1500/(f_sondy*1e6));
        rozmer = 'v [cm/s]';
        set(handles.zmenit_rozsah_button, 'String', 'cm/s -> kHz');
        set(handles.rozsah, 'String', 'cm/s')
end
grid
AVG = AVG_IMEX/korekce;
plot(cas_osa,AVG);
title(['Signal AVG - ',FileName]); xlabel('cas [ms]'); ylabel(rozmer);
set(handles.plot_AVG, 'YLim', rozsah_osy_y);
set(handles.plot_AVG, 'XGrid', 'On', 'YGrid', 'On');

% --- Executes on button press in Close_button.
function Close_button_Callback(hObject, eventdata, handles)
clear all;
close all;


function pacient_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function pacient_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function Datum_pole_Callback(hObject, eventdata, handles)

function Datum_pole_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function cas_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function cas_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function vysetrujici_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function vysetrujici_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function sonda_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function sonda_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function rozsah_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function v_zapisu_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function v_zapisu_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

function hlaseni_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function hlaseni_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

% --- Executes during object creation, after setting all properties.
function datum_CreateFcn(hObject, eventdata, handles)

function radiobutton_kHz_ButtonDownFcn(hObject, eventdata, handles)

set(handles.radiobutton_kHz, 'Value', 1);
set(handles.radiobutton_cm,  'Value', 0);
set(handles.radiobutton_kHz, 'Enable', 'off');
set(handles.radiobutton_cm, 'Enable', 'on');

function radiobutton_cm_ButtonDownFcn(hObject, eventdata, handles)

set(handles.radiobutton_kHz, 'Value', 0);
set(handles.radiobutton_cm,  'Value', 1);
set(handles.radiobutton_cm, 'Enable', 'off');
set(handles.radiobutton_kHz, 'Enable', 'on');


% --- Executes during object creation, after setting all properties.
function zmenit_rozsah_button_CreateFcn(hObject, eventdata, handles)
% --- Executes on button press in pushbutton_ulozit.
function pushbutton_ulozit_Callback(hObject, eventdata, handles)
% --- Executes on button press in pushbutton_plus.
function pushbutton_plus_Callback(hObject, eventdata, handles)
rozsah_osy_y = get(handles.plot_AVG, 'YLim');

switch strcmp(get(handles.zmenit_rozsah_button, 'String'),'kHz -> cm/s')
    case 0  
        rozsah_osy_y = rozsah_osy_y + 15;
    case 1
        rozsah_osy_y = rozsah_osy_y + 0.5;
end            
        
set(handles.plot_AVG, 'YLim', rozsah_osy_y);

% --- Executes on button press in pushbutton_minus.
function pushbutton_minus_Callback(hObject, eventdata, handles)
rozsah_osy_y = get(handles.plot_AVG, 'YLim');

switch strcmp(get(handles.zmenit_rozsah_button, 'String'),'kHz -> cm/s')
    case 0  
        rozsah_osy_y = rozsah_osy_y - 15;
    case 1
        rozsah_osy_y = rozsah_osy_y - 0.5;
end            
        
set(handles.plot_AVG, 'YLim', rozsah_osy_y);

% --- Executes on button press in pushbutton_auto.
function pushbutton_auto_Callback(hObject, eventdata, handles)
global korekce AVG_IMEX cas_osa

auto_rozsah_y = [min(AVG_IMEX/korekce) max(AVG_IMEX/korekce)];
auto_rozsah_x = [min(cas_osa)+1 max(cas_osa)+5];
predchozi_pozice = 0;
set(handles.slider2, 'Value', predchozi_pozice);
set(handles.plot_AVG, 'YLim', auto_rozsah_y, 'XLim', auto_rozsah_x);


% --- Executes on button press in pushbutton_original.
function pushbutton_original_Callback(hObject, eventdata, handles)
global cas_osa AVG_IMEX korekce rozsah f_sondy text predchozi_pozice ...
        FileName rozmer
HH = findobj(gcf,'Tag','rozsah');
set(HH,'String',(text(hex2dec('9A'):hex2dec('9D')))');
switch get(handles.rozsah, 'String')
    case 'KHz '
        rozmer = 'f [kHz]';
        set(handles.zmenit_rozsah_button, 'String', 'kHz -> cm/s');
        korekce = f_sondy*1e6*0.3/1500; 
    case 'cm/s'
        rozmer = 'v [cm/s]';
        set(handles.zmenit_rozsah_button, 'String', 'cm/s -> kHz');
        korekce = 2*f_sondy/(1000*1500/(f_sondy*1e6));
end

plot(cas_osa,AVG_IMEX / korekce);
axis([0 length(cas_osa)*5 -rozsah rozsah]);
set(handles.plot_AVG, 'XGrid', 'On', 'YGrid', 'On');
title(['Signal AVG - ',FileName]); xlabel('cas [ms]'); ylabel(rozmer);
predchozi_pozice = 0;
set(handles.slider2, 'Value', predchozi_pozice);

% --- Executes on button press in pushbutton_zoom.
function pushbutton_zoom_Callback(hObject, eventdata, handles)
zoom;

% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global predchozi_pozice

pozice = round(get(handles.slider2, 'Value'));
set(handles.slider2, 'Value', pozice);
pocet_posunu = abs(diff([predchozi_pozice pozice]));
rozsah_osy_y = get(handles.plot_AVG, 'YLim');

if pozice<predchozi_pozice
    for index1 = 1:pocet_posunu
        switch strcmp(get(handles.zmenit_rozsah_button, 'String'),'kHz -> cm/s')
        case 0  
            rozsah_osy_y = rozsah_osy_y - 15;
        case 1
            rozsah_osy_y = rozsah_osy_y - 0.5;
        end
    end
else
    for index1 = 1:pocet_posunu
        switch strcmp(get(handles.zmenit_rozsah_button, 'String'),'kHz -> cm/s')
        case 0  
            rozsah_osy_y = rozsah_osy_y + 15;
        case 1
            rozsah_osy_y = rozsah_osy_y + 0.5;
        end
    end
end
predchozi_pozice = pozice;
set(handles.plot_AVG, 'YLim', rozsah_osy_y);
% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


