tf = ctdlog(:, temp_frequency);
delta = 36.04;
n = length(tf);

% Median filter data...
tfm = tf;
for i = 30:n-29,
   tfm(i) = median(tf((i-29):(i+29)));
end;

shift = round((tfm-tf)/delta);
tfm = tf - shift*delta;

% Try removing integral number of 'quantized errors'
tft = tfm;
for i = 3:n-1,
   % Can we make it close to previous point?
   t(1) = tft(i) - delta * round((tft(i) - tft(i-1))/delta);
   err(1) = (t(1) - tft(i-1)).^2;
   % Can we make it close to point two points ago?
   t(2) = tft(i) - delta * round((tft(i) - tft(i-2))/delta);
   err(2) = (t(2) - tft(i-2)).^2;
   % Can we make it midway between last and next point?
   % t(3) = tft(i) - delta * round((tft(i)-(tft(i-1) + tft(i+1))/2)/delta);
   % err(3) = (t(3) - (tft(i-1) + tft(i+1))/2).^2;
   
   m = find(err == min(err));
   tft(i) = t(m(1));
end;

shift = round(sum(tft-tf)/delta/n);
tft = tft - shift*delta;

% figure;
% plot(tf, '.'); hold on; 
% plot(tft, 'r.', 'MarkerSize', 5), 
% hold off; zoom on;

clear shift; clear tfm; clear err; clear m; clear t; 

