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

% Last Modified by GUIDE v2.5 19-May-2004 13:39:47

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

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


% --- Executes just before log_dec is made visible.
function log_dec_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 log_dec (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = log_dec_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;
%
%=============================================================
%  Determine which cart we are using
%=============================================================
%
% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
global cart1 cart2 cart3 encoder
%
  cart1 = get(handles.radiobutton1,'Value');
  encoder = 1;
%
% If this cart is selected, deselect the other carts
%
  set(handles.radiobutton2,'Value',0);
  set(handles.radiobutton3,'Value',0);
%
  return;
%
% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
global cart1 cart2 cart3 encoder
%
  cart2 = get(handles.radiobutton2,'Value');
  encoder = 2;
%
% If this cart is selected, deselect the other carts
%
  set(handles.radiobutton1,'Value',0);
  set(handles.radiobutton3,'Value',0);
%
  return;
%
% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
global cart1 cart2 cart3 encoder
%
  cart3 = get(handles.radiobutton3,'Value');
  encoder = 3;
%
% If this cart is selected, deselect the other carts
%
  set(handles.radiobutton1,'Value',0);
  set(handles.radiobutton2,'Value',0);
%
  return;
%  
%=======================================================================
%  This routine loads the initial condition response
%=======================================================================
%
% --- Executes on button press in pushbutton1.
%
function pushbutton1_Callback(hObject, eventdata, handles)
global tIC yIC encoder low_peak high_peak separation final_time
%
%  read in the data from the workspace
%
  tIC = evalin('base','time');
  if( encoder == 3) 
      yIC = evalin('base','x3');
  elseif (encoder == 2)
      yIC = evalin('base','x2');
  else
      yIC = evalin('base','x1');
  end;
%
%  convert from counts to cm
%
  scale = 1.0; % no need to scale this is an approximation
  yIC = yIC*scale;
%
% Set the initial final time for the step response
%
  final_time = tIC(end);
  set(handles.edit1,'String',num2str(tIC(end)));
%
%  Set the ranges of the final time slider
%
  set(handles.slider1,'Min',0);
  set(handles.slider1,'Max',tIC(end));
  set(handles.slider1,'Value',final_time);
%
%  Set the initial separation (sample points) between peaks
%
  separation = 65;
  set(handles.edit4,'String',num2str(separation));
%
%  Set the ranges of the separation slider
%
  set(handles.slider4,'Min',1);
  set(handles.slider4,'Max',100);
  set(handles.slider4,'SliderStep',[0.01 .1]);
  set(handles.slider4,'Value',separation);
%
%  set the initial values for the low peaks
%
  low_peak = 2;
  set(handles.edit2,'String',num2str(low_peak));
  set(handles.slider2,'Min',1);
  set(handles.slider2,'Max',100);
  set(handles.slider2,'Value',low_peak);
  set(handles.slider2,'SliderStep',[0.01 0.1]);
%
%  set the initial values for the high peaks
%
  high_peak = 3;
  set(handles.edit3,'String',num2str(high_peak));
  set(handles.slider3,'Min',1);
  set(handles.slider3,'Max',100);
  set(handles.slider3,'Value',high_peak);
  set(handles.slider3,'SliderStep',[0.01 0.1]);
%
  return;
%
%======================================================
%  This routine plots the initial condition response
%======================================================
%
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
global tIC yIC final_time
%
  axes(handles.axes1);
  cla reset
  plot(tIC,yIC,'-'); grid;
  xlabel('Time (sec)');
  ylabel('Displacement (cm)');
  if (isempty(final_time) == 0)
      axis([0 final_time min(yIC) max(yIC)]);
  end;
%
return;
%  
%==========================================================================
%  This routine determines the final time for the IC response from the
%  slider
%==========================================================================
%
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
global final_time
%
%  Get the final time
%
  final_time = get(handles.slider1,'Value');
%
%  Put this final value in the box
%
  set(handles.edit1,'String',num2str(final_time));
%
return;
%
%=====================================================================
%  Read the final time from direct input
%====================================================================

function edit1_Callback(hObject, eventdata, handles)
global final_time
%
  final_time = str2num(get(handles.edit1,'String'));
return;
%
%=============================================================
%  This routine makes the + peaks picture
%============================================================
%
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
global tIC yIC separation index final_time peaks
%
  peaks = 1;  % peaks is -1 if a negative peak picture
  temp = yIC;
  data = temp - temp(end);
%
%  find the positive peaks of the data
%
  N = length(tIC);
  index = [];
  old_index = 0;
  for i=2:N-1
      if((data(i)>=data(i-1)) & (data(i)>data(i+1)) & (i > old_index+separation));
          index = [index i];
          old_index = i;
      end;
  end;
%
% Plot the data with the identified inflection points
%
  axes(handles.axes1);
  cla reset
  hold on
  plot(tIC,data); grid;
  xlabel('Time (sec)');
  ylabel('Displacement (cm)');
  for i=1:length(index)
    if (tIC(index(i)) < final_time)
      text(tIC(index(i)),data(index(i)),num2str(i));  % the inflection point
    end;
  end;
  if (isempty(final_time) == 0)
    axis([0 final_time min(data) max(data)]);
  end;
  hold off
%
return;
%
%==================================================================
%  This routine makes the (-) peaks picture
%====================================================================
%
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
global tIC yIC separation index final_time peaks
%
  peaks = -1; % peaks is +1 if a positive peaks picture
  temp = -yIC;
  data = temp - temp(end);
%
%  find the positive peaks of the data
%
  N = length(tIC);
  index = [];
  old_index = 0;
  for i=2:N-1
      if((data(i)>=data(i-1)) & (data(i)>data(i+1)) & (i > old_index+separation));
          index = [index i];
          old_index = i;
      end;
  end;
%
% Plot the data with the identified inflection points
%
  axes(handles.axes1);
  cla reset
  hold on
  plot(tIC,data); grid;
  xlabel('Time (sec)');
  ylabel('Discplacement (cm)');
  for i=1:length(index)
    if( tIC(index(i)) < final_time )
      text(tIC(index(i)),data(index(i)),num2str(i));  % the inflection point
  end;
  end;
  if (isempty(final_time) == 0)
    axis([0 final_time min(data) max(data)]);
  end;
  hold off
%
return;
%
%=========================================================
%  Get the Separation Value
%=========================================================
%
% --- Executes on slider movement.
function slider4_Callback(hObject, eventdata, handles)
global separation
%
%  Get the separation value
%
  separation = floor(get(handles.slider4,'Value'));
  set(handles.edit4,'String',num2str(separation));
%
  return;
%
%=============================================================
%  Read The Separation Value
%==============================================================
%
function edit4_Callback(hObject, eventdata, handles)
global separation
%
  separation = str2num(get(handles.edit4,'String'));
%
return;
%
%===============================================================
%  Get the low peak value
%===============================================================
%
% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
global low_peak high_peak
%
 low_peak = floor(get(handles.slider2,'Value'));
 set(handles.edit2,'String',num2str(low_peak));
%
%  be sure the high peak is at least one larger
%
  if (high_peak <= low_peak)
      high_peak = low_peak+1;
      set(handles.edit3,'String',num2str(high_peak));
  end;
%
return;
%
%=============================================
%  Read the low peak value
%=============================================
%
function edit2_Callback(hObject, eventdata, handles)
global low_peak high_peak
%
 low_peak = str2num(get(handles.edit2,'String'));
%
%  be sure the high peak is at least one larger
%
  if (high_peak <= low_peak)
      high_peak = low_peak+1;
      set(handles.edit3,'String',num2str(high_peak));
  end;
%
return;
%
%==============================================================
% Get the high peak value
%==============================================================
%
% --- Executes on slider movement.
function slider3_Callback(hObject, eventdata, handles)
global high_peak low_peak
%
 high_peak = floor(get(handles.slider3,'Value'));
 set(handles.edit3,'String',num2str(high_peak));
%
return;
%
%====================================================
% Read the high peak value
%======================================================
%
function edit3_Callback(hObject, eventdata, handles)
global low_peak high_peak
%
 high_peak = str2num(get(handles.edit3,'String'));
%
return;
%
%==============================================================
% Estimate Parameters
%==============================================================
%
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
global tIC yIC index low_peak high_peak omega_n zeta H
%
%  Find the damped period
%
  temp = -yIC;
  data = temp - temp(end);
%
  n = high_peak-low_peak;
  T_d = (tIC(index(high_peak))-tIC(index(low_peak)))/n;
  omega_d = 2*pi/T_d;
  delta = log(data(index(low_peak))/data(index(high_peak)));
  zeta = delta/sqrt(4*pi*pi*n*n+delta*delta);
  omega_n = omega_d/sqrt(1-zeta*zeta);
%
%  Put it all together
%
  H = tf(1,[(1/omega_n^2) 2*zeta/omega_n 1]);
%
%  Now write it out
%
  set(handles.edit5,'String',num2str(omega_n,4));
  set(handles.edit7,'String',num2str(zeta,4));
  
return;
%
%=================================================================
%  Make Log Decrement Figure
%==================================================================
%  
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
global tIC yIC index low_peak high_peak omega_n zeta H  peaks separation final_time
%
  if (peaks == 1) 
    temp = yIC;
    data = temp - temp(end);
    %
    %  find the positive peaks of the data
    %
    N = length(tIC);
    index = [];
    old_index = 0;
    for i=2:N-1
      if((data(i)>=data(i-1)) & (data(i)>data(i+1)) & (i > old_index+separation));
          index = [index i];
          old_index = i;
      end;
    end;
    %
    % Plot the data with the identified inflection points
    %
    figure
    hold on
    plot(tIC,data); grid;
    xlabel('Time (sec)');
    ylabel('Displacement (cm)');
    for i=1:length(index)
      if (tIC(index(i)) < final_time )
        text(tIC(index(i)),data(index(i)),num2str(i));  % the inflection point
    end;
    end;
    if (isempty(final_time) == 0)
      axis([0 final_time min(data) max(data)]);
    end;
    hold off
    title(['Log Decrement Analysis: Peak X_n = ' num2str(low_peak) ', Peak X_{n+N} = ' num2str(high_peak) ', \omega_n = ' num2str(omega_n,3) ' rad/sec, \zeta = ' num2str(zeta,3)]);
else
    temp = -yIC;
    data = temp - temp(end);
  %
  %  find the positive peaks of the data
  %
    N = length(tIC);
    index = [];
    old_index = 0;
    for i=2:N-1
      if((data(i)>=data(i-1)) & (data(i)>data(i+1)) & (i > old_index+separation));
          index = [index i];
          old_index = i;
      end;
    end;
  %
  % Plot the data with the identified inflection points
  %
    figure;
    hold on
    plot(tIC,data); grid;
    xlabel('Time (sec)');
    ylabel('Discplacement (cm)');
    for i=1:length(index)
      if (tIC(index(i)) < final_time)  
        text(tIC(index(i)),data(index(i)),num2str(i));  % the inflection point
    end;
    end;
    if (isempty(final_time) == 0)
      axis([0 final_time min(data) max(data)]);
    end;
    hold off
    title(['Log Decrement Analysis: Peak X_n = ' num2str(low_peak) ', Peak X_{n+N} = ' num2str(high_peak) ', \omega_n = ' num2str(omega_n,3) ' rad/sec, \zeta = ' num2str(zeta,3)]);
  end;
%
return;





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

%==========================================================================================

% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)

% hObject    handle to edit4 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (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, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (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, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



% --- Executes during object creation, after setting all properties.
function slider3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider3 (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, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

% --- Executes during object creation, after setting all properties.
function slider4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider4 (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, change
%       'usewhitebg' to 0 to use default.  See ISPC and COMPUTER.
usewhitebg = 1;
if usewhitebg
    set(hObject,'BackgroundColor',[.9 .9 .9]);
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end

% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


% --- Executes during object creation, after setting all properties.
function edit7_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit7 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



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

function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end


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


