Skip to content

Snippets

Alex Harsányi edited this page Feb 1, 2021 · 2 revisions

Walked distance

#lang racket
(require "al-interactive.rkt")

(define (walked-distance df)
  (and (df-contains? df "dst" "grade")
       (df-fold
        df
        '("dst" "grade")
        0
        (lambda (a p n)
          (if (and p n)
              (match-let ([(list pd pg) p]
                          [(list nd ng) n])
                (if (and pd pg nd ng)
                    (+ a (* (- nd pd) (+ 1 (/ (abs ng) 100.0))))
                    a))
              a)))))

TRIMP

Formula from https://fellrnr.com/wiki/TRIMP, see also https://github.com/alex-hhh/ActivityLog2/issues/48

#lang racket
(require "al-interactive.rkt")

(define (is-running? sport)
  (equal? (vector-ref sport 0) 1))

(define (compute-trimp df min-hr max-hr is-male?)
  ;; NOTE use the "elapsed" series if you want to compute TRIMP over periods
  ;; where recording was stopped.  See docs/sesion-df.md for available data
  ;; series.
  (define previous-timestamp #f)
  (define gender-factor (if is-male? 1.92 1.67))
  (if (and (df-contains? df "timer" "hr")
           (is-running? (df-get-property df 'sport)))
      (df-fold
       df
       '("timer" "hr")
       0                           ; initial TRIMP
       (lambda (curent-trimp current-entry)
         (match-let ([(list timestamp hr) current-entry])
           ;; make sure we have a previous-timestamp (not the first call) and
           ;; this is a good sample (good HR).
           (begin0
               (if (and previous-timestamp hr)
                   (let ([dt (/ (- timestamp previous-timestamp) 60.0)]
                         [hrr (/ (- hr min-hr) (- max-hr min-hr))])
                     (+ curent-trimp (* dt hrr 0.64 (exp gender-factor))))
                   curent-trimp)
             (set! previous-timestamp timestamp)))))
      ;; Return false if TRIMP cannot be computed
      #f))
Clone this wiki locally