File: floating-average

package info (click to toggle)
libstxxl 1.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 5,256 kB
  • ctags: 6,830
  • sloc: cpp: 39,594; ansic: 4,217; perl: 566; sh: 555; xml: 174; makefile: 21
file content (60 lines) | stat: -rwxr-xr-x 1,406 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/perl -w

############################################################################
#  misc/floating-average
#
#  Part of the STXXL. See http://stxxl.sourceforge.net
#
#  Copyright (C) 2007 Johannes Singler <singler@ira.uka.de>
#  Copyright (C) 2010 Andreas Beckmann <beckmann@cs.uni-frankfurt.de>
#
#  Distributed under the Boost Software License, Version 1.0.
#  (See accompanying file LICENSE_1_0.txt or copy at
#  http://www.boost.org/LICENSE_1_0.txt)
############################################################################

# Calculate the floating average over $1 values in all columns
# Reading from stdin, writing to stdout
# Example: floating-average 3 <original >averaged

use strict;

my $span = $ARGV[0];
my @sums;
my @history;
my $count_sum = 1;
my $line_number = 0;
my $line;

while($line = <STDIN>)
{
  my @parts = split /\s+/, $line;

  my $c = 0;
  my $part;
  foreach $part(@parts)
  {
    if($part =~ /^[+-]?\d+\.?\d*([eE][+-]?\d+)?$/)
    { #is number
      if($count_sum == $span)
      {
        $sums[$c] -= $history[$line_number % $span][$c] || 0;
      }
      $history[$line_number % $span][$c] = $part;
      $sums[$c] += $history[$line_number % $span][$c];
      printf("%14.6f ", $sums[$c] / $count_sum);
      ++$c;
    }
    else
    {
      print(" $part ");
    }
  }
  print("\n");

  if($count_sum < $span)
  {
    ++$count_sum;
  }
  ++$line_number;
}