Mercurial > hg > silvet
comparison yeti/scratch/convertShiftedW.yeti @ 1:662b0a8b17b9
Import piano templates
author | Chris Cannam |
---|---|
date | Mon, 17 Mar 2014 15:58:01 +0000 |
parents | |
children | 6d4df772f108 |
comparison
equal
deleted
inserted
replaced
0:3d2215d79a00 | 1:662b0a8b17b9 |
---|---|
1 | |
2 program loadShiftedW; | |
3 | |
4 /* | |
5 | |
6 The shiftedW.txt file contains the data from shiftedW.mat in the | |
7 amt_mssiplca_fast repository, exported as text via MATLAB dlmwrite. | |
8 | |
9 This is described as "3 sets of piano templates learned from MAPS | |
10 database". | |
11 | |
12 The data consists of a 4D array of dimensions 5 x 88 x 545 x 3. | |
13 | |
14 I believe these dimensions are: | |
15 * bins per semitone (5) | |
16 * semitones, or number of components (88) | |
17 * spectral profile for a single pitch template (545) | |
18 * distinct piano sources (3) | |
19 | |
20 In the text file, it is formatted as 5 rows of 143880 values. Each | |
21 row contains 88 consecutive values from shiftedW(row,1,1,1) to | |
22 shiftedW(row,88,1,1), then 88 values from shiftedW(row,1,2,1) to | |
23 shiftedW(row,88,2,1), etc. | |
24 | |
25 Here we load the file, convert it to a more structured JSON format, | |
26 and write it out again as templates.json. | |
27 | |
28 */ | |
29 | |
30 load yeti.experimental.json; | |
31 | |
32 mat = load may.matrix; | |
33 plt = load may.plot; | |
34 | |
35 bps = 5; | |
36 semitones = 88; | |
37 values = 545; | |
38 sources = 3; | |
39 | |
40 f = openInFile "shiftedW.txt" "UTF-8"; | |
41 | |
42 raw = array (map do s: array (map number (strSplit "," s)) done (f.lines ())); | |
43 | |
44 println "Read \(length raw) rows, length of first row is \(length (head raw))"; | |
45 | |
46 if length raw != 5 or length (head raw) != 88*545*3 then | |
47 failWith "Error: expected 5 rows of \(88*545*3) values each" | |
48 fi; | |
49 | |
50 reshaped = jsonList (concatMap | |
51 do source: | |
52 map do bin: | |
53 jsonObj [ | |
54 "source": jsonNum source, | |
55 "f": jsonNum bin, | |
56 "templates": mat.json | |
57 (mat.generate do i semitone: | |
58 raw[bin][source * (semitones * values) + | |
59 i * semitones + | |
60 semitone] | |
61 done { rows = values, columns = semitones }) | |
62 ] | |
63 done [0..bps-1] | |
64 done [0..sources-1]); | |
65 | |
66 o = openOutFile "templates.json" "UTF-8"; | |
67 | |
68 o.write (jsonEncode reshaped); | |
69 | |
70 o.close (); | |
71 |