Skip to content

Commit ae1572c

Browse files
committed
1章を追加
1 parent 9183a44 commit ae1572c

File tree

10 files changed

+134
-0
lines changed

10 files changed

+134
-0
lines changed

khashimoto/chapter01/q01.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 正弦波の生成: 振幅 1, 周波数 440 Hz の正弦波をサンプリング周波数 16000 Hz で 3 秒分作成しプロットせよ.
2+
3+
import numpy as np
4+
from matplotlib import pyplot
5+
6+
A = 1 # 振幅
7+
f = 440 # 周波数
8+
fs = 16000 # サンプリング周波数
9+
t = np.linspace(0, 3, 3 * fs) # 3秒分作成
10+
x = A * np.sin(2 * np.pi * f * t) # 変位
11+
12+
# プロット
13+
pyplot.plot(t, x)
14+
pyplot.savefig('q01.jpg')

khashimoto/chapter01/q02.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# WAV ファイルの作成(モノラル): 1.で作成した正弦波を 16bit PCM フォーマットで wav ファイルとして保存せよ.
2+
3+
import numpy as np
4+
import soundfile as sf
5+
6+
A = 1 # 振幅
7+
f = 440 # 周波数
8+
fs = 16000 # サンプリング周波数
9+
t = np.linspace(0, 3, 3 * fs) # 3秒分作成
10+
x = A * np.sin(2 * np.pi * f * t) # 変位
11+
12+
sf.write("q02.wav", x, fs, subtype="PCM_16") # 書き込み

khashimoto/chapter01/q03.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# WAV ファイルの作成(ステレオ): 振幅 1, 周波数 660 Hz の正弦波をサンプリング周波数 16000 Hz で 3 秒分作成し,1.で作成した信号と合わせて 2ch の wav ファイルとして保存せよ.
2+
3+
import numpy as np
4+
import soundfile as sf
5+
6+
A = 1 # 振幅
7+
f1 = 440 # 周波数
8+
f2 = 660
9+
fs = 16000 # サンプリング周波数
10+
t = np.linspace(0, 3, 3 * fs) # 3秒分作成
11+
12+
# 信号作成
13+
x = np.zeros((3 * fs, 2))
14+
x[:,0] = A * np.sin( 2 * np.pi * f1 * t )
15+
x[:,1] = A * np.sin( 2 * np.pi * f2 * t )
16+
17+
# 書き込み
18+
sf.write("q03.wav", x, fs, subtype="PCM_16")

khashimoto/chapter01/q04.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 白色雑音の生成: ホワイトノイズをサンプリング周波数 16000 Hz で 3 秒分作成しプロットせよ.
2+
3+
import numpy as np
4+
from matplotlib import pyplot
5+
6+
fs = 16000 # サンプリング周波数
7+
t = np.linspace(0, 3, 3 * fs) # 3秒分作成
8+
x = 2 * np.random.rand(3 * fs) - 1
9+
10+
# プロット
11+
pyplot.plot(t, x)
12+
pyplot.savefig('q04.jpg')

khashimoto/chapter01/q05.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# 信号の混合: 1.で作成した正弦波と 4.で作成したホワイトノイズと正弦波を混合してプロットせよ.
2+
3+
import numpy as np
4+
from matplotlib import pyplot
5+
6+
A = 1 # 振幅
7+
f = 440 # 周波数
8+
fs = 16000 # サンプリング周波数
9+
t = np.linspace(0, 3, 3 * fs) # 3秒分作成
10+
11+
x = A * np.sin(2 * np.pi * f * t) # 1.で作成した正弦波
12+
wn = 2 * np.random.rand(3 * fs) - 1 # 4.で作成したホワイトノイズ
13+
mix = x + wn # 混合音
14+
15+
# プロット
16+
pyplot.plot(t, mix)
17+
pyplot.savefig('q05.jpg')

khashimoto/chapter01/q06.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SN 比: 信号長の等しい 2 個の信号 $s[n], x[n]; (n = 0, \dots, N-1), $ の信号対雑音比 (SN比) を計算する関数を実装せよ.
2+
3+
import numpy as np
4+
5+
def snr(s, x):
6+
r = 10 * np.log10(np.sum(s ** 2) / np.sum(x ** 2))
7+
return r

khashimoto/chapter01/q07.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SN 比を指定した信号の混合(実装): SN 比を任意の値に設定できるようにホワイトノイズの振幅を調整する関数を実装せよ.元信号と所望の SN 比を入力として受け取り,ホワイトノイズを重畳した信号を出力すること.
2+
3+
import numpy as np
4+
5+
def mix_wn(org_s, snr):
6+
sna = np.sqrt(1 / (10 ** (snr/10))) # ホワイトノイズの振幅
7+
wn = 2 * sna * np.random.rand(np.size(org_s)) - sna # ホワイトノイズ
8+
mix = org_s + wn # ホワイトノイズを重畳した信号
9+
return mix

khashimoto/chapter01/q08.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SN 比を指定した信号の混合(確認): 8.で実装した関数を用いて,6.と同様にホワイトノイズと正弦波の混合信号を作成し wav ファイルとして保存せよ.ただし,SN 比が 6dB となるようにすること.
2+
3+
import numpy as np
4+
import soundfile as sf
5+
6+
def mix_wn(org_s, snr):
7+
sna = np.sqrt(1 / (10 ** (snr/10))) # ホワイトノイズの振幅
8+
wn = 2 * sna * np.random.rand(np.size(org_s)) - sna # ホワイトノイズ
9+
mix = org_s + wn # ホワイトノイズを重畳した信号
10+
return mix
11+
12+
A = 1 # 振幅
13+
f = 440 # 周波数
14+
fs = 16000 # サンプリング周波数
15+
t = np.linspace(0, 3, 3 * fs) # 3秒分作成
16+
x = A * np.sin(2 * np.pi * f * t) # 1.で作成した正弦波
17+
18+
mix = mix_wn(x, 6) # ホワイトノイズと正弦波の混合信号
19+
sf.write("q08.wav", mix, fs, subtype="PCM_16") # 書き込み

khashimoto/chapter01/q09.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# 間引きによるダウンサンプリング: 8.で保存した wav ファイルを読み込み,サンプリング周波数を 8kHz に変換して保存せよ.
2+
3+
import soundfile as sf
4+
5+
data, fs = sf.read("q08.wav") # 読み込み
6+
fs = 8000
7+
sf.write("q09.wav", data, fs, subtype="PCM_16") # 書き込み

khashimoto/chapter01/q10.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# 簡単なフィルタ処理: 9.の信号に対して 5 点移動平均フィルタを適用した結果と元の信号をプロットせよ.
2+
3+
import soundfile as sf
4+
import numpy as np
5+
from matplotlib import pyplot
6+
7+
data, fs = sf.read("q09.wav") # 読み込み
8+
result = np.convolve(data, np.ones(5), "valid") / 5 # 5点移動平均フィルタを適用
9+
10+
t1 = np.linspace(0, np.size(data) / fs, np.size(data)) # 時間軸
11+
t2 = t1
12+
for i in range(2):
13+
t2 = np.delete(t2, 0)
14+
t2 = np.delete(t2, np.size(t2) - 1)
15+
16+
# プロット
17+
pyplot.plot(t1, data)
18+
pyplot.plot(t2, result)
19+
pyplot.savefig('q10.jpg')

0 commit comments

Comments
 (0)