728x90
Librosa ?
Librosa is a python package for music and audio analysis. It provides the building blocks necessary to create music information retrieval systems.
Install
pip install librosa
Example Code
1) Import library
import librosa
import librosa.display
import numpy as np
import os
os.environ['LIBROSA_DATA_DIR'] = './sound_example/'
2) Load sound data
# Load 'libri1' example file.
## (example of library) Get file name
filename = librosa.example('libri1')
print(f"filename:{filename}") # ./sound_example/librosa/5703-47212-0000.ogg
## Load an audio file as a floating point time series.
y, sr = librosa.load(filename)
#### # y, sr = librosa.load(filename, sr=16000) # sr=none : native sampling rate
print(f" {y}, {sr}")
y : sound time serias. [np.ndarray]
sr : sampling rate. [number>0]
3) Extract feature : MFCC (Mel-frequency cepstral coefficients)
# Generate mfccs from a time series
librosa.feature.mfcc(y=y, sr=sr)
''' Output
array([[-565.919, -564.288, ..., -426.484, -434.668],
[ 10.305, 12.509, ..., 88.43 , 90.12 ],
...,
[ 2.807, 2.068, ..., -6.725, -5.159],
[ 2.822, 2.244, ..., -6.198, -6.177]], dtype=float32)
'''
# Use a pre-computed log-power Mel spectrogram
S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=8000)
librosa.feature.mfcc(S=librosa.power_to_db(S))
''' Output
array([[-559.974, -558.449, ..., -411.96 , -420.458],
[ 11.018, 13.046, ..., 76.972, 80.888],
...,
[ 2.713, 2.379, ..., 1.464, -2.835],
[ 2.712, 2.619, ..., 2.209, 0.648]], dtype=float32)
'''
# Get more components
mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40)
print( len(mfccs) ) # 40
4) Visualize the MFCC series
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=2, sharex=True)
S = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128, fmax=8000)
img = librosa.display.specshow(librosa.power_to_db(S, ref=np.max),
x_axis='time', y_axis='mel', fmax=8000,
ax=ax[0])
fig.colorbar(img, ax=[ax[0]])
ax[0].set(title='Mel spectrogram')
ax[0].label_outer()
img = librosa.display.specshow(mfccs, x_axis='time', ax=ax[1])
fig.colorbar(img, ax=[ax[1]])
ax[1].set(title='MFCC')
Output
728x90
'Data Science > Machine Learning' 카테고리의 다른 글
Gradio Example : Sentiment Analysis (0) | 2023.02.24 |
---|---|
[PyTorch] PyTorch Tutorial Projects (0) | 2023.02.07 |
[Data Labeling] Label-Studio Review (2) | 2022.09.08 |
Data Labeling Tools Each Type (0) | 2022.09.07 |
[알고리즘] 주성분 분석 (PCA : Principal Component Analysis) (0) | 2022.08.20 |
최근댓글