% timing_sweep_plot - Plot a timing sweep, optimal timing, sensitivity
%
% timing_sweep_plot(t, y, idx);
%
% Arguments:
%  t   - Time scale (ps)
%  y   - Response (dB)
%  idx - Optimal timing index

%
% Copyright (c) 2013 Dimtel, Inc., All Rights Reserved
%
% $Id$
% $Date$
% $Author$
% $Revision$
% $Log$

function timing_sweep_plot(t, y, idx)

% Determine the isolation
yy = sort(y(idx));
iso = yy(end)-yy(end-1);

clf; subplot(211); set(gca, 'fontsize', 12);
plot(t/1e3, y, t(idx)/1e3, y(idx), 'or', 'markersize', 7, 'linewidth', 2);
xlabel('Delay (ns)');
ylabel('Magnitude (dB)');
title(sprintf('Optimal timing %d ps with isolation of %.1f dB', ...
      t(idx(1)), iso));
grid;

% Plot timing sensitivity analysis
subplot(212); set(gca, 'fontsize', 12);
h = plot(t/1e3, y); hold on;
markers = 'o+*xsdph^';
leg{1} = 'Response';
Ts = t(2)-t(1);
delta = round(200/Ts); % +/-200 ps
Trange=-delta:delta;
m = 1;
for k=Trange;
  idx1 = idx + k;
  if (min(idx1) < 1) idx1 = idx1(2:end); end
  if (max(idx1) > length(y)) idx1 = idx1(1:end-1); end
  yy = sort(y(idx1));
  iso(m) = yy(end) - yy(end-1);
  h(m+1) = plot(t(idx1)/1e3, y(idx1), markers(m));
  m = m + 1;
  leg{m} = sprintf('%d ps offset', k*Ts);
end
hold off;
set(h, 'markersize', 7, 'linewidth', 2);
set(h(2:end), 'color', 'r');
xlabel('Delay (ns)');
ylabel('Magnitude (dB)');
[foo, u] = min(iso);
title(sprintf('Timing shift sensitivity: isolation %.1f dB at %d ps offset', ...
      iso(u), Trange(u)*Ts));
legend(leg, 2);
grid;
