diff options
| -rw-r--r-- | binom_coeff.awk | 6 | ||||
| -rw-r--r-- | conv.awk | 1 | ||||
| -rw-r--r-- | cov.awk | 23 | ||||
| -rw-r--r-- | det.awk | 14 | ||||
| -rw-r--r-- | diff.awk | 10 | ||||
| -rw-r--r-- | diff1.awk | 10 | ||||
| -rw-r--r-- | em.awk | 7 | ||||
| -rw-r--r-- | exp_reg.awk | 16 | ||||
| -rw-r--r-- | fib.awk | 3 | ||||
| -rw-r--r-- | fir_window.awk (renamed from hamming.awk) | 11 | ||||
| -rw-r--r-- | fir_window_gaussian.awk (renamed from gaussian.awk) | 6 | ||||
| -rw-r--r-- | fir_window_hamming.awk | 41 | ||||
| -rw-r--r-- | fir_window_vonhann.awk | 35 | ||||
| -rw-r--r-- | kalman.awk | 30 | ||||
| -rw-r--r-- | kalman_init.awk | 26 | ||||
| -rw-r--r-- | lin_reg.awk | 17 | ||||
| -rw-r--r-- | lin_reg1.awk | 14 | ||||
| -rw-r--r-- | lin_reg2.awk | 14 | ||||
| -rw-r--r-- | lpf.awk | 24 | ||||
| -rw-r--r-- | ludcmp.awk | 8 | ||||
| -rw-r--r-- | lupd.awk | 13 | ||||
| -rw-r--r-- | mean.awk | 5 | ||||
| -rw-r--r-- | mean_avg.awk | 12 | ||||
| -rw-r--r-- | pi.awk | 5 | ||||
| -rw-r--r-- | quad_reg.awk | 16 | ||||
| -rw-r--r-- | sterling_approx.awk | 12 | ||||
| -rw-r--r-- | sum1.awk | 28 | ||||
| -rw-r--r-- | sum2.awk | 19 | ||||
| -rw-r--r-- | sum3.awk | 15 | ||||
| -rw-r--r-- | sum4.awk | 13 |
30 files changed, 325 insertions, 129 deletions
diff --git a/binom_coeff.awk b/binom_coeff.awk index a19f464..6896671 100644 --- a/binom_coeff.awk +++ b/binom_coeff.awk @@ -1,7 +1,9 @@ -#! /usr/bin/awk -f +#!/usr/bin/awk -f ### binomial coeffecient # https://rosettacode.org/wiki/Evaluate_binomial_coefficients + + function binom(n, k) { b = 1 for (i=1; i<(k+1); i++) { @@ -10,9 +12,11 @@ function binom(n, k) { return b } + BEGIN { ARGV[1] ? N = ARGV[1] : N = 1 ARGV[2] ? K = ARGV[2] : K = 1 ARGV[3] ? OFMT = "%." ARGV[3] "g" : OFMT = "%g" printf(OFMT ORS, binom(N, K)) } + @@ -3,6 +3,7 @@ ### conv.awk
# [PoC] linear convolution (with hardcoded IR window).
+
BEGIN {
ARGV[1] ? X = ARGV[1] : X = 0
ARGV[2] ? OFMT = "%." ARGV[2] "g" : OFMT = "%g"
@@ -3,6 +3,7 @@ ### cov.awk # online covariance algorithm + BEGIN { OFS = FS sign = "[+-]?" @@ -12,18 +13,20 @@ BEGIN { number = "^" sign "(" decimal "|" fraction ")" exponent "$" } + +# column headers NR == 1 { - for (y=1; y<=NF; y++) - ($y ~ number) ? header[y] = "col" y : header[y] = $y + for (n=1; n<=NF; n++) + ($n ~ number) ? header[n] = "col" n : header[n] = $n } + NF > 0 { - if (NF > nf_max) - nf_max = NF + (NF > nf_max) ? nf_max = NF : nf_max = nf_max - ### columns + # columns for (y=1; y<=nf_max; y++) { - ### rows + # rows for (x=1; x<=nf_max; x++) { if ($x !~ number || $x == header[x]) continue @@ -39,18 +42,19 @@ NF > 0 { } } + END { - ### column headers + # column headers printf("cov") for (y=1; y<=nf_max; y++) { printf(OFS header[y]) } printf(ORS) - ### columns + # columns for (y=1; y<=nf_max; y++) { printf(header[y] OFS) - ### rows + # rows for (x=1; x<=nf_max; x++) { printf(OFMT, cov_samp[x,y]) if (x < nf_max) @@ -59,3 +63,4 @@ END { printf(ORS) } } + @@ -5,6 +5,7 @@ # input: square array as delimited text # output: (scalar) determinant + BEGIN { OFS = "\t" sign = "[+-]?" @@ -14,17 +15,21 @@ BEGIN { number = "^" sign "(" decimal "|" fraction ")" exponent "$" } + +# column headers NR == 1 { for (y=1; y<=NF; y++) ($y ~ number) ? header[y] = "col" y : header[y] = $y } + +# read input data NF > 0 { (NF > nf_max) ? nf_max = NF : nf_max = nf_max - ### columns + # columns for (y=1; y<=nf_max; y++) { - ### rows + # rows if ($y !~ number) { continue } @@ -36,12 +41,13 @@ NF > 0 { } } + END { print length(row_sum), length(col_sum) - ### columns + # columns for (y=1; y<=nf_max; y++) { if (y in col_sum) { - ### rows + # rows for (x=1; x<=NR; x++) { if (x in row_sum) { printf("[" OFMT "," OFMT "]" OFS OFMT OFS OFMT, @@ -3,9 +3,9 @@ ### diff.awk # print numerical diff along columns + BEGIN { OFS = FS - # OFMT = "%.9g" sign = "[+-]?" decimal = "[0-9]+[.]?[0-9]*" fraction = "[.][0-9]*" @@ -13,6 +13,8 @@ BEGIN { number = "^" sign "(" decimal "|" fraction ")" exponent "$" } + +# column headers NR == 1 { # orig data columns for (n=1; n<=NF; n++) { @@ -35,9 +37,9 @@ NR == 1 { } } -NF { - if (NF > nf_max) - nf_max = NF + +NF > 0 { + (NF > nf_max) ? nf_max = NF : nf_max = nf_max # data columns for (n=1; n<=nf_max; n++) { @@ -3,9 +3,9 @@ ### diff1.awk # print numerical diff along columns + BEGIN { OFS = FS - # OFMT = "%.9g" sign = "[+-]?" decimal = "[0-9]+[.]?[0-9]*" fraction = "[.][0-9]*" @@ -13,6 +13,8 @@ BEGIN { number = "^" sign "(" decimal "|" fraction ")" exponent "$" } + +# column headers NR == 1 { # orig data columns for (n=1; n<=NF; n++) { @@ -29,9 +31,9 @@ NR == 1 { } } -NF { - if (NF > nf_max) - nf_max = NF + +NF > 0 { + (NF > nf_max) ? nf_max = NF : nf_max = nf_max # data columns for (n=1; n<=nf_max; n++) { @@ -1,9 +1,13 @@ #!/usr/bin/awk -f +### em.awk +# list of physical EM constants + + BEGIN { ARGV[1] ? OFMT = "%." ARGV[1] "g" : OFMT = "%.12g" - ### constants + # constants pi = 4.0*atan2(1,1) # rad c0 = 299792458E0 # m/s, exact mu0 = (4.0E-7)*pi # H/m = (T*m)/A = N/(A^2) = (kg*m)/(A*s) @@ -11,3 +15,4 @@ BEGIN { print pi, c0, mu0, epsilon0 } + diff --git a/exp_reg.awk b/exp_reg.awk index f4ea12c..fac4b8f 100644 --- a/exp_reg.awk +++ b/exp_reg.awk @@ -3,6 +3,7 @@ ### lin_reg.awk # simple linear regression between columns + BEGIN { OFS = ":" sign = "[+-]?" @@ -12,23 +13,25 @@ BEGIN { number = "^" sign "(" decimal "|" fraction ")" exponent "$" } + +# column headers NR == 1 { for (n=1; n<=NF; n++) ($n ~ number) ? header[n] = "col" n : header[n] = $n } -NF { - if (NF > nf_max) - nf_max = NF - ### iterate over columns +NF > 0 { + (NF > nf_max) ? nf_max = NF : nf_max = nf_max + + # columns for (y=1; y<=nf_max; y++) { if ($y == header[n]) continue if ($y ~ number) { - ### mean + # mean count[y] += 1 data[y] = log($y) sum[y] += data[y] @@ -39,7 +42,7 @@ NF { sum_delta[y] += delta1[y] sum_delta2[y] += delta0[y]*delta1[y] - ### sample variance + # sample variance #(count[y] > 1) ? var[y] = sum_delta2[y]/(count[y] - 1) : var[y] = "" # x = row, y = col, trendline: y = A + Bx @@ -92,6 +95,7 @@ NF { } } + END { for (y=1; y<=nf_max; y++) { for (x=1; x<=nf_max; x++) { @@ -1,6 +1,9 @@ #!/usr/bin/awk -f ### fib.awk +# print nth Fibonacci number, based on R. Hamming's solution to a diff eqn +# form of the sequence + BEGIN { ARGV[1] ? n = ARGV[1] : n = 0 diff --git a/hamming.awk b/fir_window.awk index acfde97..076e5df 100644 --- a/hamming.awk +++ b/fir_window.awk @@ -7,18 +7,18 @@ # 'a0' and 'a1' parameters of the raised cosine.
-### Gaussian
+# Gaussian window
function gaussian(n) {
# sigma <= 0.5
sigma = 0.4
- e[n] = (n)/(sigma*M)
+ en[n] = (n - M)/(sigma*M)
return exp(-0.5*e[n]*e[n])
}
-### Hamming, raised cosine
+# Hamming window (raised cosine)
function hamming(n) {
- ### optimal values for equal-ripple
+ # optimal values for equal-ripple
#a0 = 0.53836
#a1 = 0.46164
@@ -28,7 +28,7 @@ function hamming(n) { }
-### vonHann, raised cosine
+# vonHann (raised cosine)
function vonhann(n) {
a0 = 0.5
a1 = 0.5
@@ -54,6 +54,5 @@ BEGIN { print n + M, 1.0
}
}
-
}
diff --git a/gaussian.awk b/fir_window_gaussian.awk index 402ea10..14ebc48 100644 --- a/gaussian.awk +++ b/fir_window_gaussian.awk @@ -1,11 +1,11 @@ #!/usr/bin/awk -f
-### gaussian.awk
+### fir_window_gaussian.awk
# generate a Gaussian window
# https://en.wikipedia.org/wiki/Window_function
-### Gaussian
+# Gaussian window
function gaussian(n) {
# sigma <= 0.5
sigma = 0.4
@@ -35,5 +35,5 @@ BEGIN { print n, 1.0
}
}
-
}
+
diff --git a/fir_window_hamming.awk b/fir_window_hamming.awk new file mode 100644 index 0000000..5660754 --- /dev/null +++ b/fir_window_hamming.awk @@ -0,0 +1,41 @@ +#!/usr/bin/awk -f
+
+### fir_window_hamming.awk
+# generate a Hamming window
+# R.W. Hamming, 'Digital Filters': H = '0.23 0.54 0.23'
+# https://en.wikipedia.org/wiki/Window_function provides a few values for the
+# 'a0' and 'a1' parameters of the raised cosine.
+
+
+# Hamming window (raised cosine)
+function hamming(n) {
+ # optimal values for equal-ripple
+ #a0 = 0.53836
+ #a1 = 0.46164
+
+ a0 = (25.0/46.0)
+ a1 = (21.0/46.0)
+ return a0 + a1*cos((pi*n)/M)
+}
+
+
+BEGIN {
+
+ ARGV[1] ? N = ARGV[1] : N = 0
+ ARGV[2] ? OFMT = "%." ARGV[2] "g" : OFMT = "%g"
+
+ # window interval goes from -M to M
+ M = 0.5*(N - 1)
+ pi = 4*atan2(1,1)
+
+ for (n=-M; n<=M; n++) {
+ if (N > 1 && M > 0) {
+ w[n] = hamming(n)
+ print n + M, w[n]/M
+ }
+ else {
+ print n + M, 1.0
+ }
+ }
+}
+
diff --git a/fir_window_vonhann.awk b/fir_window_vonhann.awk new file mode 100644 index 0000000..3c831c7 --- /dev/null +++ b/fir_window_vonhann.awk @@ -0,0 +1,35 @@ +#!/usr/bin/awk -f
+
+### fir_window_vonhann.awk
+# https://en.wikipedia.org/wiki/Window_function provides a few values for the
+# 'a0' and 'a1' parameters of the raised cosine.
+
+
+# vonHann (raised cosine)
+function vonhann(n) {
+ a0 = 0.5
+ a1 = 0.5
+ return a0 + a1*cos((pi*n)/M)
+}
+
+
+BEGIN {
+
+ ARGV[1] ? N = ARGV[1] : N = 0
+ ARGV[2] ? OFMT = "%." ARGV[2] "g" : OFMT = "%g"
+
+ # window interval goes from -M to M
+ M = 0.5*(N - 1)
+ pi = 4*atan2(1,1)
+
+ for (n=-M; n<=M; n++) {
+ if (N > 1 && M > 0) {
+ w[n] = hamming(n)
+ print n + M, w[n]/M
+ }
+ else {
+ print n + M, 1.0
+ }
+ }
+}
+
@@ -1,7 +1,7 @@ #!/usr/bin/awk -f ### kalman.awk -# simple linear regression between columns + BEGIN { sign = "[+-]?" @@ -11,6 +11,8 @@ BEGIN { number = "^" sign "(" decimal "|" fraction ")" exponent "$" } + +# column headers NR == 1 { header_nf = NF for (n=1; n<=NF; n++) { @@ -18,31 +20,30 @@ NR == 1 { } } -NF != 0 { - #printf("\n%s: %s", NR, $0) - if (NF > max_nf) - max_nf = NF - ### iterate over columns - for (y=1; y<=max_nf; y++) { +NF > 0 { + (NF > nf_max) ? nf_max = NF : nf_max = nf_max + + # columns + for (y=1; y<=nf_max; y++) { if ($y ~ number) { - ### mean + # mean count[y] += 1 sum[y] += $y sum2[y] += $y*$y mean[y] = sum[y]/count[y] - ### difference from the mean + # difference from the mean delta[y] = $y - mean[y] sum_delta[y] += delta[y] sum_delta2[y] += delta[y]*delta[y] - ### sample variance + # sample variance (count[y] - 1) ? var[y] = sum_delta2[y]/(count[y] - 1) : var[y] = 0 # x = row, y = col - for (x=1; x<=max_nf; x++) { + for (x=1; x<=nf_max; x++) { count[x,y] += 1 sum_xy[x,y] += $x*$y sum_delta_xy[x,y] += delta[x]*delta[y] @@ -72,7 +73,7 @@ NF != 0 { b[x,y] = 1 } - ### error estimate + # error estimate err_den[x,y] = count[x,y]*(count[x,y] - 2) if (count[x,y] > 2) { err[x,y] = $y - (a[x,y] + b[x,y]*$x) @@ -93,9 +94,10 @@ NF != 0 { } } + END { - for (y=1; y<=max_nf; y++) { - for (x=1; x<=max_nf; x++) { + for (y=1; y<=nf_max; y++) { + for (x=1; x<=nf_max; x++) { if (x != y && r[x,y]) { printf(OFMT OFS OFMT OFS "(%s)" OFS " = (" OFMT " +/- " OFMT ")(%s)" OFS " + (" OFMT " +/- " OFMT ")"ORS, meas_cov[x,y], est_err_cov[x,y], header[y], b[x,y], b_err[x,y], header[x], diff --git a/kalman_init.awk b/kalman_init.awk index 57f7bbd..757d80b 100644 --- a/kalman_init.awk +++ b/kalman_init.awk @@ -1,7 +1,8 @@ #!/usr/bin/awk -f -### kalman.awk -# kalman csv experiemnt +### kalman_init.awk +# kalman filter csv experiemnt + BEGIN { sign = "[+-]?" @@ -11,6 +12,8 @@ BEGIN { number = "^" sign "(" decimal "|" fraction ")" exponent "$" } + +# column headers NR == 1 { header_nf = NF for (n=1; n<=NF; n++) { @@ -18,12 +21,12 @@ NR == 1 { } } -NF != 0 { + +NF > 0 { print ORS NR ":" OFS $0 ORS - if (NF > max_nf) - max_nf = NF + (NF > nf_max) ? nf_max = NF : nf_max = nf_max - ### iterate over columns + # columns for (n=1; n<=max_nf; n++) { if ($n ~ number) { count[n] = ($n ~ number) + (last[n] ~ number) + (last2[n] ~ number) @@ -42,7 +45,7 @@ NF != 0 { meas_err[n] = sqrt(var[n]/count[n]) } - ### error covariance + # error covariance for (m=1; m<=max_nf; m++) { count[m,n]++ sum_xy[m,n] += 1 @@ -52,7 +55,7 @@ NF != 0 { print meas_cov[m,n], est_err_last_cov[m,n] } - ### kalman gain + # kalman gain if (count[n] <= 1) { Gk[n] = 1 est_err[n] = sqrt(diff[n]*diff[n]) + 2.0*sqrt(diff2[n]*diff2[n]) @@ -61,17 +64,17 @@ NF != 0 { else Gk[n] = est_err_last[n]/(est_err_last[n] + meas_err[n]) - ### update estimate and estimated error + # update estimate and estimated error est[n] = est_last[n] + Gk[n]*($n - est_last[n]) est_err[n] = (1.0 - Gk[n])*est_err_last[n] - ### visual check + # visual check printf "meas: " OFS OFMT OFS OFMT OFS OFMT, $n, meas_err[n], count[n] printf "d1: " OFS OFMT, diff[n] printf "d2: " OFS OFMT, diff2[n] printf "est: " OFS OFMT OFS OFMT OFS OFMT, est[n], est_err[n], Gk[n] - ### update previously remembered values + # update previously remembered values last2[n] = last[n] last[n] = $n diff_last2[n] = diff_last[n] @@ -86,3 +89,4 @@ NF != 0 { } } } + diff --git a/lin_reg.awk b/lin_reg.awk index 5a9df56..3006e8a 100644 --- a/lin_reg.awk +++ b/lin_reg.awk @@ -3,6 +3,7 @@ ### lin_reg.awk # simple linear regression between columns + BEGIN { OFS = ":" sign = "[+-]?" @@ -12,23 +13,26 @@ BEGIN { number = "^" sign "(" decimal "|" fraction ")" exponent "$" } + +# column headers NR == 1 { for (n=1; n<=NF; n++) ($n ~ number) ? header[n] = "col" n : header[n] = $n } -NF { - if (NF > nf_max) - nf_max = NF - ### iterate over columns +# read input data +NF > 0 { + (NF > nf_max) ? nf_max = NF : nf_max = nf_max + + # columns for (y=1; y<=nf_max; y++) { if ($y == header[n]) continue if ($y ~ number) { - ### mean + # mean count[y] += 1 sum[y] += $y sum2[y] += $y*$y @@ -38,7 +42,7 @@ NF { sum_delta[y] += delta1[y] sum_delta2[y] += delta0[y]*delta1[y] - ### sample variance + # sample variance #(count[y] > 1) ? var[y] = sum_delta2[y]/(count[y] - 1) : var[y] = "" # x = row, y = col, trendline: y = A + Bx @@ -91,6 +95,7 @@ NF { } } + END { for (y=1; y<=nf_max; y++) { for (x=1; x<=nf_max; x++) { diff --git a/lin_reg1.awk b/lin_reg1.awk index c657035..7786247 100644 --- a/lin_reg1.awk +++ b/lin_reg1.awk @@ -3,6 +3,7 @@ ### lin_reg1.awk # simple linear regression between columns + BEGIN { sign = "[+-]?" decimal = "[0-9]+[.]?[0-9]*" @@ -11,22 +12,26 @@ BEGIN { number = "^" sign "(" decimal "|" fraction ")" exponent "$" } + +# column headers NR == 1 { for (n=1; n<=NF; n++) ($n ~ number) ? header[n] = "col" n : header[n] = $n } + +# read input data NF > 0 { - if (NF > nf_max) - nf_max = NF + (NF > nf_max) ? nf_max = NF : nf_max = nf_max - ### iterate over columns + # columns for (y=1; y<=nf_max; y++) { if ($y == header[n]) continue + if ($y ~ number) { - ### mean + # mean count[y] += 1 sum[y] += $y sum2[y] += $y*$y @@ -62,6 +67,7 @@ NF > 0 { } } + END { for (y=1; y<=nf_max; y++) { for (x=1; x<=nf_max; x++) { diff --git a/lin_reg2.awk b/lin_reg2.awk index 450f0f0..d6097e3 100644 --- a/lin_reg2.awk +++ b/lin_reg2.awk @@ -3,6 +3,7 @@ ### lin_reg2.awk # simple linear regression between columns + BEGIN { sign = "[+-]?" decimal = "[0-9]+[.]?[0-9]*" @@ -11,22 +12,26 @@ BEGIN { number = "^" sign "(" decimal "|" fraction ")" exponent "$" } + +# column headers NR == 1 { for (n=1; n<=NF; n++) ($n ~ number) ? header[n] = "col" n : header[n] = $n } + +# read input data NF > 0 { - if (NF > nf_max) - nf_max = NF + (NF > nf_max) ? nf_max = NF : nf_max = nf_max - ### iterate over columns + # columns for (y=1; y<=nf_max; y++) { if ($y == header[n]) continue + if ($y ~ number) { - ### mean + # mean count[y] += 1 sum[y] += $y sum2[y] += $y*$y @@ -75,6 +80,7 @@ NF > 0 { } } + END { for (y=1; y<=nf_max; y++) { for (x=1; x<=nf_max; x++) { @@ -1,7 +1,8 @@ #!/usr/bin/awk -f
### lpf.awk
-# Low Pass Filter with Hardcoded FIR Window
+# low pass filter with hardcoded FIR window
+
BEGIN {
OFS = FS
@@ -20,19 +21,22 @@ BEGIN { window_size = split(H, H_arr, "[ ]*")
}
+
+# column headers
NR == 1 {
for (y=1; y<=NF; y++)
($y ~ number) ? header[y] = "col" y : header[y] = $y
}
+
+# read input data
NF > 0 {
- if (NF > nf_max)
- nf_max = NF
+ (NF > nf_max) ? nf_max = NF : nf_max = nf_max
input_size = window_size
output_size = (input_size + window_size - 1)
- ### columns
+ # columns
for (y=1; y<=nf_max; y++) {
if ($y == header[y])
printf(header[y] OFS header[y] "_lpf")
@@ -75,15 +79,13 @@ NF > 0 { printf(ORS)
}
-
-
-
}
+
END {
- ### rows
+ # rows
for (x=1; x<=window_size; x++) {
- ### columns
+ # columns
for (y=1; y<=nf_max; y++) {
# rotate input buffer
for (n=1; n<=input_size; n++) {
@@ -93,7 +95,7 @@ END { #delete X_arr[y,input_size]
#input_size = length(X_arr)
#print length(X_arr)
-
+
Y[y] = 0
for (n=1; n<=window_size; n++) {
for (m=1; m<=input_size; m++) {
@@ -120,7 +122,7 @@ END { printf(OFS)
else
printf(ORS)
- X_arr[input_size] = 0
+ X_arr[input_size] = 0
}
}
}
@@ -11,6 +11,7 @@ # note, matrix indexing begins with 1 and uses i=row, j=col # lu = L + U - I + # decomposition of A function lu_decomp(A, n) { @@ -43,6 +44,7 @@ function lu_decomp(A, n) { } } + # regex to identify strings that look like numbers BEGIN { OFS = FS @@ -53,12 +55,14 @@ BEGIN { number = "^" sign "(" decimal "|" fraction ")" exponent "$" } + # column headers NR == 1 { for (n=1; n<=NF; n++) ($n ~ number) ? head[n] = "col" n : head[n] = $n } + # read input data NF { @@ -74,17 +78,17 @@ NF { print(matrix[count[n],n]) } } - } + END { printf(ORS) print(NR, max_nf, size) printf(ORS) lu_decomp(matrix, size) - } + ## find solution of Ly = b, for y #for (i=0; i<n;, i++) { # sum = 0 @@ -9,6 +9,7 @@ # note, matrix indexing begins with 1 and uses i=row, j=col # lu = L + U - I + # solve A[n,n] * x[n] = b[n] function lu_solve(A, b, n) { @@ -56,6 +57,7 @@ function lu_solve(A, b, n) { } + # regex to identify strings that look like numbers BEGIN { OFS = FS @@ -66,19 +68,21 @@ BEGIN { number = "^" sign "(" decimal "|" fraction ")" exponent "$" } + # column headers NR == 1 { for (y=1; y<=NF; y++) ($y ~ number) ? header[y] = "col" y : header[y] = $y } + # read input data NF > 0 { (NF > nf_max) ? nf_max = NF : nf_max = nf_max - ### columns + # columns for (y=1; y<=nf_max; y++) { - ### rows + # rows if ($y !~ number) { continue } @@ -90,12 +94,13 @@ NF > 0 { } } + END { print length(row_sum), length(col_sum) - ### columns + # columns for (y=1; y<=nf_max; y++) { if (y in col_sum) { - ### rows + # rows for (x=1; x<=NR; x++) { if (x in row_sum) { printf("[" OFMT "," OFMT "]" OFS OFMT OFS OFMT, @@ -3,6 +3,7 @@ ### mean.awk
# calculate mean average of serialized input
+
BEGIN {
OFS = FS
#sign = "[+-]?"
@@ -12,8 +13,9 @@ BEGIN { number = "^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]*)([Ee][+-]?[0-9]+)?$"
}
+
# Welford's 'online' algorithm for variance
-NF {
+NF > 0 {
for (n=1; n<=NF; n++) {
if ($n ~ number) {
count += 1
@@ -29,6 +31,7 @@ NF { }
}
+
END {
print "mean", "std_err", "std_dev", "range", "min", "max", "total", "count"
print mean, sqrt(var/count), sqrt(var), range, min, max, (mean*count), count
diff --git a/mean_avg.awk b/mean_avg.awk index bef984d..05c93f1 100644 --- a/mean_avg.awk +++ b/mean_avg.awk @@ -5,6 +5,7 @@ # input: delimited data as text # output: list of univariate summary stats + BEGIN { OFS = FS #sign = "[+-]?" @@ -14,15 +15,19 @@ BEGIN { number = "^[+-]?([0-9]+[.]?[0-9]*|[.][0-9]*)([Ee][+-]?[0-9]+)?$" } + +# column headers NR == 1 { for (n=1; n<=NF; n++) ($n ~ number) ? header[n] = "col" n : header[n] = $n } + # Welford's 'online' algorithm for variance -NF { - if (NF > max_nf) - max_nf = NF +NF > 0 { + (NF > nf_max) ? nf_max = NF : nf_max = nf_max + + # columns for (n=1; n <= NF; n++) { if ($n ~ number) { count[n] += 1 @@ -38,6 +43,7 @@ NF { } } + END { print "col", "mean", "std_err", "std_dev", "range", "min", "max", "total", "count" for (n=1; n<=max_nf; n++) { @@ -1,4 +1,4 @@ -#! /usr/bin/awk -f +#!/usr/bin/awk -f ### pi.awk, https://en.wikipedia.org/wiki/Pi # In 1706 John Machin used the Gregory–Leibniz series to produce an algorithm @@ -11,11 +11,14 @@ # Ferguson–the best approximation achieved without the aid of a calculating # device. + function pi() { return 4*(4*atan2(1,5) - atan2(1,239)) } + BEGIN { ARGV[1] ? OFMT = "%." ARGV[1] "g" : OFMT = "%g" printf(OFMT ORS, pi()) } + diff --git a/quad_reg.awk b/quad_reg.awk index edd0c6f..2682a0f 100644 --- a/quad_reg.awk +++ b/quad_reg.awk @@ -3,6 +3,7 @@ ### quad_reg.awk # quadratic regression along columns + BEGIN { OFS = ":" pi = 4.0*atan2(1.0, 1.0) @@ -13,22 +14,26 @@ BEGIN { number = "^" sign "(" decimal "|" fraction ")" exponent "$" } + +# column headers NR == 1 { for (n=1; n<=NF; n++) ($n ~ number) ? header[n] = "col" n : header[n] = $n } + +# read input data NF > 0 { - if (NF > nf_max) - nf_max = NF + (NF > nf_max) ? nf_max = NF : nf_max = nf_max - ### iterate over columns + # columns for (y=1; y<=nf_max; y++) { if ($y == header[n]) continue + if ($y ~ number) { - ### mean + # mean count[y] += 1 sum[y] += $y sum2[y] += $y*$y @@ -42,7 +47,7 @@ NF > 0 { sum_delta[y] += delta1[y] sum_delta2[y] += delta2[y] - ### sample variance + # sample variance #(count[y] > 1) ? var[y] = sum_delta2[y]/(count[y] - 1) : var[y] = "" # x = row, y = col, trendline: y = A + Bx + Cx^2 @@ -115,6 +120,7 @@ NF > 0 { } } + END { for (x=1; x<=nf_max; x++) { for (y=1; y<=nf_max; y++) { diff --git a/sterling_approx.awk b/sterling_approx.awk index cb5f563..4b0741e 100644 --- a/sterling_approx.awk +++ b/sterling_approx.awk @@ -1,21 +1,20 @@ #!/usr/bin/awk -f - +### sterling_approx.awk # https://en.wikipedia.org/wiki/Sterling_Approximation # An alternative approximation for the Gamma function stated by Srinivasa # Ramanujan (Ramanujan 1988) is -# Gamma(1+x) ~= sqrt(pi)((x/e)^x)(8x^3 + 4x^2 + x + 1/30)^(1/6) -# for x >= 0. The equivalent approximation for ln(n!) has an asymptotic error -# of 1/(1400*n^3) ... +# Gamma(1+x) ~= sqrt(pi)((x/e)^x)(8x^3 + 4x^2 + x + 1/30)^(1/6) +# for x >= 0. The equivalent approximation for ln(n!) has an asymptotic +# error of 1/(1400*n^3) ... -### sterling_approx.awk -# https://en.wikipedia.org/wiki/Stirling%27s_approximation function pwr(x, p) { return p ? exp(p*log(x)) : 1 } + BEGIN { ARGV[1] ? n = ARGV[1] : n = 0 ARGV[2] ? OFMT = "%." ARGV[2] "g" : OFMT = "%g" @@ -26,3 +25,4 @@ BEGIN { } printf(OFMT ORS, f) } + @@ -5,15 +5,35 @@ # output: sum of each column # missing entries are treated as zeros -BEGIN { OFS = FS } -{ - if (NF > nf_max) - nf_max = NF +function isnum(n) { + sign = "[+-]?" + decimal = "[0-9]+[.]?[0-9]*" + fraction = "[.][0-9]+" + exponent = "([Ee]" sign "[0-9]+)?" + number = "^" sign "(" decimal "|" fraction ")" exponent "$" + return n ~ number +} + + +BEGIN { + OFS = FS +} + + +NR == 1 { + for (i=1; i<=NF; i++) + isnum($i) ? header[i] = "col" i : header[i] = $i +} + + +NF > 0 { + (NF > nf_max) ? nf_max = NF : nf_max = nf_max for (i=1; i<=NF; i++) sum[i] += $i } + END { for (i=1; i<=nf_max; i++) { printf(OFMT, sum[i]) @@ -3,18 +3,27 @@ ### sum2.awk, print column sums # check that each line has the same number of fields as line one -BEGIN { OFS = FS } -NR == 1 { nf_max = NF } +BEGIN { + OFS = FS +} + + +NR == 1 { + for (i=1; i<=NF; i++) + isnum($i) ? header[i] = "col" i : header[i] = $i +} + -{ +NF > 0 { + (NF > nf_max) ? nf_max = NF : nf_max = nf_max for (i=1; i<=NF; i++) sum[i] += $i - if (NF != nf_max) - print "line " NR " has " NF " entries, not " nf_max } + END { for (i=1; i<=nf_max; i++) printf(OFMT "%s", sum[i], i < nf_max ? OFS : ORS) } + @@ -11,24 +11,29 @@ function isnum(n) { } -BEGIN { OFS = FS } +BEGIN { + OFS = FS +} + NR == 1 { - nfld = NF for (i=1; i<=NF; i++) numcol[i] = isnum($i) } -{ + +NF > 0 { + (NF > nf_max) ? nf_max = NF : nf_max = nf_max for (i=1; i<=NF; i++) if (numcol[i]) sum[i] += $i } + END { - for (i=1; i<=nfld; i++) { + for (i=1; i<=nf_max; i++) { printf(numcol[i] ? sum[i] : "--") - printf(i < nfld ? OFS : ORS) + printf(i < nf_max ? OFS : ORS) } } @@ -15,17 +15,19 @@ function isnum(n) { } -BEGIN { OFS = FS } +BEGIN { + OFS = FS +} + NR == 1 { - nf_max = NF for (i=1; i<=NF; i++) isnum($i) ? header[i] = "col" i : header[i] = $i } -{ - if (NF > nf_max) - nf_max = NF + +NF > 0 { + (NF > nf_max) ? nf_max = NF : nf_max = nf_max for (i=1; i<=NF; i++) { if ($i == header[i]) continue @@ -36,6 +38,7 @@ NR == 1 { } } + END { for (i=1; i<=nf_max; i++) { printf((header[i]) ? header[i] OFS : OFS) |
