Mercurial > hg > camir-aes2014
comparison toolboxes/MIRtoolbox1.3.2/MIRToolbox/@mirmfcc/mirmfcc.m @ 0:e9a9cd732c1e tip
first hg version after svn
author | wolffd |
---|---|
date | Tue, 10 Feb 2015 15:05:51 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:e9a9cd732c1e |
---|---|
1 function varargout = mirmfcc(orig,varargin) | |
2 % c = mirmfcc(a) finds the Mel frequency cepstral coefficients (ceps), | |
3 % a numerical description of the spectrum envelope. | |
4 % | |
5 % Requires the Auditory Toolbox. | |
6 % | |
7 % Optional arguments: | |
8 % c = mirmfcc(...,'Rank',N) computes the coefficients of rank(s) N | |
9 % (default: N = 1:13). | |
10 % If a is a frame decomposition, the temporal evolution of the MFCC, | |
11 % along the successive frames, is returned. In this case, a second | |
12 % option is available: | |
13 % mirmfcc(...,'Delta',d) performs d temporal differentiations of | |
14 % the coefficients, also called delta-MFCC (for d = 1) or | |
15 % delta-delta-MFCC (for d = 2). | |
16 % mirmfcc(...,'Delta') corresponds to mirmfcc(...,'Delta',1) | |
17 % Optional arguments related to the delta computation: | |
18 % mirmfcc(...,'Radius',r) specifies, for each frame, the number of | |
19 % successive and previous neighbouring frames taken into | |
20 % consideration for the least-square approximation. | |
21 % Usually 1 or 2. | |
22 % Default value: 2. | |
23 | |
24 nbbands.key = 'Bands'; | |
25 nbbands.type = 'Integer'; | |
26 nbbands.default = 40; | |
27 option.nbbands = nbbands; | |
28 | |
29 rank.key = 'Rank'; | |
30 rank.type = 'Integer'; | |
31 rank.default = 1:13; | |
32 option.rank = rank; | |
33 | |
34 delta.key = 'Delta'; | |
35 delta.type = 'Integer'; | |
36 delta.default = 0; | |
37 delta.keydefault = 1; | |
38 option.delta = delta; | |
39 | |
40 radius.key = 'Radius'; | |
41 radius.type = 'Integer'; | |
42 radius.default = 2; | |
43 option.radius = radius; | |
44 | |
45 specif.option = option; | |
46 | |
47 varargout = mirfunction(@mirmfcc,orig,varargin,nargout,specif,@init,@main); | |
48 | |
49 | |
50 function [x type] = init(x,option) | |
51 if isamir(x,'miraudio') || isamir(x,'mirspectrum') | |
52 x = mirspectrum(x,'Mel','log','Bands',option.nbbands); | |
53 end | |
54 type = 'mirmfcc'; | |
55 | |
56 | |
57 function c = main(orig,option,postoption) | |
58 if iscell(orig) | |
59 orig = orig{1}; | |
60 end | |
61 if isa(orig,'mirmfcc') | |
62 c = orig; | |
63 if option.rank | |
64 magn = get(c,'Data'); | |
65 rank = get(c,'Rank'); | |
66 for h = 1:length(magn) | |
67 for k = 1:length(magn{h}) | |
68 m = magn{h}{k}; | |
69 r = rank{h}{k}; | |
70 r1 = r(:,1,1); | |
71 range = find(ismember(r1,option.rank)); | |
72 magn{h}{k} = m(range,:,:); | |
73 rank{h}{k} = r(range,:,:); | |
74 end | |
75 end | |
76 c = set(c,'Data',magn,'Rank',rank); | |
77 end | |
78 c = modif(c,option); | |
79 else | |
80 c.delta = 0; | |
81 %disp('Computing Mel frequency cepstral coefficients...'); | |
82 e = get(orig,'Magnitude'); | |
83 | |
84 % The following is largely based on the source code from Auditory Toolbox | |
85 % (A part that I could not call directly from MIRtoolbox) | |
86 | |
87 % (Malcolm Slaney, August 1993, (c) 1998 Interval Research Corporation) | |
88 | |
89 try | |
90 MakeERBFilters(1,1,1); % Just to be sure that the Auditory Toolbox is installed | |
91 catch | |
92 error(['ERROR IN MIRFILTERBANK: Auditory Toolbox needs to be installed.']); | |
93 end | |
94 | |
95 dc = cell(1,length(e)); | |
96 rk = cell(1,length(e)); | |
97 for h = 1:length(e) | |
98 dc{h} = cell(1,length(e{h})); | |
99 rk{h} = cell(1,length(e{h})); | |
100 for i = 1:length(e{h}) | |
101 ei = e{h}{i}; | |
102 totalFilters = size(ei,3); %Number of mel bands. | |
103 | |
104 % Figure out Discrete Cosine Transform. We want a matrix | |
105 % dct(i,j) which is totalFilters x cepstralCoefficients in size. | |
106 % The i,j component is given by | |
107 % cos( i * (j+0.5)/totalFilters pi ) | |
108 % where we have assumed that i and j start at 0. | |
109 mfccDCTMatrix = 1/sqrt(totalFilters/2)*... | |
110 cos(option.rank' * ... | |
111 (2*(0:(totalFilters-1))+1) * ... | |
112 pi/2/totalFilters); | |
113 rank0 = find(option.rank == 0); | |
114 mfccDCTMatrix(rank0,:) = mfccDCTMatrix(rank0,:) * sqrt(2)/2; | |
115 ceps = zeros(size(mfccDCTMatrix,1),size(ei,2)); | |
116 for j = 1:size(ei,2) | |
117 ceps(:,j) = mfccDCTMatrix * permute(ei(1,j,:),[3 1 2]); | |
118 end | |
119 dc{h}{i} = ceps; | |
120 rk{h}{i} = repmat(option.rank(:),[1 size(ceps,2) size(ceps,3)]); | |
121 end | |
122 end | |
123 c = class(c,'mirmfcc',mirdata(orig)); | |
124 c = purgedata(c); | |
125 c = set(c,'Title','MFCC','Abs','coefficient ranks','Ord','magnitude',... | |
126 'Data',dc,'Rank',rk); | |
127 c = modif(c,option); | |
128 end | |
129 c = {c orig}; | |
130 | |
131 | |
132 function c = modif(c,option) | |
133 d = get(c,'Data'); | |
134 fp = get(c,'FramePos'); | |
135 t = get(c,'Title'); | |
136 if option.delta | |
137 M = option.radius; | |
138 for k = 1:option.delta | |
139 for h = 1:length(d) | |
140 for i = 1:length(d{h}) | |
141 nc = size(d{h}{i},2)-2*M; | |
142 di = zeros(size(d{h}{i},1),nc); | |
143 for j = 1:M | |
144 di = di + j * (d{h}{i}(:,M+j+(1:nc)) ... | |
145 - d{h}{i}(:,M-j+(1:nc))); | |
146 end | |
147 di = di / 2 / sum((1:M).^2); % MULTIPLY BY 2 INSTEAD OF SQUARE FOR NORMALIZATION ? | |
148 d{h}{i} = di; | |
149 fp{h}{i} = fp{h}{i}(:,M+1:end-M); | |
150 end | |
151 end | |
152 t = ['Delta-',t]; | |
153 end | |
154 end | |
155 c = set(c,'Data',d,'FramePos',fp,'Delta',get(c,'Delta')+option.delta,... | |
156 'Title',t); |