-
Notifications
You must be signed in to change notification settings - Fork 3
/
dylos6ch.pl
executable file
·106 lines (89 loc) · 3.24 KB
/
dylos6ch.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/perl
#Takes in a dylos log file with and without a .csv extension and creates a proper .csv if not created already
#as well as -2ch.csv and -6ch.csv, which are low-passed versions of the original dylos file using an
#exponential weighted average filter. The -2ch.csv file is "columns" 1-3 and 4-6 added together.
#Written by Nolan Hergert for BodyTrack, Summer 2011
use strict;
use warnings;
my $windowSize = 15; #Size of averaging window (seconds)
my $alpha = 2/($windowSize+1); #Calculate time constant (RC)
#http://taylortree.com/exponential-moving-average-ema/
my $delta; #Time difference between readings
#in case we jump a reading
my $line; #Current line
my @values; #Array of values
my $timeOld; #Old Timestamp
my @averages; #Average array
my $count; #Helper var
my $republish = 0;
sub usage {
print STDERR "Usage:\n";
print STDERR "dylos6ch.pl [6 channel dylos data file]\n";
my @candidate_files = glob("*-6ch-*");
if(scalar(@candidate_files)) {
print STDERR "Example:\n";
print STDERR " dylos6ch.pl $candidate_files[0]\n";
}
exit 1;
}
if (!scalar(@ARGV)) { usage(); }
my $name = `basename $ARGV[0] .csv`;
chomp($name);
open (INFIL, "<$ARGV[0]") || die ("Can't open $ARGV[0] for reading");
if (!(-e "$name.csv")) {
open (OUTFIL, ">$name.csv") || die ("Can't open $name.csv for writing");
$republish = 1;
}
open (OUTFIL6CH, ">$name-6ch.csv") || die ("Can't open $name-6ch.csv for writing");
open (OUTFIL2CH, ">$name-2ch.csv") || die ("Can't open $name-2ch.csv for writing");
$line = readline(INFIL);
chomp($line); #remove \n
$line =~ tr/"//d; #Getting rid of all " in line
$line =~ tr/\t/,/d; #Replace tab with comma
@values = split(',',$line);
$timeOld = $values[0];
for ($count = 0; $count < 6; $count++) {
$averages[$count] = $values[$count+1]; #Prevent initial ramp-up
}
write_to_files();
while (<INFIL>) { #Iterates line by line through infil
$line = $_; #Get current line
chomp($line); #remove \n
$line =~ tr/"//d; #Getting rid of all " in line
$line =~ tr/\t/,/d; #Replace tab with comma
@values = split(',',$line);
$delta = $values[0] - $timeOld; #If delta > 1, apply delta to alpha coefficient
$timeOld = $values[0];
#Apply exponential moving average to keep it speedy
#If we miss some samples, trust the previous data that much less
#Also, the Dylos clock is slightly off, resulting in 61 readings in 60 seconds
if ($delta > 0) {
for ($count = 0; $count < 6; $count++) {
$averages[$count] = $alpha*$values[$count+1] + ((1-$alpha) ** $delta)*$averages[$count];
}
write_to_files();
}
}
sub write_to_files
{
if ($republish == 1) {
print OUTFIL join(',',@values);
print OUTFIL "\n";
}
#Print out 6-ch data
print OUTFIL6CH $values[0] . ",";
print OUTFIL6CH int($averages[0]) . ",";
print OUTFIL6CH int($averages[1]) . ",";
print OUTFIL6CH int($averages[2]) . ",";
print OUTFIL6CH int($averages[3]) . ",";
print OUTFIL6CH int($averages[4]) . ",";
print OUTFIL6CH int($averages[5]);
print OUTFIL6CH "\n";
print OUTFIL2CH $values[0] . ",";
print OUTFIL2CH int($averages[0] + $averages[1] + $averages[2]) . ",";
print OUTFIL2CH int($averages[3] + $averages[4] + $averages[5]);
print OUTFIL2CH "\n";
}
close(INFIL);
close(OUTFIL6CH);
close(OUTFIL2CH);