Skip to content

Commit 917a455

Browse files
authored
Merge pull request #3 from Yuto-Yamamoto01/main
add yyamamoto
2 parents ab01e61 + 0c22ed9 commit 917a455

File tree

15 files changed

+203
-0
lines changed

15 files changed

+203
-0
lines changed

yyamamoto/chapter00/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
Yuto Yamamoto
4+
5+
changed sentences

yyamamoto/chapter01/q01.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
A = 1 # 振幅
5+
f = 440 # 周波数 Hz
6+
sec = 3 # 信号長 s
7+
fs = 16000 # サンプリング周波数 Hz
8+
9+
t = np.arange(0, sec, 1/fs) # サンプリング点の配列
10+
11+
y = A * np.sin(2*np.pi*f*t) # 正弦波の値
12+
13+
# グラフの描画
14+
fig = plt.figure()
15+
plt.plot(t,y)
16+
fig.savefig('q01_graph')

yyamamoto/chapter01/q01_graph.png

12.7 KB
Loading

yyamamoto/chapter01/q02.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+
4+
A = 1 # 振幅
5+
f = 440 # 周波数 Hz
6+
sec = 3 # 信号長 s
7+
fs = 16000 # サンプリング周波数 Hz
8+
9+
t = np.arange(0, sec, 1/fs) # サンプリング点の配列
10+
11+
y = A * np.sin(2*np.pi*f*t) # 正弦波の値
12+
13+
14+
# ここからq2の内容
15+
16+
import soundfile as sf
17+
sf.write("q02_audio.wav", y, fs, subtype="PCM_16")

yyamamoto/chapter01/q03.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import soundfile as sf
4+
5+
A = 1 # 振幅
6+
f = 440 # 周波数 Hz
7+
sec = 3 # 信号長 s
8+
fs = 16000 # サンプリング周波数 Hz
9+
10+
t = np.arange(0, sec, 1/fs) # サンプリング点の配列
11+
12+
y1 = A * np.sin(2*np.pi*f*t) # 正弦波1
13+
14+
f = 660
15+
y2 = A * np.sin(2*np.pi*f*t) # 正弦波2
16+
17+
# 2つの正弦波を2chでもつ行列 (行ベクトルをつなげているため転置)
18+
y = np.stack([y1, y2]).T
19+
20+
sf.write("q03_audio.wav", y, fs, subtype="PCM_16")

yyamamoto/chapter01/q04.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
fs = 16000 # サンプリング周波数 Hz
5+
sec = 3 # 信号長 s
6+
7+
t = np.arange(0, sec, 1/fs) # サンプリング点の配列
8+
y = 2 * (np.random.rand(fs*sec)) - 1
9+
10+
# グラフの描画
11+
fig = plt.figure()
12+
plt.plot(t,y)
13+
fig.savefig('q04_graph')

yyamamoto/chapter01/q04_graph.png

17.9 KB
Loading

yyamamoto/chapter01/q05.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
A = 1 # 振幅
5+
f = 440 # 周波数 Hz
6+
sec = 3 # 信号長 s
7+
fs = 16000 # サンプリング周波数 Hz
8+
9+
t = np.arange(0, sec, 1/fs) # サンプリング点の配列
10+
11+
y1 = A * np.sin(2*np.pi*f*t) # 正弦波
12+
y2 = 2 * (np.random.rand(fs*sec)) - 1 # ホワイトノイズ
13+
14+
# グラフの描画
15+
fig = plt.figure()
16+
plt.plot(t, y1, label='sin')
17+
plt.plot(t, y2, label='white noise')
18+
plt.legend()
19+
fig.savefig('q05_graph')
20+

yyamamoto/chapter01/q05_graph.png

21.2 KB
Loading

yyamamoto/chapter01/q06.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import math
2+
3+
def SN_rate(s, x):
4+
N = len(s)
5+
sum_s = 0
6+
sum_x = 0
7+
for n in range(N):
8+
sum_s += s[n]*s[n]
9+
sum_x += x[n]*x[n]
10+
ans = 10 * (math.log(sum_s) - math.log(sum_x))
11+
return ans

yyamamoto/chapter01/q07.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import numpy as np
2+
import math
3+
4+
def SN_rate(s, x):
5+
N = len(s)
6+
sum_s = 0
7+
sum_x = 0
8+
for n in range(N):
9+
sum_s += s[n]*s[n]
10+
sum_x += x[n]*x[n]
11+
ans = 10 * (math.log(sum_s) - math.log(sum_x))
12+
return ans
13+
14+
15+
16+
def add_white_noise(s, sn):
17+
N = len(s)
18+
x = 2 * (np.random.rand(N)) - 1 # ホワイトノイズの作成
19+
20+
sum_s = 0
21+
sum_x = 0
22+
for n in range(N):
23+
sum_s += s[n] * s[n]
24+
sum_x += x[n] * x[n]
25+
26+
mul = pow(math.exp(-sn/10) * sum_s / sum_x, 0.5)
27+
x = x * mul
28+
29+
# return x
30+
ans = s + x
31+
return ans
32+
33+
34+
# あっているか確かめる (上の関数をホワイトノイズを出力するものに変更して確認)
35+
A = 53 # 振幅
36+
f = 423 # 周波数 Hz
37+
sec = 3 # 信号長 s
38+
fs = 16000 # サンプリング周波数 Hz
39+
40+
t = np.arange(0, sec, 1/fs) # サンプリング点の配列
41+
y = A * np.sin(2*np.pi*f*t) # 正弦波の値
42+
43+
white_noise = add_white_noise(y, 234.23)
44+
print(SN_rate(y, white_noise))

yyamamoto/chapter01/q08.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+
import math
3+
import soundfile as sf
4+
5+
def add_white_noise(s, sn):
6+
N = len(s)
7+
x = 2 * (np.random.rand(N)) - 1 # ホワイトノイズの作成
8+
9+
sum_s = 0
10+
sum_x = 0
11+
for n in range(N):
12+
sum_s += s[n] * s[n]
13+
sum_x += x[n] * x[n]
14+
15+
mul = pow(math.exp(-sn/10) * sum_s / sum_x, 0.5)
16+
x = x * mul
17+
18+
# return x
19+
ans = s + x
20+
return ans
21+
22+
23+
# 正弦波の作成
24+
A = 1 # 振幅
25+
f = 440 # 周波数 Hz
26+
sec = 3 # 信号長 s
27+
fs = 16000 # サンプリング周波数 Hz
28+
t = np.arange(0, sec, 1/fs) # サンプリング点の配列
29+
y = A * np.sin(2*np.pi*f*t) # 正弦波の値
30+
31+
y = add_white_noise(y, 6)
32+
33+
sf.write("q08_audio.wav", y, fs, subtype="PCM_16")

yyamamoto/chapter01/q09.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import soundfile as sf
2+
3+
data, samplerate = sf.read('q08_audio.wav')
4+
sf.write("q09_audio.wav", data, 8000, subtype="PCM_16")

yyamamoto/chapter01/q10.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import soundfile as sf
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
data, samplerate = sf.read('q09_audio.wav')
6+
7+
filtered = data
8+
for i in range(len(filtered) - 4):
9+
filtered[i] = (data[i]+data[i+1]+data[i+2]+data[i+3]+data[i+4])
10+
11+
t = np.arange(0, 6, 1/samplerate) # サンプリング点の配列
12+
13+
fig = plt.figure()
14+
A1 = fig.add_subplot(1,2,1)
15+
plt.plot(t, data, label='original')
16+
plt.legend()
17+
A2 = fig.add_subplot(1,2,2)
18+
plt.plot(t, filtered, label='filtered')
19+
plt.legend()
20+
fig.savefig('q10_graph')

yyamamoto/chapter01/q10_graph.png

13.9 KB
Loading

0 commit comments

Comments
 (0)