-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathharmonicCQT.py
179 lines (136 loc) · 7.91 KB
/
harmonicCQT.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
######################################################
# HARMONIC CQT #
# #
# Efficient implementation for harmonic CQT #
# #
# Johannes Zeitler ([email protected]) #
# Dec. 2020 #
######################################################
import numpy as np
import librosa
def HCQT(x, fs, frameRate=10, f_min='C1', binsPerKey=1, numOctaves=5, numHarmonics=1, centerNoteToBins=True, correctTuning=0.0, hopSizeCQT=None):
#==========================================================================#
# EFFICIENT HARMONIC CONSTANT Q TRANSFORM #
# #
# Idea: a single cqt can never exactly contain all harmonics. The cqt with #
# base f0 can contain harmonics 1, 2, 4, 8,... the cqt with base 3*f0 can #
# contain harmonics 3, 6, 12,... #
# #
# Example: 6 harmonics #
# Necessary CQTS: f0 (1,2,4), 3*f0 (3,6), 5*f0 (5) #
#========================= INPUTS =========================================#
# x .................. input signal #
# fs ................. sampling frequency of input signal [Hz] #
# frameRate .......... frame rate for HCQT [Hz] #
# f_min .............. lowest CQT frequency [Hz] or lowest note [string] #
# binsPerKey ......... CQT bins per key #
# numOctaves ......... number of CQT octaves #
# numHarmonics ....... number of harmonics for HCQT #
# centerNoteToBins ... if f_min is a note [string] and binsPerKey >=3, #
# whether to center the note frequency at binsPerKey #
# #
#========================= OUTPUT =========================================#
# HCQT ............... output HCQT [cqt bins x time frames x harmonics] #
# hopSizeCQT ......... CQT hop size #
# cqtTimes ........... CQT time steps #
#==========================================================================#
if isinstance(correctTuning, bool):
if correctTuning:
tuning = librosa.estimate_tuning(x,fs, bins_per_octave=12)*binsPerKey
else:
tuning = 0
else:
tuning = float(correctTuning)
#print('Tuning: %.3f'%(tuning))
eps = 1e-15
binsPerOctave = binsPerKey*12
# max. number of octaves to be processed in a single cqt
maxCQT_octaves = np.floor(np.log2(numHarmonics)) + numOctaves
if hopSizeCQT is None:
# CQT hop size is integer multiple of 2**maxCQT_octaves
hopSizeCQT = getHopSize(fs, frameRate, numOctaves, numHarmonics)
# if f_min is a string, convert note to hz
if isinstance(f_min, str):
f_min = librosa.note_to_hz(f_min)
# if true, center the note frequencies. I.e. if binsPerKey=3, the piano note frequencies are on bins 2, 5, 8,...
if centerNoteToBins:
f_min = f_min / (2**((binsPerKey-1)/2/binsPerOctave))
# for all harmonics, find the 'basic' cqt where they can be derived from
from_cqt = np.zeros((numHarmonics), dtype='int')
for h in range(1, numHarmonics+1):
# check if harmonic is odd -> we need a new cqt here
if np.mod(h,2) > eps:
from_cqt[h-1] = h
# if harmonic is even, get the lowest multiple of f0 that can be used as a base cqt
else:
base = h
while np.mod(base, 2) < eps:
base = base/2
from_cqt[h-1] = base
addFrame = 0
if np.mod(len(x), hopSizeCQT) < eps:
addFrame = 1
HCQT = np.zeros((numOctaves*binsPerOctave, np.ceil(len(x)/hopSizeCQT).astype(int)+addFrame, numHarmonics))#, dtype=complex)
# harmonics that have already been processed
processedHarmonics = []
# development purposes
additionalOctavesCntr = 0
for h in range(1, numHarmonics+1):
#print('h: %i'%(h))
if h in processedHarmonics:
#print('continue')
continue
numHarmonicsForCQT = np.sum(from_cqt==h)
additionalOctaves = numHarmonicsForCQT - 1
additionalOctavesCntr += additionalOctaves
#print('Perform cqt with fmin=%.8f, nBins=%i and hopLength=%i'%(f_min*h, int((numOctaves+additionalOctaves)*binsPerOctave), hopSizeCQT))
# perform the cqt
cqt = librosa.cqt(x, sr=fs, hop_length=hopSizeCQT,
fmin=f_min*h,
n_bins=int((numOctaves+additionalOctaves)*binsPerOctave),
bins_per_octave=binsPerOctave,
tuning = tuning)
# extract harmonics from cqt and insert in HCQT
for h_ in range(1, numHarmonics+1):
#print('h_: %i'%(h_))
if from_cqt[h_ - 1] == h:
cqtOctave = np.log2(h_/h).astype(int)
#print('Insert harmonic %i to HCQT, cqt bins from %i to %i'%(h_, cqtOctave*binsPerOctave, (cqtOctave+numOctaves)*binsPerOctave))
#print('CQT shape: ',cqt.shape)
HCQT[:,:,h_ - 1] = np.abs(cqt[cqtOctave*binsPerOctave:(cqtOctave+numOctaves)*binsPerOctave,:])
processedHarmonics.append(h_)
cqtTimes = librosa.times_like(HCQT.shape[1], sr=fs, hop_length=hopSizeCQT)
return HCQT, hopSizeCQT, cqtTimes
def subHCQT(x, fs, frameRate=10, f_min='C1', binsPerKey=1, numOctaves=5, numHarmonics=1, centerNoteToBins=True, correctTuning=0.0, hopSizeCQT=None, numSubHarmonics=1):
if isinstance(correctTuning, bool):
if correctTuning:
tuning = librosa.estimate_tuning(x,fs, bins_per_octave=binsPerKey*12)
else:
tuning = 0
else:
tuning = float(correctTuning)
print('Tuning: %.3f'%(tuning))
eps = 1e-15
binsPerOctave = binsPerKey*12
# max. number of octaves to be processed in a single cqt
maxCQT_octaves = np.floor(np.log2(numHarmonics)) + numOctaves
if hopSizeCQT is None:
# CQT hop size is integer multiple of 2**maxCQT_octaves
hopSizeCQT = getHopSize(fs, frameRate, numOctaves, numHarmonics)
# if f_min is a string, convert note to hz
if isinstance(f_min, str):
f_min = librosa.note_to_hz(f_min)
# if true, center the note frequencies. I.e. if binsPerKey=3, the piano note frequencies are on bins 2, 5, 8,...
if centerNoteToBins:
f_min = f_min / (2**((binsPerKey-1)/2/binsPerOctave))
hcqt,_,cqtTimes = HCQT(x, fs, frameRate=frameRate, f_min=f_min, binsPerKey=binsPerKey, numOctaves=numOctaves, numHarmonics=numHarmonics, correctTuning=tuning, hopSizeCQT=hopSizeCQT)
for s in range(numSubHarmonics):
fSub = f_min/(s+1)
shcqt, _, _ = HCQT(x, fs, frameRate=frameRate, f_min=fSub, binsPerKey=binsPerKey, numOctaves=numOctaves, numHarmonics=1, correctTuning=tuning, hopSizeCQT=hopSizeCQT)
hcqt = np.concatenate((shcqt, hcqt), axis=2)
return hcqt, hopSizeCQT, cqtTimes
def getHopSize(fs, frameRate, numOctaves, numHarmonics):
maxCQT_octaves = np.floor(np.log2(numHarmonics)) + numOctaves
# CQT hop size is integer multiple of 2**maxCQT_octaves
hopSizeCQT = int(round(fs/frameRate/(2**(maxCQT_octaves-1)))*2**(maxCQT_octaves-1))
return hopSizeCQT