본문 바로가기
Python

Visualize Speech Wave

by YJHTPII 2024. 11. 25.
반응형
# -*- coding: utf-8 -*-

import wave
import numpy as np
import matplotlib.pyplot as plt

if __name__ == "__main__":
    wav_file = '../../data/wav/xxx.wav'

    out_plot = '../../plot.png'

    with wave.open(wav_file) as wav:
        sampling_frequency = wav.getframerate()
        sample_size = wav.getsampwidth()
        num_channels = wav.getnchannels()
        num_samples = wav.getnframes()
        waveform = wav.readframes(num_samples)
        waveform = np.frombuffer(waveform, dtype=np.int16)

    print("Sample Frequency: %d [Hz]" % sampling_frequency)
    print("Sample Size: %d [Byte]" % sample_size)
    print("# of Channels: %d" % num_channels)
    print("# of Samples: %d" % num_samples)

    time_axis = np.arange(num_samples) / sampling_frequency
    plt.figure(figsize=(10,4))
    plt.plot(time_axis, waveform)
    plt.xlabel("Time [sec]")
    plt.ylabel("Value")
    plt.xlim([0, num_samples / sampling_frequency])
    plt.savefig(out_plot)

 

반응형

'Python' 카테고리의 다른 글

Visualize STFT  (0) 2024.11.27
Visualize FFT  (0) 2024.11.25
Python Task Scheduler  (0) 2024.11.20
[Python]파이썬 출력 화면 지우기  (0) 2024.05.10
[Python]PyQt Widgets  (0) 2024.05.07

댓글