loading

Logout succeed

Logout succeed. See you again!

ebook img

LAC 2009 - Slides: Signal Processing in the Pure Programming Language PDF

release year2009
file size0.6 MB
languageEnglish

Preview LAC 2009 - Slides: Signal Processing in the Pure Programming Language

Signal Processing in the Pure Programming SLaignngaul Pagroecessing in Pure Albert Gräf Dept. of Music Informatics Full paper, slides, examples at: http://pure-lang.googlecode.com/svn/docs/ • Pure, the programming language • Signal processing with Pure • The future of Pure Signal Processing in Pure 1. Pure, the Language mmoottiioonn  ((xx,,yy))  ((vvxx,,vvyy))  ((aaxx,,aayy))  ((sstteepp  ddtt::nneexxtt)) ==  [[xx,,yy,,vvxx,,vvyy]]  ::     mmoottiioonn  ((xx11,,yy11))  ((vvxx11,,vvyy11))  ((aaxx,,aayy))  nneexxtt  && wwhheenn     vvxx  ==  iiff  aabbss  xx  >>  33  tthheenn  ­­vvxx  eellssee  vvxx;;     vvyy  ==  iiff  yy  <<  ­­33  tthheenn  ­­vvyy  eellssee  vvyy;;     xx11  ==  xx++ddtt**vvxx++ddtt**ddtt**aaxx//22;;  yy11  ==  yy++ddtt**vvyy++ddtt**ddtt**aayy//22;;     vvxx11  ==  vvxx++ddtt**aaxx;;  vvyy11  ==  vvyy++ddtt**aayy;; eenndd;; mmoottiioonn  __  ((vvxx,,vvyy))  ((aaxx,,aayy))  ((mmoouussee  xx  yy::nneexxtt)) ==  (())  ::  mmoottiioonn  ((xx,,yy))  ((vvxx,,00))  ((aaxx,,aayy))  nneexxtt  &&;; uussiinngg  aaccttoorr;; bbaallll  ==  aaccttoorr  ((mmoottiioonn  ((­­33,,33))  ((00..55,,00))  ((00,,­­33))));; Signal Processing in Pure Lessons learned from Q + Term rewriting makes for a nice algebraic/functional programming language. + Dynamic typing makes it convenient to interface to interpreted realtime environments (Pd, SuperCollider, ...). – Bytecode interpretation was slow. People kept bugging me for a compiler. (Wanted one myself, actually.) – Q lacked some key features, most notably local functions. – The global mutex. Boo, hiss! (cid:222) A new implementation was needed. Signal Processing in Pure LLVM changed the game – LLVM = “Low-Level Virtual Machine” (llvm.org). – Generate native, optimized code in a platform- independent way (LLVM IR). – Built-in JIT (“Just In Time”) compilation. – Batch-compile programs to fast native code. – Dead easy to interface to C. – “Compiled scripting language.” – Gave the language a facelift along the way. Signal Processing in Pure Before: gr P X Y = P Y X; lq P X Y = not gr P X Y; qsort P [] = []; qsort P [X|Xs] = qsort P (filter (gr P X) Xs) ++   [X|qsort P (filter (lq P X) Xs)]; Main influences: Pascal, Prolog, Miranda After: qsort p [] = []; qsort p (x:xs) = qsort p [l | l = xs; l<x] +   (x : qsort p [r | r = xs; r>=x])   with x<y = p x y; x>=y = ~p x y end; Main influences: Haskell, Aardappel, Alice ML Signal Processing in Pure Features fact 0 = 1; fact n = n*fact (n­1) if n>0; – Algebraic/functional x:y:xs = y:x:xs if x>y; programming language.        = x:xs   if x==y; – Term rewriting + modern FP (lambda, currying, closures). – Lists and Octave-style tri n m = [x,y  | x = 1..n; y = 1..m; x<y]; matrices, list and matrix eye n   = {i==j | i = 1..n; j = 1..n}; comprehensions. insert nil y         = bin y nil nil; – Dynamic typing, insert (bin x l r) y = bin x (insert l y) r if y<x;                      = bin x l (insert r y); terms as data. – Eager + primes = sieve (2..inf) with   sieve (p:qs) = p : sieve [q | q = qs; q mod p] &; lazy evaluation. end; – Easy C interface. (Not really that pure!) extern int rand(); [rand | i = 1..20]; – In the planning stage: concurrency. Signal Processing in Pure What's this term rewriting? term rewriting top(push(s,x)) → x system pop(push(s,x)) → s reduce terms as “data” top(pop(push(empty,1))) → top(empty) redex reduct normal form • Whitehead et al: universal algebra, equational logic • O'Donnell et al: term rewriting as programming language • Goguen, Mahr et al: algebraic specification • Milner, Turner et al: modern functional programming Signal Processing in Pure 2. Signal Processing Signal Processing in Pure Actor-style processing create buffer wavefile fname aname = process with   process reset = () when sf_seek fp 0 0; end; read wave   process bang  = if ok res then bang else () when     n = nsamples; wave = dmatrix n;     res = sf_read_double fp wave n; copy into Pd array     pd_setbuffer aname wave;   end;   nsamples      = pd_getbuffersize aname;   ok res        = bigintp res && res>0; end when   fp::pointer = sentry sf_close                 (sf_open fname 0x10 (imatrix 10)); end; wavefile.pure: Use libsndfile to shovel chunks from a wave file into a Pd audio buffer. Signal Processing in Pure input stream gets Stream processing mapped to output stream collision detection motion (x,y) (vx,vy) (ax,ay) (step dt:next) = [x,y,vx,vy] : motion (x1,y1) (vx1,vy1) (ax,ay) next & when Newtonian motion   vx = if abs x > 3 then ­vx else vx;   vy = if y < ­3 then ­vy else vy;   x1 = x+dt*vx+dt*dt*ax/2; y1 = y+dt*vy+dt*dt*ay/2;   vx1 = vx+dt*ax; vy1 = vy+dt*ay; mouse action end; motion _ (vx,vy) (ax,ay) (mouse x y:next) = () : motion (x,y) (vx,0) (ax,ay) next &; ball object ball = actor (motion (­3,3) (0.5,0) (0,­3)); ball.pure: Bouncing ball animation using Gem.

See more

The list of books you might like