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

% Last Modified by GUIDE v2.5 06-May-2005 13:59:51

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

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

% Update handles structure
guidata(hObject, handles);

handles.prefs = varargin{1};
guidata(hObject, handles);

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

%%%%%%%%%%%%%
% MAIN CODE %
%%%%%%%%%%%%%

set(handles.maxfreq,'value',handles.prefs.features(1));
set(handles.minfreq,'value',handles.prefs.features(2));
set(handles.bandwidth,'value',handles.prefs.features(3));
set(handles.entropy,'value',handles.prefs.features(4));
set(handles.call_len,'value',handles.prefs.features(5));
set(handles.peakfreq,'value',handles.prefs.features(6));
set(handles.peakamp,'value',handles.prefs.features(7));
set(handles.dtmax,'value',handles.prefs.features(8));
set(handles.dtmin,'value',handles.prefs.features(9));
set(handles.curvefit,'value',handles.prefs.features(10));
set(handles.curvefit_eff,'value',handles.prefs.features(11));
set(handles.perthresh,'value',handles.prefs.features(12));
set(handles.power,'value',handles.prefs.features(13));

set(handles.d_maxfreq,'value',handles.prefs.features(14));
set(handles.d_minfreq,'value',handles.prefs.features(15));
set(handles.d_bandwidth,'value',handles.prefs.features(16));
set(handles.d_entropy,'value',handles.prefs.features(17));
set(handles.d_call_len,'value',handles.prefs.features(18));
set(handles.d_peakfreq,'value',handles.prefs.features(19));
set(handles.d_peakamp,'value',handles.prefs.features(20));
set(handles.d_dtmax,'value',handles.prefs.features(21));
set(handles.d_dtmin,'value',handles.prefs.features(22));
set(handles.d_curvefit,'value',handles.prefs.features(23));
set(handles.d_curvefit_eff,'value',handles.prefs.features(24));
set(handles.d_perthresh,'value',handles.prefs.features(25));
set(handles.d_power,'value',handles.prefs.features(26));

if handles.prefs.buttfilton==1;
    set(handles.BFon,'string','ON')
    set(handles.BFcut,'string',handles.prefs.cutoff)
    set(handles.BFslope,'string',handles.prefs.slope)
else
    set(handles.BFon,'string','OFF')
    set(handles.BFcut,'string','-')
    set(handles.BFslope,'string','-')
end

if handles.prefs.FIRon==1;
    set(handles.FIRon,'string','ON')
    set(handles.FIRcut,'string',handles.prefs.FIRcutoff)
else
    set(handles.FIRon,'string','OFF')
    set(handles.FIRcut,'string','-')
end

if handles.prefs.thresholdon==1;
    set(handles.THRESHon,'string','ON')
    set(handles.THRESHlev,'string',20*log10(handles.prefs.threshold))
else
    set(handles.THRESHon,'string','OFF')
    set(handles.THRESHlev,'string','-')
end

if handles.prefs.gaussLPFon==1;
    set(handles.GAUSSon,'string','ON')
    set(handles.GAUSSker,'string',handles.prefs.gaussLPF)
else
    set(handles.GAUSSon,'string','OFF')
    set(handles.GAUSSker,'string','-')
end

if ~isempty(handles.prefs.filenames)
    set(handles.listbox1,'String',handles.prefs.filenames)
else
    set(handles.listbox1,'String','')
end

% --- Outputs from this function are returned to the command line.
function varargout = window_1_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 during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1


% --- Executes on button press in minfreq.
function minfreq_Callback(hObject, eventdata, handles)
% hObject    handle to minfreq (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 minfreq
handles.prefs.features(2)=get(handles.minfreq,'value');
guidata(hObject, handles);

% --- Executes on button press in maxfreq.
function maxfreq_Callback(hObject, eventdata, handles)
% hObject    handle to maxfreq (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 maxfreq
handles.prefs.features(1)=get(handles.maxfreq,'value');
guidata(hObject, handles);

% --- Executes on button press in bandwidth.
function bandwidth_Callback(hObject, eventdata, handles)
% hObject    handle to bandwidth (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 bandwidth
handles.prefs.features(3)=get(handles.bandwidth,'value');
guidata(hObject, handles);

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

handles.prefs.features(4)=get(handles.entropy,'value');
guidata(hObject, handles);

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

handles.prefs.features(5)=get(handles.call_len,'value');
guidata(hObject, handles);

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

handles.prefs.features(6)=get(handles.peakfreq,'value');
guidata(hObject, handles);

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

handles.prefs.features(7)=get(handles.peakamp,'value');
guidata(hObject, handles);

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

handles.prefs.features(9)=get(handles.dtmin,'value');
guidata(hObject, handles);

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

handles.prefs.features(8)=get(handles.dtmax,'value');
guidata(hObject, handles);

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

handles.prefs.features(10)=get(handles.curvefit,'value');
guidata(hObject, handles);

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

handles.prefs.features(11)=get(handles.curvefit_eff,'value');
guidata(hObject, handles);

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

handles.prefs.features(12)=get(handles.perthresh,'value');
guidata(hObject, handles);

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

handles.prefs.features(13)=get(handles.power,'value');
guidata(hObject, handles);

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

handles.prefs.features(15)=get(handles.d_minfreq,'value');
guidata(hObject, handles);

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

handles.prefs.features(14)=get(handles.d_maxfreq,'value');
guidata(hObject, handles);

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

handles.prefs.features(16)=get(handles.d_bandwidth,'value');
guidata(hObject, handles);

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

handles.prefs.features(17)=get(handles.d_entropy,'value');
guidata(hObject, handles);

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

handles.prefs.features(18)=get(handles.d_call_len,'value');
guidata(hObject, handles);

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

handles.prefs.features(19)=get(handles.d_peakfreq,'value');
guidata(hObject, handles);

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

handles.prefs.features(20)=get(handles.d_peakamp,'value');
guidata(hObject, handles);

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

handles.prefs.features(22)=get(handles.d_dtmin,'value');
guidata(hObject, handles);

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

handles.prefs.features(21)=get(handles.d_dtmax,'value');
guidata(hObject, handles);

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

handles.prefs.features(23)=get(handles.d_curvefit,'value');
guidata(hObject, handles);

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

handles.prefs.features(24)=get(handles.d_curvefit_eff,'value');
guidata(hObject, handles);

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

handles.prefs.features(25)=get(handles.d_perthresh,'value');
guidata(hObject, handles);

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

handles.prefs.features(26)=get(handles.d_power,'value');
guidata(hObject, handles);

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

[filename, path]=uigetfile('*.wav');
filename=strrep(filename,'.WAV','.wav');
if isequal([filename,path],[0,0])
    return
else
    if isempty(handles.prefs.filenames)
        handles.prefs.filenames{1}=filename;
        handles.prefs.paths{1}=path;
        guidata(hObject, handles);
    else
        handles.prefs.filenames{length(handles.prefs.filenames)+1}=filename;
        handles.prefs.paths{length(handles.prefs.paths)+1}=path;
        guidata(hObject, handles);
    end
    set(handles.listbox1,'String',handles.prefs.filenames)
end


% --- Executes on button press in OK.
function OK_Callback(hObject, eventdata, handles)
% hObject    handle to OK (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.prefs.filenames)
    dial.text1='Error: No Files Selected';
    dial.butt_text='OK';
    dial.win_title='Error';
    DB=dial_box(dial);
else    
    %%%%%%%%%%%%%%
    % FIND CALLS %
    %%%%%%%%%%%%%%
    
    wave.wf=[];
    wave.spec=[];
    wave.freqint=[];
    wave.sampint=[];
    wave.fs=[];
    
    seg_data.timeint=[];
    seg_data.reg_arr=[];
    
    for n_f=1:length(handles.prefs.filenames)
        
        %disp(strcat('*** FILE: ',int2str(n_f),'(',handles.prefs.filenames{n_f},') ***'))
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        % read wave file and perform segregation and filtering %
        %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
        % delete existing seg file data
   
        no_wav=strrep(handles.prefs.filenames{n_f},'.wav','');
        if exist(strcat(handles.prefs.proj_dir,'seg_data_',no_wav,'.mat'),'file')
            delete(strcat(handles.prefs.proj_dir,'seg_data_',no_wav,'.mat'))
        end
        filename=strcat(handles.prefs.proj_dir,'seg_data_',no_wav);
        save(filename,'seg_data');
        
        % read wave file

        if exist(strcat(handles.prefs.paths{n_f},handles.prefs.filenames{n_f}))
            siz = wavread(strcat(handles.prefs.paths{n_f},handles.prefs.filenames{n_f}),'size');
        else
            dial.text1=strcat('Error: file ',handles.prefs.filenames{n_f},' not found.');
            dial.butt_text='OK';
            dial.win_title='Error';
            DB=dial_box(dial);
            return
        end
        no_shred=ceil(siz(1)/(2000000));
        shreds=round(linspace(1,siz(1)+1,no_shred+1));
        
        for n_shred=1:no_shred
            
            cd(handles.prefs.func_dir) 

            %disp('reading wave file...');
            clear spec;
            [wave.wf,wave.fs]=wavread(strcat(handles.prefs.paths{n_f},handles.prefs.filenames{n_f}),[shreds(n_shred) shreds(n_shred+1)-1]);
            
            %disp('creating spectrogram...');
            [wave.spec, wave.freqint, wave.sampint]=specgram(wave.wf);
            wave.spec=abs(wave.spec);
            
            
            progress_bar_text1=(strcat(' Shred: ',int2str(n_shred),', sample range: ',int2str(shreds(n_shred)),' - ',int2str(shreds(n_shred+1)-1),' **'));
            PB = progress_bar((n_shred-1)/no_shred , (n_f-1)/length(handles.prefs.filenames) , progress_bar_text1 , handles.prefs.filenames{n_f} , 'Finding Calls',wave.spec);
            
            
            %disp('splitting up file into calls...');
            seg_wf3(wave,no_wav,shreds(n_shred),n_shred,handles.prefs);
        end 
    end
    
    close(PB)
    
    %%%%%%%%%%%%%%%%%%%%%%
    % extract parameters %
    %%%%%%%%%%%%%%%%%%%%%%
    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];
    n_v=0;    
    for n_f=1:length(handles.prefs.filenames)
        
        no_wav=strrep(handles.prefs.filenames{n_f},'.wav','');
        filename=strcat(handles.prefs.proj_dir,'seg_data_',no_wav);  
        load(filename);
        
        handles.prefs.NoOfCalls = size(seg_data.reg_arr,1);
        guidata(hObject, handles);
        
        clear data

        PB = progress_bar(0.33 , (n_f-1)/length(handles.prefs.filenames) , 'Reading call parameters' , handles.prefs.filenames{n_f} , 'Extracting Parameters',[]);
        filtdata = [handles.prefs.threshold handles.prefs.cutoff handles.prefs.gaussLPF];
        data(1:13,:)=readdata3(handles.prefs.paths{n_f},handles.prefs.filenames{n_f},param1,filtdata,handles.prefs);
        
        

        PB = progress_bar(0.66 , (n_f-1)/length(handles.prefs.filenames) , 'Reading call difference parameters' , handles.prefs.filenames{n_f} , 'Extracting Parameters',[]);
        data(14:26,:)=readdata_diffs3(handles.prefs.paths{n_f},handles.prefs.filenames{n_f},param2,filtdata,handles.prefs);
        n_v=n_v+size(seg_data.reg_arr,1);
        
        save(strcat(handles.prefs.proj_dir,'param_data_',strrep(handles.prefs.filenames{n_f},'.wav','')),'data');   
        
    end
    
    close(PB)
    
    
    dial.text1=strcat('Process Completed. Calls found:',num2str(handles.prefs.NoOfCalls));
    dial.butt_text='OK';
    dial.win_title='Process Completed';
    DB=dial_box(dial);
end

handles.prefs.FiltSt = 1;
guidata(hObject, handles);

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

close(handles.output)
handles.prefs.features
main(handles.prefs)


% --- 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)

handles.prefs.filenames=[];
handles.prefs.paths=[];
set(handles.listbox1,'String','');
guidata(hObject, handles);
