view yeti/cqt.yeti @ 20:dad5d8a06a5d

Return only the actual results (i.e. space with zeros rather than duplicates)
author Chris Cannam <c.cannam@qmul.ac.uk>
date Wed, 30 Oct 2013 17:29:14 +0000
parents 3b044c30cffc
children 5787785f4560
line wrap: on
line source

program cqt;

cqtkernel = load cqtkernel;
resample = load may.stream.resample;
manipulate = load may.stream.manipulate;
syn = load may.stream.syntheticstream;
cm = load may.matrix.complex;
mat = load may.matrix;
framer = load may.stream.framer;
cplx = load may.complex;
fft = load may.transform.fft;
vec = load may.vector;
af = load may.stream.audiofile;

{ pow, round, floor, ceil, log2, nextPowerOfTwo } = load may.mathmisc;

cqt str =
   (sampleRate = str.sampleRate;
    maxFreq = sampleRate/2;
    minFreq = 40;
    binsPerOctave = 24;

eprintln "Here";

    octaves = ceil (log2 (maxFreq / minFreq));

eprintln "Here: about to calculate stuff with \(octaves)";

    actualMinFreq = (maxFreq / (pow 2 octaves)) * (pow 2 (1/binsPerOctave));

    eprintln "sampleRate = \(sampleRate), maxFreq = \(maxFreq), minFreq = \(minFreq), actualMinFreq = \(actualMinFreq), octaves = \(octaves), binsPerOctave = \(binsPerOctave)";

    kdata = cqtkernel.makeKernel { sampleRate, maxFreq, binsPerOctave };

    eprintln "atomsPerFrame = \(kdata.atomsPerFrame)";

    streams = manipulate.duplicated octaves str;

    //!!! can't be right!
    kernel = cm.transposed (cm.conjugateTransposed kdata.kernel);

    eprintln "have kernel";

    fftFunc = fft.forward kdata.fftSize;

    cqblocks =
        map do octave:
            frames = framer.monoFrames //!!! mono for now
                { framesize = kdata.fftSize, hop = kdata.fftHop }
                (resample.decimated (pow 2 octave) streams[octave]);
            map do frame:
                freq = fftFunc (cplx.complexArray frame (vec.zeros kdata.fftSize));
                cm.product kernel (cm.newComplexColumnVector freq);
            done frames;
        done [0..octaves-1];

    // The cqblocks list is a list<list<matrix>>. Each top-level list
    // corresponds to an octave, from highest to lowest, each having
    // twice as many elements in its list as the next octave. The
    // sub-lists are sampled in time with an effective spacing of
    // fftSize * 2^(octave-1) audio frames, and the matrices are row
    // vectors with atomsPerFrame * binsPerOctave complex elements.
    //
    // ***
    // 
    // In a typical constant-Q structure, each (2^(octaves-1) *
    // fftHop) input frames gives us an output structure conceptually
    // like this:
    //
    // [][][][][][][][]   <- fftHop frames per highest-octave output value
    // [][][][][][][][]      layered as many times as binsPerOctave (here 2)
    // [--][--][--][--]   <- fftHop*2 frames for the next lower octave
    // [--][--][--][--]      etc
    // [------][------]
    // [------][------]
    // [--------------]
    // [--------------]
    //
    // ***
    //
    // But the kernel we're using here has more than one temporally
    // spaced atom; each individual cell is a row vector with
    // atomsPerFrame * binsPerOctave elements, but that actually
    // represents a rectangular matrix of result cells with width
    // atomsPerFrame and height binsPerOctave. The columns of this
    // matrix (the atoms) then need to be spaced by 2^(octave-1)
    // relative to those from the highest octave.

    // Reshape each row vector into the appropriate rectangular matrix
/*
    cqblocks = array (map do octlist:
        map do rv:
            cm.generate do row col:
                cm.at rv ((row * kdata.atomsPerFrame) + col) 0
            done {
                rows = kdata.binsPerOctave,
                columns = kdata.atomsPerFrame
            }
        done octlist
    done cqblocks);
*/

//!!! how then do we arrange to drop a certain number of atoms (rather
//than of atoms+bins chunks)?

    assembleBlock bits =
       (eprintln "assembleBlock: structure of bits is:";
        eprintln (map length bits);

        rows = octaves * kdata.binsPerOctave;
        columns = (pow 2 (octaves - 1)) * kdata.atomsPerFrame;

        cm.generate do row col:

            // bits structure: [1,2,4,8,...]

            // each elt of bits is a list of the chunks that should
            // make up this block in that octave (lowest octave first)

            // each chunk has atomsPerFrame * binsPerOctave elts in it

            // row is disposed with 0 at the top, highest octave (in
            // both pitch and index into bits structure)

//            oct = octaves - int (row / binsPerOctave) - 1;
            oct = int (row / binsPerOctave);
            binNo = row % kdata.binsPerOctave;
            chunks = pow 2 oct;
            colsPerChunk = int (columns / chunks);
            colsPerAtom = int (colsPerChunk / kdata.atomsPerFrame);
            chunkNo = int (col / colsPerChunk);
            atomNo = int ((col % colsPerChunk) / colsPerAtom);
            atomOffset = ((col % colsPerChunk) % colsPerAtom);

//            eprintln "row \(row) of \(rows), col \(col) of \(columns): oct \(oct), bin \(binNo), chunk \(chunkNo) of \(chunks), atom \(atomNo) of \(kdata.atomsPerFrame)";

            if atomOffset == 0 then
                cm.at bits[oct][chunkNo] (binNo * kdata.atomsPerFrame + atomNo) 0;
            else
                cplx.zero
            fi;

        done { rows, columns };
        );

    processOctaveLists octs =
        case octs[0] of
        block::rest:
           (toAssemble = array 
               (map do oct:
                    n = pow 2 oct;
                    if not empty? octs[oct] then
                        forBlock = array (take n octs[oct]);
                        octs[oct] := drop n octs[oct];
                        forBlock
                    else
                        array []
                    fi
                done (keys octs));
            assembleBlock toAssemble :. \(processOctaveLists octs));
         _: []
        esac;

eprintln "cqblocks has \(length cqblocks) entries";

    octaveLists = [:];

    cqblocks = array cqblocks;
    for [1..octaves] do oct:
        octaveLists[octaves - oct] := cqblocks[oct-1];
    done;
/*
    \() (map2 do octlist octave:
println "oct \(octaves) - \(octave) = \(octaves - octave)";
             octaveLists[octaves - octave] := octlist 
         done cqblocks [1..octaves]);
*/
eprintln "octaveLists keys are: \(keys octaveLists)";
    
    processOctaveLists octaveLists;

    );

//testStream = manipulate.withDuration 96000 (syn.sinusoid 48000 500);
//testStream = manipulate.withDuration 96000 (syn.pulseTrain 48000 4);
testStream = af.open "sweep.wav";

eprintln "have test stream";

c = cqt testStream;

//m = take 1 (drop 2 c);

//thing = take 50 (drop 200 c);

//m = cm.newComplexMatrix (ColumnMajor ()) thing;
mm = cm.magnitudes (head c);

for (mat.asColumns mm) (println . strJoin "," . vec.list);

()