annotate mirex2012-matlab/cell2sparse.m @ 372:af71cbdab621 tip

Update bqvec code
author Chris Cannam
date Tue, 19 Nov 2019 10:13:32 +0000
parents 8017dd4a650d
children
rev   line source
Chris@2 1 function spCQT = cell2sparse(Xcq,octaves,bins,firstcenter,atomHOP,atomNr)
Chris@2 2 %Generates a sparse matrix containing the CQT coefficients (rasterized).
Chris@2 3 %
Chris@2 4 %The sparse matrix representation of the CQT coefficients contains all
Chris@2 5 %computed coefficients at the corresponding time-frequency location
Chris@2 6 %(similar to a spectrogram). For lower frequencies this means, that
Chris@2 7 %each coefficient is followed by zeros stemming from the fact, that the time
Chris@2 8 %resolution for lower frequencies decreases as the frequency resolution
Chris@2 9 %increases. Due to the design of the CQT kernel, however, the coefficients
Chris@2 10 %of different octaves are synchronised, meaning that for the second highest
Chris@2 11 %octave each coefficient is followed by one zero, for the next octave down
Chris@2 12 %two zeros are inserted, for the next octave four zeros are inserted and so
Chris@2 13 %on.
Chris@2 14 %
Chris@2 15 %INPUT:
Chris@2 16 % Xcq ... Cell array consisting of all coefficients for all octaves
Chris@2 17 % octaves ... Number of octaves processed
Chris@2 18 % bins ... Number of bins per octave
Chris@2 19 % firstcenter ... Location of the leftmost atom-stack in the temporal
Chris@2 20 % kernel
Chris@2 21 % atomHOP ... Spacing of two consecutive atom stacks
Chris@2 22 % atomNr ... Number of atoms per bin within the kernel
Chris@2 23 %
Chris@2 24 %Christian Schörkhuber, Anssi Klapuri 2010-06
Chris@2 25
Chris@2 26 if 0
Chris@2 27 %% this version has big memory consumption but is very fast
Chris@2 28 emptyHops = firstcenter/atomHOP;
Chris@2 29 drop = emptyHops*2^(octaves-1)-emptyHops; %distance between first value in highest octave and first value in lowest octave
Chris@2 30 spCQT = zeros(bins*octaves,size(Xcq{1},2)*atomNr-drop);
Chris@2 31
Chris@2 32 for i=1:octaves
Chris@2 33 drop = emptyHops*2^(octaves-i)-emptyHops; %first coefficients of all octaves have to be in synchrony
Chris@2 34 X = Xcq{i};
Chris@2 35 if atomNr > 1 %more than one atom per bin --> reshape
Chris@2 36 Xoct = zeros(bins,atomNr*size(X,2)-drop);
Chris@2 37 for u=1:bins %reshape to continous windows for each bin (for the case of several wins per frame)
Chris@2 38 octX_bin = X((u-1)*atomNr+1:u*atomNr,:);
Chris@2 39 Xcont = reshape(octX_bin,1,size(octX_bin,1)*size(octX_bin,2));
Chris@2 40 Xoct(u,:) = Xcont(1+drop:end);
Chris@2 41 end
Chris@2 42 X = Xoct;
Chris@2 43 else
Chris@2 44 X = X(:,1+drop:end);
Chris@2 45 end
Chris@2 46 binVec = bins*octaves-bins*i+1:bins*octaves-bins*(i-1);
Chris@2 47 spCQT(binVec,1:2^(i-1):size(X,2)*2^(i-1)) = X;
Chris@2 48
Chris@2 49 end
Chris@2 50 spCQT = sparse(spCQT); %storing as sparse matrix at the end is the fastest way. Big memory consumption though!
Chris@2 51
Chris@2 52 else
Chris@2 53 %% this version uses less memory but is noticable slower
Chris@2 54 emptyHops = firstcenter/atomHOP;
Chris@2 55 drops = emptyHops*2.^(octaves-(1:octaves))-emptyHops;
Chris@2 56 len = max(((atomNr*cellfun('size',Xcq,2)-drops).*2.^(0:octaves-1))); %number of columns of output matrix
Chris@2 57 spCQT = [];
Chris@2 58
Chris@2 59 for i=octaves:-1:1
Chris@2 60 drop = emptyHops*2^(octaves-i)-emptyHops; %first coefficients of all octaves have to be in synchrony
Chris@2 61 X = Xcq{i};
Chris@2 62 if atomNr > 1 %more than one atom per bin --> reshape
Chris@2 63 Xoct = zeros(bins,atomNr*size(X,2)-drop);
Chris@2 64 for u=1:bins %reshape to continous windows for each bin (for the case of several wins per frame)
Chris@2 65 octX_bin = X((u-1)*atomNr+1:u*atomNr,:);
Chris@2 66 Xcont = reshape(octX_bin,1,size(octX_bin,1)*size(octX_bin,2));
Chris@2 67 Xoct(u,:) = Xcont(1+drop:end);
Chris@2 68 end
Chris@2 69 X = Xoct;
Chris@2 70 else
Chris@2 71 X = X(:,1+drop:end);
Chris@2 72 end
Chris@2 73 X = upsample(X.',2^(i-1)).';
Chris@2 74 X = [X zeros(bins,len-size(X,2))];
Chris@2 75 spCQT = sparse([spCQT; X]);
Chris@2 76 end
Chris@2 77
Chris@2 78 end