Skip to content

Commit de5a60a

Browse files
committed
2章を追加 q06,q10についてはうまくいっていないため修正予定
1 parent 5e52ea4 commit de5a60a

File tree

10 files changed

+234
-0
lines changed

10 files changed

+234
-0
lines changed

kkoiso/chapter02/q01.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
def DFT(x):
4+
N = len(x)
5+
X = [0] * N
6+
for k in range(N):
7+
s=0
8+
for n in range(N):
9+
a = ((-2j) * np.pi * k * n) / N
10+
s = s + x[n] * np.exp(a)
11+
12+
X[k] = s
13+
return X
14+
15+
def IDFT(X):
16+
N = len(X)
17+
x = [0] * N
18+
for n in range(N):
19+
s=0
20+
for k in range(N):
21+
angle = (2j * np.pi * k * n) / N
22+
s = s + X[k] * np.exp(angle)
23+
x[n] =s/N
24+
return x

kkoiso/chapter02/q02.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q01 import DFT,IDFT
4+
5+
x = np.array([1, 0, 0, 0, 0, 0, 0, 0])
6+
X = DFT(x)
7+
8+
9+
print(X)
10+

kkoiso/chapter02/q03.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q01 import DFT,IDFT
4+
5+
x = np.array([1, 0, 0, 0, 0, 0, 0, 0])
6+
X = DFT(x)
7+
x_idft = IDFT(X)
8+
print(x_idft)
9+
idft_real = [val.real for val in x_idft]#実部を取りだす
10+
idft_imag = [val.imag for val in x_idft]#虚部を取り出す
11+
12+
#実部をプロット
13+
plt.figure(figsize=(12, 6))
14+
plt.subplot(2, 1, 1)
15+
plt.stem(idft_real)
16+
plt.title('IDFT(Real Part)')
17+
plt.xlabel('Sample')
18+
plt.ylabel('Amplitude')
19+
20+
21+
plt.subplot(2, 1, 2)
22+
plt.stem(idft_imag)
23+
plt.title('IDFT Result (Imaginary Part)')
24+
plt.xlabel('Sample')
25+
plt.ylabel('Amplitude')
26+
27+
plt.tight_layout()
28+
plt.show()

kkoiso/chapter02/q04.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q01 import DFT,IDFT
4+
5+
x = np.array([1, 0, 0, 0, 0, 0, 0, 0])
6+
X = DFT(x)
7+
title = "DFT impulse"
8+
9+
plt.figure(figsize=(10, 4))
10+
plt.subplot(2, 1, 1)
11+
plt.plot(np.abs(X))
12+
plt.title(title + ' Amplitude Spectrum')
13+
plt.xlabel('Frequency')
14+
plt.ylabel('Amplitude')
15+
plt.grid(True)
16+
17+
plt.subplot(2, 1, 2)
18+
plt.plot(np.angle(X))
19+
plt.title(title + ' Phase Spectrum')
20+
plt.xlabel('Frequency')
21+
plt.ylabel('Phase')
22+
plt.grid(True)
23+
24+
plt.tight_layout()
25+
plt.show()

kkoiso/chapter02/q05.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q01 import DFT,IDFT
4+
5+
x = np.array([1, 0, 0, 0, 0, 0, 0, 0])
6+
X = DFT(x)
7+
8+
9+
np_X = np.fft.fft(x)
10+
print("Custom DFT:", X)
11+
print("Np FFT:", np_X)

kkoiso/chapter02/q06.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
5+
a = 1.0
6+
freq= 440
7+
samp_rate = 16000
8+
d = 3
9+
10+
t = np.linspace(0, d, int(samp_rate * d), endpoint=False)
11+
12+
13+
sin_wave = a * np.sin(2 * np.pi * freq * t)
14+
15+
X = np.fft.fft(sin_wave)
16+
17+
#振幅をデシベル表記
18+
amplitude_spectrum = 20 * np.log10(np.abs(X))
19+
20+
# 位相スペクトルの計算
21+
phase_spectrum = np.angle(X)
22+
23+
# 対応する周波数を計算
24+
frequencies = np.fft.fftfreq(len(X), 1/samp_rate)
25+
26+
27+
plt.figure(figsize=(12, 6))
28+
# 振幅スペクトルのプロット
29+
plt.subplot(2, 1, 1)
30+
plt.plot(frequencies,amplitude_spectrum)
31+
plt.title('Amplitude Spectrum')
32+
plt.xlabel('Frequency (Hz)')
33+
plt.ylabel('Amplitude (dB)')
34+
plt.grid(True)
35+
36+
# 位相スペクトルのプロット
37+
plt.subplot(2, 1, 2)
38+
plt.plot(frequencies, phase_spectrum)
39+
plt.title('Phase Spectrum')
40+
plt.xlabel('Frequency (Hz)')
41+
plt.ylabel('Phase (radians)')
42+
plt.grid(True)
43+
44+
plt.tight_layout()
45+
plt.show()

kkoiso/chapter02/q07.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
def Hamming_Window(N):
5+
n = np.arange(N)
6+
return 0.54 - 0.46 * np.cos((2 * np.pi * n )/ (N - 1))

kkoiso/chapter02/q08.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q07 import Hamming_Window
4+
#正弦波を生成
5+
a = 1.0
6+
freq= 440
7+
samp_rate = 16000
8+
d = 3
9+
10+
t = np.linspace(0, d, int(samp_rate * d), endpoint=False)
11+
12+
13+
sin_wave = a * np.sin(2 * np.pi * freq * t)
14+
#ハミング窓を生成し、正弦波にかける
15+
signal_length = len(sin_wave)
16+
hamming_window_signal = sin_wave * Hamming_Window(signal_length)
17+
18+
#プロット
19+
plt.figure(figsize=(10, 4))
20+
plt.plot(hamming_window_signal)
21+
plt.title('Signal with Hamming Window')
22+
plt.xlabel('Sample')
23+
plt.ylabel('Amplitude')
24+
plt.grid(True)
25+
plt.show()

kkoiso/chapter02/q09.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q07 import Hamming_Window
4+
#正弦波を生成
5+
a = 1.0
6+
freq= 440
7+
samp_rate = 16000
8+
d = 3
9+
10+
t = np.linspace(0, d, int(samp_rate * d), endpoint=False)
11+
12+
13+
sin_wave = a * np.sin(2 * np.pi * freq * t)
14+
15+
signal_length = len(sin_wave)
16+
#正弦波と同じサイズのハミング窓を生成し、DFT
17+
hamming_window_spectrum = np.fft.fft(Hamming_Window(signal_length))
18+
19+
print(hamming_window_spectrum)

kkoiso/chapter02/q10.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q07 import Hamming_Window
4+
from q01 import DFT,IDFT
5+
#正弦波を生成
6+
a = 1.0
7+
freq= 440
8+
samp_rate = 16000
9+
d = 3
10+
11+
t = np.linspace(0, d, int(samp_rate * d), endpoint=False)
12+
13+
14+
sin_wave = a * np.sin(2 * np.pi * freq * t)
15+
#正弦波をDFT
16+
sin_wave_DFT = np.fft.fft(sin_wave)
17+
18+
19+
#正弦波と同じサイズのハミング窓を生成し、DFT
20+
signal_length = len(sin_wave)
21+
hamming_window_spectrum = np.fft.fft(Hamming_Window(signal_length))
22+
23+
#巡回畳み込み
24+
Z=[0] * signal_length
25+
for k in range(signal_length):
26+
s = 0
27+
for n in range(signal_length):
28+
s =s +sin_wave_DFT[n]*hamming_window_spectrum[k-n]
29+
Z[k] = s
30+
z_idft = np.fft.ifft(Z)
31+
print(z_idft)
32+
33+
34+
#プロット
35+
plt.figure(figsize=(10, 4))
36+
plt.plot(z_idft)
37+
plt.title('Signal with Hamming Window')
38+
plt.xlabel('Sample')
39+
plt.ylabel('Amplitude')
40+
plt.grid(True)
41+
plt.show()

0 commit comments

Comments
 (0)