๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
FCC STFT MFCC ๋ฅผ ์ด์ฉํ ์์ฑ ์ ํธ ๋ถ์ ๋ณธ๋ฌธ
๐ฉ๐ป ์ธ๊ณต์ง๋ฅ (ML & DL)/AI
FCC STFT MFCC ๋ฅผ ์ด์ฉํ ์์ฑ ์ ํธ ๋ถ์
์ง์ง์ํ์นด 2022. 11. 3. 15:43728x90
๋ฐ์ํ
< ๋ณธ ๋ธ๋ก๊ทธ๋ hyunlee103๋์ ๋ธ๋ก๊ทธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-) >
https://hyunlee103.tistory.com/36
[Sound AI #11] ์ค๋์ค ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ (Python Coding)
Sound of AI ์ ํ๋ธ๋ฅผ ๋ฐํ์ผ๋ก ์์ฑ๋์์ต๋๋ค. ์ด์ ํฌ์คํ ์์ ๋ค๋ฃฌ ์ค๋์ค ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ๋ฅผ ํ์ด์ฌ์ผ๋ก ๊ตฌํํด๋ณด๋ ค ํ๋ค. ์ฐ์ ์ฐ๋ฆฌ๊ฐ ์ฌ์ฉํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ import ํ์ import numpy as np import lib
hyunlee103.tistory.com
Colab์ ์ฌ์ฉํ์ต๋๋ค
!pip install pydub
!apt install ffmpeg
๐ library & data load
from os import path
from pydub import AudioSegment
import numpy as np
import matplotlib.pyplot as plt
import librosa
import librosa.display
file = 'voice.m4a'
sound = AudioSegment.from_file(file)
print(type(sound))
sig, sr = librosa.load(file, sr=None)
- y : ์์์ ํํ ๋ฐ์ดํฐ
- sr : sampling rate ์ด๋น ์ํ ๊ฐ์ (์ฃผํ์ ๋ถ์ ๋ฐ ํํ์ ์๊ฐ ๊ฐ๊ฒฉ์ ๊ฒฐ์ )
y = np.array(sound.get_array_of_samples())
plt.plot(np.arange(0, len(sig)), sig)
โ ๋จ์ ํธ๋ฆฌ์ ๋ณํ → Spectrum
fft = np.fft.fft(sig)
# ๋ณต์๊ณต๊ฐ ๊ฐ ์ ๋๊ฐ ์ทจํด์, magnitude ๊ตฌํ๊ธฐ
magnitude = np.abs(fft)
# Frequency ๊ฐ ๋ง๋ค๊ธฐ
f = np.linspace(0,sr,len(magnitude))
# ํธ๋ฆฌ์ ๋ณํ์ ํต๊ณผํ specturm์ ๋์นญ๊ตฌ์กฐ๋ก ๋์์ high frequency ๋ถ๋ถ ์ ๋ฐ์ ๋ ๋ ค๊ณ ์์ชฝ ์ ๋ฐ๋ง ์ฌ์ฉํ๋ค.
left_spectrum = magnitude[:int(len(magnitude)/2)]
left_f = f[:int(len(magnitude)/2)]
plt.figure()
plt.plot(left_f, left_spectrum)
plt.xlabel("Frequency")
plt.ylabel("Magnitude")
plt.title("Power spectrum")
โ STFT (Short Time Fourier Transform) -> Spectrogram
- ๋จ์ ํธ๋ฆฌ์ ๋ณํ๊ณผ ๋ค๋ฅด๊ฒ ์๊ฐ ์ ๋ณด๋ฅผ ๋ณด์กดํ๊ธฐ ์ํด, frame ๋จ์๋ก FFT๋ฅผ ์ํํ๋ค. ๋ฐ๋ผ์ frame ์์ frame ๋น sample ์๋ฅผ ์ง์
- Time ๋๋ฉ์ธ์ ํํ์ Frequency ๋๋ฉ์ธ์ผ๋ก ๋ณํ์ํค๋ ํธ๋ฆฌ์ ๋ณํ
- ์ ์ฒด ํํ์ ๋์์ผ๋ก ํ๋ฉด ์ ๋๋ก ๋ ์ฃผํ์ ๋ถ์์ ํ ์ ์๊ธฐ ๋๋ฌธ์, ์งง์ ์๊ฐ ๋จ์๋ก ๋ถ๋ฆฌํด์ ๊ฐ๊ฐ์ ๊ตฌ๊ฐ์ ๋ํด ๋ณํ
- Spectrogram์์๋ dB ๊ฐ์ ์ฌ์ฉํ๋ฏ๋ก, ํธ๋ฆฌ์ ๋ณํ์ ๊ฒฐ๊ณผ์ธ magnitude์ ๋ก๊ทธ Scaling์ ํตํด dB๋ก ๋ณํ
- win_length : FFT(Fast Fourier Transform์ ์ฝ์, ๋น ๋ฅด๊ฒ ๋ณํํ๋ ๋ฐฉ์)๋ฅผ ํ ๋ ์ฐธ์กฐํ ๊ทธ๋ํ์ ๊ธธ์ด
-
hop_length : ์ผ๋ง๋งํผ ์๊ฐ ์ฃผ๊ธฐ๋ฅผ ์ด๋ํ๋ฉด์ ๋ถ์์ ํ ๊ฒ์ธ์ง (์นผ๋ผ๋งต์ ์๊ฐ ์ฃผ๊ธฐ)
-
n_fft : win_length๋ณด๋ค ๊ธธ ๊ฒฝ์ฐ ๋ชจ๋ zero paddingํด์ ์ฒ๋ฆฌํ๊ธฐ ์ํจ (default๋ win_length์ ๊ฐ์)
# STFT -> spectrogram
hop_length = 512 # ์ ์ฒด frame ์
n_fft = 2048 # frame ํ๋๋น sample ์
# calculate duration hop length and window in seconds
hop_length_duration = float(hop_length)/sr
n_fft_duration = float(n_fft)/sr
# STFT
stft = librosa.stft(sig, n_fft=n_fft, hop_length=hop_length)
# ๋ณต์๊ณต๊ฐ ๊ฐ ์ ๋๊ฐ ์ทจํ๊ธฐ
magnitude = np.abs(stft)
# magnitude > Decibels
log_spectrogram = librosa.amplitude_to_db(magnitude)
# display spectrogram
plt.figure()
librosa.display.specshow(log_spectrogram, sr=sr, hop_length=hop_length)
plt.xlabel("Time")
plt.ylabel("Frequency")
plt.colorbar(format="%+2.0f dB")
plt.title("Spectrogram (dB)")
โ MFCC
# MFCCs
# extract 13 MFCCs
MFCCs = librosa.feature.mfcc(sig, sr, n_fft=n_fft, hop_length=hop_length, n_mfcc=13)
# display MFCCs
plt.figure()
librosa.display.specshow(MFCCs, sr=sr, hop_length=hop_length)
plt.xlabel("Time")
plt.ylabel("MFCC coefficients")
plt.colorbar()
plt.title("MFCCs")
# show plots
plt.show()
728x90
๋ฐ์ํ
'๐ฉโ๐ป ์ธ๊ณต์ง๋ฅ (ML & DL) > AI' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
MFCC (Mel Frequency Cepstrum Coefficient) ์์ฑ ์ ํธ ๋ถ์ํ๊ธฐ (0) | 2022.11.03 |
---|---|
[AI]_24_Q ํ์ต (Q-Learning) (0) | 2022.05.19 |
[AI]_23_P-Value & Q-Value (0) | 2022.05.08 |
[AI]_22_๊ฐํํ์ต & ๋์ ์๊ณ ๋ฆฌ์ฆ (0) | 2022.05.07 |
[AI]_21_๊ณผํ์ต ๋ง๊ธฐ! (0) | 2022.03.27 |
Comments