-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPitchTimeAuditory.m
executable file
·65 lines (51 loc) · 2.16 KB
/
PitchTimeAuditory.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
% ======================================================================
%> @brief computes the f_0 via a simple "auditory" approach
%> called by ::ComputePitch
%>
%> @param x: audio signal
%> @param iBlockLength: block length in samples
%> @param iHopLength: hop length in samples
%> @param f_s: sample rate of audio data
%>
%> @retval f fundamental frequency estimate (in Hz)
% ======================================================================
function [f, t] = PitchTimeAuditory(x, iBlockLength, iHopLength, f_s)
% number of results
iNumOfBlocks = ceil (length(x)/iHopLength);
% compute time stamps
t = ((0:iNumOfBlocks-1) * iHopLength + (iBlockLength/2))/f_s;
% allocate memory
f = zeros(1,iNumOfBlocks);
%initialization
f_max = 1000; %2000;
eta_min = round(f_s/f_max);
iNumBands = 20;
fLengthLpInS = 0.001;
iLengthLp = ceil(fLengthLpInS*f_s);
% apply filterbank
X = ToolGammatoneFb(x, f_s, iNumBands);
% half wave rectification
X(X < 0) = 0;
% smooth the results
b = ones(iLengthLp,1)/iLengthLp;
X = filtfilt (b,1,X')';
for (n = 1:iNumOfBlocks)
afSumCorr = zeros(1, iBlockLength-1);
i_start = (n-1)*iHopLength + 1;
i_stop = min(length(x),i_start + iBlockLength - 1);
% compute ACF per band and summarize
for (k = 1: iNumBands)
afCorr = xcorr([X(k,i_start:i_stop), zeros(1,iBlockLength-(i_stop-i_start)-1)],'coeff');
afSumCorr = afSumCorr + afCorr((ceil((length(afCorr)/2))+1):end);
end
% find local maxima
[afPeaks,eta_peak] = findpeaks(afSumCorr);
i_tmp = find(eta_peak > eta_min,1);
% set all entries below eta_min and the first local maximum to 0
afSumCorr(1:eta_peak(i_tmp)-1) = 0;
% calculate the acf maximum
[fDummy, f(n)] = max(afSumCorr);
end
% convert to Hz
f = f_s ./ f;
end