#!/usr/bin/perl -w
####################################################################
# Filename: finger_vs_noise.pl 
# Version: V9
# Date: 2013-06-14
# Changes: 
#    V9: - Bugfix for wrong order of the FingX, FingY and NoiseX, NoiseY
#          values
#    V8: - Bugfix for wrong renaming of file
#        - New concept for M0 noise qualification
#    V7: - Update to also handle M0 noise qualiciation values 
#         
####################################################################

use strict;

# File handles
my $INPUT_FILE; 
my $OUTPUT_M1;
my $OUTPUT_M0;

# create output files from input log file name, by removing ending *.log
my $input_log = $ARGV[0];
my $length = length($input_log) - 4;
my @filename = substr($input_log,0,$length);
my $output_M1_file = $filename[0]."_M1_converted.txt";
my $output_M0_file = $filename[0]."_M0_converted.txt";

# open all filehandles for reading and writting
open($INPUT_FILE, "<$input_log") or die "Can't open input file: $!\n";
open($OUTPUT_M1, ">$output_M1_file");
open($OUTPUT_M0, ">$output_M0_file");

# common variables used for calculation
my $cnt;
my $first=1;

# M0 variables for calculation
my $m1spix;
my $m1spiy;

my $fingY_min;
my $fingY_max;
my $fingY_spread;
my $fingY_mean;
my $fingY_std;
my $fingY_mean_2sign;
my $fingY_mean_2sigp;

my $fingX_min;
my $fingX_max;
my $fingX_spread;
my $fingX_mean;
my $fingX_std;
my $fingX_mean_2sign;
my $fingX_mean_2sigp;

my $noisY_min;
my $noisY_max;
my $noisY_spread;
my $noisY_mean;
my $noisY_std;
my $noisY_mean_2sign;
my $noisY_mean_2sigp;

my $noisX_min;
my $noisX_max;
my $noisX_spread;
my $noisX_mean;
my $noisX_std;
my $noisX_mean_2sign;
my $noisX_mean_2sigp;

# M0 variables for calculation
my $m0spix;
my $m0spiy;

my $m0_fingY_min;
my $m0_fingY_max;
my $m0_fingY_spread;
my $m0_fingY_mean;
my $m0_fingY_std;
my $m0_fingY_mean_2sign;
my $m0_fingY_mean_2sigp;

my $m0_fingX_min;
my $m0_fingX_max;
my $m0_fingX_spread;
my $m0_fingX_mean;
my $m0_fingX_std;
my $m0_fingX_mean_2sign;
my $m0_fingX_mean_2sigp;

#print "$m1spix $m1spiy $fing_min $fingY_max $fingY_spread $fingY_mean $fingY_std $fingY_mean_2sign $fingY_mean_2sigp $fingX_min $fingX_max $fingX_spread $fingX_mean $fingX_std $fingX_mean_2sign $fingX_mean_2sigp $noisY_min $noisY_max $noisY_spread $noisY_mean $noisY_std $noisY_mean_2sign $noisY_mean_2sigp $noisX_min $noisX_max $noisX_spread $noisX_mean $noisX_std $noisX_mean_2sign $noisX_mean_2sigp\n";
print $OUTPUT_M1 ("spi_speed_ME1_lift_x spi_speed_ME1_lift_y fingY_min fingY_max fingY_spread fingY_mean fingY_std fingY_mean_2sign fingY_mean_2sigp fingX_min fingX_max fingX_spread fingX_mean fingX_std fingX_mean_2sign fingX_mean_2sigp noisY_min noisY_max noisY_spread noisY_mean noisY_std noisY_mean_2sign noisY_mean_2sigp noisX_min noisX_max noisX_spread noisX_mean noisX_std noisX_mean_2sign noisX_mean_2sigp\n");

print $OUTPUT_M0 ("spi_speed_ME0_lift_x spi_speed_ME0_lift_y M0_fingY_min M0_fingY_max M0_fingY_spread M0_fingY_mean M0_fingY_std M0_fingY_mean_2sign M0_fingY_mean_2sigp M0_fingX_min M0_fingX_max M0_fingX_spread M0_fingX_mean M0_fingX_std M0_fingX_mean_2sign M0_fingX_mean_2sigp\n");

while (<$INPUT_FILE>)
{
  # Remove ANSI escape sequences, with Greedy substitution to empty string commands 
  s/\e\[J//g;
  s/\e\[K//g;
  s/\e\[;H//g;
  
	if(m/^spiME1x,(\S+)\s+,spiME1y,(\S+)\s+,M1_fingX,(\S+)\s+,M1_fingY,(\S+)\s+,M1_noisX,(\S+)\s+,M1_noisY,(\S+)\s+,spiME0x,(\S+)\s+,spiME0y,(\S+)\s+,M0_fingY,(\S+)\s+,M0_fingX,(\S+)\s+/)
	{
		if ($first)
		{
			$first = 0;
			$m1spix = $1;
# read M1 value
			$m1spiy = $2;
			
			$fingX_min = $3;
			$fingX_max = $3;
			
			$fingY_min = $4;
			$fingY_max = $4;
			
			$noisX_min = $5;
			$noisX_max = $5;
			
			$noisY_min = $6;
			$noisY_max = $6;
			
			$fingX_mean = $3;
			$fingY_mean = $4;
			$noisX_mean = $5;
			$noisY_mean = $6;
			
			$fingX_std = $3*$3;
			$fingY_std = $4*$4;
			$noisX_std = $5*$5;
			$noisY_std = $6*$6;

# read M0 values
			$m0spix = $7;
			$m0spiy = $8;
			
			$m0_fingY_min = $9;
			$m0_fingY_max = $9;
			
			$m0_fingX_min = $10;
			$m0_fingX_max = $10;
			
			$m0_fingY_mean = $9;
			$m0_fingX_mean = $10;
			
			$m0_fingY_std = $9*$9;
			$m0_fingX_std = $10*$10;
						
			$cnt=1;
			
		}
		elsif (($m1spix != $1)||($m1spiy != $2))
		{
# calculate M1 values			
			$fingY_mean = $fingY_mean/$cnt;
			$fingX_mean = $fingX_mean/$cnt;
			$noisY_mean = $noisY_mean/$cnt;
			$noisX_mean = $noisX_mean/$cnt;
			
			$fingY_std = sqrt(($fingY_std/$cnt)-($fingY_mean*$fingY_mean));
			$fingX_std = sqrt(($fingX_std/$cnt)-($fingX_mean*$fingX_mean));
			$noisY_std = sqrt(($noisY_std/$cnt)-($noisY_mean*$noisY_mean));
			$noisX_std = sqrt(($noisX_std/$cnt)-($noisX_mean*$noisX_mean));
			
			$fingY_mean_2sign = $fingY_mean-(2*$fingY_std);
			$fingY_mean_2sigp = $fingY_mean+(2*$fingY_std);
			$fingX_mean_2sign = $fingX_mean-(2*$fingX_std);
			$fingX_mean_2sigp = $fingX_mean+(2*$fingX_std);
			$noisY_mean_2sign = $noisY_mean-(2*$noisY_std);
			$noisY_mean_2sigp = $noisY_mean+(2*$noisY_std);
			$noisX_mean_2sign = $noisX_mean-(2*$noisX_std);
			$noisX_mean_2sigp = $noisX_mean+(2*$noisX_std);
			
			print $OUTPUT_M1 ("$m1spix $m1spiy $fingY_min $fingY_max $fingY_spread $fingY_mean $fingY_std $fingY_mean_2sign $fingY_mean_2sigp $fingX_min $fingX_max $fingX_spread $fingX_mean $fingX_std $fingX_mean_2sign $fingX_mean_2sigp $noisY_min $noisY_max $noisY_spread $noisY_mean $noisY_std $noisY_mean_2sign $noisY_mean_2sigp $noisX_min $noisX_max $noisX_spread $noisX_mean $noisX_std $noisX_mean_2sign $noisX_mean_2sigp\n");

# calculate m0 values
			$m0_fingY_mean = $m0_fingY_mean/$cnt;
			$m0_fingX_mean = $m0_fingX_mean/$cnt;
			
			$m0_fingY_std = sqrt(($m0_fingY_std/$cnt)-($m0_fingY_mean*$m0_fingY_mean));
			$m0_fingX_std = sqrt(($m0_fingX_std/$cnt)-($m0_fingX_mean*$m0_fingX_mean));
			
			$m0_fingY_mean_2sign = $m0_fingY_mean-(2*$m0_fingY_std);
			$m0_fingY_mean_2sigp = $m0_fingY_mean+(2*$m0_fingY_std);
			$m0_fingX_mean_2sign = $m0_fingX_mean-(2*$m0_fingX_std);
			$m0_fingX_mean_2sigp = $m0_fingX_mean+(2*$m0_fingX_std);
			
			print $OUTPUT_M0 ("$m0spix $m0spiy $fingY_min $m0_fingY_max $m0_fingY_spread $m0_fingY_mean $m0_fingY_std $m0_fingY_mean_2sign $m0_fingY_mean_2sigp $m0_fingX_min $m0_fingX_max $m0_fingX_spread $m0_fingX_mean $m0_fingX_std $m0_fingX_mean_2sign $m0_fingX_mean_2sigp\n");

# read M1 values			
			$m1spix = $1;
			$m1spiy = $2;
			
			$fingX_min = $3;
			$fingX_max = $3;
			
			$fingY_min = $4;
			$fingY_max = $4;
			
			$noisX_min = $5;
			$noisX_max = $5;
			
			$noisY_min = $6;
			$noisY_max = $6;
			
			
			$fingX_mean = $3;
			$fingY_mean = $4;
			$noisX_mean = $5;
			$noisY_mean = $6;
			
			$fingX_std = $3*$3;
			$fingY_std = $4*$4;
			$noisX_std = $5*$5;
			$noisY_std = $6*$6;

# read m0 values
			$m0spix = $7;
			$m0spiy = $8;
			
			$m0_fingY_min = $9;
			$m0_fingY_max = $9;
			
			$m0_fingX_min = $10;
			$m0_fingX_max = $10;
			
			$m0_fingY_mean = $9;
			$m0_fingX_mean = $10;
			
			$m0_fingY_std = $9*$9;
			$m0_fingX_std = $10*$10;
	
			$cnt=1;
		} 
		else
		{
# calculate M1 values
			if ($3 < $fingX_min) { $fingX_min = $3; }
			if ($3 > $fingX_max) { $fingX_max = $3; }
			if ($4 < $fingY_min) { $fingY_min = $4; }
			if ($4 > $fingY_max) { $fingY_max = $4; }			
			if ($5 < $noisX_min) { $noisX_min = $5; }
			if ($5 > $noisX_max) { $noisX_max = $5; }
			if ($6 < $noisY_min) { $noisY_min = $6; }
			if ($6 > $noisY_max) { $noisY_max = $6; }

			$fingY_spread = $fingY_max - $fingY_min;
			$fingX_spread = $fingX_max - $fingX_min;
			$noisY_spread = $noisY_max - $noisY_min;
			$noisX_spread = $noisX_max - $noisX_min;
			
			$fingX_mean = $fingX_mean + $3;
			$fingY_mean = $fingY_mean + $4;
			$noisX_mean = $noisX_mean + $5;
			$noisY_mean = $noisY_mean + $6;
			
			$fingX_std = $fingX_std + $3*$3;
			$fingY_std = $fingY_std + $4*$4;
			$noisX_std = $noisX_std + $5*$5;
			$noisY_std = $noisY_std + $6*$6;

# calculate m0 values			
			if ($9 < $m0_fingY_min) { $m0_fingY_min = $9; }
			if ($9 > $m0_fingY_max) { $m0_fingY_max = $9; }
			if ($10 < $m0_fingX_min) { $m0_fingX_min = $10; }
			if ($10 > $m0_fingX_max) { $m0_fingX_max = $10; }			

			$m0_fingY_spread = $m0_fingY_max - $m0_fingY_min;
			$m0_fingX_spread = $m0_fingX_max - $m0_fingX_min;
			
			$m0_fingY_mean = $m0_fingY_mean + $9;
			$m0_fingX_mean = $m0_fingX_mean + $10;
			
			$m0_fingY_std = $m0_fingY_std + $9*$9;
			$m0_fingX_std = $m0_fingX_std + $10*$10;

			$cnt++;
		}
	} 
}

# close all filehandles
close($INPUT_FILE);
close($OUTPUT_M1);
close($OUTPUT_M0);
