annotate util/SMALL_midiGenerate.m @ 77:62f20b91d870

add routines from sparco problems privite folder to {root}\util some changes to ksvd vs rlsdla image denoising example
author Ivan <ivan.damnjanovic@eecs.qmul.ac.uk>
date Fri, 25 Mar 2011 14:01:50 +0000
parents fc395272d53e
children 8e660fd14774
rev   line source
idamnjanovic@8 1 function reconstructed=SMALL_midiGenerate(V, Problem)
idamnjanovic@8 2 %%% Reconstraction of midi file from representation in the given dictionary
idamnjanovic@24 3 %
idamnjanovic@24 4 % Centre for Digital Music, Queen Mary, University of London.
idamnjanovic@24 5 % This file copyright 2009 Ivan Damnjanovic.
idamnjanovic@24 6 %
idamnjanovic@24 7 % This program is free software; you can redistribute it and/or
idamnjanovic@24 8 % modify it under the terms of the GNU General Public License as
idamnjanovic@24 9 % published by the Free Software Foundation; either version 2 of the
idamnjanovic@24 10 % License, or (at your option) any later version. See the file
idamnjanovic@24 11 % COPYING included with this distribution for more information.
idamnjanovic@8 12 %
idamnjanovic@8 13 % SMALL_midiGenerate is a part of SMALLbox and can be use to reconstruct
idamnjanovic@8 14 % a midi file given representation of the training set (V) in the
idamnjanovic@8 15 % dictionary Problem.A.
idamnjanovic@8 16 % Output is reconstructed structure with two fields:
idamnjanovic@8 17 % - reconstructed.notes - matrix with transcribed notes
idamnjanovic@8 18 % - reconstructed.midi - midi representation of transcription
idamnjanovic@8 19
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
idamnjanovic@8 25 ts=(Problem.windowSize*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);