3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonでオーディオの分析を行う

Posted at

ライブラリのインポート

import sys
import scipy.io.wavfile
import numpy as np
import matplotlib.pyplot as plt
import librosa
import IPython.display as ipd

オーディオを読み込んで再生ボタンを表示

'sample.wav'の部分で、使用したいオーディオファイルを指定してください。

audio_path = 'sample.wav'
librosa.load(audio_path, sr=44100)
ipd.Audio(audio_path)

xにオーディオのデータを代入

audio_path = 'sample.wav'
x , sr = librosa.load(audio_path)
print(type(x), type(sr))
print(x.shape, sr)

波形を表示

plt.figure(figsize=(14, 5))
librosa.display.waveplot(x, sr=sr)

スペクトル重心の定義式

def spectral_centroid(x, samplerate=44100):
    magnitudes=np.abs(np.fft.rfft(x)) # magnitudes of positive frequencies
    length=len(x)
    freqs=np.abs(np.fft.fftfreq(length, 1.0/samplerate)[:length//2+1]) # positive frequencies
    return np.sum(magnitudes*freqs)/np.sum(magnitudes) # return weighted mean

スペクトル重心を計算

audio_path = 'sample.wav'
x , sr = librosa.load(audio_path)
spectral_centroid(x, samplerate=44100)

スペクトル平坦度の定義式

def spectral_flatness(x): 
    X_f = fft(x) 
    N=len(X_f) 
    magnitude=abs(X_f[:N/2]) 
    sf=geom_mean(magnitude)/aritm_mean(magnitude) 
    return sf

スペクトル平坦度を計算

audio_path = 'sample.wav'
x , sr = librosa.load(audio_path)
spectral_flatness(x)

3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?