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

% Last Modified by GUIDE v2.5 05-May-2005 13:02:30

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @set_markers_OpeningFcn, ...
    'gui_OutputFcn',  @set_markers_OutputFcn, ...
    'gui_LayoutFcn',  [] , ...
    'gui_Callback',   []);
if nargin & isstr(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 set_markers is made visible.
function set_markers_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 set_markers (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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

%handles.prefs=varargin{1};

handles.prefs=varargin{1};


load('marker_data')
handles.markers=ds;

% for n=1:8
%     handles.markers.use(n)=1;
% end

handles.line_vis='on';
set(handles.checkbox11,'value',1)


set(handles.use_a,'value',handles.markers.use(1));
set(handles.use_b,'value',handles.markers.use(2));
set(handles.use_c,'value',handles.markers.use(3));
set(handles.use_d,'value',handles.markers.use(4));
set(handles.use_e,'value',handles.markers.use(5));
set(handles.use_f,'value',handles.markers.use(6));
set(handles.use_g,'value',handles.markers.use(7));
set(handles.use_h,'value',handles.markers.use(8));

handles.AH(1)=handles.axes1;
handles.AH(2)=handles.axes2;
handles.AH(3)=handles.axes3;
handles.AH(4)=handles.axes4;
handles.AH(5)=handles.axes5;
handles.AH(6)=handles.axes6;
handles.AH(7)=handles.axes7;
handles.AH(8)=handles.axes8;

% TEMPORARY MEASURE %%%%
% load('default_prefs');
% handles.prefs=prefs;
%%%%%%%%%%%%%%%%%%%%%%%%

for n=1:8
    if ~isempty(handles.markers.file{n}) && exist(handles.markers.file{n})
        wave = mark_filt(handles,handles.markers.file{n});
        axes(handles.AH(n))
        yx=[0 wave.fs/2];
        xx=[0 size(wave.spec,2)*wave.timeint];
        imagesc(xx,yx,wave.spec); colormap(hot); axis xy;
        hold on
        handles.L1(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(1,n) handles.markers.data(1,n)],'.g-');
        handles.L2(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(2,n) handles.markers.data(2,n)],'.g-');
        handles.L3(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(6,n) handles.markers.data(6,n)],'.b-');
        hold off
        guidata(hObject, handles);

    end
end


guidata(hObject, handles);

% --- Outputs from this function are returned to the command line.
function varargout = set_markers_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 use_a.
function use_a_Callback(hObject, eventdata, handles)
% hObject    handle to use_a (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 use_a

handles.markers.use(1)=get(handles.use_a,'value'); guidata(hObject, handles);

% --- Executes on button press in use_b.
function use_b_Callback(hObject, eventdata, handles)
% hObject    handle to use_b (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 use_b

handles.markers.use(2)=get(handles.use_b,'value'); guidata(hObject, handles);
% --- Executes on button press in use_c.
function use_c_Callback(hObject, eventdata, handles)
% hObject    handle to use_c (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 use_c

handles.markers.use(3)=get(handles.use_c,'value'); guidata(hObject, handles);
% --- Executes on button press in use_d.
function use_d_Callback(hObject, eventdata, handles)
% hObject    handle to use_d (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 use_d

handles.markers.use(4)=get(handles.use_d,'value'); guidata(hObject, handles);
% --- Executes on button press in use_e.
function use_e_Callback(hObject, eventdata, handles)
% hObject    handle to use_e (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 use_e

handles.markers.use(5)=get(handles.use_e,'value'); guidata(hObject, handles);
% --- Executes on button press in use_f.
function use_f_Callback(hObject, eventdata, handles)
% hObject    handle to use_f (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 use_f

handles.markers.use(6)=get(handles.use_f,'value'); guidata(hObject, handles);
% --- Executes on button press in use_g.
function use_g_Callback(hObject, eventdata, handles)
% hObject    handle to use_g (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 use_g

handles.markers.use(7)=get(handles.use_g,'value'); guidata(hObject, handles);
% --- Executes on button press in use_h.
function use_h_Callback(hObject, eventdata, handles)
% hObject    handle to use_h (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 use_h

handles.markers.use(8)=get(handles.use_h,'value'); guidata(hObject, handles);

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

[file, path]=uigetfile('*.wav');

if isequal([path, file],[0, 0])
    return
else
    if path(length(path))~='\'
        path(length(path)+1)='\';
    end
    handles.markers.file{1}=strcat(path,file);
    wave = mark_filt(handles,handles.markers.file{1});
    data = mark_data(handles,wave);
    handles.markers.data(:,1)=data;

    axes(handles.AH(1))

    n=1;
    yx=[0 wave.fs/2];
    xx=[0 size(wave.spec,2)*wave.timeint];
    imagesc(xx,yx,wave.spec); colormap(hot); axis xy;
    hold on
    handles.L1(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(1,n) handles.markers.data(1,n)],'.g-');
    handles.L2(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(2,n) handles.markers.data(2,n)],'.g-');
    handles.L3(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(6,n) handles.markers.data(6,n)],'.b-');
    hold off

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

[file, path]=uigetfile('*.wav');

if isequal([path, file],[0, 0])
    return
else
    if path(length(path))~='\'
        path(length(path)+1)='\';
    end
    handles.markers.file{2}=strcat(path,file);
    wave = mark_filt(handles,handles.markers.file{2});
    data = mark_data(handles,wave);
    handles.markers.data(:,2)=data;

    axes(handles.AH(2))

    n=2;
    yx=[0 wave.fs/2];
    xx=[0 size(wave.spec,2)*wave.timeint];
    imagesc(xx,yx,wave.spec); colormap(hot); axis xy;
    hold on
    handles.L1(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(1,n) handles.markers.data(1,n)],'.g-');
    handles.L2(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(2,n) handles.markers.data(2,n)],'.g-');
    handles.L3(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(6,n) handles.markers.data(6,n)],'.b-');

    hold off

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

[file, path]=uigetfile('*.wav');

if isequal([path, file],[0, 0])
    return
else
    if path(length(path))~='\'
        path(length(path)+1)='\';
    end
    handles.markers.file{4}=strcat(path,file);
    wave = mark_filt(handles,handles.markers.file{4});
    data = mark_data(handles,wave);
    handles.markers.data(:,4)=data;

    axes(handles.AH(4))

    n=4;
    yx=[0 wave.fs/2];
    xx=[0 size(wave.spec,2)*wave.timeint];
    imagesc(xx,yx,wave.spec); colormap(hot); axis xy;
    hold on
    handles.L1(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(1,n) handles.markers.data(1,n)],'.g-');
    handles.L2(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(2,n) handles.markers.data(2,n)],'.g-');
    handles.L3(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(6,n) handles.markers.data(6,n)],'.b-');
    hold off
    refresh


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

[file, path]=uigetfile('*.wav');

if isequal([path, file],[0, 0])
    return
else
    if path(length(path))~='\'
        path(length(path)+1)='\';
    end
    handles.markers.file{5}=strcat(path,file);
    wave = mark_filt(handles,handles.markers.file{5});
    data = mark_data(handles,wave);
    handles.markers.data(:,5)=data;

    axes(handles.AH(5))

    n=5;
    yx=[0 wave.fs/2];
    xx=[0 size(wave.spec,2)*wave.timeint];
    imagesc(xx,yx,wave.spec); colormap(hot); axis xy;
    hold on
    handles.L1(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(1,n) handles.markers.data(1,n)],'.g-');
    handles.L2(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(2,n) handles.markers.data(2,n)],'.g-');
    handles.L3(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(6,n) handles.markers.data(6,n)],'.b-');
    hold off


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

[file, path]=uigetfile('*.wav');

if isequal([path, file],[0, 0])
    return
else
    if path(length(path))~='\'
        path(length(path)+1)='\';
    end
    handles.markers.file{6}=strcat(path,file);
    wave = mark_filt(handles,handles.markers.file{6});
    data = mark_data(handles,wave);
    handles.markers.data(:,6)=data;

    axes(handles.AH(6))

    n=6;
    yx=[0 wave.fs/2];
    xx=[0 size(wave.spec,2)*wave.timeint];
    imagesc(xx,yx,wave.spec); colormap(hot); axis xy;
    hold on
    handles.L1(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(1,n) handles.markers.data(1,n)],'.g-');
    handles.L2(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(2,n) handles.markers.data(2,n)],'.g-');
    handles.L3(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(6,n) handles.markers.data(6,n)],'.b-');
    hold off


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

[file, path]=uigetfile('*.wav');

if isequal([path, file],[0, 0])
    return
else
    if path(length(path))~='\'
        path(length(path)+1)='\';
    end
    handles.markers.file{7}=strcat(path,file);
    wave = mark_filt(handles,handles.markers.file{7});
    data = mark_data(handles,wave);
    handles.markers.data(:,7)=data;

    axes(handles.AH(7))

    n=7;
    yx=[0 wave.fs/2];
    xx=[0 size(wave.spec,2)*wave.timeint];
    imagesc(xx,yx,wave.spec); colormap(hot); axis xy;
    hold on
    handles.L1(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(1,n) handles.markers.data(1,n)],'.g-');
    handles.L2(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(2,n) handles.markers.data(2,n)],'.g-');
    handles.L3(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(6,n) handles.markers.data(6,n)],'.b-');
    hold off


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

[file, path]=uigetfile('*.wav');

if isequal([path, file],[0, 0])
    return
else
    if path(length(path))~='\'
        path(length(path)+1)='\';
    end
    handles.markers.file{8}=strcat(path,file);

    wave = mark_filt(handles,handles.markers.file{8});
    data = mark_data(handles,wave);
    handles.markers.data(:,8)=data;

    axes(handles.AH(8))

    n=8;
    yx=[0 wave.fs/2];
    xx=[0 size(wave.spec,2)*wave.timeint];
    imagesc(xx,yx,wave.spec); colormap(hot); axis xy;
    hold on
    handles.L1(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(1,n) handles.markers.data(1,n)],'.g-');
    handles.L2(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(2,n) handles.markers.data(2,n)],'.g-');
    handles.L3(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(6,n) handles.markers.data(6,n)],'.b-');
    hold off

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

[file, path]=uigetfile('*.wav');

if isequal([path, file],[0, 0])
    return
else
    if path(length(path))~='\'
        path(length(path)+1)='\';
    end
    handles.markers.file{3}=strcat(path,file);
    wave = mark_filt(handles,handles.markers.file{3});
    data = mark_data(handles,wave);
    handles.markers.data(:,3)=data;

    axes(handles.AH(3))

    n=3;
    yx=[0 wave.fs/2];
    xx=[0 size(wave.spec,2)*wave.timeint];
    imagesc(xx,yx,wave.spec); colormap(hot); axis xy;
    hold on
    handles.L1(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(1,n) handles.markers.data(1,n)],'.g-');
    handles.L2(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(2,n) handles.markers.data(2,n)],'.g-');
    handles.L3(n)=plot([0 handles.markers.data(5,n)],[handles.markers.data(6,n) handles.markers.data(6,n)],'.b-');
    hold off


    refresh
    guidata(hObject, handles);
end
% --- Executes on button press in use_all.
function use_all_Callback(hObject, eventdata, handles)
% hObject    handle to use_all (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 use_all

handles.markers.use=[1 1 1 1 1 1 1 1];

set(handles.use_a,'value',handles.markers.use(1));
set(handles.use_b,'value',handles.markers.use(2));
set(handles.use_c,'value',handles.markers.use(3));
set(handles.use_d,'value',handles.markers.use(4));
set(handles.use_e,'value',handles.markers.use(5));
set(handles.use_f,'value',handles.markers.use(6));
set(handles.use_g,'value',handles.markers.use(7));
set(handles.use_h,'value',handles.markers.use(8));
guidata(hObject, handles);


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

ds=handles.markers;
save('marker_data','ds');
close(handles.output)
main(handles.prefs)

% --- Executes on button press in show_param.
function show_param_Callback(hObject, eventdata, handles)
% hObject    handle to show_param (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 show_param

if get(handles.show_param,'value')==1
    handles.line_vis='on';
elseif get(handles.show_param,'value')==0
    handles.line_vis='off';
end
guidata(hObject, handles);

line_vis(handles);

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



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

handles.markers.use=[0 0 0 0 0 0 0 0];

set(handles.use_a,'value',handles.markers.use(1));
set(handles.use_b,'value',handles.markers.use(2));
set(handles.use_c,'value',handles.markers.use(3));
set(handles.use_d,'value',handles.markers.use(4));
set(handles.use_e,'value',handles.markers.use(5));
set(handles.use_f,'value',handles.markers.use(6));
set(handles.use_g,'value',handles.markers.use(7));
set(handles.use_h,'value',handles.markers.use(8));
guidata(hObject, handles);


% function to filter marker files
function wave = mark_filt(handles,file)

[wave.wf, wave.fs]=wavread(file);
wave.spec=abs(specgram(wave.wf));

filtdata = [handles.prefs.threshold handles.prefs.FIRcutoff handles.prefs.gaussLPF];
threshold=handles.prefs.threshold;

if handles.prefs.FIRon==0
    filtdata(2)=0;
end

if handles.prefs.gaussLPFon==0
    filtdata(3)=0;
end

if handles.prefs.thresholdon==0
    filtdata(1)=0;
end

if handles.prefs.buttfilton==1
    wave.spec=butt_filt(wave,handles.prefs.cutoff/2,handles.prefs.slope);
end
pthresh_med=(2*median(wave.spec(:)));


[wave.spec, threshold]=filt_spec(wave,filtdata);

if pthresh_med>threshold
    threshold=pthresh_med;
    wave.spec2=wave.spec.*(wave.spec>=pthresh_med);
else
    wave.spec=wave.spec.*(wave.spec>=threshold);
end
wave.timeint=length(wave.wf)/(length(wave.spec)*wave.fs);
find_len=max(wave.spec,[],1);
wave.spec=wave.spec(:,min(find(find_len)):max(find(find_len)));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% function to extract parameters
function data = mark_data(handles,wave)


param1=[1 2 3 4 5 6 7 8 9 11 12 13];
param2=[1 2 3 4 5 6 7 8 9 11 12 13];
wave.timeint=length(wave.wf)/(length(wave.spec)*wave.fs);
data(1:13,1) = featurext_wk(wave,param1,wave.timeint,handles.prefs)';

wave1=wave;
wave2=wave;
wave1.spec=wave.spec(:,1:round(size(wave.spec,2)/2));
wave2.spec=wave.spec(:,round(size(wave.spec,2)/2)+1:size(wave.spec,2));
if max(wave1.spec(:))==0
    wave1.spec=wave2.spec;
elseif max(wave2.spec(:))==0
    wave2.spec=wave1.spec;
end
feature1 = featurext_wk(wave1,param2,wave.timeint,handles.prefs);
feature2 = featurext_wk(wave2,param2,wave.timeint,handles.prefs);
data(14:26,1)=(feature1-feature2)';


function line_vis(handles)
for n=1:8
    set(handles.L1(n),'visible',handles.line_vis);
    set(handles.L2(n),'visible',handles.line_vis);
    set(handles.L3(n),'visible',handles.line_vis);
end


% --- Executes on button press in checkbox11.
function checkbox11_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox11 (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 checkbox11


if get(handles.checkbox11,'value')==1
    handles.line_vis='on';
elseif get(handles.checkbox11,'value')==0
    handles.line_vis='off';
end
guidata(hObject, handles);

line_vis(handles);

