forked from alexanderlerch/ACA-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PitchTimeAcf.m
54 lines (45 loc) · 1.55 KB
/
PitchTimeAcf.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
%computes the lag of the autocorrelation function
%> 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_0 acf lag (in Hz)
%> @retval t time stamp of f_0 estimate (in s)
% ======================================================================
function [f_0, t] = PitchTimeAcf(x, iBlockLength, iHopLength, f_s)
% blocking
[x_b, t] = ToolBlockAudio(x, iBlockLength, iHopLength, f_s);
iNumOfBlocks = size(x_b, 1);
% allocate memory
f_0 = zeros(1, iNumOfBlocks);
%initialization
fMaxFreq = 2000;
fMinThresh = 0.35;
for n = 1:iNumOfBlocks
eta_min = round(f_s / fMaxFreq);
if (sum(abs(x_b(n, :))) == 0)
f_0(n) = 0;
continue;
end
% calculate the acf maximum
afCorr = xcorr(x_b(n, :), 'coeff');
afCorr = afCorr((ceil((length(afCorr)/2))+1):end);
% ignore values until threshold was crossed
eta_tmp = find (afCorr < fMinThresh, 1);
if (~isempty(eta_tmp))
eta_min = max(eta_min, eta_tmp);
end
% only take into account values after the first minimum
afDeltaCorr = diff(afCorr);
eta_tmp = find(afDeltaCorr > 0, 1);
if (~isempty(eta_tmp))
eta_min = max(eta_min, eta_tmp);
end
[fDummy, T_0(n)] = max(afCorr(1+eta_min:end));
% convert to Hz
f_0(n) = f_s ./ (T_0(n) + eta_min);
end
end