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

% Last Modified by GUIDE v2.5 08-May-2006 11:39:36

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

% Choose default command line output for SPECT

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
handles.output = hObject;
clc; 
    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
oldhandles = varargin{1};   %gets set of parameters from interSPECT1.m

%Pass each parameter into current handles structure
handles.reps = oldhandles.reps;
handles.STFTstep = oldhandles.STFTstep;
handles.userdata = oldhandles.userdata;
handles.t = oldhandles.t;
handles.tstep = oldhandles.tstep;
handles.user_start = oldhandles.user_start;
handles.user_end = oldhandles.user_end;
handles.hannwidth = oldhandles.hannwidth;
%set(handles.hannwidth, 'String' , get(oldhandles.hannwidth, 'String'));
%handles.frequency_start = oldhandles.frequency_start;
handles.frequency_end = oldhandles.frequency_end;
handles.win_shape = oldhandles.win_shape;
handles.FourierN = oldhandles.FourierN;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
        
handles.path_entered = 0;   %For future error msg if no file path entered
str2num(get(handles.hannwidth, 'String'))


       %Clear all axes
                cla(handles.spect_axes);
                cla(handles.PSD_axes);
                cla(handles.signal_axes);


       %PLOT THE SPECTROGRAM
       
       
                winN = (str2num(get(handles.hannwidth, 'String')))*handles.tstep;            %defines number of points for construction window
                
                %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                %Create specified window
                
                if get(handles.win_shape, 'Value') == 1
                    win = hann(winN);                     %defines a hannN-point Hanning window
                elseif get(handles.win_shape, 'Value') == 2
                    win = hamming(winN);                  %defines a hannN-point Hamming window
                elseif get(handles.win_shape, 'Value') == 3
                    win = blackman(winN);                  %defines a hannN-point Blackman window
                elseif get(handles.win_shape, 'Value') == 4
                    win = chebwin(winN);                  %defines a hannN-point Chebyshev window
                elseif get(handles.win_shape, 'Value') == 5
                    win = flattopwin(winN);                  %defines a hannN-point Flat-top window
                elseif get(handles.win_shape, 'Value') == 6
                    win = gausswin(winN);                  %defines a hannN-point Gaussian window
               
                end
                
                tN = (str2num(get(handles.user_end, 'String'))-str2num(get(handles.user_start, 'String')))*handles.tstep; %number of samples in the selection
                
%                 for pp = 1:handles.reps
%                     windLFP = handles.userdata(round(((pp-1)*handles.tstep*handles.STFTstep)+1):round(((pp-1)*...
%                                 handles.tstep*handles.STFTstep)+winN)).*win;   %Multiplication of window with signal
%                     handles.STFT1(:,pp) = fft(windLFP,handles.FourierN);                                                %calculates winN-point fft
%                     handles.STFT2(:,pp) = fft(flipud(windLFP),handles.FourierN);                                                %calculates winN-point fft
%                     
%                 end
% 
%                 %Calculate the number of unique points
%                 NumUniquePts = ceil((handles.FourierN+1)/2);
%                 %FFT is symmetric, throw away second half of each FFT
%                 handles.STFT1 = handles.STFT1(1:NumUniquePts,:);
% 
% 
%                 %Take the magnitude of fft of x
%                 handles.STFT1 = abs(handles.STFT1);
%                 %Scale the fft so that it is not a function of the
%                 %length of win
%                 handles.STFT1 = handles.STFT1/length(win);
%                 
%                 %Now, take the square of the magnitude of fft of x
%                 %which has been scaled properly.
%                 %Take the square of the magnitude of fft of x.
%                 handles.STFT1 = handles.STFT1.^2; 
% 
% 
%                 
%              
%                 %FFT is symmetric, throw away second half of each FFT
%                 handles.STFT2 = handles.STFT2(1:NumUniquePts,:);
% 
% 
%                 %Take the magnitude of fft of x
%                 handles.STFT2 = abs(handles.STFT2);
%                 %Scale the fft so that it is not a function of the
%                 %length of win
%                 handles.STFT2 = handles.STFT2/length(win);
%                 
%                 %Now, take the square of the magnitude of fft of x
%                 %which has been scaled properly.
%                 %Take the square of the magnitude of fft of x.
%                 handles.STFT2 = handles.STFT2.^2; 
% 
%                 %Multiply by 2 to because you
%                 %threw out the second half of FFTX above
%                 handles.myPSD = handles.STFT1+handles.STFT2;
                
                for pp = 1:handles.reps
                    windLFP = handles.userdata(round(((pp-1)*handles.tstep*handles.STFTstep)+1):round(((pp-1)*...
                                handles.tstep*handles.STFTstep)+winN)).*win;   %Multiplication of window with signal
                    handles.STFT(:,pp) = fft(windLFP,handles.FourierN);                                                %calculates winN-point fft
 
                    
                end

                % Calculate the number of unique points
                NumUniquePts = ceil((handles.FourierN+1)/2);
                % FFT is symmetric, throw away second half of each FFT
                handles.STFT = handles.STFT(1:NumUniquePts,:);


                % Take the magnitude of fft of x
                handles.STFT = abs(handles.STFT);
                % Scale the fft so that it is not a function of the
                % length of win
                handles.STFT = handles.STFT/length(win);
                
                % Now, take the square of the magnitude of fft of x
                % which has been scaled properly.
                % Take the square of the magnitude of fft of x.
                handles.STFT = handles.STFT.^2; 

                % Multiply by 2 to because you
                % threw out the second half of FFTX above
                handles.myPSD = handles.STFT*2;
                
                
                
                %handles.myPSD = handles.STFT(1:handles.FourierN/2,:).*conj(handles.STFT(1:handles.FourierN/2,:));     %Defines spectrogram surface
                
                
                
                
                %Defines meaningful frequency axis
                handles.f = (handles.tstep*(0:handles.FourierN/2)/handles.FourierN);
                %Defines meaningful time axis
                handles.y = linspace(str2num(get(handles.user_start, 'String'))+(str2num(get(handles.hannwidth, 'String'))/2),str2num(get(handles.user_end, 'String'))...
                    -(str2num(get(handles.hannwidth, 'String'))/2), handles.reps);        
            %(floor((tN/winN)*((str2num(get(handles.hannwidth, 'String')))/handles.STFTstep))...
                    %-((str2num(get(handles.hannwidth, 'String')))/handles.STFTstep)));

if (size(handles.myPSD,1) ~= length(handles.f)) | (size(handles.myPSD,2) ~= length(handles.y))
     7 %THERE IS STILL A GLITCH IN THE GUI!!!!
        end
                    
                    
                axes(handles.spect_axes)    %Selects spectrogram axes
                surf(handles.y,handles.f,(handles.myPSD))         %Plots spectrogram
                shading interp
                axis([min(handles.y) max(handles.y) 0 str2num(get(handles.frequency_end, 'String'))])
                colormap(jet)
                colorbar
                xlabel('Time (s)')
                ylabel('Frequency (Hz)')
                view(0,90);

  
       %PLOT THE SIGNAL
                
                axes(handles.signal_axes)   %Selects signal axes
%                plot(handles.signal_axes, 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))
                plot(handles.signal_axes, handles.t(str2num(get(handles.user_start, 'String'))*handles.tstep+1:str2num(get(handles.user_end, 'String'))*handles.tstep), handles.userdata)
                axis([min(handles.y) max(handles.y) min(handles.userdata)*1.1 max(handles.userdata)*1.1]);
                xlabel('Original signal - Time (s)')
                ylabel('Voltage (mV)')
                
       %PLOT THE SPECTROGAM CROSS-SECTION        
                axes(handles.PSD_axes)    %Selects spectrogram axes
                plot(handles.myPSD(:,1),(handles.f))         %Plots PSD slice @t=0
                axis([-max(max(handles.myPSD))*0.3 max(max(handles.myPSD))*1.1 0 str2num(get(handles.frequency_end, 'String'))])
                xlabel('Power (au)')
             %   ylabel('Frequency/Hz')
       
       
       
                
                
       set(handles.zoom_slider_time, 'Enable', 'off');
       set(handles.zoom_out_time, 'Enable', 'off');
       set(handles.zoom_slider_freq, 'Enable', 'off');
       set(handles.zoom_out_freq, 'Enable', 'off');
       handles.dx = max(handles.y)-min(handles.y); %x-axis length in seconds - for use with zoom functions
       handles.dy = str2num(get(handles.frequency_end, 'String')); %y-axis length in Hz - for use with zoom functions
                
handles.output = hObject;
clc; 
    
% Update handles structure
guidata(hObject, handles);

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

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
% --- Outputs from this function are returned to the command line.
function varargout = SPECT_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 slider movement.
% function zoom_slider_time_Callback(hObject, eventdata, handles)
% % hObject    handle to zoom_slider_time (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,'Value') returns position of slider
% %        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% set(handles.spect_axes, 'xlim' ,get(handles.zoom_slider_time, 'Value')*((length(handles.userdata)/handles.tstep)-handles.dx)+[0 handles.dx]);
% 



% --- Executes during object creation, after setting all properties.
function zoom_slider_time_CreateFcn(hObject, eventdata, handles)
% hObject    handle to zoom_slider_time (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function zoom_slider_freq_Callback(hObject, eventdata, handles)
% hObject    handle to zoom_slider_freq (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,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider

%update spectrogram zoom
set(handles.spect_axes, 'ylim' ,get(handles.zoom_slider_freq, 'Value')*(str2num(get(handles.frequency_end, 'String'))-handles.dy)+[0 handles.dy]);

%update spectrogram cross-section zoom
set(handles.PSD_axes, 'ylim', get(handles.zoom_slider_freq, 'Value')*...
    (str2num(get(handles.frequency_end, 'String'))-handles.dy)+[0 handles.dy]);


% --- Executes during object creation, after setting all properties.
function zoom_slider_freq_CreateFcn(hObject, eventdata, handles)
% hObject    handle to zoom_slider_freq (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end





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


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


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


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

if handles.path_entered == 1 
if get(hObject, 'Value') == 1 %If they want to have a cursor
    jj = 1;
    while get(hObject, 'Value') == 1
        [x_coords(jj),y_coords(jj)]=ginput(1);
        if get(hObject, 'Value') == 1
            set(handles.time_coord, 'String' , num2str(x_coords(1),3));
            set(handles.frequency_coord, 'String' , num2str(y_coords(1),3));
            z = [x_coords' y_coords'];
            s = xlswrite(get(handles.file_path,'String'),z);
            jj = jj+1
        else
            break
        end
    end
end
else
     warndlg('You must enter a file path to continue',...
        'Incorrect Selection','modal')
     set(hObject, 'Value',1)
end
 
 


% Update handles structure
guidata(hObject, handles);














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


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


% --- Executes during object creation, after setting all properties.
function frequency_coord_CreateFcn(hObject, eventdata, handles)
% hObject    handle to frequency_coord (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 activate_cursor.
function activate_cursor_Callback(hObject, eventdata, handles)
% hObject    handle to activate_cursor (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%Swich off excel cursor if they want normal cursor


%get(handles.write_to_excel, 'Value') == 0

% if get(handles.write_to_excel, 'Value') ==1
%     warndlg('Switch off *Write to Excel* first',...
%         'Incorrect Selection','modal')

if get(hObject, 'Value') == 1 %If they want to have a cursor
    jj = 1;
    while get(hObject, 'Value') == 1
        [x_coords(jj),y_coords(jj)]=ginput(1);
        if get(hObject, 'Value') == 1
            %Display the selected coordinates in boxes
            set(handles.time_coord, 'String' , num2str(x_coords(1),4));
            set(handles.frequency_coord, 'String' , num2str(y_coords(1),4));

            %Update PSD plot
            indPSD = round((x_coords(1)-min(handles.y))/(max(handles.y)-min(handles.y))*handles.reps);

            %messy correction code to ensure indPSD is never less than 1 or
            %greater than handles.reps
            if indPSD < 1
                indPSD = 1;
            elseif indPSD > handles.reps
                endPSD = handles.reps
            end

            axes(handles.PSD_axes)    %Selects cross-section axes
            plot(handles.myPSD(:,indPSD),(handles.f))         %Plots PSD slice @t=selected t
            xlim([-max(max(handles.myPSD))*0.3 max(max(handles.myPSD))*1.1])
            %update spectrogram cross-section zoom
            ylim(get(handles.zoom_slider_freq, 'Value')*...
              (str2num(get(handles.frequency_end, 'String'))-handles.dy)+[0 handles.dy]);
            peak = handles.f(find(handles.myPSD(:,indPSD) == max(handles.myPSD(:,indPSD))))
            half = 0.5*x_coords(1)
            xlabel('Power (au)')
            ylabel('Frequency (Hz)')

        else
            break
        end
    end
end

 
 


% Update handles structure
guidata(hObject, handles);




function file_path_Callback(hObject, eventdata, handles)
% hObject    handle to file_path (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 file_path as text
%        str2double(get(hObject,'String')) returns contents of file_path as a double
%get(hObject,'String')
handles.path_entered = 1


% Update handles structure
guidata(hObject, handles)


% --- Executes during object creation, after setting all properties.
function file_path_CreateFcn(hObject, eventdata, handles)
% hObject    handle to file_path (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 zoom_in_time.
function zoom_in_time_Callback(hObject, eventdata, handles)
% hObject    handle to zoom_in_time (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

handles.dx=(handles.dx)*0.5;
if(isequal(strfind(get(handles.zoom_slider_time, 'Enable'), 'on'),[])==1)
    set(handles.zoom_slider_time, 'Enable', 'on');
    set(handles.zoom_slider_time, 'Value', 0.5);
    set(handles.zoom_out_time, 'Enable', 'on');
end

%Update spectrogram zoom
set(handles.spect_axes, 'xlim' ,get(handles.zoom_slider_time, 'Value')*...
((max(handles.y)-min(handles.y))-handles.dx)+min(handles.y)+[0 handles.dx]);

%Update signal zoom
set(handles.signal_axes, 'xlim' ,get(handles.zoom_slider_time, 'Value')*...
((max(handles.y)-min(handles.y))-handles.dx)+min(handles.y)+[0 handles.dx]);

%adjust the slider step size [buttonclik percentage trough-click
%percentage]



set(handles.zoom_slider_time, 'SliderStep', 0.75.*get(handles.zoom_slider_time, 'SliderStep'));


guidata(hObject, handles);





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

handles.dx=(handles.dx)*2;
if(handles.dx==(max(handles.y)-min(handles.y)))
    set(handles.zoom_slider_time, 'Enable', 'off');
    set(handles.zoom_out_time, 'Enable', 'off');
end

%Update spectrogram zoom
set(handles.spect_axes, 'xlim' ,get(handles.zoom_slider_time, 'Value')*...
((max(handles.y)-min(handles.y))-handles.dx)+min(handles.y)+[0 handles.dx]);

%Update signal zoom
set(handles.signal_axes, 'xlim' ,get(handles.zoom_slider_time, 'Value')*...
((max(handles.y)-min(handles.y))-handles.dx)+min(handles.y)+[0 handles.dx]);

%adjust the slider step size [buttonclik percentage trough-click
%percentage]
set(handles.zoom_slider_time, 'SliderStep', (1/.75).*get(handles.zoom_slider_time, 'SliderStep'));


guidata(hObject, handles);




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

handles.dy=(handles.dy)*2;
if(handles.dy==str2num(get(handles.frequency_end, 'String')))
    set(handles.zoom_slider_freq, 'Enable', 'off');
    set(handles.zoom_out_freq, 'Enable', 'off');
end

%update spectrogram zoom
set(handles.spect_axes, 'ylim' ,get(handles.zoom_slider_freq, 'Value')*(str2num(get(handles.frequency_end, 'String'))-handles.dy)+[0 handles.dy]);

%update spectrogram cross-section zoom
set(handles.PSD_axes, 'ylim', get(handles.zoom_slider_freq, 'Value')*...
    (str2num(get(handles.frequency_end, 'String'))-handles.dy)+[0 handles.dy]);

%adjust the slider step size [buttonclik percentage trough-click
%percentage]
set(handles.zoom_slider_freq, 'SliderStep', (1/.75).*get(handles.zoom_slider_freq, 'SliderStep'));


guidata(hObject, handles);




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

handles.dy=(handles.dy)*0.5;
if(isequal(strfind(get(handles.zoom_slider_freq, 'Enable'), 'on'),[])==1)
    set(handles.zoom_slider_freq, 'Enable', 'on');
    set(handles.zoom_slider_freq, 'Value', 0.5);
    set(handles.zoom_out_freq, 'Enable', 'on');
end

%update spectrogram zoom
set(handles.spect_axes, 'ylim' ,get(handles.zoom_slider_freq, 'Value')*...
   (str2num(get(handles.frequency_end, 'String'))-handles.dy)+[0 handles.dy]);

%update spectrogram cross-section zoom
set(handles.PSD_axes, 'ylim', get(handles.zoom_slider_freq, 'Value')*...
    (str2num(get(handles.frequency_end, 'String'))-handles.dy)+[0 handles.dy]);

%adjust the slider step size [buttonclik percentage trough-click
%percentage]

set(handles.zoom_slider_freq, 'SliderStep', 0.75.*get(handles.zoom_slider_freq, 'SliderStep'));


guidata(hObject, handles);





% --- Executes on slider movement.
function zoom_slider_time_Callback(hObject, eventdata, handles)

%Update spectrogram zoom
set(handles.spect_axes, 'xlim' ,get(handles.zoom_slider_time, 'Value')*...
((max(handles.y)-min(handles.y))-handles.dx)+min(handles.y)+[0 handles.dx]);

%Update signal zoom
set(handles.signal_axes, 'xlim' ,get(handles.zoom_slider_time, 'Value')*...
((max(handles.y)-min(handles.y))-handles.dx)+min(handles.y)+[0 handles.dx]);





% 
% % --- Executes on button press in reset_axes.
% function reset_axes_Callback(hObject, eventdata, handles)
% % hObject    handle to reset_axes (see GCBO)
% % eventdata  reserved - to be defined in a future version of MATLAB
% % handles    structure with handles and user data (see GUIDATA)
% 
% axes(handles.spect_axes)    %Selects spectrogram axes
% axis([str2num(get(handles.user_start, 'String')) str2num(get(handles.user_end, 'String')) 0 str2num(get(handles.frequency_end, 'String'))])





% --------------------------------------------------------------------
function print_prev_Callback(hObject, eventdata, handles)
% hObject    handle to print_prev (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%printdlg(gcbf);
%Printable(handles)

if get(handles.incl_spect, 'Value')==0 && get(handles.incl_signal, 'Value')==0
     warndlg('Select the required plots',...
        'Incorrect Selection','modal')
    
elseif get(handles.incl_spect, 'Value') == 1 && get(handles.incl_signal, 'Value') == 0
figure(1)
                surf(handles.y,handles.f,(handles.myPSD))         %Plots spectrogram
                shading interp
                axis([min(handles.y) max(handles.y) 0 str2num(get(handles.frequency_end, 'String'))])
                colormap(jet)
                colorbar
                view(0,90);

elseif get(handles.incl_spect, 'Value') == 0 && get(handles.incl_signal, 'Value') == 1
    
figure(1)

                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))
                axis([min(handles.y) max(handles.y) min(handles.userdata)*1.1 max(handles.userdata)*1.1]);
    
elseif get(handles.incl_spect, 'Value') == 1 && get(handles.incl_signal, 'Value') == 1
figure(1)
subplot('position',[0.2 0.27 0.6 0.65])
                surf(handles.y,handles.f,(handles.myPSD))         %Plots spectrogram
                shading interp
                axis([min(handles.y) max(handles.y) 0 str2num(get(handles.frequency_end, 'String'))])
                colormap(jet)
                colorbar
                view(0,90);
subplot('position',[0.2 0.06 0.505 0.15])
                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))
                axis([min(handles.y) max(handles.y) min(handles.userdata)*1.1 max(handles.userdata)*1.1]);
end




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


