Monthly Archives: June 2012

Reducing large data file with awk

I have a data file with 20000 lines collected with an Arduino datalogger that I have built. When I tried to plot all these lines with gnuplot, I got a 400K graph EPS, which worked great, but loaded slowly.

In order to get a lighter graph, here’s what I did:

Each line of the datafile looks like this:

1340622508 26.12 27.65 129.70 78.90 22.97

Fist column is the Unix timestamp of the acquired data, followed by the measurements.

I simply wrote an AWK script that averages each column in groups of 100. You can change this number, by setting N=100 to another value.

Heres the script:

awk ‘{N=100; sum1+=$1; sum2+=$2; sum3+=$3; sum4+=$4; sum5+=$5; sum6+=$6} (NR%N)==0 {printf(“%d %.2f %.2f %.2f %.2f %.2f\r\n”, sum1/N, sum2/N, sum3/N, sum4/N, sum5/N, sum6/N); sum1=0; sum2=0; sum3=0; sum4=0; sum5=0; sum6=0;}’ input.csv > output.dat

Thanks to the powers of awk 🙂

Joining stdout and stderr: 2>&1

Needed to get stderr with popen:

* Purpose: Program to demonstrate the popen function.
* to do: Check that the ‘popen’ was successfull.
* Author: M J Leslie.
* Date: 08-Jan-94


FILE *fp;
char line[130]; /* line of data from unix command*/

fp = popen(“ls -l”, “r”); /* Issue the command. */

/* Read a line */
while ( fgets( line, sizeof line, fp))
printf(“%s”, line);

– popen will always execute the command from within the Bourne shell. – popen feeds the STDOUT back to your program. If you want STDERR, the following will do the trick.

fp=popen(“ls -l 2>&1”, “w”);


Remotely setting TI eZ430 Chronos display

I was playing with some stuff and wanted to dynamically monitor a sensor, and see its value on the eZ430 Chronos watch.

Here’s how I did it on Linux, but works on Windows also:
Copy eZ430-Chronos CC 1_2.tcl to display.tcl

edit display.tcl

On line 1466, before “Send heart rate to RF access point”, add these lines #Rafael
set f [open /tmp/ez]
while {[gets $f line] >= 0} {
puts “Reading file /tmp/ez”
puts “Value: $line”
set hr $line
puts “hr=$hr”
close $f

#end rafael


catch { BM_BR_SetHeartrate $hearthate } res^M


catch { BM_BR_SetHeartrate $hr } res^M

Start the display script and click on Start Bluerobin simulator. Select heartrate on the watch and press up button to start receiving data. From now on, every number put on /tmp/ez will be shown in the watch.

for i in `seq 1 20`; do echo $i > /tmp/ez; sleep 2; done