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