20def PitchTimeAuditory(x, iBlockLength, iHopLength, f_s):
21
22
23 iNumOfBlocks = math.ceil(x.size / iHopLength)
24 f_0 = np.zeros(iNumOfBlocks)
25 f_max = 2000
26 iNumBands = 20
27 fLengthLpInS = 0.001
28
29 iLengthLp = math.ceil(fLengthLpInS * f_s)
30
31
32 t = (np.arange(0, iNumOfBlocks) * iHopLength + (iBlockLength / 2)) / f_s
33
34
35 X = ToolGammatoneFb(x, f_s, iNumBands)
36
37
38 X[X < 0] = 0
39
40
41 b = np.ones(iLengthLp) / iLengthLp
42 X = filtfilt(b, 1, X)
43
44 for n in range(0, iNumOfBlocks):
45
46 eta_min = int(round(f_s / f_max))
47 afSumCorr = np.zeros(iBlockLength - 1)
48 x_tmp = np.zeros(iBlockLength)
49
50 i_start = n * iHopLength
51 i_stop = np.min([x.size - 1, i_start + iBlockLength - 1])
52
53
54 for k in range(0, iNumBands):
55
56 if X[k, np.arange(i_start, i_stop + 1)].sum() < 1e-20:
57 continue
58 else:
59 x_tmp[np.arange(0, i_stop - i_start + 1)] = X[k, np.arange(i_start, i_stop + 1)]
60
61 afCorr = np.correlate(x_tmp, x_tmp, "full") / np.dot(x_tmp, x_tmp)
62
63
64 afSumCorr += afCorr[np.arange(iBlockLength, afCorr.size)]
65
66 if afSumCorr.sum() < 1e-20:
67 continue
68
69
70 iPeaks = find_peaks(afSumCorr, height=0)
71 if iPeaks[0].size:
72 eta_min = np.max([eta_min, iPeaks[0][0] - 1])
73 f_0[n] = np.argmax(afSumCorr[np.arange(eta_min + 1, afSumCorr.size)]) + 1
74
75
76 f_0[n] = f_s / (f_0[n] + eta_min + 1)
77
78 return f_0, t