赞
踩
主要内容:
chroma特征 与 CQT (Constant-Q)特征
简单示例:
load:
- `y,sr = librosa.load(wav_file,sr=22050);`
mfcc:
- `librosa.feature.mfcc(y=y,n_mfcc=64,sr=sr,n_mels=64)`
mel:
- `librosa.feature.melspectrogram(y=y,sr=sr,n_mels=64)`
x , sr = librosa.load("./乌梅子酱-李荣浩.wav", sr=22050)
print(x.shape, sr)
(5672672,) 22050
d = librosa.get_duration(y=x, sr=22050, S=None, n_fft=2048, hop_length=512, center=True, filename=None)
sr = librosa.get_samplerate("./乌梅子酱-李荣浩.wav")
sr,d
(44100, 257.26403628117913)
audio_file, _ = librosa.effects.trim(x)
print('Audio File:', audio_file, '\n')
print('Audio File shape:', np.shape(audio_file))
Audio File: [-3.2841001e-04 -3.0707751e-04 -2.6176337e-04 ... 2.7146576e-05
2.7009228e-04 2.0527366e-05]
Audio File shape: (5627904,)
# import IPython
# IPython.display.Audio("./乌梅子酱-李荣浩.wav")
plt.figure(figsize=(20, 5))
librosa.display.waveplot(x, sr=sr)
plt.show()
X = librosa.stft(x)
Xdb = librosa.amplitude_to_db(abs(X))
plt.figure(figsize=(20, 5))
librosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='hz')
plt.colorbar()
plt.show()
mfccs = librosa.feature.mfcc(y=x, sr=sr)
mfccs.shape
plt.figure(figsize=(20, 5))
librosa.display.specshow(mfccs, sr=sr, x_axis='time')
plt.colorbar()
plt.show()
n0 = 7000
n1 = 7025
plt.figure(figsize=(14, 5))
plt.plot(x[n0:n1])
plt.show()
zero_crossings = librosa.zero_crossings(x[n0:n1], pad=False)
zero_crossings.shape, zero_crossings.sum()
((25,), 1)
# 可以使用整个音频来遍历这个并推断出整个数据的过零
zcrs = librosa.feature.zero_crossing_rate(x)
print(zcrs.shape)
plt.figure(figsize=(14, 5))
plt.plot(zcrs[0])
spectral_centroids = librosa.feature.spectral_centroid(x, sr=sr)[0]
spectral_centroids.shape
frames = range(len(spectral_centroids))
t = librosa.frames_to_time(frames)
import sklearn
def normalize(x, axis=0):
return sklearn.preprocessing.minmax_scale(x, axis=axis)
plt.figure(figsize=(20, 5))
librosa.display.waveplot(x, sr=sr, alpha=0.4)
plt.plot(t, normalize(spectral_centroids), color='r')
spectral_bandwidth_2 = librosa.feature.spectral_bandwidth(x+0.01, sr=sr)[0]
spectral_bandwidth_3 = librosa.feature.spectral_bandwidth(x+0.01, sr=sr, p=3)[0]
spectral_bandwidth_4 = librosa.feature.spectral_bandwidth(x+0.01, sr=sr, p=4)[0]
plt.figure(figsize=(20, 5))
librosa.display.waveplot(x, sr=sr, alpha=0.4)
plt.plot(t, normalize(spectral_bandwidth_2), color='r')
plt.plot(t, normalize(spectral_bandwidth_3), color='g')
plt.plot(t, normalize(spectral_bandwidth_4), color='y')
plt.legend(('p = 2', 'p = 3', 'p = 4'))
spectral_rolloff = librosa.feature.spectral_rolloff(x+0.01, sr=sr)[0]
plt.figure(figsize=(20, 5))
librosa.display.waveplot(x, sr=sr, alpha=0.4)
plt.plot(t, normalize(spectral_rolloff), color='r')
chromagram = librosa.feature.chroma_stft(x, sr=sr, hop_length=512)
plt.figure(figsize=(20, 5))
librosa.display.specshow(chromagram, x_axis='time', y_axis='chroma', hop_length=512, cmap='coolwarm')
pitches, magnitudes = librosa.piptrack(y=x, sr=sr)
print(pitches)
[[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
...
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]
[0. 0. 0. ... 0. 0. 0.]]
chroma特征:
CQT (Constant-Q)特征:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。