clear all;

%% Make some test data

amps = [1 2 3 4 5 6 7];
freqs = [0.005 0.05 0.1 0.2 0.3 0.4 0.5 0.7];
nsecs = [400 200 100 100 100 100 100 100];
gaps  = [50 50 150 50 50 50 50 50];
toffs(1) = gaps(1);
for kk=2:numel(nsecs)
  toffs(kk) = sum(gaps(1:kk)) + sum(nsecs(1:kk-1));
end

sigs = ao(plist('waveform', 'sine wave', ...
  'nsecs', nsecs, ...
  'fs', 10, ...
  'A', amps, ...
  'f', freqs, ...
  'toff', toffs, 'yunits', 'V'))

sigs.zeropad(plist('N', 1000))
sigs = sigs + ao(plist('tsfcn', '10*randn(size(t))', 'fs', 10, 'nsecs', sigs.nsecs));
sigs.setName;
% sigs.iplot

%% filter the data
pzm = pzmodel(1, {[0.05 2]}, {[0.5 2]});
pzm.setIunits('V');
pzm.setOunits('m');
f = miir(pzm, plist('fs', 10));
r = f.resp(plist('f', logspace(-3,log10(5), 1000)));
% iplot(r)

out = filter(sigs, f) + ...
  ao(plist('tsfcn', '1*randn(size(t))', 'fs', 10, 'nsecs', sigs.nsecs));
out.setName;
iplot(sigs, out)

%% Create the start times
startTimes = time;
t0 = sigs.t0;
for ii=1:numel(toffs) 
  startTimes(ii) = t0 + toffs(ii);
end
startTimes

%% Swept-sine measurement


pl = plist('start times', startTimes, ...
            'durations', nsecs, ...
            'input', sigs, 'frequencies', freqs);
          
t1 = quasiSweptSine(out, pl)          


t1.dy ./ abs(t1.y)

%% Plot abs/phase

ppl = plist('markers', {'', 'o'}, 'linestyles', {'', 'none'},...
  'yranges', {[1e-3,1e2], [-180,180]})
iplot(r, t1, ppl)

%% Plot real/imag

ppl = plist('markers', {'', 'o'}, 'linestyles', {'', 'none'},...
  'YERRL', {[], t1.y-t1.dy}, ...
  'COMPLEXPLOTTYPE', 'realimag', 'xscales', {'all', 'log'}, ...
  'yscales', {'lin', 'lin'})
iplot(r, t1, ppl)

%% Plot abs/rad

ppl = plist('markers', {'', 'o'}, 'linestyles', {'', 'none'},...
  'YERRL', {[], t1.y-t1.dy}, ...
  'COMPLEXPLOTTYPE', 'absrad', 'xscales', {'all', 'log'}, ...
  'yscales', {'lin', 'lin'})
iplot(r, t1, ppl)

%% Measure tf with ltfe

Te = ltfe(sigs, out);

iplot(Te)

Te = tfe(sigs, out, plist('navs', 100));

iplot(Te)

















