Chris@1: Chris@9: program convertShiftedW; Chris@1: Chris@1: /* Chris@1: Chris@1: The shiftedW.txt file contains the data from shiftedW.mat in the Chris@1: amt_mssiplca_fast repository, exported as text via MATLAB dlmwrite. Chris@1: Chris@1: This is described as "3 sets of piano templates learned from MAPS Chris@1: database". Chris@1: Chris@1: The data consists of a 4D array of dimensions 5 x 88 x 545 x 3. Chris@1: Chris@1: I believe these dimensions are: Chris@1: * bins per semitone (5) Chris@1: * semitones, or number of components (88) Chris@1: * spectral profile for a single pitch template (545) Chris@1: * distinct piano sources (3) Chris@1: Chris@1: In the text file, it is formatted as 5 rows of 143880 values. Each Chris@1: row contains 88 consecutive values from shiftedW(row,1,1,1) to Chris@1: shiftedW(row,88,1,1), then 88 values from shiftedW(row,1,2,1) to Chris@1: shiftedW(row,88,2,1), etc. Chris@1: Chris@1: Here we load the file, convert it to a more structured JSON format, Chris@9: and write it out again as templates.json; we also produce a set of Chris@9: three files pianoN.csv which contain only the middle bin per Chris@9: semitone for a single piano sample. Chris@1: Chris@1: */ Chris@1: Chris@1: load yeti.experimental.json; Chris@1: Chris@1: mat = load may.matrix; Chris@1: Chris@1: bps = 5; Chris@1: semitones = 88; Chris@1: values = 545; Chris@1: sources = 3; Chris@1: Chris@1: f = openInFile "shiftedW.txt" "UTF-8"; Chris@1: Chris@1: raw = array (map do s: array (map number (strSplit "," s)) done (f.lines ())); Chris@1: Chris@1: println "Read \(length raw) rows, length of first row is \(length (head raw))"; Chris@1: Chris@1: if length raw != 5 or length (head raw) != 88*545*3 then Chris@1: failWith "Error: expected 5 rows of \(88*545*3) values each" Chris@1: fi; Chris@1: Chris@9: for [0..sources-1] do source: Chris@9: o = openOutFile "piano\(source+1).csv" "UTF-8"; Chris@9: for [0..semitones-1] do semitone: Chris@9: template = map do i: Chris@9: raw[int(bps/2)][source * (semitones * values) + Chris@9: i * semitones + Chris@9: semitone] Chris@9: done [0..values-1]; Chris@9: o.write ((strJoin "," (map string template)) ^ "\n"); Chris@9: done; Chris@9: o.close (); Chris@9: done; Chris@9: Chris@1: reshaped = jsonList (concatMap Chris@1: do source: Chris@1: map do bin: Chris@1: jsonObj [ Chris@1: "source": jsonNum source, Chris@1: "f": jsonNum bin, Chris@1: "templates": mat.json Chris@1: (mat.generate do i semitone: Chris@1: raw[bin][source * (semitones * values) + Chris@1: i * semitones + Chris@1: semitone] Chris@1: done { rows = values, columns = semitones }) Chris@1: ] Chris@1: done [0..bps-1] Chris@1: done [0..sources-1]); Chris@1: Chris@1: o = openOutFile "templates.json" "UTF-8"; Chris@1: Chris@1: o.write (jsonEncode reshaped); Chris@1: Chris@1: o.close (); Chris@1: