function varargout = spectral(varargin)
%SPECTRAL(varargin)
%
%  varargin (''): Callback string parameter used internally
%
%Examples:
%  spectral

global sguih

loadnew = 0; loadstr = 'useold';
if (nargin == 1)
  if (strcmp(varargin{1},'new')) loadnew = 1; loadstr = 'choosenew'; end %if
end %if

if (nargin == 0 | loadnew)  % LAUNCH GUI

  [fig, handles] = openSpectral;
  sguih = handles;
  
  try
    chooseFile(loadstr, -1, [], 3, handles, 1);
  catch
    disp(lasterr);
  end
  set(handles.txtStatus, 'String', 'Ready');
  
  if nargout > 0
    varargout{1} = fig;
  end

elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK

  handles = sguih;
  lv = length(varargin);
  callfunc = cell(lv+1);
  callfunc{1} = varargin{1};
  callfunc{2} = handles;
  for i=2:lv
    callfunc{i+1} = varargin{i};
  end %for

  try
    %[varargout{1:nargout}] = 
    feval(callfunc{:}); % FEVAL switchyard
  catch
    disp(lasterr);
  end
  if ~strcmp(varargin{1}, 'axsData_ButtonDownFcn') 
    set(handles.axsData, 'UserData', []);
  end %if
  if (get(handles.txtStatus, 'UserData'))
    set(handles.txtStatus, 'String', 'Ready');
  end %if
  set(handles.txtStatus, 'UserData', 1);
  
end

function [fig, handles] = openSpectral()
% This is the machine-generated representation of a Handle Graphics object
% and its children.  Note that handle values may change when these objects
% are re-created. This may cause problems with any callbacks written to
% depend on the value of the handle at the time the object was saved.
% This problem is solved by saving the output as a FIG-file.
% 
% NOTE: certain newer features in MATLAB may not have been saved in this
% M-file due to limitations of this format, which has been superseded by
% FIG-files.  Figures which have been annotated using the plot editor tools
% are incompatible with the M-file/MAT-file format, and should be saved as
% FIG-files.


% --------------------------------------------------------------------
fig = figure('Units','normalized', ...
	'FileName','spectral.fig', ...
	'Color',get(0,'defaultUicontrolBackgroundColor'), ...
	'HandleVisibility','off', ...
	'IntegerHandle','off', ...
	'MenuBar','none', ...
	'Name','Spectral', ...
	'NumberTitle','off', ...
	'Position',[.2170138888888888 .3009259259259259 .4340277777777777 .3761574074074073], ...
	'Tag','figSpectral', ...
	'UserData',0);
handles.figSpectral = fig;
handles.frmAnalyze = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.558 .1630769230769231 .402 .3107692307692308], ...
	'String',{''}, ...
	'Style','frame', ...
	'Tag','frmAnalyze');
handles.frmCD = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.558 .5025 .402 .1876923076923077], ...
	'String',{''}, ...
	'Style','frame', ...
	'Tag','frmCD');
handles.frmECG = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.558 .7169230769230769 .402 .2492307692307692], ...
	'String',{''}, ...
	'Style','frame', ...
	'Tag','frmECG');
handles.txtStatus = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'HandleVisibility','callback', ...
	'HorizontalAlignment','left', ...
	'ListboxTop',0, ...
	'Position',[.5573033707865169 -.003460207612456748 .4 .09688581314878893], ...
	'String','Ready', ...
	'Style','text', ...
	'Tag','txtStatus', ...
	'UserData',1);
handles.axsData = axes('Parent',fig, ...
	'CameraUpVector',[0 1 0], ...
	'Color',get(0,'defaultUicontrolBackgroundColor'), ...
	'HandleVisibility','callback', ...
	'Position',[.078 .1938461538461539 .462 .7723076923076925], ...
	'Tag','axsData', ...
	'XColor',[.5 .5 .5], ...
	'YColor',[.5 .5 .5], ...
	'ZColor',[0 0 0]);
h0 = text('Parent',handles.axsData, ...
	'Color',[.5 .5 .5], ...
	'HandleVisibility','off', ...
	'HorizontalAlignment','center', ...
	'Position',[.4956709956709957 -.09362549800796804 9.160254037844386], ...
	'VerticalAlignment','cap');
set(handles.axsData,'XLabel',h0);
h1 = text('Parent',handles.axsData, ...
	'Color',[.5 .5 .5], ...
	'HandleVisibility','off', ...
	'HorizontalAlignment','center', ...
	'Position',[-.1234 .4960 9.1603], ...
	'Rotation',90, ...
	'VerticalAlignment','baseline');
set(handles.axsData,'YLabel',h1);
h2 = text('Parent',handles.axsData, ...
	'Color',[0 0 0], ...
	'HandleVisibility','off', ...
	'HorizontalAlignment','right', ...
	'Position',[-.170995670995671 1.037848605577689 9.160254037844386], ...
	'Visible','off');
set(handles.axsData,'ZLabel',h2);
h3 = text('Parent',handles.axsData, ...
	'Color',[0 0 0], ...
	'HandleVisibility','off', ...
	'HorizontalAlignment','center', ...
	'Position',[.5 1.0259 9.1603], ...
	'VerticalAlignment','bottom');
set(handles.axsData,'Title',h3);
set(handles.axsData,'Children', [h0 h1 h2 h3]);
handles.btnChooser = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnChooser_Callback'', ''choosenew'')', ...
	'HandleVisibility','callback', ...
	'HitTest','off', ...
	'ListboxTop',0, ...
	'Position',[.5800 .7477 .119 .0738], ...
	'String','Chooser', ...
	'Tag','btnChooser');
handles.btnPreview = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnPreview_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.7 .7476923076923078 .1195 .07384615384615385], ...
	'String','Preview', ...
	'Tag','btnPreview');
handles.btnEcg2hr = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnEcg2hr_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.82 .7477 .119 .0738], ...
	'String','Ecg2hr', ...
	'Tag','btnEcg2hr');
handles.btnJumpLeft = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnJumpLeft_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.1180 .04 .04 .0646], ...
	'Tag','btnJumpLeft', ...
	'UserData',5);
handles.btnToBeginning = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnToBeginning_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.078 .04 .04 .06461538461538462], ...
	'Tag','btnToBeginning', ...
	'UserData',0);
handles.btnMoveLeft = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnMoveLeft_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.158 .04 .04 .06461538461538462], ...
	'Tag','btnMoveLeft', ...
	'UserData',1);
handles.btnJumpRight = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnJumpRight_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.46 .04 .04 .06461538461538462], ...
	'Tag','btnJumpRight', ...
	'UserData',5);
handles.btnMoveRight = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnMoveRight_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.42 .04 .04 .06461538461538462], ...
	'Tag','btnMoveRight', ...
	'UserData',1);
handles.btnToEnd = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnToEnd_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.5 .04 .04 .06461538461538462], ...
	'Tag','btnToEnd', ...
	'UserData',1);
disableMove(handles);
handles.edtMin = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''edtMin_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.198 .04 .112 .0646], ...
	'String','0', ...
	'Style','edit', ...
	'Tag','edtMin');
handles.edtMax = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''edtMax_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.308 .04 .112 .0646], ...
	'String','1', ...
	'Style','edit', ...
	'Tag','edtMax');
handles.tbnDataEdit = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''tbnDataEdit_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.58 .5323 .119 .0708], ...
	'String','Edit', ...
	'Style','togglebutton', ...
	'Tag','tbnDataEdit', ...
	'UserData',0);
handles.rbnClearAdd = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''rbnClearAdd_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.578 .6123076923076923 .16 .04615384615384616], ...
	'String','Clear+Add', ...
	'Style','radiobutton', ...
	'Tag','rbnClearAdd', ...
	'Value',1);
handles.rbnClear = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''rbnClear_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.744 .6123076923076923 .1 .04615384615384616], ...
	'String','Clear', ...
	'Style','radiobutton', ...
	'Tag','rbnClear');
handles.rbnAdd = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''rbnAdd_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.85 .6123076923076923 .09 .04615384615384616], ...
	'String','Add', ...
	'Style','radiobutton', ...
	'Tag','rbnAdd');
handles.btnRevert = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnRevert_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.7 .5323 .119 .0708], ...
	'String','Revert', ...
	'Tag','btnRevert');
handles.btnOutput = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnOutput_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.82 .5323076923076924 .1195 .07076923076923078], ...
	'String','Output', ...
	'Tag','btnOutput');
handles.btnFTA = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnFTA_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.5800 .3667820069204152 0.119 .07612456747404844], ...
	'String','FTA', ...
	'Tag','btnFTA');
handles.btnMFTA = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnMFTA_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[0.7 .3667820069204152 0.1195 .07612456747404844], ...
	'String','MFTA', ...
	'Tag','btnMFTA');
handles.btnTFTA = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnTFTA_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[0.82 .3667820069204152 0.119 .07612456747404844], ...
	'String','TFTA', ...
	'Tag','btnTFTA');
handles.edtrows = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''edtrows_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.641 .2862 .118 .0615], ...
	'String','1', ...
	'Style','edit', ...
	'Tag','edtrows');
handles.edtcols = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''edtcols_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.76 .2862 .118 .0615], ...
	'String','1', ...
	'Style','edit', ...
	'Tag','edtcols');
handles.txtrows = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.578 .2861538461538462 .06 .05230769230769231], ...
	'String','rows', ...
	'Style','text', ...
	'Tag','txtrows');
handles.txtcols = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.88 .2861538461538462 .06 .05230769230769231], ...
	'String','cols', ...
	'Style','text', ...
	'Tag','txtcols');
handles.btnEMDV = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnEMDV_Callback'')', ...
	'Enable','off', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.58 0.1938461538461538 .179 .08], ...
	'String','EMDV', ...
	'Tag','btnEMDV');
handles.btnSplice = uicontrol('Parent',fig, ...
	'Units','normalized', ...
	'Callback','spectral(''btnSplice_Callback'')', ...
	'Enable','on', ...
	'HandleVisibility','callback', ...
	'ListboxTop',0, ...
	'Position',[.759 0.1938461538461538 .179 .08], ...
	'String','Splice', ...
	'Tag','btnSplice');

% --------------------------------------------------------------------
function setLevel(level, h)
level1 = [h.btnPreview,h.btnEcg2hr];
level2 = [h.btnToBeginning,h.btnJumpLeft,h.btnMoveLeft,h.edtMin,...
          h.edtMax,h.btnMoveRight,h.btnJumpRight,h.btnToEnd];
level3 = [h.frmCD,h.rbnClearAdd,h.rbnClear,h.rbnAdd,...
          h.tbnDataEdit,h.btnOutput,h.btnRevert...
          h.frmAnalyze,h.btnFTA,h.btnMFTA,h.txtrows,h.edtrows,...
          h.edtcols,h.txtcols,h.btnTFTA,h.btnEMDV];

if (level >= 2)
  set(h.axsData, 'Color', [1 1 1]);
  set(h.axsData, 'XColor', [0 0 0]);
  set(h.axsData, 'YColor', [0 0 0]);
  enableMove(h);
else
  set(h.axsData, 'Color', get(0,'defaultUicontrolBackgroundColor'));
  set(h.axsData, 'XColor', [.5 .5 .5]);
  set(h.axsData, 'YColor', [.5 .5 .5]);
  disableMove(h);
end %if
if (level >= 1) set(level1, 'Enable', 'on'); else set(level1, 'Enable', 'off'); end %if
if (level >= 2) set(level2, 'Enable', 'on'); else set(level2, 'Enable', 'off'); end %if
if (level >= 3) set(level3, 'Enable', 'on'); else set(level3, 'Enable', 'off'); end %if

% --------------------------------------------------------------------
function xAxisUpdate(begpt, endpt, reset, handles)
begpt = round(begpt*1000)/1000;
endpt = round(endpt*1000)/1000;
if (begpt == endpt) return; end %if
if (reset)
  set(handles.btnToBeginning, 'UserData', begpt);
  set(handles.btnToEnd, 'UserData', endpt);
end %if
set(handles.axsData, 'xlim', [begpt endpt]); 
set(handles.edtMin, 'String', num2str(begpt));
set(handles.edtMax, 'String', num2str(endpt));
xinc = (endpt-begpt)/4;
set(handles.btnJumpLeft, 'UserData', xinc*3);
set(handles.btnJumpRight, 'UserData', xinc*3);
set(handles.btnMoveLeft, 'UserData', xinc);
set(handles.btnMoveRight, 'UserData', xinc)

% --------------------------------------------------------------------
function chooseFile(fileuse, rows, xlim, levelSet, handles, forceRefresh)
set(handles.txtStatus, 'String', 'Choosing a file...');
[ecgfilename, cdfilename, rrfilename, hrfilename, ecgfilenameonly] = chooser(fileuse);
curname = get(handles.figSpectral, 'Name');
newname = ['Spectral - ' ecgfilename];
if ((forceRefresh) | not(strcmp(curname, newname)))
  set(handles.figSpectral, 'Name', newname);
  set(handles.txtStatus, 'String', 'Opening file...');
  if (exist(ecgfilename, 'file') & exist(cdfilename, 'file') & ...
      exist(rrfilename, 'file') & exist(hrfilename, 'file'))
    [t ecgread] = textread(ecgfilename, '%f %f', rows);
    [cdt cdbeat] = textread(cdfilename, '%f %f', rows, 'headerlines', 1);
    plot(t, ecgread, 'k', 'Parent', handles.axsData);
    set(handles.axsData, 'NextPlot', 'Add');
    plot(cdt, cdbeat, 'g', 'Parent', handles.axsData);
  [hrt hrpt hrnt ema navg slopediff] = textread(hrfilename, '%f %f %f %f %f %f', rows, 'headerlines', 1);
  plot(hrt, hrpt, 'r', 'Parent', handles.axsData);
  plot(hrt, hrnt, 'r', 'Parent', handles.axsData);
  plot(hrt, ema, 'b', 'Parent', handles.axsData);
  plot(hrt, navg, 'g', 'Parent', handles.axsData);
  plot(hrt, slopediff, 'c', 'Parent', handles.axsData);
    set(handles.axsData, 'NextPlot', 'Replace');
    if isempty(xlim) xlim = [t(1) t(length(t))]; end %if
    if (xlim(1) == xlim(2)) xlim = [xlim(1)-.5 xlim(2)+.5]; end %if
    xAxisUpdate(xlim(1), xlim(2), 1, handles);
    set(handles.axsData, 'ylim', [-1 1]);
    set(handles.axsData, 'ButtonDownFcn', 'spectral(''axsData_ButtonDownFcn'');');
    set(get(handles.axsData, 'Children'), 'ButtonDownFcn', 'spectral(''axsData_ButtonDownFcn'');');
    setLevel(levelSet, handles);
  else
    plot(0:1, 0:1, 'Parent', handles.axsData, 'Visible', 'off');
    setLevel(1, handles);
  end %if
end %if

% --------------------------------------------------------------------
function varargout = btnChooser_Callback(handles, varargin)
set(handles.tbnDataEdit, 'Value', 0);
chooseFile('choosenew', -1, [], 3, handles, 0);

% --------------------------------------------------------------------
function varargout = btnPreview_Callback(handles, varargin)
set(handles.tbnDataEdit, 'Value', 0);
set(handles.txtStatus, 'String', 'Analyzing...');
setLevel(0, handles);
try
  ecg2hr(10);
  [ecgfilename, cdfilename, rrfilename, hrfilename] = chooser('useold');
  fs = textread(cdfilename, '%f', 1);
  if (isempty(fs)) fs = 1000; end %if
  chooseFile('useold', 10*fs, [0 2], 2, handles, 1);
catch
  disp(lasterr);
  setLevel(1, handles);
end %try

% --------------------------------------------------------------------
function varargout = btnEcg2hr_Callback(handles, varargin)
set(handles.tbnDataEdit, 'Value', 0);
set(handles.txtStatus, 'String', 'Analyzing...');
setLevel(0, handles);
try
  ecg2hr;
  [ecgfilename, cdfilename, rrfilename, hrfilename] = chooser('useold');
  chooseFile('useold', -1, [], 3, handles, 1); 
catch
  disp(lasterr);
  setLevel(1, handles);
end %try

% --------------------------------------------------------------------
function varargout = axsData_ButtonDownFcn(handles, varargin)
if (get(handles.tbnDataEdit, 'Value') == 1) editData(handles); return; end %if
clickline = get(handles.axsData, 'CurrentPoint');
x = clickline(1,1);
y = clickline(1,2);
xlim = get(handles.axsData, 'xlim'); xmin = xlim(1);
ylim = get(handles.axsData, 'ylim'); ymin = ylim(1);
if (x >= xmin & y >= ymin)
  firstxy = get(handles.axsData, 'UserData');
  if isempty(firstxy)
    set(handles.axsData, 'UserData', [x y]);
    set(handles.txtStatus, 'UserData', 0);
    set(handles.txtStatus, 'String', ['Point (' num2str(round(x)) ',' num2str(round(y)) ...
      ') chosen.  Click far corner to zoom in or an axes to escape.']);
  else
    newx = firstxy(1);
    newy = firstxy(2);
    xlim = [min(x, newx) max(x, newx)];
    ylim = [min(y, newy) max(y, newy)];
    if (xlim(1) == xlim(2) | ylim(1) == ylim(2))
      set(handles.axsData, 'UserData', []);
      error('Please select a larger graph window.');
    end %if
    xAxisUpdate(xlim(1), xlim(2), 0, handles);
    set(handles.axsData, 'ylim', ylim);
    set(handles.axsData, 'UserData', []);
  end %if
elseif (x < xmin & y < ymin)
  if isempty(get(handles.axsData, 'UserData'))
    begpt = get(handles.btnToBeginning, 'UserData');
    endpt = get(handles.btnToEnd, 'UserData');
    xAxisUpdate(begpt, endpt, 0, handles);
    set(handles.axsData, 'ylim', [-1 1]);
  end %if
  set(handles.axsData, 'UserData', []);
elseif (x < xmin)
  if isempty(get(handles.axsData, 'UserData'))
    yinc = (ylim(2) - ylim(1))/2;
    ylim = [ylim(1)-yinc ylim(2)+yinc];
    set(handles.axsData, 'ylim', ylim);
  end %if
  set(handles.axsData, 'UserData', []);
elseif (y < ymin)
  if isempty(get(handles.axsData, 'UserData'))
    xinc = (xlim(2) - xlim(1))/2;
    xlim = [xlim(1)-xinc xlim(2)+xinc];
    xAxisUpdate(xlim(1), xlim(2), 0, handles);
  end %if
  set(handles.axsData, 'UserData', []);
end %if

% --------------------------------------------------------------------
function varargout = edtihbv_Callback(handles, varargin)
% Stub for Callback of the uicontrol handles.edtihbv.
% --------------------------------------------------------------------
function varargout = edtsdt_Callback(handles, varargin)
% Stub for Callback of the uicontrol handles.edtsdt.
% --------------------------------------------------------------------
function varargout = edtwaveq_Callback(handles, varargin)
% Stub for Callback of the uicontrol handles.edtwaveq.

% --------------------------------------------------------------------
function varargout = rbnClearAdd_Callback(handles, varargin)
set(handles.rbnClearAdd, 'Value', 1);
set(handles.rbnClear, 'Value', 0);
set(handles.rbnAdd, 'Value', 0);
set(handles.tbnDataEdit, 'UserData', 0);

% --------------------------------------------------------------------
function varargout = rbnClear_Callback(handles, varargin)
set(handles.rbnClearAdd, 'Value', 0);
set(handles.rbnClear, 'Value', 1);
set(handles.rbnAdd, 'Value', 0);
set(handles.tbnDataEdit, 'UserData', -1);

% --------------------------------------------------------------------
function varargout = rbnAdd_Callback(handles, varargin)
set(handles.rbnClearAdd, 'Value', 0);
set(handles.rbnClear, 'Value', 0);
set(handles.rbnAdd, 'Value', 1);
set(handles.tbnDataEdit, 'UserData', 1);

% --------------------------------------------------------------------
function editData(handles)
clickline = get(handles.axsData, 'CurrentPoint');
x = clickline(1,1);
y = clickline(1,2);
xlim = get(handles.axsData, 'xlim');
ylim = get(handles.axsData, 'ylim');
if (x < xlim(1) | y < ylim(1)) set(handles.tbnDataEdit, 'Value', 0); return; end %if
axeschildren = get(handles.axsData, 'Children');
xdata = get(axeschildren(1), 'XData');
ydata = get(axeschildren(1), 'YData');
min = get(handles.btnToBeginning, 'UserData');
max = get(handles.btnToEnd, 'UserData');
if (x < min | x > max) error('Please choose a point within the data range.'); end %if
[ecgfilename, cdfilename, rrfilename, hrfilename] = chooser('useold');
fs = textread(cdfilename, '%f', 1);
hb = round(fs * (x - min)) + 1;
i = 0;
while (xdata(hb) ~= x & i < 100)
  hb = hb + round((x-xdata(hb))*fs);
  i = i+1;
end %while
clearrange = round(fs * .066);
set(handles.axsData, 'Visible', 'off');
editCode = get(handles.tbnDataEdit, 'UserData');
if (editCode <= 0)
  clearbeg = hb-clearrange; if (clearbeg < 0) clearbeg = 0; end %if
  clearend = hb+clearrange; if (clearend > length(ydata)) clearend = length(ydata); end %if
  for i=(clearbeg:clearend)
    try
      ydata(i) = 0;
    end %try
  end %for
end %if
if (editCode >=0)
  try
    ydata(hb) = y;
  end %try
end %if
set(axeschildren(1), 'YData', ydata);
set(handles.axsData, 'Visible', 'on');


% --------------------------------------------------------------------
function varargout = tbnDataEdit_Callback(handles, varargin)
% Stub for Callback of the uicontrol handles.tbnDataEdit.

% --------------------------------------------------------------------
function varargout = btnOutput_Callback(handles, varargin)
set(handles.txtStatus, 'String', 'Exporting data...');
setLevel(0, handles);
axeschildren = get(handles.axsData, 'Children');
xdata = get(axeschildren(1), 'XData');
ydata = get(axeschildren(1), 'YData');
[ecgfilename, cdfilename, rrfilename, hrfilename] = chooser('useold');
fs = textread(cdfilename, '%f', 1);
cdfid = fopen(cdfilename,'w');
fprintf(cdfid, '%5g\n', fs);
fprintf(cdfid, '%11g %10g\n', [xdata;ydata]);
fclose(cdfid);
cd2hr('useold');
setLevel(3, handles);

% --------------------------------------------------------------------
function varargout = btnRevert_Callback(handles, varargin)
chooseFile('useold', -1, [], 3, handles, 1);

% --------------------------------------------------------------------
function varargout = btnToBeginning_Callback(handles, varargin)
set(handles.txtStatus, 'String', 'Regraphing...');
min = str2double(get(handles.edtMin, 'String'));
max = str2double(get(handles.edtMax, 'String'));
begpt = get(handles.btnToBeginning, 'UserData');
xAxisUpdate(begpt, begpt+max-min, 0, handles);

% --------------------------------------------------------------------
function varargout = btnJumpLeft_Callback(handles, varargin)
set(handles.txtStatus, 'String', 'Regraphing...');
min = str2double(get(handles.edtMin, 'String'));
max = str2double(get(handles.edtMax, 'String'));
jumpdist = get(handles.btnJumpLeft, 'UserData');
xAxisUpdate(min-jumpdist, max-jumpdist, 0, handles);

% --------------------------------------------------------------------
function varargout = btnMoveLeft_Callback(handles, varargin)
set(handles.txtStatus, 'String', 'Regraphing...');
min = str2double(get(handles.edtMin, 'String'));
max = str2double(get(handles.edtMax, 'String'));
movedist = get(handles.btnMoveLeft, 'UserData');
xAxisUpdate(min-movedist, max-movedist, 0, handles);

% --------------------------------------------------------------------
function varargout = edtMin_Callback(handles, varargin)
set(handles.txtStatus, 'String', 'Regraphing...');
min = str2double(get(handles.edtMin, 'String'));
max = str2double(get(handles.edtMax, 'String'));
if (min > max) max = min + 1; end %if
xAxisUpdate(min, max, 0, handles);

% --------------------------------------------------------------------
function varargout = edtMax_Callback(handles, varargin)
set(handles.txtStatus, 'String', 'Regraphing...');
min = str2double(get(handles.edtMin, 'String'));
max = str2double(get(handles.edtMax, 'String'));
if (max < min) min = max - 1; end %if
xAxisUpdate(min, max, 0, handles);

% --------------------------------------------------------------------
function varargout = btnMoveRight_Callback(handles, varargin)
set(handles.txtStatus, 'String', 'Regraphing...');
min = str2double(get(handles.edtMin, 'String'));
max = str2double(get(handles.edtMax, 'String'));
movedist = get(handles.btnMoveRight, 'UserData');
xAxisUpdate(min+movedist, max+movedist, 0, handles);

% --------------------------------------------------------------------
function varargout = btnJumpRight_Callback(handles, varargin)
set(handles.txtStatus, 'String', 'Regraphing...');
min = str2double(get(handles.edtMin, 'String'));
max = str2double(get(handles.edtMax, 'String'));
jumpdist = get(handles.btnJumpRight, 'UserData');
xAxisUpdate(min+jumpdist, max+jumpdist, 0, handles);

% --------------------------------------------------------------------
function varargout = btnToEnd_Callback(handles, varargin)
set(handles.txtStatus, 'String', 'Regraphing...');
min = str2double(get(handles.edtMin, 'String'));
max = str2double(get(handles.edtMax, 'String'));
diff = max-min;
endpt = get(handles.btnToEnd, 'UserData');
xAxisUpdate(endpt-diff, endpt, 0, handles);

% --------------------------------------------------------------------
function varargout = btnFTA_Callback(handles, varargin)
set(handles.tbnDataEdit, 'Value', 0);
set(handles.txtStatus, 'String', 'Analyzing...');
fta('useold');

% --------------------------------------------------------------------
function varargout = btnMFTA_Callback(handles, varargin)
set(handles.tbnDataEdit, 'Value', 0);
set(handles.txtStatus, 'String', 'Analyzing...');
rows = str2double(get(handles.edtrows, 'String'));
cols = str2double(get(handles.edtcols, 'String'));
mfta(rows, cols);
chooseFile('useold', -1, [], 3, handles, 1);

% --------------------------------------------------------------------
function varargout = edtrows_Callback(handles, varargin)
% Stub for Callback of the uicontrol handles.edtrows.
% --------------------------------------------------------------------
function varargout = edtcols_Callback(handles, varargin)
% Stub for Callback of the uicontrol handles.edtcols.

% --------------------------------------------------------------------
function varargout = btnTFTA_Callback(handles, varargin)
set(handles.tbnDataEdit, 'Value', 0);
set(handles.txtStatus, 'String', 'Analyzing...');
setLevel(0, handles);
tfta('useold');
setLevel(3, handles);

% --------------------------------------------------------------------
function varargout = btnEMDV_Callback(handles, varargin)
set(handles.tbnDataEdit, 'Value', 0);
set(handles.txtStatus, 'String', 'Analyzing...');
%setLevel(0, handles);
emda('useold');
setLevel(3, handles);

% --------------------------------------------------------------------
function varargout = btnSplice_Callback(handles, varargin)
set(handles.tbnDataEdit, 'Value', 0);
set(handles.txtStatus, 'String', 'Splicing...');
splice;

% --------------------------------------------------------------------
function enableMove(h)
bgc = get(0,'defaultUicontrolBackgroundColor');
l = scl(bgc, .75); b = zeros(3);
tb = zeros(8,10,3); jl = zeros(8,10,3); ml = zeros(8,10,3);
te = zeros(8,10,3); jr = zeros(8,10,3); mr = zeros(8,10,3);
tbs = 'bblllblllbbbllbbllbbbblbbblbbbbbbbbbbbbbbbbbbbbbbbbblbbblbbbbbllbbllbbbblllblllb';
jls = 'llllblllbllllbbllbblllbbblbbbllbbbbbbbbllbbbbbbbblllbbblbbbllllbbllbblllllblllbl';
mls = 'llllllbllllllllbblllllllbbbllllllbbbbllllllbbbblllllllbbbllllllllbblllllllllblll';
tes = 'blllblllbbbbllbbllbbbbblbbblbbbbbbbbbbbbbbbbbbbbbbbbblbbblbbbbllbbllbbblllblllbb';
jrs = 'lblllblllllbbllbbllllbbblbbblllbbbbbbbbllbbbbbbbbllbbblbbblllbbllbbllllblllbllll';
mrs = 'lllblllllllllbbllllllllbbblllllllbbbbllllllbbbbllllllbbblllllllbbllllllllbllllll';
for i=(1:80) tb(floor((i-1)/10)+1, mod((i-1), 10)+1, 1:3) = eval([tbs(i) '(1:3)']); end %for
for i=(1:80) jl(floor((i-1)/10)+1, mod((i-1), 10)+1, 1:3) = eval([jls(i) '(1:3)']); end %for
for i=(1:80) ml(floor((i-1)/10)+1, mod((i-1), 10)+1, 1:3) = eval([mls(i) '(1:3)']); end %for
for i=(1:80) te(floor((i-1)/10)+1, mod((i-1), 10)+1, 1:3) = eval([tes(i) '(1:3)']); end %for
for i=(1:80) jr(floor((i-1)/10)+1, mod((i-1), 10)+1, 1:3) = eval([jrs(i) '(1:3)']); end %for
for i=(1:80) mr(floor((i-1)/10)+1, mod((i-1), 10)+1, 1:3) = eval([mrs(i) '(1:3)']); end %for
set(h.btnToBeginning, 'CData', tb); set(h.btnJumpLeft, 'CData', jl); set(h.btnMoveLeft, 'CData', ml);
set(h.btnToEnd, 'CData', te); set(h.btnJumpRight, 'CData', jr); set(h.btnMoveRight, 'CData', mr);


% --------------------------------------------------------------------
function disableMove(h)
bgc = get(0,'defaultUicontrolBackgroundColor');
w = [1 1 1]; l = scl(bgc, .75); m = scl(bgc, .5);
tb = zeros(9,11,3); jl = zeros(9,11,3); ml = zeros(9,11,3);
te = zeros(9,11,3); jr = zeros(9,11,3); mr = zeros(9,11,3);
tbs = 'mmlllmlllmlmmwlmmwlmmwmmwmmmwmmmwmmmmmmmmmmwmmmmmmmmmmwmmwmmmwmmmwmmwlmmwlmmwmmwllmwllmwlwwlllwlllw';
jls = 'llllmlllmlllllmmwlmmwlllmmmwmmmwllmmmmmmmmwllmmmmmmmmwlllmmmwmmmwllllmmwlmmwlllllmwllmwllllllwlllwl';
mls = 'llllllmlllllllllmmwlllllllmmmwllllllmmmmwllllllmmmmwlllllllmmmwllllllllmmwlllllllllmwllllllllllwlll';
tes = 'mlllmlllmmlmmllmmllmmwmmmlmmmlmmwmmmmmmmmmmwmmmmmmmmmmwmmmwmmmwmmwmmwwmmwwmmwmwwlmwwlmmwlwlllwlllww';
jrs = 'lmlllmllllllmmllmmlllllmmmlmmmllllmmmmmmmmlllmmmmmmmmwllmmmwmmmwwllmmwwmmwwlllmwwlmwwlllllwlllwllll';
mrs = 'lllmllllllllllmmlllllllllmmmllllllllmmmmlllllllmmmmwllllllmmmwwllllllmmwwlllllllmwwlllllllllwllllll';
for i=(1:99) tb(floor((i-1)/11)+1, mod((i-1), 11)+1, 1:3) = eval([tbs(i) '(1:3)']); end %for
for i=(1:99) jl(floor((i-1)/11)+1, mod((i-1), 11)+1, 1:3) = eval([jls(i) '(1:3)']); end %for
for i=(1:99) ml(floor((i-1)/11)+1, mod((i-1), 11)+1, 1:3) = eval([mls(i) '(1:3)']); end %for
for i=(1:99) te(floor((i-1)/11)+1, mod((i-1), 11)+1, 1:3) = eval([tes(i) '(1:3)']); end %for
for i=(1:99) jr(floor((i-1)/11)+1, mod((i-1), 11)+1, 1:3) = eval([jrs(i) '(1:3)']); end %for
for i=(1:99) mr(floor((i-1)/11)+1, mod((i-1), 11)+1, 1:3) = eval([mrs(i) '(1:3)']); end %for
set(h.btnToBeginning, 'CData', tb); set(h.btnJumpLeft, 'CData', jl); set(h.btnMoveLeft, 'CData', ml);
set(h.btnToEnd, 'CData', te); set(h.btnJumpRight, 'CData', jr); set(h.btnMoveRight, 'CData', mr);

% --------------------------------------------------------------------
function [fc] = scl(ic, lightness) % lightness: 1 is bright, 0 is dark
if (abs(max(ic)-lightness) < .1 & abs(min(ic)-lightness) < .1)
  fc = ic; 
else
  mic = mean(ic); if (mic == 0) mic = 10; end %if
  fc = atan((ic/mic-.5)*3*lightness)/pi+.5;
end %if
