スペクトログラム

f:id:seinzumtode:20210620024245p:plain

f:id:seinzumtode:20210620024252p:plain 

import os 
import numpy as np
from scipy import signal
from scipy.io import wavfile
import wave
import matplotlib.pyplot as plt

def WaveAnalyzer(filename):
    path = os.getcwd()
    fp = path+'/'+filename
    print(fp)
    wavefile = wave.open(fp,"rb")
    rate, voice = wavfile.read(fp)
    channels = wavefile.getnchannels()
    samplewidth=wavefile.getsampwidth()
    fs = wavefile.getframerate()
    fn=wavefile.getnframes()
    if channels == 2:
        voice = np.mean(voice,axis=1)
    N = voice.shape[0]
    length = N/rate
    print('channels',channels)
    print('sample width[byte]',samplewidth)
    print('sampling freq[Hz]',fs)
    print('number of frames',fn)
    print("time length of wav-file[sec] {:.2f}".format(length))
    f , ax = plt.subplots(figsize=(10,5))
    ax.plot(np.arange(N)/rate,voice)
    ax.set_xlabel('time[sec]',fontsize=12)
    ax.set_ylabel('amplitude',fontsize=12)

    winlength = 2**10
    freqs,times,Sx=signal.spectrogram(voice,
                                        fs=rate,
                                        window='hamming',
                                        nperseg=winlength,
                                        detrend=False,
                                        scaling='spectrum')
    Sx[Sx==0]=np.finfo(float).eps
    f , ax =plt.subplots(figsize=(12,6))
    ax.pcolormesh(times,freqs/1000,10*np.log10(Sx),cmap='viridis')
    ax.set_xlabel('time[sec]',fontsize=12)
    ax.set_ylabel('frequency[kHz]',fontsize=12) 
    plt.show()
    wavefile.close()

WaveAnalyzer('taunt.wav')
# WaveAnalyzer('StarWars3.wav')