#!/usr/bin/perl

# 1us-wrap.pl
# Filter a series of data values and scale to keep in a 0 - 1 range.
# Algorithm shamelessly stolen from Tom Van Baak (tvb@leapsecond.com)

# Copyright 2003 John Ackermann   Version: 0.9 -- 26 April 2003
# May be freely used provided these notices are retained, and a copy
# of any modified version that's distributed to third persons is sent 
# to jra@febo.com

$Value = 0.0;
$PreviousValue = 0.5;
$Base = 0.0;
$OutVal = 0.0;

while ($Value=<STDIN>) {
	if ($PreviousValue > 0.7 && $Value < 0.3) {
       	    $Base = $Base + 1.0;
        }

        elsif ($PreviousValue < 0.3 && $Value > 0.7) {
       	    $Base = $Base - 1.0;
	}
	endif;

	$PreviousValue = $Value;

	$OutVal = $Value + $Base;
	print($OutVal,"\n");
};

sub round {
	my($number) = shift;
	return int($number +.5 * ($number <=> 0) );
	};
