-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPitchTimeAcf.m
executable file
·43 lines (36 loc) · 1.44 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
% ======================================================================
%> @brief 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 acf lag (in Hz)
% ======================================================================
function [f, t] = PitchTimeAcf(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
fMaxFreq = 1000; %2000
eta_min = round(f_s/fMaxFreq);
for (n = 1:iNumOfBlocks)
i_start = (n-1)*iHopLength + 1;
i_stop = min(length(x),i_start + iBlockLength - 1);
% calculate the acf maximum
afCorr = xcorr(x(i_start:i_stop),'coeff');
afCorr = afCorr((ceil((length(afCorr)/2))+1):end);
if 1+eta_min < length(afCorr)
[fDummy, f(n)] = max(afCorr(1+eta_min:end));
else
[fDummy, f(n)] = max(afCorr(end-1:end)); % HACK
end
end
% convert to Hz
f = f_s ./ (f + eta_min);
end