Skip to main content

Poor Man's Integrate

Well, not so poor after all. If you have data you'd like to integrate, such as spectra of any kind, there's no need for octave or other numerical software (at that point). You can do that with the bash. How? Easy:

1
2
3
4
5
6
7
8
#!/bin/bash
sum=$(awk '{sum += $2} END { printf ("%f\n", sum) }' $1)
points=$(cat $1 | wc -l)
firstx=$(cut -f 1 $i | head -n 1)
lastx=$(cut -f 1 $i | tail -n 1)
delx=$(calc $lastx-$firstx)
int=$(calc $delx*$sum/$points)
echo $int

The command 'calc' refers to any of the one-line calculators described here. The script integrates the second column ("$2") of your data file. Save it as 'pmi' and call it with 'pmi data.dat'. The script takes 90 ms for 100000 values on my machine (measured with haui's great 'time' program). Haui might be able to reduce this execution time to a mere blink of the eye by perl magic. 8)

Contents © 2018 Cobra · About · Privacy · Powered by Nikola · Creative Commons License BY-NC-SA