-
Notifications
You must be signed in to change notification settings - Fork 1
/
toCDF.pl
executable file
·70 lines (53 loc) · 1.33 KB
/
toCDF.pl
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
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl -w
use strict;
my $in_handle = \*STDIN;
my $granularity = 100;
$granularity = shift(@ARGV) if (scalar(@ARGV) > 0);
my @data;
my $min_datum;
my $max_datum;
LINE: while (my $line = <$in_handle>) {
next if $line =~ /^\s*$/ || $line =~ /^\s*#/; # skip comments and blank lines
chomp($line);
my @fields = split(/\t/, $line);
my $value = 0 + $fields[0];
if (defined($max_datum)) {
if ($value > $max_datum) {
$max_datum = $value;
}
}
else {
$max_datum = $value;
}
if (defined($min_datum)) {
if ($value < $min_datum) {
$min_datum = $value;
}
}
else {
$min_datum = $value;
}
push @data, $value;
}
close($in_handle);
# sort data, ascending
@data = sort { $a <=> $b } @data;
my $x_spread = $max_datum - $min_datum;
my $y_step = 1 / $granularity;
my $last_print_x = 0;
my $last_print_y = 0;
# print first datum
print "$min_datum\t0\n";
my ($this_x, $this_y);
for (my $i=0; $i < @data; $i++) {
$this_x = ($data[$i] - $min_datum) / $x_spread;
$this_y = $i/scalar(@data);
# if this_y is more than 1/[granularity] greater than last_print_y,
# then its time for a new data point to be printed
if ($this_y >= ($last_print_y + $y_step)) {
print "$data[$i]\t$this_y\n";
$last_print_x = $this_x;
$last_print_y = $this_y;
}
}
# done