Skip to content

Instantly share code, notes, and snippets.

@tam17aki
Last active July 23, 2020 09:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tam17aki/41d1e0e130bff56920d5aeea68094a7a to your computer and use it in GitHub Desktop.
Save tam17aki/41d1e0e130bff56920d5aeea68094a7a to your computer and use it in GitHub Desktop.
YINによる基本周波数推定
#!/usr/bin/env python3
# MIT License
# Copyright (C) 2020 by Akira TAMAMORI
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Commentary:
# YIN による基本周波数推定
import matplotlib.pyplot as plt
import numpy as np
from scipy.io import wavfile
import librosa
IN_WAVE_FILE = "in.wav"
FRAME_LENGTH = 2048 # フレーム長 (FFTサイズ)
HOP_LENGTH = 80 # フレームのシフト長
MAX_Fo = 200 # 分析における基本周波数の最大値 (Hz)
MIN_Fo = 60 # 分析における基本周波数の最小値 (Hz)
# 音声のロード
fs, data = wavfile.read(IN_WAVE_FILE)
data = data.astype(np.float64)
# 基本周波数の推定
fo = librosa.yin(data, fmin=MIN_Fo, fmax=MAX_Fo, sr=fs,
frame_length=FRAME_LENGTH, hop_length=HOP_LENGTH,
trough_threshold=0.1)
# 波形表示
fig = plt.figure(figsize=(12, 6))
n_samples = len(data)
time = np.arange(n_samples) / fs
axes = fig.add_subplot(2, 1, 1)
axes.plot(time, data)
axes.set_xlabel("Time (sec)")
axes.set_ylabel("Amplitude")
axes.set_title("Waveform")
axes.set_xlim(0, np.max(time))
axes = fig.add_subplot(2, 1, 2)
axes.plot(fo)
axes.set_xlabel("Frame number")
axes.set_ylabel("Frequency (Hz)")
axes.set_title("Estimation of fundamental frequency via YIN method")
axes.set_xlim(0, len(fo) - 1)
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment