annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/@mirmfcc/mirmfcc.m @ 0:cc4b1211e677 tip

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