山内セミナーⅠ(2021/07/07)

関連サイトと資料

サンプルプログラム(9)

list29.py
import wave
import numpy as np
import matplotlib.pyplot as plt
  
def createGraph(fname):
    in_wav = wave.open(fname, 'r')
    p = in_wav.getparams()
  
    data = in_wav.readframes(p.nframes)
    data = np.frombuffer(data, dtype='int16')
  
    plt.plot(data)
    plt.show()
  
if __name__ == '__main__':
    target = 'sample1.wav'
    createGraph(target)
    

list30.py
import wave
import numpy as np
import matplotlib.pyplot as plt
  
def createGraph2(listfn):
    for i, fname in enumerate(listfn):
        in_wav = wave.open(fname, 'r')
        p = in_wav.getparams()
  
        data = in_wav.readframes(p.nframes)
        data = np.frombuffer(data, dtype='int16')       
  
        if p.nchannels == 1:
            plt.figure(i+1)
            plt.plot(data)
            plt.title(fname)
        elif p.nchannels == 2:
            fig = plt.figure(i+1)
            ax1 = fig.add_subplot(2, 1, 1)
            ax2 = fig.add_subplot(2, 1, 2)
  
            dataL = data[::2].copy()
            dataR = data[1::2].copy()
  
            ax1.plot(dataL)
            ax1.set_title(fname + '(Left Channel)')
            ax2.plot(dataR)
            ax2.set_title(fname + '(Right Channel)')
    
    plt.show()
  
if __name__ == '__main__':
    targets = ['sample1.wav', 'loop1.wav']
    createGraph2(targets)
    

list31.py
import wave
import numpy as np
import matplotlib.pyplot as plt
  
def createGraph3(listfn):
    for i, fname in enumerate(listfn):
        in_wav = wave.open(fname, 'r')
        p = in_wav.getparams()
  
        data = in_wav.readframes(p.nframes)
        data = np.frombuffer(data, dtype='int16')
  
        if p.nchannels == 1:
            plt.figure(i+1)
            t = np.arange(0, data.size/p. framerate, 1/p. framerate)
            plt.plot(t, data)
            plt.title(fname)
            plt.xlabel('time [sec]')
            plt.ylabel('amplitude')
            plt.grid(True)
        elif p.nchannels == 2:
            fig = plt.figure(i+1)
            ax1 = fig.add_subplot(2, 1, 1)
            ax2 = fig.add_subplot(2, 1, 2)
  
            dataL = data[::2].copy()
            dataR = data[1::2].copy()
            tL = np.arange(0, dataL.size/p. framerate, 1/p. framerate)
            tR = np.arange(0, dataR.size/p. framerate, 1/p. framerate)
  
            ax1.plot(tL, dataL)
            ax1.set_title(fname + '(Left Channel)')
            ax1.set_xlabel('time [sec]')
            ax1.set_ylabel('amplitude')
            ax1.grid(True)
            ax2.plot(tR, dataR)
            ax2.set_title(fname + '(Right Channel)')
            ax2.set_xlabel('time [sec]')
            ax2.set_ylabel('amplitude')
            ax2.grid(True)
    
    plt.show()
  
if __name__ == '__main__':
    targets = ['sample1.wav', 'loop1.wav']
    createGraph3(targets)
    

サンプルプログラム(10)

list32.py
import numpy as np
import matplotlib.pyplot as plt
  
if __name__ == '__main__':
    signal = np.concatenate([np.ones(10), np.zeros(44), np.ones(10)])
    plt.figure(1)
    plt.plot(signal)
    plt.title('Original')
  
    f = np.fft.fft(signal)
    plt.figure(2)
    plt.plot(f.real, color='green', label='Real')
    plt.plot(f.imag, color='red', label='Imaginary')
    plt.title('Fourier transformed')
    plt.legend()
  
    f_ifft = np.fft.ifft(f)
    plt.figure(3)
    plt.plot(f_ifft)
    plt.title('Inverse Fourier transformed')
  
    plt.show()
    

listTS.py
import wave
import numpy as np
import matplotlib.pyplot as plt
  
def createGraph5(listData):
    for i, (fname, t1, title) in enumerate(listData, 1):
        plt.figure(i)
  
        in_wav = wave.open(fname, 'r')
        p = in_wav.getparams()
  
        data = in_wav.readframes(p.nframes)
        data = np.frombuffer(data, dtype='int16')
        n1 = int(t1 * p.framerate)
        n2 = n1+1024
        t = []
        for i in range(n1,n2):
            t.append(i/p.framerate)
  
        print('data-length={0}'.format(len(t)))
        plt.plot(t, data[n1:n2])
        plt.title(title)
        plt.xlabel('time [sec]')
        plt.ylabel('amplitude')
    
    plt.show()
    

list33.py
from listTS import *
  
if __name__ == '__main__':
    target = 'sample1.wav'
    list1 = []
    list1.append([target, 0.218, 'part1 of sample1.wav'])
    list1.append([target, 0.843, 'part2 of sample1.wav'])
    list1.append([target, 1.418, 'part3 of sample1.wav'])
    list1.append([target, 2.018, 'part4 of sample1.wav'])
    list1.append([target, 2.593, 'part5 of sample1.wav'])
  
    createGraph5(list1)
    

listFFT.py
import wave
import numpy as np
import matplotlib.pyplot as plt
  
def showSpectrum(listData):
    N = 1024
    for i, (fname, t1, title) in enumerate(listData, 1):
        plt.figure(i)

        in_wav = wave.open(fname, 'r')
        p = in_wav.getparams()
  
        data = in_wav.readframes(p.nframes)
        data = np.frombuffer(data, dtype='int16')
        n1 = int(t1 * p.framerate)
        n2 = n1+N

        data = data[n1:n2]
        spec = np.abs(np.fft.fft(data, N))[:N//2]
        f = np.fft.fftfreq(N, d = 1.0 / p.framerate)[:N//2]

        plt.plot(f, spec)
        plt.xlabel("frequency [Hz]")
        plt.ylabel("amplitude spectrum")
        plt.title(title)
    
    plt.show()
  
def showSpectrum2(listData):
    N = 1024
    hamming = np.hamming(N)
  
    for i, (fname, t1, title) in enumerate(listData, 1):
        plt.figure(i)
  
        in_wav = wave.open(fname, 'r')
        p = in_wav.getparams()
  
        data = in_wav.readframes(p.nframes)
        data = np.frombuffer(data, dtype='int16')
        n1 = int(t1 * p.framerate)
        n2 = n1+N
  
        data = data[n1:n2] * hamming
        spec = np.abs(np.fft.fft(data, N))[:N//2]
        f = np.fft.fftfreq(N, d = 1.0 / p.framerate)[:N//2]
  
        plt.plot(f, spec)
        plt.xlabel("frequency [Hz]")
        plt.ylabel("amplitude spectrum")
        plt.title(title)
    
    plt.show()
    

list34.py
from listFFT import *
  
if __name__ == '__main__':
    target = 'sample1.wav'
    list1 = []
    list1.append([target, 0.218, 'part1 of sample1.wav'])
    list1.append([target, 0.843, 'part2 of sample1.wav'])
    list1.append([target, 1.418, 'part3 of sample1.wav'])
    list1.append([target, 2.018, 'part4 of sample1.wav'])
    list1.append([target, 2.593, 'part5 of sample1.wav'])
  
    showSpectrum(list1)
    

list35.py
from listFFT import *
  
if __name__ == '__main__':
    target = 'sample1.wav'
    list1 = []
    list1.append([target, 0.218, 'part1 of sample1.wav(hamming)'])
    list1.append([target, 0.843, 'part2 of sample1.wav(hamming)'])
    list1.append([target, 1.418, 'part3 of sample1.wav(hamming)'])
    list1.append([target, 2.018, 'part4 of sample1.wav(hamming)'])
    list1.append([target, 2.593, 'part5 of sample1.wav(hamming)'])
  
    showSpectrum2(list1)