view yeti/scratch/convertShiftedW.yeti @ 165:f73be84f5c90

Use 20ms resolution in hq mode, 40ms only in draft mode
author Chris Cannam
date Tue, 20 May 2014 16:48:33 +0100
parents 6d4df772f108
children
line wrap: on
line source

program convertShiftedW;

/*

   The shiftedW.txt file contains the data from shiftedW.mat in the
   amt_mssiplca_fast repository, exported as text via MATLAB dlmwrite.

   This is described as "3 sets of piano templates learned from MAPS
   database".

   The data consists of a 4D array of dimensions 5 x 88 x 545 x 3.

   I believe these dimensions are:
    * bins per semitone (5)
    * semitones, or number of components (88)
    * spectral profile for a single pitch template (545)
    * distinct piano sources (3)

   In the text file, it is formatted as 5 rows of 143880 values.  Each
   row contains 88 consecutive values from shiftedW(row,1,1,1) to
   shiftedW(row,88,1,1), then 88 values from shiftedW(row,1,2,1) to
   shiftedW(row,88,2,1), etc.

   Here we load the file, convert it to a more structured JSON format,
   and write it out again as templates.json; we also produce a set of
   three files pianoN.csv which contain only the middle bin per
   semitone for a single piano sample.

*/

load yeti.experimental.json;

mat = load may.matrix;

bps = 5;
semitones = 88;
values = 545;
sources = 3;

f = openInFile "shiftedW.txt" "UTF-8";

raw = array (map do s: array (map number (strSplit "," s)) done (f.lines ()));

println "Read \(length raw) rows, length of first row is \(length (head raw))";

if length raw != 5 or length (head raw) != 88*545*3 then
   failWith "Error: expected 5 rows of \(88*545*3) values each"
fi;

for [0..sources-1] do source:
    o = openOutFile "piano\(source+1).csv" "UTF-8";
    for [0..semitones-1] do semitone:
        template = map do i:
            raw[int(bps/2)][source * (semitones * values) +
                            i * semitones +
                            semitone]
        done [0..values-1];
        o.write ((strJoin "," (map string template)) ^ "\n");
    done;
    o.close ();
done;

reshaped = jsonList (concatMap
    do source:
        map do bin:
            jsonObj [
                "source": jsonNum source,
                "f": jsonNum bin,
                "templates": mat.json
                   (mat.generate do i semitone:
                        raw[bin][source * (semitones * values) +
                                 i * semitones +
                                 semitone]
                    done { rows = values, columns = semitones })
            ]
        done [0..bps-1]
    done [0..sources-1]);

o = openOutFile "templates.json" "UTF-8";

o.write (jsonEncode reshaped);

o.close ();