annotate toolboxes/MIRtoolbox1.3.2/MIRToolbox/mirflux.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 = mirflux(orig,varargin)
Daniel@0 2 % f = mirflux(x) measures distance between successive frames.
Daniel@0 3 % First argument:
Daniel@0 4 % If x is a spectrum, this corresponds to spectral flux.
Daniel@0 5 % But the flux of any other data can be computed as well.
Daniel@0 6 % If x is an audio file or audio signal, the spectral flux is
Daniel@0 7 % computed by default.
Daniel@0 8 % Optional arguments:
Daniel@0 9 % f = mirflux(x,'Frame',...) specifies the frame parameters, if x is
Daniel@0 10 % not already decomposed into frames. Default values are frame
Daniel@0 11 % length of .2 seconds and hop factor of 1.3.
Daniel@0 12 % f = mirflux(x,'Dist',d) specifies the distance between
Daniel@0 13 % successive frames: (IF 'COMPLEX': DISTANCE = 'CITY' ALWAYS)
Daniel@0 14 % d = 'Euclidian': Euclidian distance (Default)
Daniel@0 15 % d = 'City': City-block distance
Daniel@0 16 % d = 'Cosine': Cosine distance (or normalized correlation)
Daniel@0 17 % f = mirflux(...,'Inc'): Only positive difference between frames are
Daniel@0 18 % summed, in order to focus on increase of energy solely.
Daniel@0 19 % f = mirflux(...,'Complex'), for spectral flux, combines use of
Daniel@0 20 % energy and phase information (Bello et al, 2004).
Daniel@0 21 % f = mirflux(...,'Halfwave'): performs a half-wave rectification on
Daniel@0 22 % the result.
Daniel@0 23 % f = mirflux(...,'Median',l,C): removes small spurious peaks by
Daniel@0 24 % subtracting to the result its median filtering. The median
Daniel@0 25 % filter computes the point-wise median inside a window of length
Daniel@0 26 % l (in seconds), that includes a same number of previous and
Daniel@0 27 % next samples. C is a scaling factor whose purpose is to
Daniel@0 28 % artificially rise the curve slightly above the steady state of
Daniel@0 29 % the signal. If no parameters are given, the default values are:
Daniel@0 30 % l = 0.2 s. and C = 1.3
Daniel@0 31 % f = mirflux(...,'Median',l,C,'Halfwave'): The scaled median
Daniel@0 32 % filtering is designed to be succeeded by the half-wave
Daniel@0 33 % rectification process in order to select peaks above the
Daniel@0 34 % dynamic threshold calculated with the help of the median
Daniel@0 35 % filter. The resulting signal is called "detection function"
Daniel@0 36 % (Alonso, David, Richard, 2004). To ensure accurate detection,
Daniel@0 37 % the length l of the median filter must be longer than the
Daniel@0 38 % average width of the peaks of the detection function.
Daniel@0 39 %
Daniel@0 40 % (Bello et al, 2004) Juan P. Bello, Chris Duxbury, Mike Davies, and Mark
Daniel@0 41 % Sandler, On the Use of Phase and Energy for Musical Onset Detection in
Daniel@0 42 % the Complex Domain, IEEE SIGNAL PROCESSING LETTERS, VOL. 11, NO. 6,
Daniel@0 43 % JUNE 2004
Daniel@0 44
Daniel@0 45 dist.key = 'Dist';
Daniel@0 46 dist.type = 'String';
Daniel@0 47 dist.choice = {'Euclidian','City','Cosine'}; %PDIST???? (euclidean)
Daniel@0 48 dist.default = 'Euclidian';
Daniel@0 49 option.dist = dist;
Daniel@0 50
Daniel@0 51 inc.key = 'Inc';
Daniel@0 52 inc.type = 'Boolean';
Daniel@0 53 inc.default = 0;
Daniel@0 54 option.inc = inc;
Daniel@0 55
Daniel@0 56 complex.key = 'Complex';
Daniel@0 57 complex.type = 'Boolean';
Daniel@0 58 complex.default = 0;
Daniel@0 59 option.complex = complex;
Daniel@0 60
Daniel@0 61 h.key = 'Halfwave';
Daniel@0 62 h.type = 'Boolean';
Daniel@0 63 h.default = 0;
Daniel@0 64 h.when = 'After';
Daniel@0 65 option.h = h;
Daniel@0 66
Daniel@0 67 median.key = 'Median';
Daniel@0 68 median.type = 'Integer';
Daniel@0 69 median.number = 2;
Daniel@0 70 median.default = [0 0];
Daniel@0 71 median.keydefault = [.2 1.3];
Daniel@0 72 median.when = 'After';
Daniel@0 73 option.median = median;
Daniel@0 74
Daniel@0 75 frame.key = 'Frame';
Daniel@0 76 frame.type = 'Integer';
Daniel@0 77 frame.number = 2;
Daniel@0 78 frame.default = [.05 .5];
Daniel@0 79 option.frame = frame;
Daniel@0 80
Daniel@0 81 specif.option = option;
Daniel@0 82
Daniel@0 83 varargout = mirfunction(@mirflux,orig,varargin,nargout,specif,@init,@main);
Daniel@0 84
Daniel@0 85
Daniel@0 86 function [x type] = init(x,option)
Daniel@0 87 if isamir(x,'miraudio')
Daniel@0 88 if isframed(x)
Daniel@0 89 x = mirspectrum(x);
Daniel@0 90 else
Daniel@0 91 x = mirspectrum(x,'Frame',option.frame.length.val,option.frame.length.unit,...
Daniel@0 92 option.frame.hop.val,option.frame.hop.unit);
Daniel@0 93 end
Daniel@0 94 end
Daniel@0 95 if isa(x,'mirdesign')
Daniel@0 96 x = set(x,'Overlap',1);
Daniel@0 97 end
Daniel@0 98 type = 'mirscalar';
Daniel@0 99
Daniel@0 100
Daniel@0 101 function f = main(s,option,postoption)
Daniel@0 102 if iscell(s)
Daniel@0 103 s = s{1};
Daniel@0 104 end
Daniel@0 105 t = get(s,'Title');
Daniel@0 106 if isa(s,'mirscalar') && ...
Daniel@0 107 (strcmp(t,'Harmonic Change Detection Function') || ...
Daniel@0 108 (length(t)>4 && strcmp(t(end-3:end),'flux')) || ...
Daniel@0 109 (length(t)>5 && strcmp(t(end-4:end-1),'flux')))
Daniel@0 110 if not(isempty(postoption))
Daniel@0 111 f = modif(s,postoption);
Daniel@0 112 end
Daniel@0 113 else
Daniel@0 114 if isa(s,'mirspectrum')
Daniel@0 115 t = 'Spectral';
Daniel@0 116 end
Daniel@0 117 m = get(s,'Data');
Daniel@0 118 if option.complex
Daniel@0 119 if not(isa(s,'mirspectrum'))
Daniel@0 120 error('ERROR IN MIRFLUX: ''Complex'' option only defined for spectral flux.');
Daniel@0 121 end
Daniel@0 122 ph = get(s,'Phase');
Daniel@0 123 end
Daniel@0 124 param.complex = option.complex;
Daniel@0 125 param.inc = option.inc;
Daniel@0 126 fp = get(s,'FramePos');
Daniel@0 127 if strcmp(t,'Tonal centroid')
Daniel@0 128 t = 'Harmonic Change Detection Function';
Daniel@0 129 else
Daniel@0 130 t = [t,' flux'];
Daniel@0 131 end
Daniel@0 132 disp(['Computing ' t '...'])
Daniel@0 133 ff = cell(1,length(m));
Daniel@0 134 newsr = cell(1,length(m));
Daniel@0 135 dist = str2func(option.dist);
Daniel@0 136 %[tmp s] = gettmp(s); %get(s,'Tmp');
Daniel@0 137 for h = 1:length(m)
Daniel@0 138 ff{h} = cell(1,length(m{h}));
Daniel@0 139 if not(iscell(m{h}))
Daniel@0 140 m{h} = {m{h}};
Daniel@0 141 end
Daniel@0 142 for i = 1:length(m{h})
Daniel@0 143 mi = m{h}{i};
Daniel@0 144 if size(mi,3) > 1 && size(mi,1) == 1
Daniel@0 145 mi = reshape(mi,size(mi,2),size(mi,3))';
Daniel@0 146 end
Daniel@0 147 if option.complex
Daniel@0 148 phi = ph{h}{i};
Daniel@0 149 end
Daniel@0 150 fpi = fp{h}{i};
Daniel@0 151 %if 0 %not(isempty(tmp)) && isstruct(tmp) && isfield(tmp,'mi')
Daniel@0 152 % mi = [tmp.mi mi];
Daniel@0 153 % fpi = [tmp.fpi fpi];
Daniel@0 154 %end
Daniel@0 155 nc = size(mi,2);
Daniel@0 156 np = size(mi,3);
Daniel@0 157 if nc == 1
Daniel@0 158 warning('WARNING IN MIRFLUX: Flux can only be computed on signal decomposed into frames.');
Daniel@0 159 ff{h}{i} = NaN(1,1,np);
Daniel@0 160 else
Daniel@0 161 if option.complex
Daniel@0 162 fl = zeros(1,nc-2,np);
Daniel@0 163 for k = 1:np
Daniel@0 164 d = diff(phi(:,:,k),2,2);
Daniel@0 165 d = d/(2*pi) - round(d/(2*pi));
Daniel@0 166 g = sqrt(mi(:,3:end,k).^2 + mi(:,2:(end-1),k).^2 ...
Daniel@0 167 - 2.*mi(:,3:end,k)...
Daniel@0 168 .*mi(:,2:(end-1),k)...
Daniel@0 169 .*cos(d));
Daniel@0 170 fl(1,:,k) = sum(g);
Daniel@0 171 end
Daniel@0 172 fp{h}{i} = fpi(:,3:end);
Daniel@0 173 else
Daniel@0 174 fl = zeros(1,nc-1,np);
Daniel@0 175 for k = 1:np
Daniel@0 176 for j = 1:nc-1
Daniel@0 177 fl(1,j,k) = dist(mi(:,j,k),mi(:,j+1,k),option.inc);
Daniel@0 178 end
Daniel@0 179 end
Daniel@0 180 fp{h}{i} = fpi(:,2:end);
Daniel@0 181 end
Daniel@0 182 ff{h}{i} = fl;
Daniel@0 183 %tmp.mi = mi(:,end,:);
Daniel@0 184 %tmp.fpi = fpi(:,end,:);
Daniel@0 185 end
Daniel@0 186 end
Daniel@0 187 %tmp = [];
Daniel@0 188 if size(fpi,2)<2
Daniel@0 189 newsr{h} = 0;
Daniel@0 190 else
Daniel@0 191 newsr{h} = 1/(fpi(1,2)-fpi(1,1));
Daniel@0 192 end
Daniel@0 193 end
Daniel@0 194 f = mirscalar(s,'Data',ff,'FramePos',fp,'Sampling',newsr,...
Daniel@0 195 'Title',t,'Parameter',param); %,'Tmp',tmp);
Daniel@0 196 %f = settmp(f,tmp);
Daniel@0 197 if not(isempty(postoption))
Daniel@0 198 f = modif(f,postoption);
Daniel@0 199 end
Daniel@0 200 end
Daniel@0 201
Daniel@0 202
Daniel@0 203 function f = modif(f,option)
Daniel@0 204 fl = get(f,'Data');
Daniel@0 205 r = get(f,'Sampling');
Daniel@0 206 for h = 1:length(fl)
Daniel@0 207 for i = 1:length(fl{h})
Daniel@0 208 fli = fl{h}{i};
Daniel@0 209 nc = size(fli,2);
Daniel@0 210 np = size(fli,3);
Daniel@0 211 if option.median(1)
Daniel@0 212 ffi = zeros(1,nc,np);
Daniel@0 213 lr = round(option.median(1)*r{i});
Daniel@0 214 for k = 1:np
Daniel@0 215 for j = 1:nc
Daniel@0 216 ffi(:,j,k) = fli(:,j,k) - ...
Daniel@0 217 option.median(2) * median(fli(:,max(1,j-lr):min(nc-1,j+lr),k));
Daniel@0 218 end
Daniel@0 219 end
Daniel@0 220 fli = ffi;
Daniel@0 221 end
Daniel@0 222 if option.h
Daniel@0 223 fli = hwr(fli);
Daniel@0 224 end
Daniel@0 225 fl{h}{i} = fli;
Daniel@0 226 end
Daniel@0 227 end
Daniel@0 228 f = set(f,'Data',fl);
Daniel@0 229
Daniel@0 230
Daniel@0 231 function y = Euclidian(mi,mj,inc)
Daniel@0 232 if inc
Daniel@0 233 y = sqrt(sum(max(0,(mj-mi)).^2));
Daniel@0 234 else
Daniel@0 235 y = sqrt(sum((mj-mi).^2));
Daniel@0 236 end
Daniel@0 237
Daniel@0 238
Daniel@0 239 function y = City(mi,mj,inc)
Daniel@0 240 if inc
Daniel@0 241 y = sum(max(0,(mj-mi)));
Daniel@0 242 else
Daniel@0 243 y = sum(abs(mj-mi));
Daniel@0 244 end
Daniel@0 245
Daniel@0 246
Daniel@0 247 function d = Cosine(r,s,inc)
Daniel@0 248 nr = sqrt(r'*r);
Daniel@0 249 ns = sqrt(s'*s);
Daniel@0 250 if or(nr == 0, ns == 0);
Daniel@0 251 d = 1;
Daniel@0 252 else
Daniel@0 253 d = 1 - r'*s/nr/ns;
Daniel@0 254 end