;;; -*- lexical-binding: t -*- ;; ;; this file: ;; https://dataswamp.org/~incal/emacs-init/variance.el ;; ;; For a file with tick-time integers (one at each line) ;; compute how much the ticks deviates from the desired ;; fixed-interrupt rate. (require 'cl-lib) (defun get-variance (mean) (save-excursion (goto-char (point-min)) (let ((sum 0) (offsets 0)) (cl-loop (let ((offset (thing-at-point 'number))) (if offset (progn (cl-incf offsets) (setq sum (+ sum (expt (- offset mean) 2))) (forward-line) ) (cl-return) ))) (/ sum offsets) ))) (defun tick-stats (desired-tick) (interactive "n desired tick: ") (save-excursion (goto-char (point-min)) (let ((sum 0) (offsets 0) (max) (min) ) (cl-loop (let((t0 (thing-at-point 'number))) (forward-line) (let((t1 (thing-at-point 'number))) (save-current-buffer (set-buffer (get-buffer-create "offsets.log")) (if t1 (let((offset (- t1 t0 desired-tick))) (cl-incf offsets) (setq sum (+ sum offset)) (when (or (not max) (> offset max)) (setq max offset)) (when (or (not min) (> min offset)) (setq min offset)) (insert (format "%d\n" offset)) ) (goto-char (point-min)) (insert (let*((mean (/ sum offsets)) (variance (get-variance mean)) ) (format "readings: %d\nmean: %f\nvariance: %f\nstandard deviation: %f\nmin: %d\nmax: %d\n\n" offsets mean variance (sqrt variance) min max))) (write-file "stats.log") (cl-return) )))))))) (provide 'variance)