function mx = massTrans(X, mass, resolution)
%
% MX = massTrans(X, MASS, RESOLUTION)
% Transform an input vector or matrix X (thought of as one or more mass
% spectra collected at equal time points) to an output vector MX equally
% spaced on the mass scale. This uses the calibration vector MASS to give
% the mapping from times to masses and the scalar RESOLUTION to determine
% the spacing between mass measurements. The main application is to allow
% the plotting of images or heat maps on the mass axis instead of the time
% axis. For example,
%	mx = massTrans(X, mass, 1);
%	imagesc(mx)

% Copyright (c) 2003, 2004 UT M.D. Anderson Cancer Center. All rights reserved.
% See the accompanying file "license.txt" for details.

cc = median(diff(diff(mass)))/2;

tt = 1:length(mass);
ss = diff(mass) - 2*cc*tt(1:end-1) - cc;
bb = median(ss);

rr = mass - cc*tt.^2 - bb.*tt;
aa = median(rr);

start = floor(max(0, min(mass)));
finish = ceil(max(mass));
newmass = start:resolution:finish;
timer = (-bb + sqrt(bb.^2 - 4*cc*(aa-newmass)))/(2*cc);

[nr nc] = size(X);
s = find(1 <= timer & timer < nc);
ti = floor(timer(s));
tr = timer(s) - ti;

g = X(:, ti+1) - X(:, ti);
echo on
size(ti)
size(g)
size(tr)
size(repmat(tr, [nr 1]))
echo off
mx = X(:, ti) + repmat(tr, [nr 1]) .* g;
