●ハン窓
w(x) = 0.5 - 0.5cos(2πx) 但し 0 <= x <= 1
●ハミング窓
w(x) = 0.54 - 0.46cos(2πx) 但し 0 <= x <= 1
#coding UTF-8
from scipy import arange, hamming, sin, pi
from scipy.fftpack import fft, ifft
from matplotlib import pyplot as plt
from scipy import arange, hamming, sin, pi
from scipy.fftpack import fft, ifft
from matplotlib import pyplot as plt
fs = 8000 # Sampling rate
L = 1024 # Sampling length
L = 1024 # Sampling length
# 440Hzのsin波を作る
sin440 = sin(2. * pi * arange(L) * 440. / fs)
# 600Hzのsin波を作る
sin600= 2 * sin(2. * pi * arange(L) * 600. / fs)
# 800Hzのsin波を作る
sin800 = 3 * sin(2. * pi * arange(L) * 800. / fs)
sin440 = sin(2. * pi * arange(L) * 440. / fs)
# 600Hzのsin波を作る
sin600= 2 * sin(2. * pi * arange(L) * 600. / fs)
# 800Hzのsin波を作る
sin800 = 3 * sin(2. * pi * arange(L) * 800. / fs)
# 3つを足し合わせる
sinall = sin440 + sin600 + sin800
sinall = sin440 + sin600 + sin800
# 窓関数:ハミング窓(図の上から2番目)
window = hamming(L)
window = hamming(L)
# フーリエ変換
spect_nw = fft(sinall) # 窓関数なし
spect = fft(sinall * window) # 窓関数あり(掛け合わせる)
half_spect_nw = abs(spect_nw[: int(L / 2 )+ 1])
half_spect = abs(spect[: int(L / 2) + 1])
spect_nw = fft(sinall) # 窓関数なし
spect = fft(sinall * window) # 窓関数あり(掛け合わせる)
half_spect_nw = abs(spect_nw[: int(L / 2 )+ 1])
half_spect = abs(spect[: int(L / 2) + 1])
# フーリエ逆変換
inv_spect = ifft(spect)
inv_spect /= window
inv_spect = ifft(spect)
inv_spect /= window
# 図を表示
fig = plt.figure(figsize=(20, 70))
plt.rcParams["font.size"] = 30
fig.add_subplot(611)
plt.plot(sinall)
plt.xlim([0, L])
plt.grid(True)
plt.title("1. 入力信号")
fig = plt.figure(figsize=(20, 70))
plt.rcParams["font.size"] = 30
fig.add_subplot(611)
plt.plot(sinall)
plt.xlim([0, L])
plt.grid(True)
plt.title("1. 入力信号")
fig.add_subplot(612)
plt.plot(window)
plt.xlim([0, L])
plt.grid(True)
plt.title("2. ハミング窓")
plt.plot(window)
plt.xlim([0, L])
plt.grid(True)
plt.title("2. ハミング窓")
fig.add_subplot(613)
plt.plot(sinall * window)
plt.xlim([0, L])
plt.grid(True)
plt.title("3. 入力信号x窓関数")
plt.plot(sinall * window)
plt.xlim([0, L])
plt.grid(True)
plt.title("3. 入力信号x窓関数")
fig.add_subplot(614)
plt.plot(half_spect_nw)
plt.xlim([0, len(half_spect_nw)])
plt.grid(True)
plt.title("4. スペクトルグラム (窓関数無し)")
plt.plot(half_spect_nw)
plt.xlim([0, len(half_spect_nw)])
plt.grid(True)
plt.title("4. スペクトルグラム (窓関数無し)")
fig.add_subplot(615)
plt.plot(half_spect)
plt.xlim([0, len(half_spect)])
plt.grid(True)
plt.title("5. スペクトルグラム (窓関数あり)")
plt.plot(half_spect)
plt.xlim([0, len(half_spect)])
plt.grid(True)
plt.title("5. スペクトルグラム (窓関数あり)")
fig.add_subplot(616)
plt.plot(inv_spect)
plt.xlim([0, L])
plt.title("6. 逆変換後の信号")
fig.subplots_adjust(hspace=0.5) # グラフ間の隙間調整
plt.grid(True)
plt.show()
plt.plot(inv_spect)
plt.xlim([0, L])
plt.title("6. 逆変換後の信号")
fig.subplots_adjust(hspace=0.5) # グラフ間の隙間調整
plt.grid(True)
plt.show()
