Skip to content

Commit ddd5b49

Browse files
authored
Merge pull request #11 from woodnx/skotsugi/chapter02
Add chapter02
2 parents c734c5f + 6aca3ca commit ddd5b49

File tree

17 files changed

+216
-0
lines changed

17 files changed

+216
-0
lines changed

skotsugi/chapter02/q01.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import numpy as np
2+
3+
def dft(x):
4+
N = len(x)
5+
X = np.zeros(N, dtype = 'complex_')
6+
7+
for k in range(N):
8+
for n in range(N):
9+
X[k] += x[n] * np.exp(-1j * 2 * np.pi * k * n / N)
10+
11+
return X
12+
13+
def idft(X):
14+
N = len(X)
15+
x = np.zeros(N, dtype = 'complex_')
16+
17+
for n in range(N):
18+
for k in range(N):
19+
x[k] += X[k] * np.exp(1j * 2 * np.pi * k * n / N) / N
20+
21+
return x
22+
23+
if __name__ == "__main__":
24+
d = dft([1, 0, 0, 0])
25+
D = idft(d)
26+
print(d, D)

skotsugi/chapter02/q02.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from q01 import dft
2+
3+
delta = [1, 0, 0, 0, 0, 0, 0, 0]
4+
5+
print(dft(delta))

skotsugi/chapter02/q03.png

17.5 KB
Loading

skotsugi/chapter02/q03.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q01 import dft, idft
4+
5+
delta = [1, 0, 0, 0, 0, 0, 0, 0]
6+
d = dft(delta)
7+
D = idft(d)
8+
9+
print(D)
10+
11+
fig, axs = plt.subplots(2)
12+
13+
plt.ylim(0, 1)
14+
15+
axs[0].stem(np.real(D))
16+
axs[0].set_title('Real Part')
17+
axs[1].stem(np.imag(D))
18+
axs[1].set_title('Imaginary Part')
19+
20+
fig.tight_layout()
21+
22+
plt.savefig('./skotsugi/chapter02/q03.png')

skotsugi/chapter02/q04.png

14.6 KB
Loading

skotsugi/chapter02/q04.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import matplotlib.pyplot as plt
2+
3+
def show_spectrum(filename, amp, phs):
4+
fig, axs = plt.subplots(1, 2)
5+
6+
axs[0].stem(amp)
7+
axs[0].set_title('Amplitude')
8+
axs[1].stem(phs)
9+
axs[1].set_title('Phase')
10+
11+
fig.tight_layout()
12+
13+
plt.savefig(filename)
14+
15+
if __name__ == "__main__":
16+
import numpy as np
17+
from q01 import dft
18+
19+
delta = [1, 0, 0, 0, 0, 0, 0, 0]
20+
d = dft(delta)
21+
22+
amp = np.abs(d)
23+
phs = np.angle(d)
24+
25+
show_spectrum('./skotsugi/chapter02/q04.png', amp, phs)
26+

skotsugi/chapter02/q05.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+
from q01 import dft
3+
4+
delta = [1, 0, 0, 0, 0, 0, 0, 0]
5+
6+
d = dft(delta)
7+
f = np.fft.fft(delta)
8+
9+
diff = d - f
10+
print(diff)

skotsugi/chapter02/q06.png

17.1 KB
Loading

skotsugi/chapter02/q06.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import numpy as np
2+
3+
def sin(A: float, f: float, fs: float, sec: float):
4+
n = np.arange(0, fs*sec) / fs
5+
return A * np.sin(2 * np.pi * f * n)
6+
7+
if __name__ == "__main__":
8+
import matplotlib.pyplot as plt
9+
10+
fs = 16000
11+
sec = 3.0
12+
N = int(fs*sec)
13+
14+
y = sin(1.0, 440, fs, sec)
15+
16+
Y = np.fft.fft(y)
17+
freq = np.fft.fftfreq(N, d=1/fs)
18+
19+
amp = np.abs(Y)
20+
phs = np.angle(Y)
21+
phs[amp < 1e-5] = 0 # これをしないと,絶対値がすごく小さな値の偏角も計算されてしまい,でたらめな位相スペクトルが出てしまう
22+
23+
fig, axs = plt.subplots(1, 2)
24+
25+
axs[0].stem(freq, amp)
26+
axs[0].set_xlim([0, max(freq)])
27+
axs[0].set_title('Amplitude')
28+
axs[1].stem(freq, phs)
29+
axs[1].set_xlim([0, max(freq)])
30+
axs[1].set_title('Phase')
31+
32+
fig.tight_layout()
33+
plt.savefig('./skotsugi/chapter02/q06.png')

skotsugi/chapter02/q07.png

20.5 KB
Loading

skotsugi/chapter02/q07.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import numpy as np
2+
3+
def hamming(N: int):
4+
return [ 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) for n in range(int(N)) ]
5+
6+
if __name__ == "__main__":
7+
import matplotlib.pyplot as plt
8+
9+
w = hamming(16000 * 3.0)
10+
11+
plt.plot(w)
12+
plt.savefig('./skotsugi/chapter02/q07.png')

skotsugi/chapter02/q08.png

18.2 KB
Loading

skotsugi/chapter02/q08.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
from q06 import sin
4+
from q07 import hamming
5+
6+
# paramaters
7+
fs = 16000
8+
sec = 3.0
9+
f = 440
10+
N = fs * sec
11+
12+
n = np.arange(0, N) / fs
13+
y = sin(1.0, f, fs, sec) * hamming(N)
14+
15+
# plt.xlim(0, 0.05)
16+
plt.plot(n, y)
17+
plt.savefig('./skotsugi/chapter02/q08.png')

skotsugi/chapter02/q09.png

16.7 KB
Loading

skotsugi/chapter02/q09.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 q07 import hamming
4+
5+
fs = 16000
6+
sec = 3
7+
N = fs * sec
8+
wN = 2 ** 8
9+
10+
w = hamming(N)
11+
W = np.fft.fft(w)
12+
13+
amp = 20 * np.log10(np.abs(W))
14+
#amp = np.abs(W)
15+
phs = np.angle(W)
16+
phs[amp < 1e-5] = 0
17+
18+
freq = np.fft.fftfreq(int(N), d=1/fs)
19+
20+
fig, axs = plt.subplots(1, 2)
21+
axs[0].stem(freq, amp)
22+
axs[0].set_title('Amplitude')
23+
axs[1].stem(freq, phs)
24+
axs[1].set_title('Phase')
25+
26+
fig.tight_layout()
27+
28+
plt.savefig('./skotsugi/chapter02/q09.png')

skotsugi/chapter02/q10.png

18.2 KB
Loading

skotsugi/chapter02/q10.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import numpy as np
2+
3+
def conv(X, Y):
4+
N = len(X)
5+
Z = np.zeros(N, dtype = 'complex_')
6+
k = np.arange(N)
7+
8+
for i in range(N):
9+
Z[k] += X[i] * Y[k - i]
10+
11+
return Z
12+
13+
14+
if __name__ == "__main__":
15+
import matplotlib.pyplot as plt
16+
from q06 import sin
17+
from q07 import hamming
18+
19+
fs = 16000
20+
sec = 3
21+
N = int(fs * sec)
22+
23+
x = sin(1, 440, fs, sec)
24+
X = np.fft.fft(x)
25+
26+
y = hamming(N)
27+
Y = np.fft.fft(y)
28+
29+
Z = conv(X, Y)
30+
z = np.fft.ifft(Z) / N
31+
32+
print(z)
33+
34+
n = np.arange(0, N) / fs
35+
#plt.xlim(0, 0.05)
36+
plt.plot(n, z.real)
37+
plt.savefig('./skotsugi/chapter02/q10.png')

0 commit comments

Comments
 (0)