wtab.lsp Version 1.00 31 October 2004 Author Steven Jones Contact jones57@swbell.net include the word "nyquist" in subject line The contents of this file are released under the terms of the GNU General Public License. See the file LICENSE.txt Defines functions for the creation of wave tables The file auxwaves.lsp creates some specific wave tables.
function
(wtab:make prtlst [:tabsize][:f0][:ascale]) Generate wave table from list of partials. prtlst - List. The partials List. The harmonic content is specified as a list of partials. Each partial is itself a list of the form (fi ai) where fi is the relative frequency of and ai the relative amplitude of partial i. :tabsize - Integer. The table size. Default *default-table-size* :f0 - Flonum. The reference frequency. All partial frequencies are relative to f0. Default 1.0 :ascale - Flonum. Output scale factor. Default 1.0 return - Table
function
(wtab:get-max-amplitude prtlst) Determine the maximum amplitude in partial list. prtlst - List. A list of the form ((f0 a0)(f1 a1)...(fn an)) return - Flonum. The value ai from prtlst such that ai >= every other a in prtlst.
function
(wtab:normalize prtlst) Scale amplitude values in partial list such that the highest amplitude partial has a new amplitude of 1.0 prtlst - List. A list of the form ((f0 a0)(f1 a1)...(fn an)) return - List.
function
(wtab:scale prtlst sf) Scale each partial amplitude by factor sf. prtlst - List. A list of the form ((f0 a0)(f1 a1)...(fn an)) sf - Flonum. The scale factor return - List. The result is a list ((f0 a0*sf)(f1 a1*sf)...(fn an*sf))
function
(wtab:make-plist n [:frqfn][:ampfn]) Generate partial list suitable for use by wtab:make n - integer. Number of partials :frqfn - closure. The relative frequency as function of partial number. The function should accept a single integer value. The default is an identity which returns its argument f(n)->n :ampfn - closure. The relative amplitude as function of partial number. The function should take a single integer value. The default returns the reciprocal of its argument a(n)->1/n return - list. The result is a list of the form ((f_n a_n)(f_n-1 a_n-1) ... (f_0 a_0)).
function
(pwl->wtab [t0 a0 [ t1 a1 [ t2 a2 ... [tn]]]]) Generate wave table using pwl See Nyquist manual for pwl usage. return - table.
function
(wtab:gwave [w [s]]) Create wave table where the wave shape is continuously adjustable between pulse, triangle and sawtooth. w - flonum. Relative on/off times. 0 <= w <= 1. Use w=0.5 for square and triangle waves, use w != 0.5 for asymmetrical shapes. Default 0.5 s - flonum. The "sawtoothness" of the shape. 0 <= s <= 1. For s around 0 the shape is pulse like. For s near 1 the shape is more pointy. Default 0.5 return - table. Note the resulting table is not band limited.
function
(wtab:pulse [w [y1 [y2]]]) Generate pulse wave table w - flonum. Relative pulse width. 0 <= w <= 1. Default 0.5 y1 - flonum. "on" amplitude. default +1 y2 - flonum. "off" amplitude. default -1 return - table. Note the resulting table is not band limited.
;; wtab.lsp ;; Version 1.00 31 October 2004 ;; Author Steven Jones ;; ;; Contact jones57@swbell.net include the word "nyquist" in subject line ;; ;; The contents of this file are released under the terms of the GNU General ;; Public License. See the file LICENSE.txt ;; ;; Defines functions for the creation of wave tables ;; The file auxwaves.lsp creates some specific wave tables. ;; (provide 'wtab) (current-file "wtab") ;; @doc function wtab:make ;; (wtab:make prtlst [:tabsize][:f0][:ascale]) ;; Generate wave table from list of partials. ;; ;; prtlst - List. The partials List. The harmonic content ;; is specified as a list of partials. Each partial is itself a ;; list of the form (fi ai) where fi is the relative frequency of ;; and ai the relative amplitude of partial i. ;; ;; :tabsize - Integer. The table size. Default *default-table-size* ;; ;; :f0 - Flonum. The reference frequency. All partial frequencies are ;; relative to f0. Default 1.0 ;; ;; :ascale - Flonum. Output scale factor. Default 1.0 ;; ;; return - Table ;; (defun wtab:make (prtlst &key (tabsize *default-table-size*)(f0 1.0)(ascale 1)) (let ((snd (simrep (index (length prtlst)) (let* ((pair (nth index prtlst)) (frq (car pair)) (amp (car (cdr pair)))) (scale amp (build-harmonic frq tabsize)))))) (list (scale ascale snd) (hz-to-step f0) t))) ;; @doc function wtab:get-max-amplitude ;; (wtab:get-max-amplitude prtlst) ;; Determine the maximum amplitude in partial list. ;; ;; prtlst - List. A list of the form ((f0 a0)(f1 a1)...(fn an)) ;; return - Flonum. The value ai from prtlst such that ai >= every other ;; a in prtlst. ;; (defun wtab:get-max-amplitude (prtlst) (let ((mx 0)) (dolist (slst prtlst) (setf mx (max mx (abs (second slst))))) mx)) ;; @doc function wtab:normalize ;; (wtab:normalize prtlst) ;; Scale amplitude values in partial list such that the highest amplitude ;; partial has a new amplitude of 1.0 ;; ;; prtlst - List. A list of the form ((f0 a0)(f1 a1)...(fn an)) ;; return - List. ;; (defun wtab:normalize (prtlst) (let* ((peak (float (wtab:get-max-amplitude prtlst))) (norm (if (zerop peak) 0 (/ peak))) (acc '())) (dolist (slst prtlst) (push (list (car slst)(* norm (second slst))) acc)) acc)) ;; @doc function wtab:scale ;; (wtab:scale prtlst sf) ;; Scale each partial amplitude by factor sf. ;; ;; prtlst - List. A list of the form ((f0 a0)(f1 a1)...(fn an)) ;; sf - Flonum. The scale factor ;; return - List. The result is a list ((f0 a0*sf)(f1 a1*sf)...(fn an*sf)) ;; (defun wtab:scale (prtlst sf) (if prtlst (let ((prt (car prtlst))) (cons (list (car prt)(* sf (second prt))) (wtab:scale (cdr prtlst) sf))) nil)) ;; @doc function wtab:make-plist ;; (wtab:make-plist n [:frqfn][:ampfn]) ;; Generate partial list suitable for use by wtab:make ;; ;; n - integer. Number of partials ;; ;; :frqfn - closure. The relative frequency as function of partial number. ;; The function should accept a single integer value. The default ;; is an identity which returns its argument f(n)->n ;; ;; :ampfn - closure. The relative amplitude as function of partial number. ;; The function should take a single integer value. The default ;; returns the reciprocal of its argument a(n)->1/n ;; ;; return - list. The result is a list of the form ;; ((f_n a_n)(f_n-1 a_n-1) ... (f_0 a_0)). ;; (defun wtab:make-plist (n &key (frqfn #'(lambda (n) n))(ampfn #'(lambda (n)(/ 1.0 n)))) (if (plusp n) (cons (list (funcall frqfn n)(funcall ampfn n)) (wtab:make-plist (- n 1) :frqfn frqfn :ampfn ampfn)) nil)) ;; @doc function pwl->wtab ;; (pwl->wtab [t0 a0 [ t1 a1 [ t2 a2 ... [tn]]]]) ;; Generate wave table using pwl ;; See Nyquist manual for pwl usage. ;; ;; return - table. ;; (defun pwl->wtab (&rest args) (list (pwl-list args)(hz-to-step 1) t)) ;; @doc function wtab:gwave ;; (wtab:gwave [w [s]]) ;; Create wave table where the wave shape is continuously adjustable between ;; pulse, triangle and sawtooth. ;; ;; w - flonum. Relative on/off times. 0 <= w <= 1. Use w=0.5 for square ;; and triangle waves, use w != 0.5 for asymmetrical shapes. ;; Default 0.5 ;; ;; s - flonum. The "sawtoothness" of the shape. 0 <= s <= 1. For s around ;; 0 the shape is pulse like. For s near 1 the shape is more pointy. ;; Default 0.5 ;; ;; return - table. Note the resulting table is not band limited. ;; (defun wtab:gwave (&optional (w 0.5)(s 0.5)) (let ((ax 0) (ay -1) (bx (/ (* s w) 2.0)) (by (- 1.0 s)) (cx w) (cy 1) (dx (* s (/ (+ 1 w) 2.0))) (dy (- s 1)) (ex 1) (ey -1)) (pwl->wtab ax ay bx by cx cy dx dy ex ey 1))) ;; @doc function wtab:pulse ;; (wtab:pulse [w [y1 [y2]]]) ;; Generate pulse wave table ;; ;; w - flonum. Relative pulse width. 0 <= w <= 1. Default 0.5 ;; ;; y1 - flonum. "on" amplitude. default +1 ;; ;; y2 - flonum. "off" amplitude. default -1 ;; ;; return - table. Note the resulting table is not band limited. ;; (defun wtab:pulse (&optional (w 0.5)(y1 +1)(y2 -1)) (let ((ax 0) (ay y1) (bx 0) (by y2) (cx w) (cy y2) (dx w) (dy y1) (ex 1) (ey y1)) (pwl->wtab ax ay bx by cx cy dx dy ex ey 1)))