-
Notifications
You must be signed in to change notification settings - Fork 1
/
cqhc.py
294 lines (249 loc) · 10.8 KB
/
cqhc.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
"""
This Python module implements the constant-Q harmonic coefficients (CQHCs) and other related functions.
Functions:
mfcc - Compute the mel-frequency cepstral coefficients (MFCCs) (using librosa).
cqtspectrogram - Compute the (magnitude) constant-Q transform (CQT) spectrogram (using librosa).
cqtdeconv - Deconvolve the CQT spectrogram into a pitch-normalized spectral component and an energy-normalized pitch component.
cqhc - Compute the constant- harmonic coefficients (CQHCs).
Author:
Zafar Rafii
zafarrafii@gmail.com
http://zafarrafii.com
https://github.com/zafarrafii
https://www.linkedin.com/in/zafarrafii/
12/27/21
"""
import numpy as np
import librosa
def mfcc(
audio_signal, sampling_frequency, window_length, step_length, number_coefficients=20
):
"""
Compute the mel-frequency cepstral coefficients (MFCCs) (using librosa).
Inputs:
audio_signal: audio signal (number_samples,)
sampling_frequency: sampling frequency in Hz
window_length: window length in samples
step_length: step length in samples
number_coefficients: number of MFCCs (default: 20 coefficients)
Output:
audio_mfcc: audio MFCCs (number_coefficients, number_frames)
Example: Compute the MFCCs from an audio file.
# Import the modules
import numpy as np
import cqhc
import librosa
import librosa.display
import matplotlib.pyplot as plt
# Load the audio signal
file_path = r'bass_acoustic_000-036-075.wav'
audio_signal, sampling_frequency = librosa.load(file_path, sr=None, mono=True)
# Define the parameters and compute the MFCCs
window_length = pow(2, int(np.ceil(np.log2(0.04 * sampling_frequency))))
step_length = int(window_length / 2)
number_coefficients = 20
audio_mfcc = cqhc.mfcc(audio_signal, sampling_frequency, window_length, step_length, number_coefficients)
# Display the MFCCs
plt.figure(figsize=(14, 4))
librosa.display.specshow(audio_mfcc, x_axis='time', sr=sampling_frequency, hop_length=step_length, cmap='jet')
plt.title('MFCCs')
plt.ylabel('Coefficient')
plt.tight_layout()
plt.show()
"""
# Compute the MFCCs using librosa's mfcc
audio_mfcc = librosa.feature.mfcc(
y=audio_signal,
sr=sampling_frequency,
n_fft=window_length,
hop_length=step_length,
n_mfcc=number_coefficients,
)
return audio_mfcc
def cqtspectrogram(
audio_signal,
sampling_frequency,
step_length,
minimum_frequency=32.70,
octave_resolution=12,
):
"""
Compute the (magnitude) constant-Q transform (CQT) spectrogram (using librosa).
Inputs:
audio_signal: audio signal (number_samples,)
sampling_frequency: sampling frequency in Hz
step_length: step length in samples
minimum_frequency: minimum frequency in Hz (default: 32.70 Hz = C1)
octave_resolution: number of frequency channels per octave (default: 12 frequency channels per octave)
Output:
cqt_spectrogram: magnitude CQT spectrogram (number_frequencies, number_frames)
Example: Compute the CQT spectrogram from an audio file.
# Import the modules
import numpy as np
import cqhc
import librosa
import librosa.display
import matplotlib.pyplot as plt
# Load the audio signal
file_path = r'bass_acoustic_000-036-075.wav'
audio_signal, sampling_frequency = librosa.load(file_path, sr=None, mono=True)
# Define the parameters and compute the CQT spectrogram
step_length = int(pow(2, int(np.ceil(np.log2(0.04 * sampling_frequency)))) / 2)
minimum_frequency = 32.70
octave_resolution = 12
cqt_spectrogram = cqhc.cqtspectrogram(audio_signal, sampling_frequency, step_length, minimum_frequency, \
octave_resolution)
# Display the CQT spectrogram
plt.figure(figsize=(14, 4))
librosa.display.specshow(librosa.amplitude_to_db(cqt_spectrogram), x_axis='time', y_axis='cqt_note', \
sr=sampling_frequency, hop_length=step_length, fmin=minimum_frequency, \
bins_per_octave=octave_resolution, cmap='jet')
plt.title('CQT spectrogram')
plt.tight_layout()
plt.show()
"""
# Derive the number of frequency channels
maximum_frequency = sampling_frequency / 2
number_frequencies = round(
octave_resolution * np.log2(maximum_frequency / minimum_frequency)
)
# Compute the magnitude CQT spectrogram using librosa
cqt_spectrogram = np.abs(
librosa.cqt(
audio_signal,
sr=sampling_frequency,
hop_length=step_length,
fmin=minimum_frequency,
bins_per_octave=octave_resolution,
n_bins=number_frequencies,
)
)
return cqt_spectrogram
def cqtdeconv(cqt_spectrogram):
"""
Deconvolve the constant-Q transform (CQT) spectrogram into a pitch-normalized spectral component and an energy-normalized pitch component.
Inputs:
cqt_spectrogram: CQT spectrogram (number_frequencies, number_frames)
Output:
spectral_component: pitch-normalized spectral component (number_frequencies, number_frames)
pitch_component: energy-normalized pitch component (number_frequencies, number_frames)
Example: Deconvolve a CQT spectrogram into its spectral component and pitch component.
# Import the modules
import numpy as np
import cqhc
import librosa
import librosa.display
import matplotlib.pyplot as plt
# Load the audio signal
file_path = r'bass_acoustic_000-036-075.wav'
audio_signal, sampling_frequency = librosa.load(file_path, sr=None, mono=True)
# Define the parameters and compute the CQT spectrogram
step_length = int(pow(2, int(np.ceil(np.log2(0.04 * sampling_frequency)))) / 2)
minimum_frequency = 32.70
octave_resolution = 12
cqt_spectrogram = cqhc.cqtspectrogram(audio_signal, sampling_frequency, step_length, minimum_frequency, octave_resolution)
# Deconvolve the CQT spectrogram into a spectral component and pitch component
spectral_component, pitch_component = cqhc.cqtdeconv(cqt_spectrogram)
# Display the CQT spectrogram, spectral component, and pitch component
plt.figure(figsize=(14, 4))
plt.subplot(1, 3, 1)
librosa.display.specshow(librosa.amplitude_to_db(cqt_spectrogram), x_axis='time', y_axis='cqt_note', \
sr=sampling_frequency, hop_length=step_length, fmin=minimum_frequency, \
bins_per_octave=octave_resolution, cmap='jet')
plt.title('CQT spectrogram')
plt.subplot(1, 3, 2)
librosa.display.specshow(librosa.amplitude_to_db(spectral_component), x_axis='time', y_axis='cqt_note', \
sr=sampling_frequency, hop_length=step_length, fmin=minimum_frequency, \
bins_per_octave=octave_resolution, cmap='jet')
plt.title('Spectral component')
plt.subplot(1, 3, 3)
librosa.display.specshow(pitch_component, x_axis='time', y_axis='cqt_note', sr=sampling_frequency, \
hop_length=step_length, fmin=minimum_frequency, bins_per_octave=octave_resolution, cmap='jet')
plt.title('Pitch component')
plt.tight_layout()
plt.show()
"""
# Get the number of frequency channels
number_frequencies = np.shape(cqt_spectrogram)[0]
# Compute the Fourier transform of every frame and their magnitude
ftcqt_spectrogram = np.fft.fft(cqt_spectrogram, 2 * number_frequencies - 1, axis=0)
absftcqt_spectrogram = abs(ftcqt_spectrogram)
# Derive the spectral component and the pitch component
spectral_component = np.real(
np.fft.ifft(absftcqt_spectrogram, axis=0)[0:number_frequencies, :]
)
pitch_component = np.real(
np.fft.ifft(ftcqt_spectrogram / (absftcqt_spectrogram + 1e-16), axis=0)[
0:number_frequencies, :
]
)
return spectral_component, pitch_component
def cqhc(
audio_signal,
sampling_frequency,
step_length,
minimum_frequency=32.70,
octave_resolution=12,
number_coefficients=20,
):
"""
Compute the constant-Q harmonic coefficients (CQHCs).
Inputs:
audio_signal: audio signal (number_samples,)
sampling_frequency: sampling frequency in Hz
step_length: step length in samples
minimum_frequency: minimum frequency in Hz (default: 32.70 Hz = C1)
octave_resolution: number of frequency channels per octave (default: 12 frequency channels per octave)
number_coefficients: number of CQHCs (default: 20 coefficients)
Output:
audio_cqhc: CQHCs (number_coefficients, number_frames)
Example: Compute the CQHCs from an audio file.
# Import the modules
import numpy as np
import cqhc
import librosa
import librosa.display
import matplotlib.pyplot as plt
# Load the audio signal
file_path = r'bass_acoustic_000-036-075.wav'
audio_signal, sampling_frequency = librosa.load(file_path, sr=None, mono=True)
# Define the parameters and compute the CQHCs
step_length = int(pow(2, int(np.ceil(np.log2(0.04 * sampling_frequency)))) / 2)
minimum_frequency = 32.70
octave_resolution = 12
number_coefficients = 20
audio_cqhc = cqhc.cqhc(audio_signal, sampling_frequency, step_length, minimum_frequency, octave_resolution, \
number_coefficients)
# Display the CQHCs
plt.figure(figsize=(14, 4))
librosa.display.specshow(librosa.power_to_db(audio_cqhc), x_axis='time', sr=sampling_frequency, hop_length=step_length, \
cmap='jet')
plt.title('CQHCs')
plt.ylabel('Coefficient')
plt.tight_layout()
plt.show()
"""
# Compute the power CQT spectrogram
cqt_spectrogram = np.power(
cqtspectrogram(
audio_signal,
sampling_frequency,
step_length,
minimum_frequency,
octave_resolution=12,
),
2,
)
# Derive the spectral component
number_frequencies = np.shape(cqt_spectrogram)[0]
spectral_component = np.real(
np.fft.ifft(
abs(np.fft.fft(cqt_spectrogram, 2 * number_frequencies - 1, axis=0)), axis=0
)[0:number_frequencies, :]
)
# Extract the CQHCs
coefficient_indices = np.round(
octave_resolution * np.log2(np.arange(1, number_coefficients + 1))
).astype(int)
audio_cqhc = spectral_component[coefficient_indices, :]
return audio_cqhc