All pastes #523677 Raw Edit

Unnamed

public text v1 · immutable
#523677 ·published 2007-05-30 21:20 UTC
rendered paste body

(define (normalize-impl n mult)
  (cond
    ((=  n 0)   (list 0 1))
    ((>= n 1)   (normalize-impl (* n 0.1) (* mult 10 )))
    ((< n  0.1) (normalize-impl (* n 10 ) (* mult 0.1)))
    (else (list n mult))))

(define (normalize x)
  (normalize-impl x 1))

(define (nearest-impl val optlst higher lower)
  (if (null? optlst) (list higher lower)
    (let ((next (car optlst))
          (rest (cdr optlst)))
      (cond
       ((< val next) (nearest-impl val rest (min higher next) lower))
       (else (nearest-impl val rest higher (max lower next)))))))

(define (nearest val lst)
  (nearest-impl val lst (apply max lst) (apply min lst)))

(define (nearest-wrap val lst)
  (nearest-impl val lst (* 10 (apply min lst)) (* 0.1 (apply max lst))))


; \brief Returns values in a preferred resistor series
;
; \par Function description
; Given a resistor series "E6" "E12" or "E24", return a list of the normalised values.
;
; \param[in]  series  Resistor seres in use 'E6', 'E12' or 'E24'
; \return The values in the resistor series, normalised to be >= 0.1 and < 1.0
;
(define (res_values series)
  (cond
    ((string= series "E6")
      '( 0.10        0.15        0.22        0.33        0.47        0.68 )
    )
    ((string= series "E12")
      '( 0.10  0.12  0.15  0.18  0.22  0.27  0.33  0.39  0.47  0.56  0.68  0.82 )
    )
    ((string= series "E24")
      '( 0.10  0.11  0.12  0.13  0.15  0.16  0.18  0.20  0.22  0.24  0.27  0.30
         0.33  0.36  0.39  0.43  0.47  0.51  0.56  0.62  0.68  0.75  0.82  0.91
    )
    (else ())))
)

; \brief Resistor prefered value helper
;
; \par Function description
; Given a resistor series and desired value, return a list of close matches.
;
; \param[in]  series  Resistor seres in use 'E6', 'E12' or 'E24'
; \param[in]  value   Resistor value desired
; \return  A list of closest prefered values
;
(define (preferred series val)
  (if (= val 0)
    0
    (let* (
            (norm (normalize val))
            (norm_val (car  norm))
            (norm_mul (cadr norm))
          )
          (map
            (lambda (x) (* x norm_mul))
            (nearest-wrap norm_val (res_values series))
          ))
  )
)