-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from woodnx/skotsugi/chapter01
Skotsugi add chapter01
- Loading branch information
Showing
13 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
A = 1.0 # 振幅 | ||
f0 = 440 # 周波数[Hz] | ||
sec = 3.0 # 信号の長さ[s] | ||
fs = 16000 # サンプリング周波数[Hz] | ||
PI = np.pi # 円周率 | ||
|
||
n = np.arange(0, fs*sec) / fs # 間隔(公差)を指定(start: 0, stop: sec, step: 1/sf) | ||
y = A * np.sin(2 * PI * f0 * n) | ||
|
||
plt.plot(n, y) | ||
|
||
plt.xlim(0, 1 / f0) | ||
plt.xlabel("time [sec]") | ||
plt.ylabel("Amplitude") | ||
|
||
plt.savefig("./skotsugi/chapter01/q1.png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import numpy as np | ||
import soundfile as sf | ||
|
||
def make_sin_wave(A, f, fs, sec): | ||
n = np.arange(0, fs*sec) / fs | ||
return A * np.sin(2 * np.pi * f * n) | ||
|
||
if __name__ == "__main__": | ||
fs = 16000 # サンプリング周波数[Hz] | ||
|
||
y = make_sin_wave(1.0, 440, fs, 3.0) | ||
|
||
sf.write("./skotsugi/chapter01/q02.wav", y, fs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import numpy as np | ||
import soundfile as sf | ||
from q02 import make_sin_wave | ||
|
||
fs = 16000 # サンプリング周波数[Hz] | ||
sec = 3.0 | ||
|
||
y = np.zeros((int(fs*sec), 2), dtype=float) | ||
y[:,0] = make_sin_wave(1.0, 440, fs, sec) | ||
y[:,1] = make_sin_wave(1.0, 660, fs, sec) | ||
|
||
sf.write("./skotsugi/chapter01/q03.wav", y, fs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
|
||
def make_white_noise(length): | ||
return np.random.normal(size=int(length)) | ||
|
||
if __name__ == "__main__": | ||
np.random.seed(0) | ||
|
||
fs = 16000 # サンプリング周波数[Hz] | ||
sec = 3.0 | ||
|
||
y = make_white_noise(fs*sec) | ||
n = np.arange(0, 3.0, 1/fs) | ||
|
||
plt.plot(n, y) | ||
|
||
plt.xlim(0) | ||
plt.xlabel("time [sec]") | ||
plt.ylabel("Amplitude") | ||
|
||
plt.savefig("./skotsugi/chapter01/q4.png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
from q02 import make_sin_wave | ||
from q04 import make_white_noise | ||
|
||
np.random.seed(0) | ||
|
||
fs = 16000 # サンプリング周波数[Hz] | ||
sec = 3.0 | ||
|
||
y = make_white_noise(fs*sec) + make_sin_wave(1.0, 440, fs, 3.0) | ||
|
||
n = np.arange(0, 3.0, 1/fs) | ||
|
||
plt.plot(n, y) | ||
|
||
plt.xlim(0) | ||
plt.xlabel("time [sec]") | ||
plt.ylabel("Amplitude") | ||
|
||
plt.savefig("./skotsugi/chapter01/q5.png") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import math | ||
import numpy as np | ||
from q02 import make_sin_wave | ||
from q04 import make_white_noise | ||
|
||
def square_mean(x): | ||
# return np.sum(x ** 2) # <- これだとうまくいかなかった | ||
return np.mean(x ** 2) | ||
|
||
def sn_rate(s, x): | ||
if (len(s) != len(x)): | ||
return | ||
|
||
s_sum = square_mean(s) | ||
x_sum = square_mean(x) | ||
|
||
return 10 * math.log10(s_sum / x_sum) | ||
|
||
##### DEBUG ##### | ||
if __name__ == "__main__": | ||
np.random.seed(0) | ||
|
||
fs = 16000 | ||
sec = 3.0 | ||
|
||
s = make_sin_wave(1.0, 440, fs, 3.0) | ||
x = make_white_noise(fs*sec) | ||
|
||
print(sn_rate(s, x)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import numpy as np | ||
from q02 import make_sin_wave | ||
from q04 import make_white_noise | ||
from q06 import square_mean, sn_rate | ||
|
||
def add_noise_to_signal(signal, snr): | ||
s_power = square_mean(signal) | ||
|
||
snr_linear = 10.0 ** (snr / 10.0) | ||
n_power = s_power / snr_linear | ||
|
||
noise = np.sqrt(n_power) * make_white_noise(len(signal)) | ||
noisy_signal = signal + noise | ||
|
||
return noisy_signal, noise | ||
|
||
##### DEBUG ##### | ||
if __name__ == "__main__": | ||
np.random.seed(0) | ||
|
||
fs = 16000 | ||
sec = 3.0 | ||
|
||
signal = make_sin_wave(1.0, 440, fs, sec) | ||
noisy_signal, noise = add_noise_to_signal(signal, 20) | ||
|
||
print(sn_rate(signal, noise)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import numpy as np | ||
import soundfile as sf | ||
from q02 import make_sin_wave | ||
from q07 import add_noise_to_signal | ||
|
||
np.random.seed(0) | ||
|
||
fs = 16000 | ||
sec = 3.0 | ||
|
||
signal = make_sin_wave(1.0, 440, fs, sec) | ||
noisy_signal, noise = add_noise_to_signal(signal, 6) | ||
|
||
sf.write("./skotsugi/chapter01/q08.wav", noisy_signal, fs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import soundfile as sf | ||
|
||
signal, fs = sf.read('./skotsugi/chapter01/q08.wav') | ||
|
||
down_scaled = signal[0:-1:2] | ||
|
||
sf.write("./skotsugi/chapter01/q09.wav", down_scaled, 8000) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import numpy as np | ||
|
||
def dma(x, k): | ||
y = np.zeros(len(x)) | ||
for i in range(len(x) - k): | ||
y[i] = np.mean(x[i:i+k]) | ||
return y | ||
|
||
if __name__ == "__main__": | ||
import soundfile as sf | ||
signal, fs = sf.read('./skotsugi/chapter01/q09.wav') | ||
|
||
dmaed = dma(signal, 5) | ||
|
||
sf.write("./skotsugi/chapter01/q10.wav", dmaed, 8000) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.