#!/bin/sh
# computes first and second derivatives of (sorted) stdin
# new version using parabolic fits to tripel

grep -v "#" | awk '{x=$1; if ((NR>1)&&((x-xold)**2>0.0000001)) {print $0; xold=x}}' | \
awk '{xnew=$1; ynew=$2; n+=1; if (NF==0) {n=0; printf "\n"} if (n>2) \
{xl=xold-x; xr=xnew-x; yl=yold-y; yr=ynew-y; \
dy=(xr**2*yl-xl**2*yr)/(xl*xr*(xr-xl)); \
d2y=2*(xr*yl-xl*yr)/(xl*xr*(xl-xr)); \
print x, y, dy, d2y} xold=x; x=xnew; yold=y; y=ynew}'
