23def computeBeatHisto(x, f_s, cMethod='FFT', afWindow=None, iBlockLength=1024, iHopLength=8):
24
25 if afWindow is None:
26 afWindow = ToolComputeHann(iBlockLength)
27
28 assert (afWindow.shape[0] == iBlockLength), "parameter error: invalid window dimension"
29
30
31 x = ToolPreprocAudio(x)
32
33
34 [d, t, peaks] = computeNoveltyFunction('Flux', x, f_s, afWindow, iBlockLength, iHopLength)
35
36 if cMethod == 'Corr':
37
38 r_dd = np.correlate(d, d, "full") / np.dot(d, d)
39 r_dd = r_dd[np.arange(d.shape[0], r_dd.size)]
40
41 Bpm = np.flip(60 / t[np.arange(1, t.shape[0])])
42 T = np.flip(r_dd)
43
44 elif cMethod == 'FFT':
45
46 iHistoLength = 65536
47 afWindow = np.zeros(2*iHistoLength)
48 afWindow[np.arange(0, iHistoLength)] = ToolComputeHann(iHistoLength)
49 f_s = f_s / iHopLength
50 if len(d) < 2 * iHistoLength:
51 d = [d, np.zeros([1, 2 * iHistoLength - len(d)])]
52
53 [D, f, t] = computeSpectrogram(d, f_s, afWindow, 2*iHistoLength, iHistoLength/4)
54
55 T = D.mean(axis=1, keepdims=True)
56
57
58 Bpm = f * 60
59 lIdx = np.argwhere(Bpm < 30)[-1]
60 hIdx = np.argwhere(Bpm > 200)[0]
61 T = T[np.arange(lIdx, hIdx)]
62 Bpm = Bpm[np.arange(lIdx, hIdx)]
63 else:
64 T = 0
65 Bpm = 0
66
67 return T, Bpm
68
69