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

% Last Modified by GUIDE v2.5 08-May-2006 14:32:56

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

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



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
oldhandles = varargin{1};   %gets set of parameters from interSPECT.m
% 
% %Pass each parameter into current handles structure
 handles.userdata = oldhandles.userdata;
 handles.t = oldhandles.t;
 handles.tstep = oldhandles.tstep;
 set(handles.user_start, 'String',  get(oldhandles.user_start, 'String'));    %handles.user_start = oldhandles.user_start;
 set(handles.user_end, 'String',  get(oldhandles.user_end, 'String'));      %handles.user_end = oldhandles.user_end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%str2num(get(handles.user_end, 'String'))


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

%Generate a sythetic signal
%t1 = 0;
%t2 = 10;
        cla(handles.user_selection_plot);
        
%        handles.tstep = 1000;   %defines number of samples per real time second
%        handles.t = linspace(0,t2,handles.tstep*t2);
        %userdata = 3*sin(3*t)+0.5*cos(45*t)+sin(t)+2*sin(5*t)+6*sin(t).*sin(400*t);
      %  userdata = (sin(exp(handles.t).*2*pi.*10)+sin(handles.t.*2*pi*80)./4);
      
       % set(handles.user_start, 'String', num2str(t1));
        %set(handles.user_end, 'String', num2str(t2));
      %  handles.userdata = userdata;
        
        update_plot(handles);
        

        %WANTED??????????????????????   (To Set default values of parameters)
%         goahead = questdlg('Revert to default parameters?','Default','Yes','No ','Yes')
%         if goahead == 'Yes'
%                 %Set default values of parameters
%                 set(handles.win_shape, 'Value', 1);
%                 set(handles.hannwidth, 'String', 3);
%                 set(handles.step_size, 'String', 50);
%                 set(handles.frequency_start, 'String', 0);
%                 set(handles.frequency_end, 'String', 200);
%                 set(handles.user_start, 'String', t1);
%                 set(handles.user_end, 'String', t2);
%         end
%         

        
        
        %Make "Create Spectrogram" button inactive
        set(handles.create_spectrogram, 'Enable', 'off')
        set(handles.fouri_epon, 'Value',3)
        handles.STFTstep = 0.05;    %Set default step length
        handles.FourierN = 2048;    %Set default fft order 512 = 2^9)
        
        
        guidata(hObject,handles);
        
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = interSPECT_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;




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


    if(str2num(get(handles.hannwidth, 'String'))>(str2num(get(handles.user_end, 'String'))-str2num(get(handles.user_start, 'String'))))
        errordlg('The window length must be less than the duration of the selected signal',...
        'Incorrect Selection','modal')
    set(handles.hannwidth, 'String' ,num2str((str2num(get(handles.user_end, 'String'))-str2num(get(handles.user_start, 'String')))-0.1 ,3))
    end


    

update_plot(handles);
   
guidata(hObject,handles);




% --- Executes during object creation, after setting all properties.
function user_start_CreateFcn(hObject, eventdata, handles)
% hObject    handle to user_start (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 user_end_Callback(hObject, eventdata, handles)
% hObject    handle to user_end (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 user_end as text
%        str2double(get(hObject,'String')) returns contents of user_end as a double
       
    if(str2num(get(handles.hannwidth, 'String'))>(str2num(get(handles.user_end, 'String'))-str2num(get(handles.user_start, 'String'))))
        errordlg('The window length must be less than the duration of the selected signal',...
        'Incorrect Selection','modal')
    set(handles.hannwidth, 'String' ,num2str((str2num(get(handles.user_end, 'String'))-str2num(get(handles.user_start, 'String')))-0.1 ,3))
    end



update_plot(handles);
   
        guidata(hObject,handles);



% --- Executes during object creation, after setting all properties.
function user_end_CreateFcn(hObject, eventdata, handles)
% hObject    handle to user_end (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 hannwidth_Callback(hObject, eventdata, handles)
% hObject    handle to hannwidth (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 hannwidth as text
%        str2double(get(hObject,'String')) returns contents of hannwidth as a double
 
    %Warning if window length is longer than selected signal
    if(str2num(get(handles.hannwidth, 'String'))>=(str2num(get(handles.user_end, 'String'))-str2num(get(handles.user_start, 'String'))-handles.STFTstep))
        errordlg('The window length must be less than the duration of the selected signal',...
        'Incorrect Selection','modal')
    set(handles.hannwidth, 'String' ,num2str((str2num(get(handles.user_end, 'String'))-str2num(get(handles.user_start, 'String')))*0.5 ,3))

  
    
    
    %CHECK THIS!!!!!!!!!!!!!!!!!!!
    %     elseif str2num(get(handles.hannwidth,'String')) > str2num(get(handles.hannwidth,'String'))     
%     errordlg('Step size must be no greater than window length',...
%         'Incorrect Selection','modal')
%     set(handles.step_size, 'String' ,(num2str((str2num(get(handles.hannwidth, 'String'))) ,3)));
    end

        %Make "Create Spectrogram" button inactive
        set(handles.create_spectrogram, 'Enable', 'off')
    
    

% --- Executes during object creation, after setting all properties.
function hannwidth_CreateFcn(hObject, eventdata, handles)
% hObject    handle to hannwidth (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 step_size_Callback(hObject, eventdata, handles)
% hObject    handle to step_size (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 step_size as text
%        str2double(get(hObject,'String')) returns contents of step_size as a double

if str2num(get(hObject,'String')) > str2num(get(handles.hannwidth,'String'))     
    errordlg('Step size must be no greater than window length',...
        'Incorrect Selection','modal')
    set(handles.step_size, 'String' ,(num2str((str2num(get(handles.hannwidth, 'String'))) ,3)));
elseif str2num(get(hObject,'String'))>=((str2num(get(handles.user_end, 'String'))-str2num(get(handles.user_start, 'String')))-str2num(get(handles.hannwidth, 'String')))
    errordlg('Window length and step size too long for selected signal ',...
        'Incorrect Selection','modal')
    set(handles.step_size, 'String' ,(num2str(((str2num(get(handles.user_end, 'String'))-str2num(get(handles.user_start, 'String')))-str2num(get(handles.hannwidth, 'String')))*0.5 ,3)));
end
    
    %Make "Create Spectrogram" button inactive
    set(handles.create_spectrogram, 'Enable', 'off')
    
guidata(hObject,handles);
    
% --- Executes during object creation, after setting all properties.
function step_size_CreateFcn(hObject, eventdata, handles)
% hObject    handle to step_size (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

% 
% DISABLED
% function frequency_start_Callback(hObject, eventdata, handles)
% % hObject    handle to frequency_start (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 frequency_start as text
% %        str2double(get(hObject,'String')) returns contents of frequency_start as a double
%     
%     %Make "Create Spectrogram" button inactive
%     set(handles.create_spectrogram, 'Enable', 'off')
% 
% 
% % --- Executes during object creation, after setting all properties.
% function frequency_start_CreateFcn(hObject, eventdata, handles)
% % hObject    handle to frequency_start (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








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

% 
% if(get(handles.estimate_time, 'Value')==1)
% 
% else
% (str2num(get(handles.hannwidth, 'String')))

set(handles.user_end, 'String', str2num(get(handles.user_start, 'String'))+str2num(get(handles.hannwidth, 'String'))+(handles.STFTstep*(handles.reps-1)));  %+str2num(get(handles.hannwidth, 'String'))+

handles.userdata = handles.userdata(floor(str2num(get(handles.user_start, 'String'))*handles.tstep)+1:floor(str2num(get(handles.user_end, 'String'))*handles.tstep));
op= length(handles.userdata);



SPECT(handles)




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


%handles.STFTstep = 1/(str2num(get(handles.step_size, 'String')))*(str2num(get(handles.hannwidth, 'String')));
handles.STFTstep = str2num(get(handles.step_size, 'String'));

%Time per replication based on a quick test using a 512-point fft
%handles.reps = floor((((str2num(get(handles.user_end, 'String')))-(str2num(get(handles.user_start, 'String'))))...
  %                  -(str2num(get(handles.hannwidth, 'String'))))/handles.STFTstep);
handles.reps = floor((((str2num(get(handles.user_end, 'String')))-(str2num(get(handles.user_start, 'String'))))...
                     -(str2num(get(handles.hannwidth,'String'))))/handles.STFTstep)+1;

                    
%Time per replication based on a quick test using a 512-point fft
t_est = 0.005*handles.reps*(2^(log2(handles.FourierN)-9));
set(handles.time_estimate, 'String' ,num2str(t_est,3));

  if(str2num(get(handles.time_estimate, 'String'))>10)
        goahead = questdlg('Over 10 seconds...Proceed?','WARNING','Yes','No ','Yes')
        if goahead == 'Yes'
            %Make "Create Spectrogram" button active
            set(handles.create_spectrogram, 'Enable', 'on')
        else
            %Make "Create Spectrogram" button inactive
            set(handles.create_spectrogram, 'Enable', 'off')
        end
  else
             %Make "Create Spectrogram" button active
            set(handles.create_spectrogram, 'Enable', 'on')
  end





guidata(hObject,handles);

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




% --- Executes during object creation, after setting all properties.
function time_estimate_CreateFcn(hObject, eventdata, handles)
% hObject    handle to time_estimate (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


% --- Executes on button press in no_selection.
function handles = no_selection_Callback(hObject, eventdata, handles)
% hObject    handle to no_selection (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 no_selection
	
    %Make "Create Spectrogram" button inactive
    set(handles.create_spectrogram, 'Enable', 'off')


    if(get(handles.no_selection, 'Value')==1) %%if its checked
        set(handles.user_start, 'String', num2str(0));
        set(handles.user_end, 'String', num2str(length(handles.userdata)/handles.tstep));
        set(handles.user_end, 'Enable', 'off')
        set(handles.user_start, 'Enable', 'off')
    else
        set(handles.user_end, 'Enable', 'on')
        set(handles.user_start, 'Enable', 'on')
    end

        plot(handles.user_selection_plot, handles.t(str2num(get(handles.user_start, 'String'))*handles.tstep+1:str2num(get(handles.user_end, 'String'))*handles.tstep), handles.userdata(str2num(get(handles.user_start, 'String'))*handles.tstep+1:str2num(get(handles.user_end, 'String'))*handles.tstep))
        xlim(handles.user_selection_plot, [0 str2num(get(handles.user_end, 'String'))]);
        set(gcf,'doublebuffer','on');


       
function update_plot(handles)
       
        %Make "Create Spectrogram" button inactive
        set(handles.create_spectrogram, 'Enable', 'off')


        plot(handles.user_selection_plot, handles.t(round(str2num(get(handles.user_start, 'String'))*handles.tstep)+1:round(str2num(get(handles.user_end, 'String'))*handles.tstep)), handles.userdata(round(str2num(get(handles.user_start, 'String'))*handles.tstep)+1:round(str2num(get(handles.user_end, 'String'))*handles.tstep)))
        xlabel(handles.user_selection_plot,'Time (s)')
        ylabel(handles.user_selection_plot,'Signal (mV)')
        xlim(handles.user_selection_plot, [str2num(get(handles.user_start, 'String')) str2num(get(handles.user_end, 'String'))]);
        set(gcf,'doublebuffer','on');

        
% --- Executes on selection change in win_shape.
function win_shape_Callback(hObject, eventdata, handles)
% hObject    handle to win_shape (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 win_shape contents as cell array
%        contents{get(hObject,'Value')} returns selected item from win_shape





% --- Executes during object creation, after setting all properties.
function win_shape_CreateFcn(hObject, eventdata, handles)
% hObject    handle to win_shape (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





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

if str2num(get(hObject,'String'))>250
             set(hObject, 'String' ,250);
end
    

%     %Warning if frequency_start is greater than frequency_end
%     if(str2num(get(handles.frequency_start, 'String'))>=(str2num(get(handles.frequency_end, 'String'))))
%         errordlg('The upper frequency limit must be greater than the lower frequency limit',...
%         'Incorrect Selection','modal')
%     set(handles.frequency_end, 'String' ,(num2str((str2num(get(handles.frequency_start, 'String'))+1) ,3)));
%     end



% --- Executes during object creation, after setting all properties.
function frequency_end_CreateFcn(hObject, eventdata, handles)
% hObject    handle to frequency_end (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






% --- Executes on selection change in fouri_epon.
% USER selects no. of points they want for FFT
function fouri_epon_Callback(hObject, eventdata, handles)
% hObject    handle to fouri_epon (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 fouri_epon contents as cell array
%        contents{get(hObject,'Value')} returns selected item from fouri_epon

handles.FourierN = 2^(get(handles.fouri_epon, 'Value')+8);

%Make "Create Spectrogram" button inactive
set(handles.create_spectrogram, 'Enable', 'off')

guidata(hObject,handles);

% --- Executes during object creation, after setting all properties.
function fouri_epon_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fouri_epon (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


