function varargout = PSD(varargin)
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
%     G U I   H E L P   I N F O R M A T I O N                                   % 
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
% %help File for PSD. Fig                                                       % 
%                                                                               % 
%       The GUI is designed to offer the ability to plot the Power Spectral     % 
%       Density and Power Spectrum. In addition it also calculates the          %
%       power in each of the standard and user-defined frequency bands.         %
%       It is not called from the Matlab Workspace, but instead from            %
%       interPSD.m, part of the LFP Analyser Package.                           %
%                                                                               % 
%       The Power in each band is calculated using Matlab's avgpower            %
%       function in the signal processing toolbox. this is performed in the     % 
%       function 'PopulatePowData'.                                             %
%       The Power Spectrum is calculated using a mean-square method, not        %
%       using the PSD information. The phase information is also                %
%       calculated from the FFT of the data, not by the specified PSD           %
%       method. No Window is used in calculating the Power Spectrum or          %
%       Phase information.                                                      %
%                                                                               % 
%       The Plot can be controlled using the option in the Panels to the        %
%       Right of the plot as well as the zoom buttons and sliders located       %
%       around the axes.                                                        %
%                                                                               % 
%       The selected plot can be exported to a new figure for publishing        %
%       by clicking the 'Figure' Button in the 'Export' Panel.                  %
%       The points from the selected plot can be exported to Excel by           %
%       clicking the 'Datapoints' Button in the 'Export' Panel.                 %
%                                                                               % 
%       The Power information can be exported to excel by clicking the          %
%       'Values' button in the 'Export' Panel.                                  %
%                                                                               % 
%       A histogram showing the power in each band in mV^2 as well as a         %
%       percentage of the total power can also be obtained by first             %
%       selecting the bands to be viewed by clicking the 'select' checkbox      %          
%       in the correct panel and then clicking the 'Histogram' button in        %
%       the 'Export' panel.                                                     %
%                                                                               % 
%       The selected plot can be exported to a new figure for publishing        %  
%       by clicking the 'Figure' Button in the 'Export' Panel.                  %
%       The selected plot can be exported to a new figure for publishing        %
%       by clicking the 'Figure' Button in the 'Export' Panel.                  %
%                                                                               % 
%       As its input arguments, PSD takes the handles structure from            %
%       interPSD.m and within its PSD_OpeningFcn, it assigns several of         %
%       interPSD's handles fields to fields in its own handles structure        %
%                                                                               % 
%       Within the Algorithm, the data plotted on the axes is stored in         %
%       handles.data                                                            %
%                                                                               % 
%       handles.dx & handles.dy determine how much of the data should be        %
%       visible. This allows the data to be zoomed in and out.                  %
%                                                                               % 
%       The Power data in the edit boxes at the bottom of the screen do         %
%       not alter once the GUI is made visible.                                 %
%                                                                               %       
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %     

%  This Software is distributed under the GNU General Public Liscence           % 
%  a copy of this liscence sould be accompany this software and can be viewed   %
%  in 'Help'->'Liscence Information'.                                           %
%                                                                               % 
% Copyright (C) 2006  Richard Peter                                             %
%                                                                               %  
% This program is free software; you can redistribute it and/or modify          %
% it under the terms of the GNU General Public License as published by          %
% the Free Software Foundation; either version 2 of the License, or             %
% (at your option) any later version.                                           %
%                                                                               % 
% This program is distributed in the hope that it will be useful,               %
% but WITHOUT ANY WARRANTY; without even the implied warranty of                %
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 %
% GNU General Public License for more details.                                  %
%                                                                               %  
% You should have received a copy of the GNU General Public License             %
% along with this program; if not, write to the Free Software                   %
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA %
%                                                                               % 
%                                                                               % 
%  For further information contact Richard Peter at                             % 
%  infinitybeckon@hotmail.com                                                   % 
%                                                                               %
%                                                                               %   
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @PSD_OpeningFcn, ...
                   'gui_OutputFcn',  @PSD_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 PSD is made visible.
function PSD_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 PSD (see VARARGIN)

% Choose default command line output for PSD
handles.output = hObject;
%% Initialisation Code
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Initialisation Code Goes Here...

%data information
handles.tstep = varargin{1}.tstep;                  %sampling freq
handles.userdata = varargin{1}.userdata;            %user's data
handles.CB = get(varargin{1}.CustomBndTgl, 'Value');%Custom Bands Boolean
handles.custombands = varargin{1}.CustomBands;      %user-defined freq bands

%PSD informaiton
handles.Pxx = varargin{1}.Pxx;                  %PSD y-vals
handles.f = varargin{1}.f;                      %PSD x-vals
handles.method = varargin{1}.method;            %PSD method used
handles.Hpsd = varargin{1}.Hpsd;                %for getting average data 
handles.FreqAx = varargin{1}.FreqAx;
                            %If it was the multitaper method get the error
if handles.method == 3
    handles.Pxxc = varargin{1}.Pxxc;
end

RefreshWindow(handles);                     %Reset the Buttons to default
handles = UpdatePlot(handles);                        %Now update the plot
handles.dx = max(handles.f);
handles.fx = handles.f;
handles.dy = max(handles.data)+0.1*max(handles.data);

handles.NumFigs = 1;                            % Counter to number figures
PopulatePowData(handles);
clear('handles.Hpsd');                            %Delete to save memory
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Update handles structure
guidata(hObject, handles);

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

% --- Outputs from this function are returned to the command line.
function varargout = PSD_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 DeltaPcPow_Callback(hObject, eventdata, handles)
% hObject    handle to DeltaPcPow (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 DeltaPcPow as text
%        str2double(get(hObject,'String')) returns contents of DeltaPcPow as a double


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


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


% --- Executes on button press in GetHistogram. 
% --- The user wants a histogram
function GetHistogram_Callback(hObject, eventdata, handles)
Db = [get(handles.DeltaSel, 'Value'), get(handles.ThetaSel, 'Value'), ...
    get(handles.AlphSel, 'Value'), get(handles.BetSel, 'Value'), get(handles.GamLSel, 'Value')...
    get(handles.GamHSel, 'Value'), get(handles.GamVHSel, 'Value'), get(handles.Cust1Sel, 'Value')...
    get(handles.Cust2Sel, 'Value'), get(handles.Cust3Sel, 'Value')];
GetThese= find(Db ==1);
Pows = [str2num(get(handles.DeltaPow, 'String')), str2num(get(handles.ThetaPow, 'String')), ...
    str2num(get(handles.AlphaPow, 'String')), str2num(get(handles.BetaPow, 'String')), str2num(get(handles.GamLPow, 'String'))...
    str2num(get(handles.GamHPow, 'String')), str2num(get(handles.GamVHPow, 'String')), str2num(get(handles.Cust1Pow, 'String'))...
    str2num(get(handles.Cust2Pow, 'String')), str2num(get(handles.Cust3Pow, 'String'))];
PcPows = [str2num(get(handles.DeltaPcPow, 'String')), str2num(get(handles.ThetaPcPow, 'String')), ...
    str2num(get(handles.AlphaPcPow, 'String')), str2num(get(handles.BetaPcPow, 'String')), str2num(get(handles.GamLPcPow, 'String'))...
    str2num(get(handles.GamHPcPow, 'String')), str2num(get(handles.GamVHPcPow, 'String')), str2num(get(handles.Cust1PcPow, 'String'))...
    str2num(get(handles.Cust2PcPow, 'String')), str2num(get(handles.Cust3PcPow, 'String'))];
m = length(GetThese);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Labels = {'1-4', '4-8', '8-14', '14-30'...
    '30-50', '50-100', '100-150'};
if handles.CB
    %If there are user-defined bands
    Cb =handles.custombands;
    if(length(Cb)>0)
        Labels{8} = strcat(num2str(Cb(1)), ' - ', num2str(Cb(2)));
    end
    if(length(Cb)>2)
        Labels{9} = strcat(num2str(Cb(3)), ' - ', num2str(Cb(4)));
    end
    if(length(Cb)>4)
        Labels{10} = strcat(num2str(Cb(5)), ' - ', num2str(Cb(6)));
    end
end


if(m ==0) 
    %The user has not selected any Bands!
    errordlg('You must select at least one frequency band',...
            'Incorrect Selection','modal')
    return
end

fighandle = figure(handles.NumFigs+1);
handles.NumFigs=handles.NumFigs+1;
h = newplot(fighandle);

subplot(2,1,1);
bar(Pows(GetThese));
set(gca,'XTickLabel',Labels(GetThese))
title('Power mV^2');
%ylim(gca, [0 max(Pows(GetThese))+0.1*max(Pows(GetThese))]);

subplot(2, 1, 2)
bar(PcPows(GetThese))
set(gca,'XTickLabel',Labels(GetThese))
title('Percentage of Total Power');
% ylim(gca, [0 max(PcPows(GetThese))+0.1*max(PcPows(GetThese))]);

                
% Update handles structure
guidata(hObject, handles);

%EoF

% --- Executes on button press in GetFigure.
%Puts the axes into its own figure...
function GetFigure_Callback(hObject, eventdata, handles)

fighandle       = figure(handles.NumFigs+1);
handles.NumFigs =handles.NumFigs+1;
h               = newplot(fighandle);

logx = get(handles.LogXAx, 'Value');
logy = get(handles.LogYAx, 'Value');
if get(handles.PlotPSDTgl, 'Value')
    %If the user wants  PSD plotting

    %Next plot the figure on the chosen axes type
    handles.data = handles.Pxx;
    if  logx && logy
        loglog(h, handles.fx, handles.data)
    elseif logx && ~logy
        semilogx(h, handles.fx, handles.data)
    elseif ~logx && logy
        semilogy(h, handles.fx, handles.data)
    else
        plot(h, handles.fx, handles.data)
    end
    
    xmin = str2num(get(handles.XAxStart, 'String'));  % Get the minimum xlim
    xmax = str2num(get(handles.XAxEnd, 'String'));    %Get the max xlim
%     xmin = get(handles.XSlider, 'Value')*...
%         (handles.tstep/2)-handles.dx;   % Get the minimum xlim
%     xmax = xmin+dx;                     % add on the view length
    xlim(h, [xmin xmax]);                  % set the limits
    
    ylimit = max(handles.data)+0.1*max(handles.data);%the maximum y-axis value 
    ymin = 0;
%     ymin = get(handles.YSlider, 'Value')*(ylim-handles.dy); % the minimum displayed
    ymax = ylimit;                        % (same as ylim)
    ylim(h, [ymin ymax]);                  % Set the x lims
%     title(h, 'Power Spectral Density');    
%     xlabel(h, 'frequency (Hz)');
%     ylabel(h, 'Power (dB/Hz)');
    
    handles.fx = handles.f;
%max(handles.data)+0.1*max(handles.data);
    
    set(handles.XSlider, 'Enable', 'off');
    set(handles.XZoomOut, 'Enable', 'off');
    set(handles.YSlider, 'Enable', 'off');
    set(handles.YZoomOut, 'Enable', 'off');
    
elseif get(handles.PlotPowSpecTgl, 'Value')
    %The user wants a Power Spectrum...
    user_sel_length =length(handles.userdata);      %length of data
    handles.N = 2^(nextpow2(user_sel_length));      %Number of points in FFT
    F=fft(handles.userdata, handles.N);             %FFT the data
    handles.P=F.*conj(F);
    % Scale the fft so that it is not a function of the length of the
    % user's data.
    handles.P = handles.P/(user_sel_length^2);
    % times by 2 because we discarded the second half, where half the
    % power was!
    handles.P = handles.P*2;

    % DC Component should be unique.
    handles.P(1) = handles.P(1)/2;
    % Nyquist component should also be unique.
    if ~rem(handles.P,2)
        % Here handles.P is even; therefore,Nyquist point is included so
        % divide it by 2 also
        handles.P(end) = handles.P(end)/2;;
    end
    handles.data = handles.P(1:floor(handles.N)/2);
    %Now create a vector for the x-axis of the Power spectrum
    f = 1000*(0 :(handles.N)/2-1 )/(handles.N);
    cla(h);
    %Next plot the figure on the chosen axes type
    if  logx && logy
        loglog(h, f, handles.data)
    elseif logx && ~logy
        semilogx(h, f, handles.data)
    elseif ~logx && logy
        semilogy(h, f, handles.data)
    else
        handles.data = handles.data;
        plot(h, f, handles.data)
    end
    xlabel(h,'frequency (Hz)')
    ylabel(h,'signal(mV^2)')
    
    xmin = str2num(get(handles.XAxStart, 'String'));                  % set the x limits
    xmax = str2num(get(handles.XAxEnd, 'String'));                    
    xlim(h, [xmin xmax]);                        
    
    ylimit = max(handles.P)+0.1*max(handles.P);         % Set the y lims
    ymin = 0;
    ylim(h, [ymin ylimit]);                      
    
else
        %The user wants the Phase plot...
    user_sel_length =length(handles.userdata);      %length of data
    handles.N = 2^(nextpow2(user_sel_length));      %Number of points in FFT
    F=fft(handles.userdata, handles.N);             %FFT the data
    %Get the phase of this data
    handles.data = unwrap(angle(F));
    
    %Comment out the following line to plot phase in radians
    handles.data= handles.data/pi*180;
    
    %Now create a vector for the x-axis of the Power spectrum
        %Remember: there will be an even number of fft points so the nyquist
        %frequency is included in the FFT
    f = handles.tstep*(0 :handles.N-1)/(handles.N-1);
    cla(h);
    %Next plot the figure on the chosen axes type
    if  logx && logy
        loglog(h, f, handles.data)
    elseif logx && ~logy
        semilogx(h, f, handles.data)
    elseif ~logx && logy
        semilogy(h, f, handles.data)
    else
        plot(h, f, handles.data)
    end
    xmin = str2num(get(handles.XAxStart, 'String'));                  % set the x limits
    xmax = str2num(get(handles.XAxEnd, 'String'));                    
    xlim(handles.axes, [xmin xmax]);    
    ylimit = max(handles.data)+0.1*abs(max(handles.data));         % Set the y lims
    ymin = min(handles.data)-abs(0.1*min(handles.data));
    ylim(handles.axes, [ymin ylimit]);     
    
set(h, 'xlim' ,get(handles.XSlider, 'Value')*...
   (str2num(get(handles.XAxEnd, 'String'))-handles.dx)+[0 handles.dx]);
    
ylimstart = min(handles.data)-abs(min(handles.data)*0.1);
yrange = max(handles.data)-min(handles.data);
set(h, 'ylim' ,get(handles.YSlider, 'Value')*...
   ((yrange)+0.1*(yrange)-handles.dy)+[ylimstart ylimstart+handles.dy]);                   

end

if get(handles.PLotFreqBandLims, 'Value')
    %if the user wants markers for the frequency band limits
    bandlims = [1 4 8 14 30 50 100 150];
    if ~handles.FreqAx
        bandlims = bandlims/handles.tstep;      %if they want it normalised
    end                                         %What if its two-sided!!!!?
    ii = [];
    axes(h);
    for ii = 2:2:16
        X = [bandlims(ii/2) bandlims(ii/2)];
        Y = [ymin ylimit];
        line(X, Y, 'Color', 'r');
    end
    if handles.CB 
        for ii = 2:2:length(handles.custombands)*2
            X = [handles.custombands(ii/2) handles.custombands(ii/2)];
            Y = [ymin ylimit];
            line(X, Y, 'Color', 'g');
        end
        
    end
end
if get(handles.PlotPSDTgl, 'Value')
    title(h, 'Power Spectral Density')
    if handles.FreqAx
        xlabel(handles.axes, 'Frequency (Hz)');
        ylabel(handles.axes, 'Power (dB/Hz)');
    else
        xlabel(handles.axes, 'Radians/Sample');
        ylabel(handles.axes, 'Power (dB/Radians/Sample)');
    end
elseif get(handles.PlotPowSpecTgl, 'Value')
	title(h, 'Power Spectrum')
    xlabel(h,'frequency (Hz)')
    ylabel(h,'signal(mV^2)')
else
    title(h, 'Phase Spectrum')
    xlabel(h, 'Frequency Hz')
    %Comment as appropriate:
    ylabel(handles.axes, 'Phase (Deg.)');
    %ylabel(handles.axes, 'Phase (Radians)'); 
end

% Update handles structure
guidata(hObject, handles);



% End Of Function

% --- Executes on button press in GetPoints.
function GetPoints_Callback(hObject, eventdata, handles)
% low = get(handles.XSlider, 'Value')*...
%     (str2num(get(handles.XAxEnd, 'String'))-handles.dx)+[0 handles.dx]);

%Find the lowest frequency point to be plotted
low = get(handles.XSlider, 'Value')*...
    (str2num(get(handles.XAxEnd, 'String'))-handles.dx);
%Find the location of this in the frequency axis data
x1 = find(handles.fx>=low, 1, 'first');
%Now find this frequency+dx in the data
x2 = find(handles.fx>=low+handles.dx, 1, 'first');
%Now store the trimmed x and y values to be used in Cursor_export
handles.xvalues = handles.fx(x1:x2);
handles.yvalues = handles.data(x1:x2);
Cursor_export(handles);


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


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


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


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



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


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


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


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


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



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


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


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



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


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


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



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


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


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



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


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


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



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


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


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



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


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


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



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

% Update handles structure
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function XAxStart_CreateFcn(hObject, eventdata, handles)
% hObject    handle to XAxStart (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 XAxEnd_Callback(hObject, eventdata, handles)
% hObject    handle to XAxEnd (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 XAxEnd as text
%        str2double(get(hObject,'String')) returns contents of XAxEnd as a double

handles = UpdatePlot(handles);
% Update handles structure
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function XAxEnd_CreateFcn(hObject, eventdata, handles)
% hObject    handle to XAxEnd (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 PLotFreqBandLims.
function PLotFreqBandLims_Callback(hObject, eventdata, handles)
% hObject    handle to PLotFreqBandLims (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = UpdatePlot(handles);
% Hint: get(hObject,'Value') returns toggle state of PLotFreqBandLims


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



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


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


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


% --- Executes during object creation, after setting all properties.
function PtPow_CreateFcn(hObject, eventdata, handles)
% hObject    handle to PtPow (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 slider movement.
function XSlider_Callback(hObject, eventdata, handles)
% hObject    handle to XSlider (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.axes, 'xlim' ,get(handles.XSlider, 'Value')*...
   (str2num(get(handles.XAxEnd, 'String'))-handles.dx)+[0 handles.dx]);

% --- Executes during object creation, after setting all properties.
function XSlider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to XSlider (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 YSlider_Callback(hObject, eventdata, handles)
% hObject    handle to YSlider (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
ylimstart = min(min(handles.data)-abs(min(handles.data)*0.1));
yrange = max(max(handles.data)-min(handles.data));
% ylimstart = (min(handles.data)-abs(min(handles.data)*0.1));
% yrange = (max(handles.data))-(min(handles.data));
set(handles.axes, 'ylim' ,get(handles.YSlider, 'Value')*...
   ((yrange)+0.1*(yrange)-handles.dy)+[ylimstart ylimstart+handles.dy]);

% --- Executes during object creation, after setting all properties.
function YSlider_CreateFcn(hObject, eventdata, handles)
% hObject    handle to YSlider (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 button press in XZoomIn.
function XZoomIn_Callback(hObject, eventdata, handles)
% hObject    handle to XZoomIn (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.XSlider, 'Enable'), 'on'),[])==1)
    set(handles.XSlider, 'Enable', 'on');
    set(handles.XSlider, 'Value', 0.5);
    set(handles.XZoomOut, 'Enable', 'on');
end
N = length(handles.userdata);
xlims = get(handles.axes, 'xlim');
set(handles.axes, 'xlim' ,get(handles.XSlider, 'Value')*...
   (str2num(get(handles.XAxEnd, 'String'))-handles.dx)+[0 handles.dx]);
%adjust the slider step size [buttonclik percentage trough-click
%percentage]

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


guidata(hObject, handles);

% --- Executes on button press in XZoomOut.
function XZoomOut_Callback(hObject, eventdata, handles)
% hObject    handle to XZoomOut (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 == str2num(get(handles.XAxEnd, 'String')))
    set(handles.XSlider, 'Enable', 'off');
    set(handles.XZoomOut, 'Enable', 'off');
end
N = length(handles.userdata);

set(handles.axes, 'xlim' ,get(handles.XSlider, 'Value')*...
   (str2num(get(handles.XAxEnd, 'String'))-handles.dx)+[0 handles.dx]);
%adjust the slider step size [buttonclik percentage trough-click
%percentage]

set(handles.XSlider, 'SliderStep', 1/0.75.*get(handles.XSlider, 'SliderStep'));


guidata(hObject, handles);

% --- Executes on button press in YZoomIn.
function YZoomIn_Callback(hObject, eventdata, handles)
% hObject    handle to YZoomIn (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.YSlider, 'Enable'), 'on'),[])==1)
    set(handles.YSlider, 'Enable', 'on');
    set(handles.YSlider, 'Value', 0.5);
    set(handles.YZoomOut, 'Enable', 'on');
end


ylimstart = min(min(handles.data)-abs(min(handles.data)*0.1));
yrange = max(max(handles.data)-min(handles.data));
set(handles.axes, 'ylim' ,get(handles.YSlider, 'Value')*...
   ((yrange)+0.1*(yrange)-handles.dy)+[ylimstart ylimstart+handles.dy]);

% set(handles.axes, 'ylim' ,get(handles.YSlider, 'Value')*...
%    (max(handles.data)+0.1*max(handles.data)-handles.dy)+[0 handles.dy]);
%adjust the slider step size [buttonclik percentage trough-click
%percentage]

set(handles.YSlider, 'SliderStep', 0.5.*get(handles.YSlider, 'SliderStep'));


guidata(hObject, handles);
%EoF

% --- Executes on button press in YZoomOut.
function YZoomOut_Callback(hObject, eventdata, handles)
% hObject    handle to YZoomOut (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 ~(get(handles.PlotPhaseTgl, 'Value'))
    yrange = max(max(handles.data));
else
yrange = max(max(handles.data)-min(handles.data));
end
if(handles.dy==(yrange)+0.1*(yrange))
    set(handles.YSlider, 'Enable', 'off');
    set(handles.YZoomOut, 'Enable', 'off');
end
ylimstart = min(min(handles.data)-abs(min(handles.data)*0.1));

set(handles.axes, 'ylim' ,get(handles.YSlider, 'Value')*...
   ((yrange)+0.1*(yrange)-handles.dy)+[ylimstart ylimstart+handles.dy]);
%adjust the slider step size [buttonclik percentage trough-click
%percentage]

set(handles.YSlider, 'SliderStep', 1/0.75.*get(handles.YSlider, 'SliderStep'));


guidata(hObject, handles);


% --- Executes on button press in PlotPSDTgl.
function PlotPSDTgl_Callback(hObject, eventdata, handles)
if get(hObject, 'Value')
    set(handles.PlotPowSpecTgl, 'Value', 0);
    set(handles.PlotPhaseTgl, 'Value', 0);
    handles = UpdatePlot(handles);
else
    set(hObject, 'Value', 1);
end

% Update handles structure
guidata(hObject, handles);

% --- Executes on button press in PlotPowSpecTgl.
%When the user changes the plot
function PlotPowSpecTgl_Callback(hObject, eventdata, handles)
if get(hObject, 'Value')
    set(handles.PlotPSDTgl, 'Value', 0);
    set(handles.PlotPhaseTgl, 'Value', 0);
    handles = UpdatePlot(handles);
else
    set(hObject, 'Value', 1);
end


% Update handles structure
guidata(hObject, handles);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Hint: get(hObject,'Value') returns toggle state of PlotPowSpecTgl
%% UPDATE PLOTS FUNCTION 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function newhandles = UpdatePlot(handles)
%Plot the data 
logx = get(handles.LogXAx, 'Value');
logy = get(handles.LogYAx, 'Value');
if get(handles.PlotPSDTgl, 'Value')
    %If the user wants  PSD plotting
    cla(handles.axes);
    %Next plot the figure on the chosen axes type
    
    
%%%If the error in the multi-taper method is required
    if handles.method == 3 && get(handles.MTMErrorTgl, 'Value')
    handles.data = [handles.Pxx handles.Pxxc];
    else
    handles.data = [handles.Pxx];
    end


    if  logx && logy
        loglog(handles.axes, handles.fx, handles.data)
    elseif logx && ~logy
        semilogx(handles.axes, handles.f, handles.data)
    elseif ~logx && logy
        semilogy(handles.axes, handles.f, handles.data)
    else
        plot(handles.axes, handles.f, handles.data)
    end
    
    xmin = str2num(get(handles.XAxStart, 'String'));  % Get the minimum xlim
    xmax = str2num(get(handles.XAxEnd, 'String'));    %Get the max xlim
%     xmin = get(handles.XSlider, 'Value')*...
%         (handles.tstep/2)-handles.dx;   % Get the minimum xlim
%     xmax = xmin+dx;                     % add on the view length
    xlim(handles.axes, [xmin xmax]);                  % set the limits
    
    ylimit = max(max(handles.data)+0.1*max(handles.data));%the maximum y-axis value 
    ymin = 0;
%     ymin = get(handles.YSlider, 'Value')*(ylim-handles.dy); % the minimum
%     displayed
    ylim(handles.axes, [ymin ylimit]);                  % Set the y lims
    title(handles.axes, 'Power Spectral Density'); 
    if handles.FreqAx
        xlabel(handles.axes, 'Frequency (Hz)');
        ylabel(handles.axes, 'Power (dB.Hz^{-1})');
    else
        xlabel(handles.axes, 'Radians.Sample^{-1}');
        ylabel(handles.axes, 'Power (dB/(Radians.Sample^{-1}))');
    end
    
    handles.fx = handles.f;
    handles.dx = str2num(get(handles.XAxEnd, 'String'));

    handles.dy = ylimit;%max(handles.data)+0.1*max(handles.data);
    
    set(handles.XSlider, 'Enable', 'off');
    set(handles.XZoomOut, 'Enable', 'off');
    set(handles.YSlider, 'Enable', 'off');
    set(handles.YZoomOut, 'Enable', 'off');
    set(handles.XSlider, 'SliderStep', [0.0100    0.1000]);
    set(handles.YSlider, 'SliderStep', [0.0100    0.1000]);
    
elseif get(handles.PlotPowSpecTgl, 'Value')
    %The user wants a Power Spectrum...
    user_sel_length =length(handles.userdata);      %length of data
    handles.N = 2^(nextpow2(user_sel_length));      %Number of points in FFT
    F=fft(handles.userdata, handles.N);             %FFT the data
    handles.P=F.*conj(F);
    % Scale the fft so that it is not a function of the length of the
    % user's data.
    handles.P = handles.P/(user_sel_length^2);
    % times by 2 because we discarded the second half, where half the
    % power was!
    handles.P = handles.P*2;

    % DC Component should be unique.
    handles.P(1) = handles.P(1)/2;
    % Nyquist component should also be unique.
    if ~rem(handles.P,2)
        % Here handles.P is even; therefore,Nyquist point is included so
        % divide it by 2 also
        handles.P(end) = handles.P(end)/2;
    end
    
    %Now create a vector for the x-axis of the Power spectrum
    f = handles.tstep*(0 :(handles.N)/2-1 )/(handles.N);
    handles.data = handles.P(1:floor(handles.N)/2);
    cla(handles.axes);
    %Next plot the figure on the chosen axes type
    if  logx && logy
        loglog(handles.axes, f, handles.data)
    elseif logx && ~logy
        semilogx(handles.axes, f, handles.data)
    elseif ~logx && logy
        semilogy(handles.axes, f, handles.data)
    else
        plot(handles.axes, f, handles.data)
    end
    
    xmin = str2num(get(handles.XAxStart, 'String'));                  % set the x limits
    xmax = str2num(get(handles.XAxEnd, 'String'));                    
    xlim(handles.axes, [xmin xmax]);                        
    
    ylimit = max(handles.P)+0.1*max(handles.P);         % Set the y lims
    ymin = 0;
    ylim(handles.axes, [ymin ylimit]);                      
    title(handles.axes, 'Power Spectrum');
    if handles.FreqAx
        xlabel(handles.axes, 'frequency (Hz)');
        ylabel(handles.axes, 'Power (mV^2)');
    else
        xlabel(handles.axes, 'Radians  (Sample)');
        ylabel(handles.axes, 'Power  (mV^2)');
    end
    
    handles.fx = f;
    handles.dx = str2num(get(handles.XAxEnd, 'String'));
    handles.dy = max(handles.data)+0.1*max(handles.data);
    set(handles.XSlider, 'Enable', 'off');
    set(handles.XZoomOut, 'Enable', 'off');
    set(handles.YSlider, 'Enable', 'off');
    set(handles.YZoomOut, 'Enable', 'off');
    set(handles.XSlider, 'SliderStep', [0.0100    0.1000]);
    set(handles.YSlider, 'SliderStep', [0.0100    0.1000]);
    
else
        %The user wants the Phase plot...
    user_sel_length =length(handles.userdata);      %length of data
    handles.N = 2^(nextpow2(user_sel_length));      %Number of points in FFT
    F=fft(handles.userdata, handles.N);             %FFT the data
    %Get the phase of this data
    handles.data = unwrap(angle(F));
    
    %Comment out the following line to plot phase in radians
    handles.data = handles.data/pi*180;
    %If you do change this, dont forget to change the y-axis labels
    
    %Now create a vector for the x-axis of the Power spectrum
        %Remember: there will be an even number of fft points so the nyquist
        %frequency is included in the FFT
    f = handles.tstep*(0 :handles.N-1)/(handles.N-1);
    cla(handles.axes);
    %Next plot the figure on the chosen axes type
    if  logx && logy
        loglog(handles.axes, f, handles.data)
    elseif logx && ~logy
        semilogx(handles.axes, f, handles.data)
    elseif ~logx && logy
        semilogy(handles.axes, f, handles.data)
    else
        plot(handles.axes, f, handles.data)
    end
    xlabel(handles.axes, 'frequency (Hz)');
    %Comment as appropriate
    ylabel(handles.axes, 'Phase (Deg.)');
    %ylabel(handles.axes, 'Phase (Radians)');    
    
    xmin = str2num(get(handles.XAxStart, 'String'));                  % set the x limits
    xmax = str2num(get(handles.XAxEnd, 'String'));                    
    xlim(handles.axes, [xmin xmax]);    
    
    set(handles.XSlider, 'Enable', 'off');
    set(handles.XZoomOut, 'Enable', 'off');
    set(handles.XSlider, 'SliderStep', [0.0100    0.1000]);
  
    ylimit = max(handles.data)+0.1*abs(max(handles.data));         % Set the y lims
    ymin = min(handles.data)-abs(0.1*min(handles.data));
    ylim(handles.axes, [ymin ylimit]);                      
    title(handles.axes, 'Phase Spectrum');
    set(handles.YSlider, 'SliderStep', [0.0100    0.1000]);
    set(handles.YSlider, 'Enable', 'off');
    set(handles.YZoomOut, 'Enable', 'off');         
    
    handles.fx = f;
    handles.dx = str2num(get(handles.XAxEnd, 'String'));
    handles.dy = ylimit-ymin;
   

    
    
end

if get(handles.PLotFreqBandLims, 'Value')
    %if the user wants markers for the frequency band limits
    bandlims = [1 4 8 14 30 50 100 150];
    if ~handles.FreqAx
        bandlims = bandlims/handles.tstep;      %if they want it normalised
    end                                         %What if its two-sided!!!!?
    ii = [];
    axes(handles.axes);
    for ii = 2:2:16
        X = [bandlims(ii/2) bandlims(ii/2)];
%         if ~(get(handles.PlotPhaseTgl, 'Value'))
%         Y = [0 ylimit];
%         else
           Y = [ymin ylimit];
%         end
        line(X, Y, 'Color', 'r');
    end
    if handles.CB 
        for ii = 2:2:length(handles.custombands)*2
            X = [handles.custombands(ii/2) handles.custombands(ii/2)];
            Y = [0 ylimit];
            line(X, Y, 'Color', 'g');
        end
        
    end
end
newhandles = handles;

%End of Function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Refresh Window Function
% Executes when the Window is initialised
function RefreshWindow(handles)
set(handles.PlotPSDTgl, 'Value', 1);
set(handles.PlotPowSpecTgl, 'Value', 0);
set(handles.PlotPhaseTgl, 'Value', 0);
set(handles.XAxStart, 'String', num2str(0));
set(handles.XAxEnd, 'String', num2str(max(handles.f)));
set(handles.PLotFreqBandLims, 'Value', 0);
set(handles.XSlider, 'Value', 0.5);
set(handles.XSlider, 'Enable', 'off');
set(handles.YSlider, 'Value', 0.5);
set(handles.YSlider, 'Enable', 'off');
set(handles.LinXAx, 'Value', 1);
set(handles.LogXAx, 'Value', 0);
set(handles.LinYAx, 'Value', 1);
set(handles.Cust1Pow ,'String', '-');
set(handles.Cust1PcPow, 'String', '-');
set(handles.Cust2Pow ,'String', '-');
set(handles.Cust2PcPow, 'String', '-');
set(handles.Cust3Pow ,'String', '-');
set(handles.Cust3PcPow, 'String', '-');
set(handles.XZoomOut, 'Enable', 'off');
set(handles.YZoomOut, 'Enable', 'off');
set(handles.MTMErrorTgl, 'Value', 0);
if handles.method == 3
    set(handles.MTMErrorTgl, 'Enable', 'on');
else
    set(handles.MTMErrorTgl, 'Enable', 'off');
end
 %End of function





% --- Executes on button press in LogXAx.
function LogXAx_Callback(hObject, eventdata, handles)
% hObject    handle to LogXAx (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 LogXAx
if get(hObject, 'Value')
    set(handles.LinXAx, 'Value', 0);
    else
    set(handles.LogXAx, 'Value', 1);
end
handles = UpdatePlot(handles);;


% --- Executes on button press in LinXAx.
function LinXAx_Callback(hObject, eventdata, handles)
% hObject    handle to LinXAx (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 LinXAx
if get(hObject, 'Value')
    set(handles.LogXAx, 'Value', 0);
else
    set(handles.LinXAx, 'Value', 1);
end
handles = UpdatePlot(handles);;



% --- Executes on button press in LinYAx.
function LinYAx_Callback(hObject, eventdata, handles)
% hObject    handle to LinYAx (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 LinYAx
if get(hObject, 'Value')
    set(handles.LogYAx, 'Value', 0);
    else
    set(handles.LinYAx, 'Value', 1);
end
handles = UpdatePlot(handles);;

% --- Executes on button press in LogYAx.
function LogYAx_Callback(hObject, eventdata, handles)
% hObject    handle to LogYAx (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 LogYAx
if get(hObject, 'Value')
    set(handles.LinYAx, 'Value', 0);
    else
    set(handles.LogYAx, 'Value', 1);
end
handles = UpdatePlot(handles);;
%End Of Function

%% Function to Udate the Power information
function PopulatePowData(handles)
%Write a function to plot the power data in the boxes...
Hs = handles.Hpsd;
% if handles.FreqAx
%     
% else
%     AV = avgpower(Hs, [0.01 499.9]);
% end
% handles.FreqAx
if handles.FreqAx
    AV = avgpower(Hs);
    bl = [1 4 8 14 30 50 100 150];
    b = [0 1 150 handles.tstep/2];
    if handles.CB
        handles.custombands = handles.custombands;
    end
elseif strcmp(get(Hs, 'SpectrumType'), 'Twosided')
    AV = avgpower(Hs);
    bl = [1 4 8 14 30 50 100 150]*pi/(handles.tstep/2);
    b2 = [999 996 992 986 970 950 900 850]*pi/(handles.tstep/2);
    b = [0 1 150 handles.tstep/2]*pi/(handles.tstep/2);
    bb = [handles.tstep handles.tstep-1 handles.tstep-150 handles.tstep/2]*pi/(handles.tstep/2);
    if handles.CB
        handles.custombands = handles.custombands*pi/(handles.tstep/2);
    end    
else
    AV = avgpower(Hs, [0 pi]);
    bl = [1 4 8 14 30 50 100 150]*pi/(handles.tstep/2);
    b = [0 1 150 handles.tstep/2]*pi/(handles.tstep/2);
    if handles.CB
        handles.custombands = handles.custombands*pi/(handles.tstep/2);
    end
end

if ~handles.FreqAx && strcmp(get(Hs, 'SpectrumType'), 'Twosided')
%Stet the box's values
set(handles.DeltaPow, 'String', num2str(avgpower(Hs, [bl(1) bl(2)]) + avgpower(Hs, [b2(2) b2(1)])));
set(handles.ThetaPow, 'String', num2str(avgpower(Hs, [bl(2) bl(3)]) + avgpower(Hs, [b2(3) b2(2)])));
set(handles.AlphaPow, 'String', num2str(avgpower(Hs, [bl(3) bl(4)]) + avgpower(Hs, [b2(4) b2(3)])));
set(handles.BetaPow, 'String', num2str(avgpower(Hs, [bl(4) bl(5)]) + avgpower(Hs, [b2(5) b2(4)])));
set(handles.GamLPow, 'String', num2str(avgpower(Hs, [bl(5) bl(6)]) + avgpower(Hs, [b2(6) b2(5)])));
set(handles.GamHPow, 'String', num2str(avgpower(Hs, [bl(6) bl(7)]) + avgpower(Hs, [b2(7) b2(6)])));
set(handles.GamVHPow, 'String', num2str(avgpower(Hs, [bl(7) bl(8)]) + avgpower(Hs, [b2(8) b2(7)])));

if handles.method == 2
    %The value of 0.004 is required to make the twosided Welch psd not give an
%error - This is a known problem and if you have the solution, please
%contact one of the authors
    set(handles.SubDeltaPow, 'String', num2str(avgpower(Hs, [b(1) b(2)]) + avgpower(Hs, [bb(2) bb(1)-0.004])));
else
    %The avgpower function cannot go upto 2*pi so we subtract 0.001
    set(handles.SubDeltaPow, 'String', num2str(avgpower(Hs, [b(1) b(2)]) + avgpower(Hs, [bb(2) bb(1)-0.001])));
end

set(handles.SuperGamVHPow, 'String', num2str(avgpower(Hs, [b(3) b(4)]) + avgpower(Hs, [bb(4) bb(3)])));
set(handles.TotalPow, 'String', num2str(avgpower(Hs)));
else
set(handles.DeltaPow, 'String', num2str(avgpower(Hs, [bl(1) bl(2)])));
set(handles.ThetaPow, 'String', num2str(avgpower(Hs, [bl(2) bl(3)])));
set(handles.AlphaPow, 'String', num2str(avgpower(Hs, [bl(3) bl(4)])));
set(handles.BetaPow, 'String', num2str(avgpower(Hs, [bl(4) bl(5)])));
set(handles.GamLPow, 'String', num2str(avgpower(Hs, [bl(5) bl(6)])));
set(handles.GamHPow, 'String', num2str(avgpower(Hs, [bl(6) bl(7)])));
set(handles.GamVHPow, 'String', num2str(avgpower(Hs, [bl(7) bl(8)])));
set(handles.SubDeltaPow, 'String', num2str(avgpower(Hs, [b(1) b(2)])));
set(handles.SuperGamVHPow, 'String', num2str(avgpower(Hs, [b(3) b(4)])));
set(handles.TotalPow, 'String', num2str(avgpower(Hs)));
end
%Now the averages
set(handles.DeltaPcPow, 'String', num2str(str2num(get(handles.DeltaPow, 'String'))/AV*100));
set(handles.ThetaPcPow, 'String', num2str(str2num(get(handles.ThetaPow, 'String'))/AV*100));
set(handles.AlphaPcPow, 'String', num2str(str2num(get(handles.AlphaPow, 'String'))/AV*100));
set(handles.BetaPcPow, 'String', num2str(str2num(get(handles.BetaPow, 'String'))/AV*100));
set(handles.GamLPcPow, 'String', num2str(str2num(get(handles.GamLPow, 'String'))/AV*100));
set(handles.GamHPcPow, 'String', num2str(str2num(get(handles.GamHPow, 'String'))/AV*100));
set(handles.GamVHPcPow, 'String', num2str(str2num(get(handles.GamVHPow, 'String'))/AV*100));

    
    
if handles.CB 
    %If there are user-defined bands
    Cb =handles.custombands;
    if(length(Cb)>0) && ~handles.FreqAx && strcmp(get(Hs, 'SpectrumType'), 'Twosided')
        set(handles.Cust1Pow, 'String', num2str(avgpower(Hs, [Cb(1) Cb(2)])*2));
        set(handles.Cust1PcPow, 'String', num2str(avgpower(Hs, [Cb(1) Cb(2)])*2/AV*100));
    else(length(Cb)>0)
        set(handles.Cust1Pow, 'String', num2str(avgpower(Hs, [Cb(1) Cb(2)])));
        set(handles.Cust1PcPow, 'String', num2str(avgpower(Hs, [Cb(1) Cb(2)])/AV*100));
    end

    if(length(Cb)>2) &&  ~handles.FreqAx && strcmp(get(Hs, 'SpectrumType'), 'Twosided')
        set(handles.Cust2Pow, 'String', num2str(avgpower(Hs, [Cb(3) Cb(4)])*2));
        set(handles.Cust2PcPow, 'String', num2str(avgpower(Hs, [Cb(3) Cb(4)])*2/AV*100));     
    elseif(length(Cb)>2)
        set(handles.Cust2Pow, 'String', num2str(avgpower(Hs, [Cb(3) Cb(4)])));
        set(handles.Cust2PcPow, 'String', num2str(avgpower(Hs, [Cb(3) Cb(4)])/AV*100));     
    end

    if(length(Cb)>4) &&  ~handles.FreqAx && strcmp(get(Hs, 'SpectrumType'), 'Twosided')
        set(handles.Cust3Pow, 'String', num2str(avgpower(Hs, [Cb(5) Cb(6)])*2));
        set(handles.Cust3PcPow , 'String', num2str(avgpower(Hs, [Cb(5) Cb(6)])*2/AV*100));
    elseif (length(Cb)>4)
        set(handles.Cust3Pow, 'String', num2str(avgpower(Hs, [Cb(5) Cb(6)])));
        set(handles.Cust3PcPow , 'String', num2str(avgpower(Hs, [Cb(5) Cb(6)])/AV*100));
    end
end


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





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


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


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


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


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



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


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


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


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



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


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


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




% --- Executes on button press in PlotPhaseTgl.
function PlotPhaseTgl_Callback(hObject, eventdata, handles)
if get(hObject, 'Value')
    set(handles.PlotPSDTgl, 'Value', 0);
    set(handles.PlotPowSpecTgl, 'Value', 0);
    handles = UpdatePlot(handles);
else
    set(hObject, 'Value', 1);
end
% Update handles structure
guidata(hObject, handles);


% --- is pressed whent the user wants to plot the error of the MTM method
function MTMErrorTgl_Callback(hObject, eventdata, handles)
%Update the plot
UpdatePlot(handles);
% Update handles structure
guidata(hObject, handles);
%Now some null callbacks and create functions for the power information
%edit boxes. Within the Fig file the callback and create fuction is set to
%null_Callback or nullCreateFcn
% --- Executes during object creation, after setting all properties.
function null_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
%This execuste when the user clicks something that does not require
%immediate action
function null_Callback(hObject, eventdata, handles)
function FILE_Callback(hObject, eventdata, handles)
function FILE_Close_Callback(hObject, eventdata, handles)
delete(gcf);
function HELP_Callback(hObject, eventdata, handles)
function HELP_LiscenceInfo_Callback(hObject, eventdata, handles)
LiscenceInfo;
function HELP_About_Callback(hObject, eventdata, handles)
About;









