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

% Last Modified by GUIDE v2.5 14-Mar-2003 10:19:33

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @psd_select_OpeningFcn, ...
                   'gui_OutputFcn',  @psd_select_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


% --- Executes just before psd_select is made visible.
function psd_select_OpeningFcn(hObject, eventdata, handles, varargin)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Choose default command line output for psd_select
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% load input variables
data=varargin{1};

% update GUI
data=update_GUI(handles,data);

% save settings
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


% --- Outputs from this function are returned to the command line.
function varargout = psd_select_OutputFcn(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
varargout{1} = handles.output;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Hints: contents = get(hObject,'String') returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1

data=get(handles.figure1,'UserData');
data.config.accel.freq.windowing=get(hObject,'Value');
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

return;


% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
set(hObject,'Value',1)
data.config.accel.freq.backtracking(1)=0;
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
set(hObject,'Value',1)
data.config.accel.freq.backtracking(1)=1;
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


% --- Executes on button press in checkbox1.
function checkbox1_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
data.config.accel.freq.max_hold=get(hObject,'Value');
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
data.config.accel.freq.show_EC1=get(hObject,'Value');
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


% --- Executes on button press in checkbox3.
function checkbox3_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
data.config.accel.freq.plot_by_location=get(hObject,'Value');
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


% --- Executes on button press in checkbox4.
function checkbox4_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
data.config.accel.freq.single_axes=get(hObject,'Value');
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


% --- Executes on button press in checkbox5.
function checkbox5_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
data.config.accel.freq.plot_mag=get(hObject,'Value');
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


function edit1_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
value=abs(round(str2double(get(hObject,'string'))));
data.config.accel.freq.n_fft=2^value;
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


function edit2_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
data.config.accel.freq.sample_rate=abs(round(str2double(get(hObject,'string'))));
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


function edit3_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
data.config.accel.freq.backtracking(2)=abs(round(str2double(get(hObject,'string'))));
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


function edit4_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
value=abs(str2double(get(hObject,'string')));
if (value<data.config.accel.freq.x_limits(2))
   data.config.accel.freq.x_limits(1)=value;
else
   err_text=sprintf('Min must be less than Max range value!\n');
   fault(err_text);
end;
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


function edit5_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
value=abs(str2double(get(hObject,'string')));
if (value>data.config.accel.freq.x_limits(1))
   data.config.accel.freq.x_limits(2)=value;
else
   err_text=sprintf('Max must be greater than Min range value!\n');
   fault(err_text);
end;
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


function edit6_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
value=abs(str2double(get(hObject,'string')));
if (value<data.config.accel.freq.y_limits(2))
   data.config.accel.freq.y_limits(1)=value;
else
   err_text=sprintf('Min must be less than Max range value!\n');
   fault(err_text);
end;
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


function edit7_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
data=get(handles.figure1,'UserData');
value=abs(str2double(get(hObject,'string')));
if (value>data.config.accel.freq.y_limits(1))
   data.config.accel.freq.y_limits(2)=value;
else
   err_text=sprintf('Max must be greater than Min range value!\n');
   fault(err_text);
end;
data=update_GUI(handles,data);
set(handles.figure1,'UserData',data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
uiresume(handles.figure1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
set(handles.figure1,'UserData','');
uiresume(handles.figure1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


% --------------------------------------------------------------------
function varargout = CloseReq_Callback(h, eventdata, handles)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pushbutton2_Callback(h, eventdata, handles);
%closereq;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;


function [data] = update_GUI (handles,data)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

n_fft=log(data.config.accel.freq.n_fft)/log(2);
set(handles.edit1,'string',sprintf('%.0f',n_fft));
set(handles.text14,'string',sprintf('%.0f',data.config.accel.freq.n_fft));

set(handles.edit2,'string',sprintf('%.0f',data.config.accel.freq.sample_rate));

if (data.config.accel.freq.backtracking(1)==0)
   set(handles.radiobutton1,'Value',1);
   set(handles.radiobutton2,'Value',0);
   set(handles.edit3,'Enable','off');
elseif (data.config.accel.freq.backtracking(1)==1)
   set(handles.radiobutton1,'Value',0);
   set(handles.radiobutton2,'Value',1);
   set(handles.edit3,'Enable','on');
end;
set(handles.edit3,'string',sprintf('%.0f',data.config.accel.freq.backtracking(2)));

set(handles.popupmenu1,'Value',data.config.accel.freq.windowing);

set(handles.checkbox1,'Value',data.config.accel.freq.max_hold);
set(handles.checkbox2,'Value',data.config.accel.freq.show_EC1);
set(handles.checkbox3,'Value',data.config.accel.freq.plot_by_location);
set(handles.checkbox4,'Value',data.config.accel.freq.single_axes);
set(handles.checkbox5,'Value',data.config.accel.freq.plot_mag);

set(handles.edit4,'string',sprintf('%.3f',data.config.accel.freq.x_limits(1)));
set(handles.edit5,'string',sprintf('%.3f',data.config.accel.freq.x_limits(2)));
set(handles.edit6,'string',sprintf('%.3f',data.config.accel.freq.y_limits(1)));
set(handles.edit7,'string',sprintf('%.3f',data.config.accel.freq.y_limits(2)));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return;
