-
Notifications
You must be signed in to change notification settings - Fork 0
/
PitchTimeAmdf.m
executable file
·54 lines (44 loc) · 1.66 KB
/
PitchTimeAmdf.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
% ======================================================================
%> @brief computes the lag of the average magnitude difference 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 amdf lag (in Hz)
% ======================================================================
function [f, t] = PitchTimeAmdf(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;
f_min = 70;%50;
eta_min = round(f_s/f_max);
eta_max = round(f_s/f_min);
for (n = 1:iNumOfBlocks)
i_start = (n-1)*iHopLength + 1;
i_stop = min(length(x),i_start + iBlockLength - 1);
% calculate the acf maximum
afAMDF = amdf(x(i_start:i_stop), eta_max);
if 1+eta_min >= length(afAMDF)
[fDummy, f(n)] = min(afAMDF(end-1:end)); % HACK
else
[fDummy, f(n)] = min(afAMDF(1+eta_min:end));
end
end
% convert to Hz
f = f_s ./ (f + eta_min);
end
function [AMDF] = amdf(x, eta_max)
K = length(x);
AMDF = ones(1, K);
for (eta=0:min(K-1,eta_max))
AMDF(eta+1) = sum(abs(x(1:K-eta)-x(eta+1:end)))/K;
end
end