#!/usr/bin/perl -w
# read stdin of columns "t y x" and write to stdout columns "x y'",
# where y' is the difference between y and a linear model.
# N.B. since we rewind, stdin must be seekable.

# e.g. to plot x motor error, in steps, vs encoder counts:
# logaxis 100 x > x.log; gnuplot> plot "<autodiff < x.log"

# test seekability
seek (STDIN, 0, 2) || die "Can not seek with stdin";

# read once to find ranges and slope
seek (STDIN, 0, 0);
while (<>) {
    ($t, $y1, $x1) = split();
    $y0 = $y1, $x0 = $x1 if (!defined $y0);
}
$m = ($y1-$y0)/($x1-$x0);

# read again to print differences from line.
seek (STDIN, 0, 0);
while (<>) {
    ($t, $y, $x) = split();
    printf "%d %d\n", $x-$x0, $y-$y0 - $m*($x-$x0);
}

# For RCS Only -- Do Not Edit
# @(#) $RCSfile: autodiff,v $ $Date: 2003/04/15 20:48:10 $ $Revision: 1.1.1.1 $ $Name:  $
