# hysteresis-function

## Recommended Posts

here is a little function, to use or optimize...

greetings

andré

```
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; a little lisp-function that's switches by a "hysteresis" from one value to the other ;;;;;;;;;;;;;;;;;;;;;;;
;;;; could be used for any-value
;;;; :start-weight -> tendency at the beginning
;;;; :sensitivity -> change-step of tendency when switch/match
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;; subfunctions

(defun weighted-random (list)
(loop for item in list
with rand-num = (random (loop for x in list sum (second x)))
when (< rand-num add) return (first item)))

(defun weighted-t/nil (on-weight)
(let ((off-weight (- 1 on-weight)))
(weighted-random (list (list 't on-weight) (list 'nil off-weight)))))

(weighted-t/nil 0.5)

;;;; mainfunction

(defun binary-hysteresis-1.0 (&key number-of-values (values '(0 1)) (start-weight 0.1) (sensitivity 0.02))
(loop repeat number-of-values
with weight = start-weight
with cnt = 0
when (equal (weighted-t/nil weight) 'nil)
collect (first values)  else collect (second values) and do (incf cnt)
when (= cnt 3) do (setq weight (+ weight sensitivity)
cnt 0)))

;;;; example with 0/1
(length-list-plot (binary-hysteresis-1.0 :number-of-values 200
:values '(0 1)
:start-weight 0.1
:sensitivity 0.07))

;;;; example with two pitches
(make-omn :pitch (binary-hysteresis-1.0 :number-of-values 200
:values '(c4 c5)
:start-weight 0.05
:sensitivity 0.1)
:length (gen-repeat 200 '(1/32)))```

##### Share on other sites

hI André,

thanks for this but it seem we need the function weighted-random:

"In weighted-t/nil: Undefined function weighted-random"

S.

##### Share on other sites

you are wright! :-) violà!! have a look to the edited post!

there was also a "BUG" in the code, now it's seems okay!!

##### Share on other sites

BINARY-HYSTERESIS-1.1

version with :start-point (default is ( /  numbers-of-values 2))

it's interesting to play with :tendency and :start-weight

for example -> start-weight = high (ca. 0.5) -> oscillation is beginning quickly (at start-point) but when tendency is low it needs time to switch definitly... etc...

```(defun binary-hysteresis-1.1 (&key number-of-values (values '(0 1)) (start-weight 0.1) (sensitivity 0.02) (start-point (if (evenp number-of-values)
(/ number-of-values 2)
(/ (1- number-of-values) 2))))
(loop repeat number-of-values
with weight = start-weight
with cnt1 = 0
for cnt2 = 0 then (incf cnt2)
when (or (equal (weighted-t/nil weight) 'nil)
(< cnt2 start-point))
collect (first values) else collect (second values) and do (incf cnt1)
when (= cnt1 3) do (setq weight (+ weight sensitivity)
cnt1 0)))

;;;; example with 0/1
(length-list-plot (binary-hysteresis-1.1 :number-of-values 200
:values '(0 1)
:start-weight 0.4
:sensitivity 0.05
:start-point 80))```

##### Share on other sites

Thanks you for this interesting hysterical functions ;-)

SB.

##### Share on other sites

"hysterical" with a value-list = multiple states :-)

```;;;; SUBFUNCTIONS

(defun weighted-random (list)
(loop for item in list
with rand-num = (random (loop for x in list sum (second x)))
when (< rand-num add) return (first item)))

(defun weighted-t/nil (on-weight)
(let ((off-weight (- 1 on-weight)))
(weighted-random (list (list 't on-weight) (list 'nil off-weight)))))

(weighted-t/nil 0.5)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;; MAINFUNCTION

(defun binary-hysteresis-1.2 (&key number-of-values (value-list '((0 1) (4 0) (0 1) (4 7))) (start-weight 0.1) (sensitivity 0.02) (static 1.0))
(loop repeat number-of-values
with weight = start-weight
with cnt1 = 0
with cnt-v = 0
when (equal (weighted-t/nil weight) 'nil)
collect (first (nth cnt-v value-list)) else collect (second (nth cnt-v value-list)) and do (incf cnt1)
when (= cnt1 3) do (setq weight (+ weight sensitivity)
cnt1 0)

when (> weight static)
do (incf cnt-v)
and do (setq weight start-weight)
when (= cnt-v (length value-list)) do (setq cnt-v 0)))

;;example
(length-list-plot (binary-hysteresis-1.2 :number-of-values 600
:start-weight 0.1
:sensitivity 0.05
:value-list '((0 1) (10 2) (7 3))
:static 2.0)) ```

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
• #### Browser

• Video Gallery

• Lessons