comparison matlab/MATLAB-Chroma-Toolbox_2.0/pitch_to_CENS.m @ 0:b54ee0a0be67

Import MATLAB Chroma Toolbox
author Chris Cannam
date Wed, 05 Aug 2015 21:08:56 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:b54ee0a0be67
1 function [f_CENS,sideinfo] = pitch_to_CENS(f_pitch,parameter,sideinfo)
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % Name: pitch_to_CENS
4 % Date of Revision: 2011-03
5 % Programmer: Meinard Mueller, Sebastian Ewert
6 %
7 % Description:
8 % Normalized statistical chroma-based energy distribution feature (CENS).
9 % The following is computed:
10 % * energy for each chroma band
11 % * normalisation of the chroma vectors
12 % * local statistics:
13 % - component-wise quantisation of the normalized chroma vectors
14 % - upfirdn filters and downsamples each column of f_stat_help
15 % - normalize each vector with its l^2 norm
16 %
17 % Remark:
18 % * parameter.inputFeatureRate specifies the feature rate of f_pitch. The value
19 % is used to derive the output feature rate given via sideinfo.
20 %
21 % Input:
22 % f_pitch
23 % parameter.quantSteps = [40 20 10 5] / 100;
24 % parameter.quantWeights = [ 1 1 1 1]/4;
25 % parameter.normThresh = 0.001;
26 % parameter.winLenSmooth = 41;
27 % parameter.downsampSmooth = 10;
28 % parameter.midiMin = 21;
29 % parameter.midiMax = 108;
30 % parameter.inputFeatureRate = 0;
31 % parameter.save = 0;
32 % parameter.saveDir = '';
33 % parameter.saveFilename = '';
34 % parameter.visualize = 0;
35 % sideinfo
36 %
37 % Output:
38 % f_CENS
39 % sideinfo
40 %
41 % License:
42 % This file is part of 'Chroma Toolbox'.
43 %
44 % 'Chroma Toolbox' is free software: you can redistribute it and/or modify
45 % it under the terms of the GNU General Public License as published by
46 % the Free Software Foundation, either version 2 of the License, or
47 % (at your option) any later version.
48 %
49 % 'Chroma Toolbox' is distributed in the hope that it will be useful,
50 % but WITHOUT ANY WARRANTY; without even the implied warranty of
51 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52 % GNU General Public License for more details.
53 %
54 % You should have received a copy of the GNU General Public License
55 % along with 'Chroma Toolbox'. If not, see <http://www.gnu.org/licenses/>.
56 %
57 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
58
59 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
60 % Check parameters
61 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62
63 if nargin<3
64 sideinfo=[];
65 end
66 if nargin<2
67 parameter=[];
68 end
69 if nargin<1
70 error('Please specify input data f_pitch');
71 end
72
73 if isfield(parameter,'quantSteps')==0
74 parameter.quantSteps = [40 20 10 5] / 100;
75 end
76 if isfield(parameter,'quantWeights')==0
77 parameter.quantWeights = [ 1 1 1 1]/4;
78 end
79 if isfield(parameter,'normThresh')==0
80 parameter.normThresh = 0.001;
81 end
82 if isfield(parameter,'winLenSmooth')==0
83 parameter.winLenSmooth = 41;
84 end
85 if isfield(parameter,'downsampSmooth')==0
86 parameter.downsampSmooth = 10;
87 end
88 if isfield(parameter,'midiMin')==0
89 parameter.midiMin = 21;
90 end
91 if isfield(parameter,'midiMax')==0
92 parameter.midiMax = 108;
93 end
94 if isfield(parameter,'inputFeatureRate')==0
95 parameter.inputFeatureRate = 0;
96 end
97 if isfield(parameter,'save')==0
98 parameter.save = 0;
99 end
100 if isfield(parameter,'saveDir')==0
101 parameter.saveDir = '';
102 end
103 if isfield(parameter,'saveFilename')==0
104 parameter.saveFilename = '';
105 end
106 if isfield(parameter,'visualize')==0
107 parameter.visualize = 0;
108 end
109
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 % Main program
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
114 seg_num = size(f_pitch,2);
115
116 % calculate energy for each chroma band
117 f_chroma_energy = zeros(12,seg_num);
118 for p=parameter.midiMin:parameter.midiMax
119 chroma = mod(p,12)+1;
120 f_chroma_energy(chroma,:) = f_chroma_energy(chroma,:)+f_pitch(p,:);
121 end
122
123 % normalize the chroma vectors
124 f_chroma_energy_distr = zeros(12,seg_num);
125 for k=1:seg_num
126 if sum(f_chroma_energy(:,k)>parameter.normThresh)>0
127 seg_energy_square = sum(f_chroma_energy(:,k));
128 f_chroma_energy_distr(:,k) = ((f_chroma_energy(:,k))/seg_energy_square);
129 end
130 end
131
132 % calculate a CENS feature
133
134 % component-wise quantisation of the normalized chroma vectors
135 f_stat_help = zeros(12,seg_num);
136 for n=1:length(parameter.quantSteps)
137 f_stat_help = f_stat_help + (f_chroma_energy_distr>parameter.quantSteps(n))*parameter.quantWeights(n);
138 end
139
140 % Temporal smoothing and downsampling
141 [f_chroma_energy_stat,CENSfeatureRate] = smoothDownsampleFeature(f_stat_help,parameter);
142
143 % last step: normalize each vector with its l^2 norm
144 f_CENS = normalizeFeature(f_chroma_energy_stat,2, parameter.normThresh);
145
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 % Update sideinfo
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 sideinfo.CENS.version = 1;
150 sideinfo.CENS.midiMin = parameter.midiMin;
151 sideinfo.CENS.midiMax = parameter.midiMax;
152 sideinfo.CENS.featureRate = CENSfeatureRate;
153 sideinfo.CENS.quantSteps = parameter.quantSteps;
154 sideinfo.CENS.quantWeights = parameter.quantWeights;
155 sideinfo.CENS.normThresh = parameter.normThresh;
156 sideinfo.CENS.winLenSmooth = parameter.winLenSmooth;
157 sideinfo.CENS.downsampSmooth = parameter.downsampSmooth;
158
159 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
160 % Saving to file
161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162 if parameter.save
163 filename = strcat(parameter.saveFilename,'_CENS_',num2str(parameter.winLenSmooth),'_',num2str(parameter.downsampSmooth));
164 save(strcat(parameter.saveDir,filename),'f_CENS','sideinfo');
165 end
166
167 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
168 % Visualization
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170 if parameter.visualize
171 parameterVis.featureRate = CENSfeatureRate;
172 parameterVis.title = 'CENS chromagram';
173 visualizeChroma(f_CENS,parameterVis)
174 end
175
176 end