annotate yeti/scratch/convertShiftedW.yeti @ 7:f3d14ba3d8e2

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