katkost@3: function f0 = f0detectionyin(x,fs,ws,minf0,maxf0) katkost@3: % x: input signal, fs: sampling rate, ws: integration window length, minf0: minimum f0, maxf0: maximum f0 katkost@3: % f0: fundamental frequency detected in Hz katkost@3: maxlag = ws-2; % maximum lag katkost@3: th = 0.1; % set threshold katkost@3: d = zeros(maxlag,1); % init variable katkost@3: d2 = zeros(maxlag,1); % init variable katkost@3: % compute d(tau) katkost@3: x1 = x(1:ws); katkost@3: cumsumx = sum(x1.^2); katkost@3: cumsumxl = cumsumx; katkost@3: xy = xcorr(x(1:ws*2),x1); katkost@3: xy = xy(ws*2+1:ws*3-2); katkost@3: for lag=0:maxlag-1 katkost@3: d(1+lag) = cumsumx + cumsumxl - 2*xy(1+lag); katkost@3: cumsumxl = cumsumxl - x(1+lag).^2 + x(1+lag+ws+1)^2; katkost@3: end katkost@3: cumsum = 0; katkost@3: % compute d'(tau) katkost@3: d2(1) = 1; katkost@3: for lag=1:maxlag-1 katkost@3: cumsum = cumsum + d(1+lag); katkost@3: d2(1+lag) = d(1+lag)*lag./cumsum; katkost@3: end katkost@3: % limit the search to the target range katkost@3: minf0lag = 1+round(fs./minf0); % compute lag corresponding to minf0 katkost@3: maxf0lag = 1+round(fs./maxf0); % compute lag corresponding to maxf0 katkost@3: if (maxf0lag>1 && maxf0lag1 && minf0lag0) katkost@3: I = find(d2(mloc)0) katkost@3: candf0lag = mloc(I(1)); katkost@3: else katkost@3: [Y,I2] = min(d2(mloc)); katkost@3: candf0lag = mloc(I2); katkost@3: end katkost@3: candf0lag = candf0lag; % this is zero-based indexing katkost@3: if (candf0lag>1 & candf0lag 0.2) % voiced/unvoiced threshold katkost@3: f0 = 0; % set to unvoiced katkost@3: end katkost@3: