comparison src/scheme/old/genfilter.scm @ 0:bf79fb79ee13

Initial Mercurial check in.
author samer
date Tue, 17 Jan 2012 17:50:20 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bf79fb79ee13
1 ;;; Preamble
2 (load "audio.scm")
3 (load "lineout.scm")
4 (load "functions.scm")
5
6 (import "samer.mds.*")
7
8 ;;; transfer filter fn to vector of values
9 (define (set-filter F fn)
10 (define f (.mat F))
11 (for-each
12 (lambda (i) (.set f 0 i (fn i)))
13 (range 0 N))
14 (.changed F)
15 )
16
17 ;; convert vector of values into a filter fn
18 (define (filter-fn F)
19 (let ((mat (.mat F)))
20 (lambda (i) (.get mat 0 i))))
21
22 ;; returns fn f(i) that returns 1 if d'th coordinate of P(i) <op> th
23 ;; eg (coor-test P 2 > 0.5) is function which returns P(i,2) > 0.5
24 (define (coor-test P d op th)
25 (lambda (i) (if (op (.get P i d) th) 1.0 0.0)))
26
27
28 ;;; set up tasks for ICA domain filtering using f as a function
29 ;;; which returns (f i) as the scaling factor for the ith ICA component
30 ;;; source is an AudioSource.
31 (define (direct-recon source N hop f)
32 (define x (linein source N hop))
33 (define W (Matrix. "W" N N)) ; ICA matrix
34 (define K (Matrix. "K" N N)) ; total filtering matrix
35 (define z (VVector. "z" N)) ; reconstruction
36 (addtask (Ops.times z K x) (Ops.update z))
37 (overlap-and-add z hop)
38 (on-change W (.assign K (total-matrix W f)))
39 (matexec W "load")
40 )
41
42 (define (ica-recon s A sink hop)
43 (define F (VVector. "F" N)) ; the source domain filter
44 (define z (VVector. "z" N))
45 (addtasks (Ops.timesEquals s F) (Ops.times z A s))
46 (overlap-and-add sink z hop))
47
48
49 (define (recon source N hop sink)
50 (define x (norm (linein source N hop)))
51 (define ica (mk-ica x laplace-spec))
52 (define s (.output ica))
53 (define F (VVector. "F" N)) ; the source domain filter
54 (define W (.getWeightMatrix ica))
55 (define A (.getBasisMatrix ica))
56 (define z (VVector. "z" N))
57 (define order (Matrix. "order" 5 512))
58
59 (addtasks (Ops.timesEquals s F) (Ops.times z A s))
60 (overlap-and-add sink z hop)
61
62 (on-change W (exec ica "basis"))
63 (matexec W "load")
64 )
65
66 ;; returns total transformation matrix formed by filtering
67 ;; in the ICA domain. f is a function which returns the scaling
68 ;; factor for the i'th component, ie (f i) is a scalar.
69 (define (total-matrix W f)
70 (define D (Jama.Matrix. N N 0.0))
71 (for-each
72 (lambda (i) (.set D i i (f i)))
73 (range 0 N))
74 (.times (.inverse W) (.times D W))
75 )
76
77 ; eg (total-matrix W (coor-test P 1 > 0.0))
78 ; filters out all components whos MDS y-coor <= 0
79
80 ;; transfer values from G to F reordered by order
81 (define-method (rank-filter F order G)
82 (rank-filter F order G 0 (.size G)))
83
84 (define-method (rank-filter F order G start end)
85 (Mathx.set F (Zero.))
86 (let ((f (.mat F)) (g (.mat G)) (I (.mat order)))
87 (for-each
88 (lambda (i) (.set f 0 (.get I 0 i) (.get g 0 i)))
89 (range start end)))
90 (.changed F)
91 )
92
93 ;;; filter is a certain spherically symmetric kernel centred on
94 ;;; a particular component. Geometry directly from correlation
95 ;;; matrix (not from MDS configuation)
96 (define (proximity-filter R F fn)
97 (ProximityFilter. R F (VFunction. "proximity kernel" fn)) f)
98
99 (define (geometric-filter P f)
100 (define n (.getRowDimension P))
101 (define E (.getColumnDimension P))
102 (define off (VDouble. "offset" 0.0))
103 (define norm (VVector. "normal" E))
104 (define hp (LogisticHyperplane. (.array norm) (.value$ off)))
105 (define gf (GeometricFilter. P f hp))
106 (define obs (observer (.setOffset hp (.value$ off)) (.run gf) (.changed f)))
107 (.addObserver P obs)
108 (.addObserver off obs)
109 (.addObserver norm obs)
110 f)
111
112