% CB_ADDSIGNAL callback fires when the user clicks 'add signal'
%
% M Hewitson
%
% $Id: cb_addSignal.m,v 1.1 2008/10/19 11:42:40 hewitson Exp $
%
function cb_addSignal(varargin)

  myh = varargin{1};
  mainfig = varargin{end};
  
  % sigType handle
  sh = findobj(mainfig.handle, 'Tag', 'SignalTypeSelect');
  
  % Get selected signal
  val = get(sh, 'Value');
  sigs = get(sh, 'String');  
  sig = sigs{val};
  
  % Build signals
  switch lower(sig)
    case 'sine wave'
      obj = buildSineWave(mainfig);
    case 'white noise'
      obj = buildWhiteNoise(mainfig);
    case 'chirp'
      obj = buildChirp(mainfig);
    case 'gaussian pulse'
      obj = buildGaussPulse(mainfig);
    case 'square wave'
      obj = buildSquarewave(mainfig);
    case 'sawtooth'
      obj = buildSawtooth(mainfig);
    case 'noise generator'
      obj = buildNoiseGen(mainfig);
    case 'polynomial'
      obj = buildPolynomial(mainfig);
    case 'custom'
      obj = buildCustom(mainfig);
    otherwise
      error('### I can''t build signal ''%s'' yet', sig);
  end
  
  % Add this AO
  mainfig.signals = [mainfig.signals obj];
  
  % Refresh list
  refreshSignalList(mainfig);
  
end

%--------------------------------------------------------------------------
% Build a noisegenerator signal
function obj = buildPolynomial(mainfig)
  
  [fs, t0, name] = getFsT0(mainfig);
  
  % get coeffs
  h = findobj(mainfig.handle, 'Tag', 'Poly_Coeffs');
  coeffs = eval(['[' get(h, 'String') ']']);
  if isempty(name) || strcmpi(name, 'none')
    name = sprintf('poly(%s)', mat2str(coeffs));
  end
  % get Nsecs
  h = findobj(mainfig.handle, 'Tag', 'Poly_Length');
  Nsecs = eval(['[' get(h, 'String') ']']);
  % Build
  obj = ao(plist('polyval', coeffs, ...
    'nsecs', Nsecs, 'fs', fs, 't0', t0, 'name', name));
end

%--------------------------------------------------------------------------
% Build a noisegenerator signal
function obj = buildNoiseGen(mainfig)
  
  [fs, t0, name] = getFsT0(mainfig);
  
  % get f0
  h = findobj(mainfig.handle, 'Tag', 'NoiseGen_Gain');
  g = eval(get(h, 'String'));
  % get poles
  h = findobj(mainfig.handle, 'Tag', 'NoiseGen_Poles');
  poles = eval(get(h, 'String'));  
  % get zeroes
  h = findobj(mainfig.handle, 'Tag', 'NoiseGen_Zeros');
  zeros = eval(get(h, 'String'));  
  if isempty(name) || strcmpi(name, 'none')
    name = sprintf('noisegen');
  end
  % get Nsecs
  h = findobj(mainfig.handle, 'Tag', 'NoiseGen_Length');
  Nsecs = eval(['[' get(h, 'String') ']']);
  % Build
  pzm = pzmodel(g, poles, zeros);
  obj = ao(plist('pzmodel', pzm, ...
    'nsecs', Nsecs, 'fs', fs, 't0', t0, 'name', name));
end

%--------------------------------------------------------------------------
% Build a sawtooth signal
function obj = buildSawtooth(mainfig)
  
  [fs, t0, name] = getFsT0(mainfig);
  
  % get f0
  h = findobj(mainfig.handle, 'Tag', 'Sawtooth_F');
  f0 = eval(get(h, 'String'));
  % get duty
  h = findobj(mainfig.handle, 'Tag', 'Sawtooth_Width');
  wd = eval(get(h, 'String'));  
  if isempty(name) || strcmpi(name, 'none')
    name = sprintf('saw(%g)', f0);
  end
  % get Nsecs
  h = findobj(mainfig.handle, 'Tag', 'Sawtooth_Length');
  Nsecs = eval(['[' get(h, 'String') ']']);
  % Build
  obj = ao(plist('waveform', 'Sawtooth', ...
    'f0', f0, 'width', wd, ...
    'nsecs', Nsecs, 'fs', fs, 't0', t0, 'name', name));
end

%--------------------------------------------------------------------------
% Build a square wave signal
function obj = buildSquarewave(mainfig)
  
  [fs, t0, name] = getFsT0(mainfig);
  
  % get f0
  h = findobj(mainfig.handle, 'Tag', 'SquareWave_F');
  f0 = eval(get(h, 'String'));
  % get duty
  h = findobj(mainfig.handle, 'Tag', 'SquareWave_Duty');
  duty = eval(get(h, 'String'));  
  if isempty(name) || strcmpi(name, 'none')
    name = sprintf('square(%g)', f0);
  end
  % get Nsecs
  h = findobj(mainfig.handle, 'Tag', 'SquareWave_Length');
  Nsecs = eval(['[' get(h, 'String') ']']);
  % Build
  obj = ao(plist('waveform', 'Square wave', ...
    'f0', f0, 'duty', duty, ...
    'nsecs', Nsecs, 'fs', fs, 't0', t0, 'name', name));
end

%--------------------------------------------------------------------------
% Build a gauss pulse signal
function obj = buildGaussPulse(mainfig)
  
  [fs, t0, name] = getFsT0(mainfig);
  
  % get f0
  h = findobj(mainfig.handle, 'Tag', 'GPulse_F0');
  f0 = eval(get(h, 'String'));
  % get BW
  h = findobj(mainfig.handle, 'Tag', 'GPulse_BW');
  bw = eval(get(h, 'String'));  
  if isempty(name) || strcmpi(name, 'none')
    name = sprintf('gpulse(%g)', f0);
  end
  % get Nsecs
  h = findobj(mainfig.handle, 'Tag', 'GPulse_Length');
  Nsecs = eval(['[' get(h, 'String') ']']);
  % Build
  obj = ao(plist('waveform', 'Gaussian pulse', ...
    'f0', f0, 'bw', bw, ...
    'nsecs', Nsecs, 'fs', fs, 't0', t0, 'name', name));
end


%--------------------------------------------------------------------------
% Build a chirp signal
function obj = buildChirp(mainfig)
  
  [fs, t0, name] = getFsT0(mainfig);
  
  % get f0
  h = findobj(mainfig.handle, 'Tag', 'Chirp_F0');
  f0 = eval(get(h, 'String'));
  % get f1
  h = findobj(mainfig.handle, 'Tag', 'Chirp_F1');
  f1 = eval(get(h, 'String'));
  if isempty(name) || strcmpi(name, 'none')
    name = sprintf('chirp(%g->%g)', f0,f1);
  end
  % get T1
  h = findobj(mainfig.handle, 'Tag', 'Chirp_T1');
  t1 = eval(get(h, 'String'));  
  % get Nsecs
  h = findobj(mainfig.handle, 'Tag', 'Chirp_Length');
  Nsecs = eval(['[' get(h, 'String') ']']);
  % Build
  obj = ao(plist('waveform', 'chirp', ...
    'f0', f0, 'f1', f1, 't1', t1, ...
    'nsecs', Nsecs, 'fs', fs, 't0', t0, 'name', name));
end


%--------------------------------------------------------------------------
% Build a custom signal
function obj = buildCustom(mainfig)
  
  [fs, t0, name] = getFsT0(mainfig);
  
  % get type
  h = findobj(mainfig.handle, 'Tag', 'Custom_Fcn');
  fcn = get(h, 'String');
  if isempty(name) || strcmpi(name, 'none')
    name = fcn;
  end
  % get Nsecs
  h = findobj(mainfig.handle, 'Tag', 'Custom_Length');
  Nsecs = eval(['[' get(h, 'String') ']']);
  % Build
  obj = ao(plist('tsfcn', fcn, ...
     'nsecs', Nsecs, 'fs', fs, 't0', t0, 'name', name));
end


%--------------------------------------------------------------------------
% Build a white noise
function obj = buildWhiteNoise(mainfig)
  
  [fs, t0, name] = getFsT0(mainfig);
  
  % get type
  h = findobj(mainfig.handle, 'Tag', 'WhiteNoise_Type');
  types = get(h, 'String');
  type = types{get(h, 'Value')};
  if strcmpi(name, 'none') || isempty(name)
    name = type;
  end
  % get Sigma
  h = findobj(mainfig.handle, 'Tag', 'WhiteNoise_Sigma');
  sigma = eval(['[' get(h, 'String') ']']);
  % get Nsecs
  h = findobj(mainfig.handle, 'Tag', 'WhiteNoise_Length');
  Nsecs = eval(['[' get(h, 'String') ']']);
  % Build
  obj = ao(plist('waveform', 'noise', ...
     'type', type, 'sigma', sigma, ...
     'nsecs', Nsecs, 'fs', fs, 't0', t0, 'name', name));
end

%--------------------------------------------------------------------------
% Build a sine wave
function obj = buildSineWave(mainfig)
  
  [fs, t0, name] = getFsT0(mainfig);
  
  % get amplitudes
  h = findobj(mainfig.handle, 'Tag', 'SineWave_Amplitudes');
  amps = eval(['[' get(h, 'String') ']']);
  % get frequencies
  h = findobj(mainfig.handle, 'Tag', 'SineWave_Frequencies');
  freqs = eval(['[' get(h, 'String') ']']);
  % get phases
  h = findobj(mainfig.handle, 'Tag', 'SineWave_Phases');
  phases = eval(['[' get(h, 'String') ']']);
  % get Nsecs
  h = findobj(mainfig.handle, 'Tag', 'SineWave_Lengths');
  Nsecs = eval(['[' get(h, 'String') ']']);
  % get Toffs
  h = findobj(mainfig.handle, 'Tag', 'SineWave_Starts');
  Toffs = eval(['[' get(h, 'String') ']']);
  if strcmpi(name, 'none') || isempty(name)
    name = 'sin(';
    name = [name sprintf('%.2g,', freqs)];
    name = [name(1:end-1) ')'];
  end
  
  % Build
  obj = ao(plist('waveform', 'sine wave', ...
     'A', amps, 'f', freqs, 'phi', phases*pi/180, ...
     'nsecs', Nsecs, 'toff', Toffs, 'fs', fs, 't0', t0, 'name', name));
  
end

%--------------------------------------------------------------------------
% Retrieve the sample rate and start time and name
% 
function  [fs, t0, name] = getFsT0(mainfig)
  % name
  h = findobj(mainfig.handle, 'Tag', 'NameEdit');
  name = get(h, 'String');
  % T0
  h = findobj(mainfig.handle, 'Tag', 'T0edit');
  t0 = time(get(h, 'String'));
  % FS
  h = findobj(mainfig.handle, 'Tag', 'SampleRateEdit');
  fs = eval(get(h, 'String'));  
end


