Jump to content

Featured Replies

Posted

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)))
            for add = (second item) then (+ add (second item))
            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)))

 

  • Author

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

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

  • Author

 

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))

 

  • Author

"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)))
            for add = (second item) then (+ add (second item))
            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)) 

 

Create an account or sign in to comment


Copyright © 2014-2025 Opusmodus™ Ltd. All rights reserved.
Product features, specifications, system requirements and availability are subject to change without notice.
Opusmodus, the Opusmodus logo, and other Opusmodus trademarks are either registered trademarks or trademarks of Opusmodus Ltd.
All other trademarks contained herein are the property of their respective owners.

Powered by Invision Community

Important Information

Terms of Use Privacy Policy