aidiary
3/4/2015 - 12:36 PM

指定フレームでのスペクトルとメルケプストラムを描画

指定フレームでのスペクトルとメルケプストラムを描画

import subprocess

def extract_binary_mcep(wav_file, mcep_file):
    cmd = "bcut +s -s 22 %s | x2x +sf | frame -p 80 | window | mcep -m 25 -a 0.42 > %s" % (wav_file, mcep_file)
    subprocess.call(cmd, shell=True)

def draw_mcep_at_frame(wav_file, mcep_file, frame):
    # frame位置でのスペクトルを求める
    cmd = "bcut +s -s 22 %s | x2x +sf | frame -p 80 | bcut +f -l 256 -s %d -e %d | window | spec | dmp +f > spec.txt" % (wav_file, frame, frame)
    subprocess.call(cmd, shell=True)
    
    # frame位置でのメルケプストラムをスペクトルに変換
    cmd = "bcut +f -n 25 -s %d -e %d < %s | mgc2sp -m 25 -a 0.42 | dmp +f > mcep.txt" % (frame, frame, mcep_file)
    subprocess.call(cmd, shell=True)

    # スペクトルとメルケプストラムを描画
    spec = np.loadtxt("spec.txt")
    plt.plot(spec[:, 1])

    mcep = np.loadtxt("mcep.txt")
    plt.plot(mcep[:, 1], "r-", lw=2)
    
    plt.xlabel("frequency bin")
    plt.ylabel("log magnitude")
    plt.show()

frame = 50

wav_file = "wav/clb/arctic_a0005.wav"
mcep_file = "clb_a0005.mcep"
extract_binary_mcep(wav_file, mcep_file)
draw_mcep_at_frame(wav_file, mcep_file, frame)

wav_file = "wav/slt/arctic_a0005.wav"
mcep_file = "slt_a0005.mcep"
extract_binary_mcep(wav_file, mcep_file)
draw_mcep_at_frame(wav_file, mcep_file, frame)