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