annotate util/SMALL_midiGenerate.m @ 170:68fb71aa5339 danieleb

Added dictionary decorrelation functions and test script for Letters paper.
author Daniele Barchiesi <daniele.barchiesi@eecs.qmul.ac.uk>
date Thu, 06 Oct 2011 14:33:41 +0100
parents b14209313ba4
children
rev   line source
idamnjanovic@8 1 function reconstructed=SMALL_midiGenerate(V, Problem)
ivan@155 2 %% Reconstruction of midi file from representation in the given dictionary
ivan@128 3 %
ivan@128 4 % SMALL_midiGenerate is a part of SMALLbox and can be use to reconstruct
ivan@128 5 % a midi file given representation of the training set (V) in the
ivan@128 6 % dictionary Problem.A.
ivan@128 7 % Output is reconstructed structure with two fields:
ivan@128 8 % - reconstructed.notes - matrix with transcribed notes
ivan@128 9 % - reconstructed.midi - midi representation of transcription
ivan@128 10
idamnjanovic@24 11 %
idamnjanovic@24 12 % Centre for Digital Music, Queen Mary, University of London.
idamnjanovic@24 13 % This file copyright 2009 Ivan Damnjanovic.
idamnjanovic@24 14 %
idamnjanovic@24 15 % This program is free software; you can redistribute it and/or
idamnjanovic@24 16 % modify it under the terms of the GNU General Public License as
idamnjanovic@24 17 % published by the Free Software Foundation; either version 2 of the
idamnjanovic@24 18 % License, or (at your option) any later version. See the file
idamnjanovic@24 19 % COPYING included with this distribution for more information.
idamnjanovic@24 20 %%
idamnjanovic@8 21 U=Problem.A; % Dictionary used for representation
idamnjanovic@8 22 fs=Problem.fs; % Sampling rate
idamnjanovic@8 23 f=Problem.f; % vector of frequencies at wihch spectrogram is computed
idamnjanovic@8 24
ivan@155 25 ts=(Problem.windowSize*(1-Problem.overlap))/fs; %size of an analysis frame in seconds
idamnjanovic@8 26
idamnjanovic@8 27 %%
idamnjanovic@8 28 % Components pitch estimation using modified SWIPE algorithm by Arthuro
idamnjanovic@8 29 % Camacho
idamnjanovic@8 30 %
idamnjanovic@8 31 % Columns of matrix U are spectrograms of the notes learned from the
idamnjanovic@8 32 % training set. We are estimating pitches of these notes by also
idamnjanovic@8 33 % restricting pitch values to the one of the 88 piano notes.
idamnjanovic@8 34
idamnjanovic@8 35 pitch=zeros(size(U,2),1);
idamnjanovic@8 36
idamnjanovic@8 37 for i=1:size(U,2)
idamnjanovic@8 38
idamnjanovic@8 39 pitch(i) = SMALL_swipe(U(:,i),fs, f, [27.50 8192], 1/12);
idamnjanovic@8 40
idamnjanovic@8 41 end
idamnjanovic@8 42
idamnjanovic@8 43 %%
idamnjanovic@8 44 % If some of columns of U have the same pitch, their contribution to the
idamnjanovic@8 45 % score (matrix V) is summed.
idamnjanovic@8 46
idamnjanovic@8 47 [Ps,idx]=sort(pitch);
idamnjanovic@8 48 ndp=1;
idamnjanovic@8 49 Pd(ndp)=Ps(1);
idamnjanovic@8 50 Vnew(ndp,:)=V(idx(1),:);
idamnjanovic@8 51 for i=2:88
idamnjanovic@8 52 if Ps(i)> Ps(i-1)
idamnjanovic@8 53
idamnjanovic@8 54 ndp=ndp+1;
idamnjanovic@8 55 Vnew(ndp,:)=V(idx(i),:);
idamnjanovic@8 56 Pd(ndp)=Ps(i);
idamnjanovic@8 57
idamnjanovic@8 58 else
idamnjanovic@8 59 Vnew(ndp,:)=Vnew(ndp,:)+V(idx(i),:);
idamnjanovic@8 60 end
idamnjanovic@8 61 end
idamnjanovic@8 62 %%
idamnjanovic@8 63 % Generate midi matrix
idamnjanovic@8 64
idamnjanovic@8 65 midx=0;
idamnjanovic@8 66 for i=1:ndp
idamnjanovic@8 67
idamnjanovic@8 68 % Threshold for finding onsets and offsets of notes
idamnjanovic@8 69
idamnjanovic@8 70 thr=mean(Vnew(i,:));%+std(Vnew(i,:));
idamnjanovic@8 71
idamnjanovic@8 72 if(Pd(i)~=0)
idamnjanovic@8 73 for j=1:size(Vnew,2)
idamnjanovic@8 74 if Vnew(i,j)<thr
idamnjanovic@8 75 Vnew(i,j)=0;
idamnjanovic@8 76 if(j>1)
idamnjanovic@8 77 if (Vnew(i,j-1)==1)
idamnjanovic@8 78 try
idamnjanovic@8 79 M(midx,6)=(j-1)*ts;
idamnjanovic@8 80 if (M(midx,6)-M(midx,5))<2*ts
idamnjanovic@8 81 midx=midx-1;
idamnjanovic@8 82 end
idamnjanovic@8 83 catch
idamnjanovic@8 84 pause;
idamnjanovic@8 85 end
idamnjanovic@8 86 end
idamnjanovic@8 87 end
idamnjanovic@8 88 else
idamnjanovic@8 89 Vnew(i,j)=1;
idamnjanovic@8 90 if(j>1)
idamnjanovic@8 91 if (Vnew(i,j-1)==0)
idamnjanovic@8 92 midx=midx+1;
idamnjanovic@8 93 M(midx,1)=1;
idamnjanovic@8 94 M(midx,2)=1;
idamnjanovic@8 95 M(midx,3)=69 +round( 12 *log2(Pd(i)/440));
idamnjanovic@8 96 M(midx,4)=80;
idamnjanovic@8 97 M(midx,5)=(j-1)*ts;
idamnjanovic@8 98 end
idamnjanovic@8 99 else
idamnjanovic@8 100 midx=midx+1;
idamnjanovic@8 101 M(midx,1)=1;
idamnjanovic@8 102 M(midx,2)=1;
idamnjanovic@8 103 M(midx,3)=69 + round(12 *log2(Pd(i)/440));
idamnjanovic@8 104 M(midx,4)=80;
idamnjanovic@8 105 M(midx,5)=0;
idamnjanovic@8 106 end
idamnjanovic@8 107 end
idamnjanovic@8 108 end
idamnjanovic@8 109 if M(midx,6)==0
idamnjanovic@8 110 M(midx,6)=(j-1)*ts;
idamnjanovic@8 111 end
idamnjanovic@8 112 end
idamnjanovic@8 113 end
idamnjanovic@8 114
idamnjanovic@8 115 M=sortrows(M,5);
idamnjanovic@8 116 reconstructed.notes=M;
idamnjanovic@8 117 reconstructed.midi = matrix2midi(M);