Skip to content

Commit

Permalink
Merge pull request #4 from 153hashimoto/main
Browse files Browse the repository at this point in the history
add khashimoto
  • Loading branch information
taishi-n authored May 20, 2024
2 parents 917a455 + ae1572c commit a7b64b6
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 0 deletions.
4 changes: 4 additions & 0 deletions khashimoto/chapter00/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
## README


example
14 changes: 14 additions & 0 deletions khashimoto/chapter01/q01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 正弦波の生成: 振幅 1, 周波数 440 Hz の正弦波をサンプリング周波数 16000 Hz で 3 秒分作成しプロットせよ.

import numpy as np
from matplotlib import pyplot

A = 1 # 振幅
f = 440 # 周波数
fs = 16000 # サンプリング周波数
t = np.linspace(0, 3, 3 * fs) # 3秒分作成
x = A * np.sin(2 * np.pi * f * t) # 変位

# プロット
pyplot.plot(t, x)
pyplot.savefig('q01.jpg')
12 changes: 12 additions & 0 deletions khashimoto/chapter01/q02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# WAV ファイルの作成(モノラル): 1.で作成した正弦波を 16bit PCM フォーマットで wav ファイルとして保存せよ.

import numpy as np
import soundfile as sf

A = 1 # 振幅
f = 440 # 周波数
fs = 16000 # サンプリング周波数
t = np.linspace(0, 3, 3 * fs) # 3秒分作成
x = A * np.sin(2 * np.pi * f * t) # 変位

sf.write("q02.wav", x, fs, subtype="PCM_16") # 書き込み
18 changes: 18 additions & 0 deletions khashimoto/chapter01/q03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# WAV ファイルの作成(ステレオ): 振幅 1, 周波数 660 Hz の正弦波をサンプリング周波数 16000 Hz で 3 秒分作成し,1.で作成した信号と合わせて 2ch の wav ファイルとして保存せよ.

import numpy as np
import soundfile as sf

A = 1 # 振幅
f1 = 440 # 周波数
f2 = 660
fs = 16000 # サンプリング周波数
t = np.linspace(0, 3, 3 * fs) # 3秒分作成

# 信号作成
x = np.zeros((3 * fs, 2))
x[:,0] = A * np.sin( 2 * np.pi * f1 * t )
x[:,1] = A * np.sin( 2 * np.pi * f2 * t )

# 書き込み
sf.write("q03.wav", x, fs, subtype="PCM_16")
12 changes: 12 additions & 0 deletions khashimoto/chapter01/q04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 白色雑音の生成: ホワイトノイズをサンプリング周波数 16000 Hz で 3 秒分作成しプロットせよ.

import numpy as np
from matplotlib import pyplot

fs = 16000 # サンプリング周波数
t = np.linspace(0, 3, 3 * fs) # 3秒分作成
x = 2 * np.random.rand(3 * fs) - 1

# プロット
pyplot.plot(t, x)
pyplot.savefig('q04.jpg')
17 changes: 17 additions & 0 deletions khashimoto/chapter01/q05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# 信号の混合: 1.で作成した正弦波と 4.で作成したホワイトノイズと正弦波を混合してプロットせよ.

import numpy as np
from matplotlib import pyplot

A = 1 # 振幅
f = 440 # 周波数
fs = 16000 # サンプリング周波数
t = np.linspace(0, 3, 3 * fs) # 3秒分作成

x = A * np.sin(2 * np.pi * f * t) # 1.で作成した正弦波
wn = 2 * np.random.rand(3 * fs) - 1 # 4.で作成したホワイトノイズ
mix = x + wn # 混合音

# プロット
pyplot.plot(t, mix)
pyplot.savefig('q05.jpg')
7 changes: 7 additions & 0 deletions khashimoto/chapter01/q06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# SN 比: 信号長の等しい 2 個の信号 $s[n], x[n]; (n = 0, \dots, N-1), $ の信号対雑音比 (SN比) を計算する関数を実装せよ.

import numpy as np

def snr(s, x):
r = 10 * np.log10(np.sum(s ** 2) / np.sum(x ** 2))
return r
9 changes: 9 additions & 0 deletions khashimoto/chapter01/q07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# SN 比を指定した信号の混合(実装): SN 比を任意の値に設定できるようにホワイトノイズの振幅を調整する関数を実装せよ.元信号と所望の SN 比を入力として受け取り,ホワイトノイズを重畳した信号を出力すること.

import numpy as np

def mix_wn(org_s, snr):
sna = np.sqrt(1 / (10 ** (snr/10))) # ホワイトノイズの振幅
wn = 2 * sna * np.random.rand(np.size(org_s)) - sna # ホワイトノイズ
mix = org_s + wn # ホワイトノイズを重畳した信号
return mix
19 changes: 19 additions & 0 deletions khashimoto/chapter01/q08.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# SN 比を指定した信号の混合(確認): 8.で実装した関数を用いて,6.と同様にホワイトノイズと正弦波の混合信号を作成し wav ファイルとして保存せよ.ただし,SN 比が 6dB となるようにすること.

import numpy as np
import soundfile as sf

def mix_wn(org_s, snr):
sna = np.sqrt(1 / (10 ** (snr/10))) # ホワイトノイズの振幅
wn = 2 * sna * np.random.rand(np.size(org_s)) - sna # ホワイトノイズ
mix = org_s + wn # ホワイトノイズを重畳した信号
return mix

A = 1 # 振幅
f = 440 # 周波数
fs = 16000 # サンプリング周波数
t = np.linspace(0, 3, 3 * fs) # 3秒分作成
x = A * np.sin(2 * np.pi * f * t) # 1.で作成した正弦波

mix = mix_wn(x, 6) # ホワイトノイズと正弦波の混合信号
sf.write("q08.wav", mix, fs, subtype="PCM_16") # 書き込み
7 changes: 7 additions & 0 deletions khashimoto/chapter01/q09.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 間引きによるダウンサンプリング: 8.で保存した wav ファイルを読み込み,サンプリング周波数を 8kHz に変換して保存せよ.

import soundfile as sf

data, fs = sf.read("q08.wav") # 読み込み
fs = 8000
sf.write("q09.wav", data, fs, subtype="PCM_16") # 書き込み
19 changes: 19 additions & 0 deletions khashimoto/chapter01/q10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# 簡単なフィルタ処理: 9.の信号に対して 5 点移動平均フィルタを適用した結果と元の信号をプロットせよ.

import soundfile as sf
import numpy as np
from matplotlib import pyplot

data, fs = sf.read("q09.wav") # 読み込み
result = np.convolve(data, np.ones(5), "valid") / 5 # 5点移動平均フィルタを適用

t1 = np.linspace(0, np.size(data) / fs, np.size(data)) # 時間軸
t2 = t1
for i in range(2):
t2 = np.delete(t2, 0)
t2 = np.delete(t2, np.size(t2) - 1)

# プロット
pyplot.plot(t1, data)
pyplot.plot(t2, result)
pyplot.savefig('q10.jpg')

0 comments on commit a7b64b6

Please sign in to comment.