├── tkawamura ├── README.md └── chapter01 │ ├── 02.py │ ├── 03.py │ ├── 04.py │ └── 05.py ├── .automation ├── requirements.txt └── README.md ├── progress.png ├── smotoyama ├── chapter01 │ ├── 06.py │ ├── 07.py │ ├── 04.py │ ├── 09.py │ ├── 01.py │ ├── 02.py │ ├── 05.py │ ├── 03.py │ ├── 10.py │ └── 08.py ├── chapter03 │ ├── 02.py │ ├── 01.py │ ├── 08.py │ ├── 06.py │ ├── 05.py │ ├── 07.py │ ├── 03.py │ ├── 10.py │ └── 09.py └── chapter02 │ ├── 09.py │ ├── 07.py │ ├── 08.py │ ├── 02.py │ ├── 10.py │ ├── 01.py │ ├── 03.py │ ├── 04.py │ └── 05.py ├── ynakamura ├── chapter01 │ ├── 07.py │ ├── 03.py │ ├── 02.py │ ├── 01.py │ ├── 05.py │ ├── 06.py │ ├── 08.py │ ├── 04.py │ ├── 09.py │ └── 10.py ├── chapter02 │ ├── 05.py │ ├── 07.py │ ├── 09.py │ ├── 08.py │ ├── 06.py │ ├── 01.py │ ├── 02.py │ ├── 03.py │ ├── 10.py │ └── 04.py ├── chapter04 │ ├── 01.py │ ├── 05.py │ ├── 02.py │ ├── 06.py │ └── 03.py ├── chapter03 │ ├── 06.py │ ├── 02.py │ ├── 05.py │ ├── 03.py │ ├── 08.py │ ├── 01.py │ ├── 07.py │ ├── 09.py │ └── 10.py └── chapter05 │ ├── 01.py │ ├── 02.py │ ├── 03.py │ ├── 04.py │ └── 06.py ├── tkawamura2 ├── chapter02 │ ├── 07.py │ ├── 06.py │ ├── 08.py │ ├── 01.py │ ├── 09.py │ ├── 02.py │ ├── 03.py │ ├── 04.py │ ├── 05.py │ └── 10.py ├── chapter01 │ ├── 08.py │ ├── 07.py │ ├── 03.py │ ├── 05.py │ ├── 02.py │ ├── 01.py │ ├── 06.py │ ├── 04.py │ ├── 09.py │ └── 10.py ├── chapter04 │ ├── 01.py │ ├── 05.py │ ├── 02.py │ ├── 06.py │ └── 03.py ├── chapter03 │ ├── 02.py │ ├── 06.py │ ├── 03.py │ ├── 01.py │ ├── 05.py │ ├── 07.py │ ├── 08.py │ ├── 10.py │ └── 09.py └── chapter05 │ ├── 04.py │ ├── 05.py │ ├── 01.py │ ├── 02.py │ ├── 06.py │ └── 03.py ├── muesaka ├── chapter02 │ ├── 07.py │ ├── 02.py │ ├── 09.py │ ├── 01.py │ ├── 05.py │ ├── 08.py │ ├── 04.py │ ├── 06.py │ ├── 03.py │ └── 10.py ├── chapter03 │ ├── 02.py │ ├── 01.py │ ├── 03.py │ ├── 08.py │ ├── 05.py │ ├── 06.py │ ├── 07.py │ ├── 10.py │ └── 09.py ├── chapter01 │ ├── 03.py │ ├── 07.py │ ├── 08.py │ ├── 02.py │ ├── 05.py │ ├── 04.py │ └── 01.py └── chapter04 │ ├── 01.py │ ├── 05.py │ ├── 02.py │ ├── 06.py │ └── 03.py ├── knishida ├── chapter02 │ ├── 07.py │ ├── 01.py │ ├── 08.py │ ├── 09.py │ ├── 02.py │ ├── 05.py │ ├── 03.py │ ├── 06.py │ └── 04.py ├── chapter01 │ ├── 05.py │ ├── 01.py │ ├── 06.py │ ├── 02.py │ ├── 03.py │ ├── 04.py │ ├── 07.py │ ├── 08.py │ ├── 09.py │ └── 10.py └── chapter03 │ ├── 02.py │ ├── 06.py │ ├── 08.py │ ├── 01.py │ ├── 05.py │ ├── 03.py │ ├── 07.py │ ├── 10.py │ └── 09.py ├── ttakahashi ├── chapter01 │ ├── 03.py │ ├── 02.py │ ├── 05.py │ ├── 01.py │ ├── 04.py │ ├── 06.py │ ├── 07.py │ ├── 08.py │ ├── 09.py │ └── 10.py ├── chapter02 │ ├── 09.py │ ├── 02.py │ ├── 05.py │ ├── 07.py │ ├── 08.py │ ├── 01.py │ ├── 06.py │ ├── 03.py │ └── 04.py └── chapter03 │ ├── 08.py │ ├── 06.py │ ├── 05.py │ ├── 01.py │ ├── 02.py │ ├── 07.py │ ├── 03.py │ └── 10.py ├── kkazama ├── chapter04 │ ├── 01.py │ ├── 05.py │ ├── 02.py │ ├── 06.py │ └── 03.py ├── chapter01 │ ├── 05.py │ ├── 03.py │ ├── 08.py │ ├── 02.py │ ├── 01.py │ ├── 06.py │ ├── 07.py │ ├── 04.py │ ├── 09.py │ └── 10.py ├── chapter02 │ ├── 07.py │ ├── 09.py │ ├── 02.py │ ├── 08.py │ ├── 06.py │ ├── 05.py │ ├── 01.py │ ├── 04.py │ ├── 03.py │ └── 10.py └── chapter03 │ ├── 08.py │ ├── 06.py │ ├── 05.py │ ├── 02.py │ ├── 01.py │ ├── 03.py │ ├── 07.py │ ├── 09.py │ └── 10.py ├── dsugawara ├── chapter04 │ ├── 01.py │ ├── 05.py │ ├── 02.py │ ├── 06.py │ └── 03.py ├── chapter02 │ ├── 07.py │ ├── 09.py │ ├── 05.py │ ├── 08.py │ ├── 06.py │ ├── 02.py │ ├── 01.py │ ├── 03.py │ └── 10.py ├── chapter03 │ ├── 02.py │ ├── 06.py │ ├── 01.py │ ├── 03.py │ ├── 08.py │ ├── 05.py │ ├── 07.py │ └── 10.py ├── chapter01 │ ├── 03.py │ ├── 08.py │ ├── 01.py │ ├── 07.py │ ├── 02.py │ ├── 06.py │ ├── 05.py │ ├── 10.py │ ├── 04.py │ └── 09.py └── chapter05 │ ├── 01.py │ ├── 04.py │ ├── 02.py │ └── 06.py ├── ykuriki ├── chapter02 │ ├── 07.py │ ├── 09.py │ ├── 02.py │ ├── 05.py │ ├── 08.py │ ├── 06.py │ └── 01.py ├── chapter01 │ ├── 06.py │ ├── 01.py │ ├── 04.py │ ├── 02.py │ ├── 05.py │ ├── 03.py │ ├── 07.py │ ├── 09.py │ └── 08.py ├── chapter03 │ ├── 02.py │ ├── 06.py │ ├── 05.py │ ├── 01.py │ ├── 03.py │ ├── 08.py │ └── 07.py ├── chapter04 │ ├── 01.py │ ├── 05.py │ └── 02.py └── chapter05 │ ├── 04.py │ ├── 01.py │ ├── 02.py │ └── 03.py └── kyamada ├── Chapter03 ├── 06.py ├── 05.py ├── 08.py ├── 02.py ├── 01.py ├── 07.py └── 03.py ├── chapter01 ├── 05.py ├── 07.py ├── 01.py ├── 08.py ├── 06.py └── 02.py └── chapter02 ├── 07.py ├── 02.py ├── 04.py ├── 03.py ├── 01.py └── 06.py /tkawamura/README.md: -------------------------------------------------------------------------------- 1 | # tkawamura 2 | -------------------------------------------------------------------------------- /.automation/requirements.txt: -------------------------------------------------------------------------------- 1 | numpy 2 | matplotlib 3 | seaborn -------------------------------------------------------------------------------- /progress.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onolab-tmu/asp-tutorial-2022/HEAD/progress.png -------------------------------------------------------------------------------- /smotoyama/chapter01/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def cul_SNR(y, x): 5 | snr = 10 * np.log10(np.sum(y ** 2) / np.sum(x ** 2)) 6 | return snr 7 | -------------------------------------------------------------------------------- /ynakamura/chapter01/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def calculate_snr(signal, noise): 5 | return 10 * np.log(np.sum(signal**2) / np.sum(noise**2)) 6 | -------------------------------------------------------------------------------- /tkawamura2/chapter02/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def hamming(N): 5 | w = 0.54 - 0.46 * np.cos(2 * np.pi * np.arange(N) / (N - 1)) 6 | return w 7 | -------------------------------------------------------------------------------- /ynakamura/chapter02/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | N = 8 5 | delta = np.zeros(N) 6 | delta[0] = 1 7 | Delta = np.fft.fft(delta) 8 | 9 | print(Delta) 10 | -------------------------------------------------------------------------------- /muesaka/chapter02/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def hamming(N): 5 | n = np.arange(N) 6 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 7 | return w 8 | -------------------------------------------------------------------------------- /ynakamura/chapter02/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def hamming(N): 5 | n = np.arange(N) 6 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 7 | return w 8 | -------------------------------------------------------------------------------- /knishida/chapter02/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def Hamming(N): 5 | n = np.arange(N) 6 | win = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 7 | return win 8 | -------------------------------------------------------------------------------- /ynakamura/chapter01/03.py: -------------------------------------------------------------------------------- 1 | import wavio 2 | 3 | 4 | wav = wavio.read("sin_fin440_fs16k.wav") 5 | wavio.write("sin_fin440_fs8k.wav", wav.data[0::2], wav.rate // 2, sampwidth=2) 6 | -------------------------------------------------------------------------------- /ttakahashi/chapter01/03.py: -------------------------------------------------------------------------------- 1 | import soundfile as sf 2 | 3 | y, sr = sf.read("outputs/02.wav") 4 | sf.write("outputs/03.wav", y, 8000, format="WAV", subtype="PCM_16") 5 | 6 | print("success!") 7 | -------------------------------------------------------------------------------- /tkawamura2/chapter01/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def set_snr(s, snr): 4 | A_s = np.linalg.norm(s) 5 | x = np.random.randn(round(len(s))) 6 | x = A_s * x /(10**(snr/20)*np.linalg.norm(x)) 7 | return x -------------------------------------------------------------------------------- /kkazama/chapter04/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def zero_pad(x, L, S): 4 | x = np.pad(x, L-S, L-S) 5 | a = len(x) % S 6 | if a != 0: 7 | x = np.pad(x, (0, S-a)) 8 | 9 | return x 10 | -------------------------------------------------------------------------------- /dsugawara/chapter04/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def padding(L, S, x): 5 | 6 | x = np.pad(x, [L - S, L - S]) 7 | temp = S - (x.size % S) # 末尾に詰める0の個数を求める 8 | x = np.pad(x, [0, temp]) 9 | return x 10 | -------------------------------------------------------------------------------- /tkawamura2/chapter04/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def pad(x, L, S): 5 | x_ = np.pad(x, [L - S, L - S]) 6 | a = np.mod(x_.size, S) 7 | if a != 0: 8 | x_ = np.pad(x_, [0, S - a]) 9 | return x_ 10 | -------------------------------------------------------------------------------- /dsugawara/chapter02/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # Humming窓 5 | def Humming(N): 6 | w = [] 7 | for n in range(N): 8 | a = 0.54 - 0.46 * np.cos(2*np.pi*n/(N-1)) 9 | w.append(a) 10 | return w 11 | -------------------------------------------------------------------------------- /ynakamura/chapter04/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def zero_pad(x, L, S): 5 | x_pad = np.pad(x, L - S) 6 | if x_pad.shape[0] % S != 0: 7 | x_pad = np.pad(x_pad, ((0, S - (x_pad.shape[0] % S)))) 8 | return x_pad 9 | -------------------------------------------------------------------------------- /knishida/chapter01/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | sec = 3.0 5 | fs = 16000 6 | 7 | t = np.arange(0, sec, 1 / fs) 8 | n = np.random.rand(round(fs * sec)) 9 | 10 | plt.plot(t, n) 11 | plt.show() 12 | -------------------------------------------------------------------------------- /tkawamura2/chapter01/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import math 3 | 4 | def snr(s, x): 5 | N = len(s) 6 | if N == len(x): 7 | return np.NaN 8 | sn_ratio = np.linalg.norm(s)**2/np.linalg.norm(x)**2 9 | return 10*math.log(sn_ratio) -------------------------------------------------------------------------------- /tkawamura2/chapter01/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from scipy.io.wavfile import read, write 4 | from librosa import resample 5 | 6 | fs, y = read("440.wav") 7 | y_ = resample(y, fs, 8000) 8 | 9 | write("440_8000.wav", 8000, y_) -------------------------------------------------------------------------------- /kkazama/chapter01/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | A = 1 #振幅 5 | sec = 3 #信号の長さ 6 | fs = 16000 #サンプリング周波数 7 | 8 | x = 2 * A * (np.random.rand(round(fs*sec)) - 0.5) #ホワイトノイズの生成 9 | 10 | plt.plot(x) 11 | plt.show() 12 | -------------------------------------------------------------------------------- /muesaka/chapter03/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def circular_convolution(x, h): 5 | N = len(x) 6 | z = np.zeros(N) 7 | 8 | for n in range(N): 9 | for k in range(N): 10 | z[n] += x[k] * h[(n - k) % N] 11 | return z 12 | -------------------------------------------------------------------------------- /smotoyama/chapter03/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def circular_conv(x, h): 5 | N = len(x) 6 | z = np.zeros(N) 7 | for n in range(z.size): 8 | for k in range(z.size): 9 | z[n] += x[k] * h[(n - k) % N] 10 | return z 11 | -------------------------------------------------------------------------------- /kkazama/chapter01/03.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | import librosa 4 | import soundfile as sf 5 | 6 | data, sr = sf.read("sin.wav") 7 | 8 | sr = 8000 9 | 10 | sf.write("new_sin.wav", data, sr, subtype="PCM_16") 11 | 12 | 13 | -------------------------------------------------------------------------------- /smotoyama/chapter01/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def adj_SNR(x, snr): 5 | n = np.random.rand(round(len(x))) 6 | sigma = np.sqrt(np.sum(x ** 2) / np.sum(n ** 2) * 10 ** (-snr / 10)) 7 | noize = sigma * n 8 | y = x + noize 9 | return y 10 | -------------------------------------------------------------------------------- /ynakamura/chapter01/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import wavio 3 | 4 | 5 | fs = 16000 6 | sec = 3 7 | fin = 440 8 | t = np.linspace(0.0, sec, int(fs * sec)) 9 | 10 | x = np.sin(2.0 * np.pi * fin * t) 11 | 12 | wavio.write("sin_fin440_fs16k.wav", x, fs, sampwidth=2) 13 | -------------------------------------------------------------------------------- /ynakamura/chapter03/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | x = np.zeros((10)) 6 | x[0] = 1 7 | 8 | y = np.zeros((10)) 9 | for n in range(10): 10 | y[n] = 0.3 * y[n - 1] + 0.4 * x[n] 11 | 12 | 13 | plt.stem(y) 14 | plt.show() 15 | -------------------------------------------------------------------------------- /tkawamura2/chapter03/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def circ_conv(x, h): 5 | N = x.size 6 | M = h.size 7 | z = np.zeros(M) 8 | for n in range(M): 9 | for k in range(N): 10 | z[n] = z[n] + x[k]*h[np.mod(n-k, M)] 11 | return z 12 | -------------------------------------------------------------------------------- /dsugawara/chapter03/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def Circlar_convolution(x, h): 5 | 6 | N = x.size 7 | z = np.zeros(N) 8 | 9 | for n in range(0, N): 10 | for k in range(0, N): 11 | z[n] += x[k] * h[(n - k) % N] 12 | 13 | return z 14 | -------------------------------------------------------------------------------- /tkawamura2/chapter01/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | sec = 3.0 # 信号の長さ s 5 | fs = 16000 # サンプリング周波数 Hz 6 | 7 | t = np.arange(0, sec, 1/fs) 8 | x = np.random.randn(round(fs*sec)) 9 | 10 | plt.plot(t, x) 11 | plt.xlim([0, 0.03]) 12 | plt.show() -------------------------------------------------------------------------------- /dsugawara/chapter01/03.py: -------------------------------------------------------------------------------- 1 | # 2.で保存した wav ファイルを読み込み,サンプリング周波数を 8kHz に変換して保存せよ. 2 | 3 | from os import lseek 4 | import soundfile 5 | 6 | file = "02py_sin.wav" 7 | data, samplerate = soundfile.read(file) 8 | 9 | 10 | soundfile.write(file="03py_sin.wav", data=data, samplerate=8000) 11 | -------------------------------------------------------------------------------- /knishida/chapter01/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | A = 1 5 | f = 440.0 6 | sec = 3.0 7 | fs = 16000 8 | 9 | t = np.arange(0, sec, 1 / fs) 10 | y = A * np.sin(2 * np.pi * f * t) 11 | 12 | plt.plot(t, y) 13 | plt.xlim(0, 0.03) 14 | plt.show() 15 | -------------------------------------------------------------------------------- /kkazama/chapter04/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def mix_win(w, S): 5 | L = len(w) 6 | Q = L // S 7 | ws = np.zeros(L) 8 | for l in range(L): 9 | a = l - (Q - 1) 10 | if a >= 0: 11 | ws[l] = w[l] / np.sum(w[a:Q] ** 2) 12 | return ws 13 | -------------------------------------------------------------------------------- /ynakamura/chapter01/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | fs = 16000 6 | sec = 3 7 | fin = 440 8 | t = np.linspace(0.0, sec, int(fs * sec)) 9 | 10 | x = np.sin(2.0 * np.pi * fin * t) 11 | 12 | plt.plot(t, x) 13 | plt.xlim(0, 0.03) 14 | plt.show() 15 | -------------------------------------------------------------------------------- /ynakamura/chapter01/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | fs = 16000 6 | sec = 3 7 | t = np.linspace(0.0, sec, fs * sec) 8 | 9 | whitenoise = np.random.rand(fs * sec) * 2 - 1 10 | 11 | plt.plot(t, whitenoise) 12 | plt.xlim(0, 0.03) 13 | plt.show() 14 | -------------------------------------------------------------------------------- /muesaka/chapter01/03.py: -------------------------------------------------------------------------------- 1 | import better_exceptions 2 | import soundfile as sf 3 | better_exceptions.MAX_LENGTH = None 4 | 5 | 6 | if __name__ == "__main__": 7 | sr = 8000 # サンプリング周波数(変更後) 8 | 9 | data = sf.read('02.wav')[0] # 信号だけ取り出す 10 | sf.write('03.wav', data, sr, 'PCM_16') 11 | -------------------------------------------------------------------------------- /smotoyama/chapter01/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | if __name__ == "__main__": 6 | fs = 16000 7 | s = 3 8 | 9 | t = np.arange(0, s, 1/fs) 10 | n = x = np.random.rand(round(fs * s)) 11 | 12 | plt.plot(t, n) 13 | plt.show() 14 | -------------------------------------------------------------------------------- /muesaka/chapter01/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import better_exceptions 3 | better_exceptions.MAX_LENGTH = None 4 | 5 | 6 | def calculate_snr(s1, s2): # SN比計算 7 | s1 = np.sum(np.array(s1) ** 2) 8 | s2 = np.sum(np.array(s2) ** 2) 9 | snr = 10 * np.log10(s1/s2, 10) 10 | return snr 11 | -------------------------------------------------------------------------------- /dsugawara/chapter03/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | x = np.zeros(10) 5 | x[0] = 1 6 | 7 | y = np.zeros(10) 8 | y[0] = 0.4 * x[0] 9 | for n in range(1, 10): 10 | y[n] = 0.3 * y[n - 1] + 0.4 * x[n] 11 | 12 | 13 | plt.stem(y) 14 | plt.savefig("06py_sabun.png") 15 | -------------------------------------------------------------------------------- /tkawamura2/chapter03/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | x = np.zeros(10) 6 | x[0] = 1 7 | 8 | N = x.size 9 | y = np.zeros(N) 10 | y[0] = 0.4*x[0] 11 | 12 | for n in np.arange(1, N): 13 | y[n] = 0.3*y[n-1] + 0.4*x[n] 14 | 15 | plt.stem(y) 16 | plt.show() 17 | -------------------------------------------------------------------------------- /smotoyama/chapter03/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def linear_conv(x, h): 5 | N = len(x) 6 | z = np.zeros(2 * N - 2) 7 | for n in range(z.size): 8 | for k in range(N): 9 | if n - k >= 0 and n - k <= N - 1: 10 | z[n] += x[k] * h[n - k] 11 | return z 12 | -------------------------------------------------------------------------------- /tkawamura2/chapter01/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from scipy.io.wavfile import read, write 4 | 5 | a = 1 #振幅 6 | fs = 16000 #サンプリング周波数 7 | f = 440 #周波数 8 | sec = 3 #秒 9 | 10 | x = np.arange(0, sec, 1/fs) 11 | y = np.sin(2*x*np.pi*f) 12 | 13 | write("440.wav", fs, y) -------------------------------------------------------------------------------- /tkawamura2/chapter01/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | a = 1 #振幅 5 | fs = 16000 #サンプリング周波数 6 | f = 440 #周波数 7 | sec = 3 #秒 8 | 9 | x = np.arange(0, sec, 1/fs) 10 | y = np.sin(2*x*np.pi*f) 11 | 12 | plt.figure() 13 | plt.plot(x, y) 14 | plt.xlim([0, 0.03]) 15 | plt.show() -------------------------------------------------------------------------------- /ynakamura/chapter03/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def circular_conv(x, h): 5 | N = x.shape[0] 6 | z = np.array([]) 7 | for n in range(N - 1): 8 | sum = 0 9 | for k in range(N): 10 | sum += x[k] * h[(n - k) % N] 11 | z = np.append(z, sum) 12 | return z 13 | -------------------------------------------------------------------------------- /ynakamura/chapter04/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def composite_win(S, win): 5 | L = win.shape[0] 6 | Q = L // S 7 | for nL in range(L): 8 | sum = 0 9 | for nQ in range(Q): 10 | sum += win[nL - nQ * S] ** 2 11 | win_s = win / sum 12 | return np.array(win_s) 13 | -------------------------------------------------------------------------------- /smotoyama/chapter01/09.py: -------------------------------------------------------------------------------- 1 | import soundfile as sf 2 | from scipy.signal import decimate 3 | 4 | if __name__ == "__main__": 5 | s = 3 6 | fs_re = 8000 7 | x, sr = sf.read("sound_noize.wav") 8 | x_re = decimate(x, 2) 9 | 10 | sf.write("sound_re.wav", data=x_re, samplerate=fs_re, subtype="PCM_16") 11 | -------------------------------------------------------------------------------- /dsugawara/chapter04/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def optimal_window(S, win): 5 | 6 | L = win.size 7 | Q = L // S 8 | for l in range(0, L): 9 | sigma = 0 10 | for m in range(0, Q): 11 | sigma += win[l - m * S] ** 2 12 | win_s = win / sigma 13 | 14 | return win_s 15 | -------------------------------------------------------------------------------- /smotoyama/chapter01/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | if __name__ == "__main__": 6 | A = 1 7 | f = 440 8 | fs = 16000 9 | s = 3 10 | 11 | t = np.arange(0, s, 1 / fs) 12 | x = x = A * np.sin(2 * np.pi * f * t) 13 | 14 | plt.plot(t, x) 15 | plt.show() 16 | -------------------------------------------------------------------------------- /tkawamura2/chapter03/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def circpad_linerconv(x, h): 5 | N = x.size 6 | h = np.pad(h, [0, int(N / 2) + 1]) 7 | M = h.size 8 | z = np.zeros(M) 9 | for n in range(M): 10 | for k in range(N): 11 | z[n] = z[n] + x[k]*h[np.mod(n-k, M)] 12 | return z 13 | -------------------------------------------------------------------------------- /muesaka/chapter03/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def linear_convolution(x, h): 5 | N = len(x) 6 | z = np.zeros(2 * N - 2) 7 | 8 | for n in range(2 * N - 2): 9 | for k in range(N): 10 | if (n - k >= 0) and (n - k <= N - 1): 11 | z[n] += x[k] * h[n - k] 12 | return z 13 | -------------------------------------------------------------------------------- /tkawamura2/chapter03/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def liner_conv(x, h): 5 | N = x.size 6 | M = x.size + h.size - 1 7 | z = np.zeros(M) 8 | for n in range(M): 9 | for k in range(N): 10 | if n-k >= 0 and n-k <= N - 1: 11 | z[n] = z[n] + x[k]*h[n-k] 12 | return z 13 | -------------------------------------------------------------------------------- /dsugawara/chapter03/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def Liner_convolution(x, h): 5 | N = x.size 6 | z = np.zeros(2 * (N - 1)) 7 | 8 | for n in range(2 * (N - 1)): 9 | for k in range(N): 10 | if 0 <= n - k and n - k <= N - 1: 11 | z[n] += x[k] * h[n - k] 12 | 13 | return z 14 | -------------------------------------------------------------------------------- /knishida/chapter01/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | A = 1 5 | f = 440.0 6 | sec = 3.0 7 | fs = 16000 8 | 9 | t = np.arange(0, sec, 1 / fs) 10 | y = A * np.sin(2 * np.pi * f * t) 11 | 12 | n = np.random.rand(round(fs * sec)) 13 | 14 | plt.plot(t, y + n) 15 | plt.xlim(0, 0.03) 16 | plt.show() 17 | -------------------------------------------------------------------------------- /kkazama/chapter01/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | #SNR計算 4 | def cal_sn(s, x): 5 | snr = 10 * np.log10(np.sum(s * s) / np.sum(x * x)) 6 | 7 | return snr 8 | 9 | #ノイズの振幅調整 10 | def cal_adj_amp(s, x, snr): 11 | x = x / np.sqrt(sum(x * x)) 12 | x = x * np.sqrt(sum(s * s)) 13 | x = x * 10 ** (-snr / 20) 14 | 15 | return x -------------------------------------------------------------------------------- /kkazama/chapter02/07.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | def hamm(N): 6 | w = np.zeros(N) 7 | for n in range(N): 8 | w[n] = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 9 | return w 10 | 11 | ham = hamm(6) 12 | 13 | plt.plot(ham) 14 | plt.show() -------------------------------------------------------------------------------- /muesaka/chapter01/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import better_exceptions 3 | better_exceptions.MAX_LENGTH = None 4 | 5 | 6 | def generate_snr_noise(snr, s1, t2, s2): # 指定したSN比になるように信号の振幅を調整 7 | s2 = s2 / np.sqrt(np.sum(s2 ** 2)) 8 | s2 = s2 * np.sqrt(np.sum(s1 ** 2)) 9 | s2 = s2 * 10 ** (-1 * snr / 20) 10 | return t2, s2 11 | -------------------------------------------------------------------------------- /smotoyama/chapter01/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import soundfile as sf 3 | 4 | 5 | if __name__ == "__main__": 6 | A = 1 7 | f = 440 8 | fs = 16000 9 | s = 3 10 | 11 | t = np.arange(0, s, 1 / fs) 12 | x = x = A * np.sin(2 * np.pi * f * t) 13 | 14 | sf.write("sound.wav", data=x, samplerate=fs, subtype="PCM_16") 15 | -------------------------------------------------------------------------------- /ttakahashi/chapter02/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def hamming(N): 5 | n = np.arange(N) 6 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 7 | return w 8 | 9 | 10 | sec = 3.0 # 信号の長さ s 11 | sr = 16000 # サンプリング周波数 Hz 12 | 13 | N = sec * sr 14 | w = hamming(N) 15 | w_fft = np.fft.fft(w) 16 | 17 | print("success!") 18 | -------------------------------------------------------------------------------- /.automation/README.md: -------------------------------------------------------------------------------- 1 | # .automation 2 | 3 | This folder holds an automation script to visualize the progress. 4 | 5 | ## make_progress.py 6 | 7 | This script uses GitHub Actions so that when a push to the repository is committed, it will complete the following: 8 | 9 | - Checkout the source code 10 | - Update progress.png 11 | - Update progress.json -------------------------------------------------------------------------------- /kkazama/chapter01/02.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | import soundfile as sf 4 | 5 | A = 1 6 | f = 440 7 | sec = 3 8 | fs = 16000 9 | 10 | t = np.arange(0, sec, 1/fs) 11 | 12 | xt = A * np.sin(2 * np.pi * f * t) 13 | 14 | sf.write(file = "sin.wav", data = xt, samplerate = fs, format = "WAV", subtype = "PCM_16") -------------------------------------------------------------------------------- /ykuriki/chapter02/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def ham_wnd(N): 6 | """ハミング窓を生成する 7 | 8 | Args: 9 | N (int): 窓の長さ 10 | 11 | Returns: 12 | w (ndarray): ハミング窓(float型1次元配列) 13 | 14 | """ 15 | n = np.arange(N) 16 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 17 | 18 | return w 19 | -------------------------------------------------------------------------------- /ynakamura/chapter03/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | x = np.zeros((10)) 6 | x[0] = 1 7 | 8 | y = np.array([]) 9 | for n in range(10): 10 | y = np.append( 11 | y, 12 | 0.2 * x[n] + 0.2 * x[n - 1] + 0.2 * x[n - 2] + 0.2 * x[n - 3] + 0.2 * x[n - 4], 13 | ) 14 | 15 | plt.stem(y) 16 | plt.show() 17 | -------------------------------------------------------------------------------- /dsugawara/chapter03/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def Circar_convolution_padded(x, h): 5 | 6 | x = np.pad(x, [0, h.size - 1]) 7 | h = np.pad(h, [0, x.size - 1]) 8 | N = x.size 9 | z = np.zeros(N) 10 | 11 | for n in range(0, N): 12 | for k in range(0, N): 13 | z[n] += x[k] * h[(n - k) % N] 14 | 15 | return z 16 | -------------------------------------------------------------------------------- /muesaka/chapter03/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def padding_circular_convolution(x_in, h): 5 | x = np.pad(x_in, ((0, len(x_in) - 2))) 6 | h = np.pad(h, ((0, len(x_in) - 2))) 7 | N = len(x) 8 | z = np.zeros(N) 9 | 10 | for n in range(N): 11 | for k in range(N): 12 | z[n] += x[k] * h[(n - k) % N] 13 | return z 14 | -------------------------------------------------------------------------------- /ynakamura/chapter01/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | fs = 16000 6 | sec = 3 7 | fin = 440 8 | t = np.linspace(0.0, sec, int(fs * sec)) 9 | 10 | x = np.sin(2.0 * np.pi * fin * t) 11 | 12 | whitenoise = np.random.rand(int(fs * sec)) * 2 - 1 13 | 14 | plt.plot(t, (x + whitenoise)) 15 | plt.xlim(0, 0.03) 16 | plt.show() 17 | -------------------------------------------------------------------------------- /ynakamura/chapter01/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def adjust_amp(x, snr, fs=16000, sec=3): 5 | whitenoise = np.random.rand(int(fs * sec)) * 2 - 1 6 | a = np.sqrt(np.sum(x**2) / np.sum(whitenoise**2) * (10 ** (-snr / 10))) 7 | print(np.sum(whitenoise**2)) 8 | whitenoise = a * np.random.rand(int(fs * sec)) * 2 - 1 9 | return whitenoise 10 | -------------------------------------------------------------------------------- /dsugawara/chapter01/08.py: -------------------------------------------------------------------------------- 1 | # SN 比を任意の値に設定できるようにホワイトノイズの振幅を調整する関数を実装せよ. 2 | 3 | import numpy as np 4 | 5 | 6 | def make_noise(signal, is_SNR): 7 | noise = np.random.randn(sf, 1) 8 | 9 | noise = noise / np.sqrt(np.sum(noise**2)) 10 | noise = noise * np.sqrt(np.sum(signal**2)) 11 | noise = noise * 10 ** (-1 * is_SNR / 20) 12 | 13 | return noise 14 | -------------------------------------------------------------------------------- /knishida/chapter01/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from scipy.io.wavfile import write 3 | 4 | A = 1 5 | f = 440.0 6 | sec = 3.0 7 | fs = 16000 8 | 9 | t = np.arange(0, sec, 1 / fs) 10 | y = A * np.sin(2 * np.pi * f * t) 11 | 12 | writefilename = "02_out.wav" 13 | 14 | y = y * 100 15 | 16 | wav = y.astype(np.int16) 17 | print(wav) 18 | write(writefilename, fs, wav) 19 | -------------------------------------------------------------------------------- /knishida/chapter01/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from scipy.io.wavfile import write, read 3 | from scipy import signal 4 | 5 | readfilename = "02_out.wav" 6 | writefilename = "03_out.wav" 7 | 8 | fs, data = read(readfilename) 9 | 10 | data_8 = signal.decimate(data, 2) 11 | fs_8 = 8000 12 | 13 | wav = data_8.astype(np.int16) 14 | write(writefilename, fs_8, wav) 15 | -------------------------------------------------------------------------------- /muesaka/chapter04/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def padding(L, S, x): 5 | zeros = np.zeros(L - S, dtype=float) 6 | x_pad = np.concatenate((zeros, x, zeros)) 7 | x_pad_mod = len(x_pad) % S 8 | if x_pad_mod != 0: 9 | zeros = np.zeros(x_pad_mod, dtype=float) 10 | x_pad = np.concatenate((x_pad, zeros)) 11 | 12 | return x_pad 13 | -------------------------------------------------------------------------------- /kkazama/chapter01/01.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | A = 1 #振幅 6 | f = 440 #周波数 7 | sec = 3 #長さ 8 | fs = 16000 #サンプリング周波数 9 | 10 | t = np.arange(0, sec, 1/fs) #時間インデックス 11 | 12 | xt = A * np.sin(2 * np.pi * f * t) #正弦波 13 | 14 | plt.xlim(0, 0.03) #軸範囲 15 | plt.plot(t, xt) 16 | plt.show() 17 | -------------------------------------------------------------------------------- /tkawamura2/chapter04/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def syn_win(win, S): 5 | L = win.size 6 | Q = int(np.floor(L / S)) 7 | swin = np.empty(L, dtype=np.float64) 8 | for i in range(L): 9 | k = i - (Q - 1) * S 10 | if k < 0: 11 | k = 0 12 | swin[i] = win[i] / np.sum(win[k: i + (Q - 1) * S]**2) 13 | return swin 14 | -------------------------------------------------------------------------------- /ttakahashi/chapter01/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import soundfile as sf 3 | 4 | A = 1 # 振幅 5 | f = 440 # 周波数 Hz 6 | sec = 3.0 # 信号の長さ s 7 | sr = 16000 # サンプリング周波数 Hz 8 | 9 | t = np.arange(sec * sr) / sr # サンプリング点の生成 10 | y = A * np.sin(2 * np.pi * f * t) # 正弦波の生成 11 | 12 | sf.write("outputs/02.wav", y, sr, format="WAV", subtype="PCM_16") 13 | 14 | print("success!") 15 | -------------------------------------------------------------------------------- /ttakahashi/chapter01/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | sec = 3.0 # 信号の長さ s 5 | sr = 16000 # サンプリング周波数 Hz 6 | 7 | t = np.arange(sec * sr) / sr # サンプリング点の生成 8 | x = np.random.rand(round(sec * sr)) # ホワイトノイズの生成 9 | 10 | plt.xlim(0, 0.03) 11 | plt.plot(t, x) 12 | plt.savefig("outputs/05.pdf") 13 | plt.show() 14 | 15 | print("success!") 16 | -------------------------------------------------------------------------------- /dsugawara/chapter01/01.py: -------------------------------------------------------------------------------- 1 | # 振幅 1, 周波数 440 Hz の正弦波をサンプリング周波数 16kHz で 3 秒分作成しプロットせよ. 2 | 3 | import numpy as np 4 | from matplotlib import pyplot as plt 5 | 6 | 7 | A = 1.0 8 | f = 440.0 9 | sf = 16000 10 | sec = 3.0 11 | 12 | t = np.arange(0, sec, 1 / sf) 13 | 14 | y = A * np.sin(2 * np.pi * f * t) 15 | 16 | plt.xlim(0, 0.03) 17 | plt.plot(t, y) 18 | plt.savefig("01py_sin.png") 19 | -------------------------------------------------------------------------------- /smotoyama/chapter01/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | if __name__ == "__main__": 6 | A = 1 7 | f = 440 8 | fs = 16000 9 | s = 3 10 | 11 | t = np.arange(0, s, 1 / fs) 12 | x = A * np.sin(2 * np.pi * f * t) 13 | n = np.random.rand(round(fs * s)) 14 | 15 | y = x + n 16 | 17 | plt.plot(t, y) 18 | plt.show() 19 | -------------------------------------------------------------------------------- /ykuriki/chapter01/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | # 関数 4 | def SNR(s, x): 5 | """s[n]とx[n]の信号対雑音比(SN比)を計算する 6 | 7 | Args: 8 | s (ndarray): 信号 (float型1次元配列) 9 | x (ndarray): ノイズ (float型1次元配列) 10 | 11 | Returns: 12 | snr (float): 信号対雑音比 (SN比) 13 | 14 | """ 15 | snr = 10 * np.log10(np.sum(s @ s) / np.sum(x @ x)) 16 | 17 | return snr 18 | -------------------------------------------------------------------------------- /ynakamura/chapter01/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import wavio 3 | 4 | 5 | fs = 16000 6 | sec = 3 7 | fin = 660 8 | t = np.linspace(0.0, sec, int(fs * sec)) 9 | 10 | y = np.sin(2.0 * np.pi * fin * t) 11 | 12 | fin = 440 13 | 14 | x = np.sin(2.0 * np.pi * fin * t) 15 | 16 | mix = np.array([x, y]).T 17 | 18 | wavio.write("sin_fin440_fin_660_fs16k.wav", mix[:, :], fs, sampwidth=2) 19 | -------------------------------------------------------------------------------- /dsugawara/chapter01/07.py: -------------------------------------------------------------------------------- 1 | # 信号長の等しい 2 個の信号 の信号対雑音比(SN比) を計算する関数を実装せよ. 2 | 3 | import numpy as np 4 | 5 | 6 | def culclation_SNR(s, x, n): 7 | i = 0 8 | s2 = 0 # s[n]の2乗 9 | x2 = 0 # x[n]の2乗 10 | 11 | while i < n: 12 | s2 = s2 + s[n] ** 2 13 | x2 = x2 + x[n] ** 2 14 | i += 1 15 | 16 | SNR = 10 * np.log10(s2 / x2) 17 | 18 | return SNR 19 | -------------------------------------------------------------------------------- /tkawamura2/chapter05/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def spa_corr(X): 5 | X_ = X.transpose(1, 0, 2) 6 | X_conj = np.conj(X_.transpose(0, 2, 1)) 7 | return np.einsum("nft, ntk -> nfk", X_, X_conj) 8 | 9 | 10 | X = np.array([[[1, -1j, -1, 1j], [2, -2j, -2, 2j], [3, -3j, -3, 3j]], 11 | [[4, -2j, 1, 0], [2, -1j, 0, 0], [1, -1j, 1, 0]]]) 12 | print(spa_corr(X)) 13 | -------------------------------------------------------------------------------- /ynakamura/chapter02/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def hamming(N): 5 | n = np.arange(N) 6 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 7 | return w 8 | 9 | 10 | fs = 16000 11 | sec = 3 12 | fin = 440 13 | t = np.arange(sec * fs) / fs 14 | 15 | x = np.sin(2.0 * np.pi * fin * t) 16 | 17 | w = hamming(x.shape[0]) 18 | 19 | W = np.fft.fft(w) 20 | 21 | print(W) 22 | -------------------------------------------------------------------------------- /kkazama/chapter03/08.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | 4 | 5 | def freq_res(a, b, f, fs): 6 | N = len(a) 7 | M = len(b) 8 | omega = (2 * np.pi * f) / fs 9 | 10 | sum_a = np.sum(a * np.exp(-1j * omega * np.arange(1, N))) 11 | sum_b = np.sum(b * np.exp(-1j * omega * np.arange(M))) 12 | 13 | r = sum_b / (1 + sum_a) 14 | 15 | return r 16 | -------------------------------------------------------------------------------- /ykuriki/chapter01/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | # パラメータ 6 | fs = 16000 7 | f = 440 8 | r = 1 9 | time = 3 10 | 11 | t = np.arange(0, fs * time) / fs 12 | sig = r * np.sin(f * 2 * np.pi * t) 13 | 14 | # プロット 15 | plt.plot(t, sig) 16 | plt.xlim([0, 0.03]) 17 | plt.xlabel("time [s]") 18 | plt.ylabel("amplitude") 19 | plt.grid() 20 | 21 | plt.show() 22 | -------------------------------------------------------------------------------- /knishida/chapter03/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def circular_convolution(x, h): 5 | N = len(x) 6 | z = np.zeros(N) 7 | for n in range(N): 8 | for k in range(N): 9 | z[n] += x[k] * h[(n - k) % N] 10 | return z 11 | 12 | 13 | if __name__ == "__main__": 14 | x = [4, 3, 2, 1] 15 | y = [1, 0, -1, 0] 16 | 17 | print(circular_convolution(x, y)) 18 | -------------------------------------------------------------------------------- /tkawamura2/chapter02/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | a = 1 # 振幅 5 | fs = 16000 # サンプリング周波数 6 | f = 440 # 周波数 7 | sec = 1 # 秒 3→1 8 | 9 | x = np.arange(sec * fs) / fs 10 | y = np.sin(2 * x * np.pi * f) 11 | 12 | Y = np.fft.fft(y) 13 | 14 | plt.plot(x, 20 * np.log10(np.abs(Y))) 15 | plt.show() 16 | 17 | plt.plot(x, 20 * np.log10(np.angle(Y))) 18 | plt.show() 19 | -------------------------------------------------------------------------------- /ykuriki/chapter01/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | # パラメータ 6 | fs = 16000 7 | time = 3 8 | 9 | np.random.seed(2286) 10 | 11 | t = np.arange(0, fs * time) / fs 12 | noise = np.random.rand(fs * time) 13 | 14 | # プロット 15 | plt.plot(t, noise) 16 | plt.xlim([0, 0.03]) 17 | plt.xlabel("time [s]") 18 | plt.ylabel("amplitude") 19 | plt.grid() 20 | 21 | plt.show() 22 | -------------------------------------------------------------------------------- /knishida/chapter03/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | if __name__ == "__main__": 5 | N = 10 6 | x = np.zeros(N) 7 | x[0] = 1 8 | y = np.zeros(N) 9 | 10 | for n in range(N): 11 | if n == 0: 12 | y[n] = 0.4 * x[n] 13 | else: 14 | y[n] = 0.3 * y[n - 1] + 0.4 * x[n] 15 | 16 | plt.stem(y) 17 | plt.show() 18 | -------------------------------------------------------------------------------- /ynakamura/chapter03/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def zeropad_circular_conv(x, h): 5 | x = np.pad(x, (0, x.shape[0] - 1)) 6 | h = np.pad(h, (0, h.shape[0] - 1)) 7 | N = x.shape[0] 8 | z = np.array([]) 9 | for n in range(N): 10 | sum = 0 11 | for k in range(N - 1): 12 | sum += x[k] * h[(n - k) % N] 13 | z = np.append(z, sum) 14 | return z 15 | -------------------------------------------------------------------------------- /kkazama/chapter02/09.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | 6 | def hamm(N): 7 | w = np.zeros(N) 8 | for n in range(N): 9 | w[n] = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 10 | return w 11 | 12 | 13 | # 窓関数 14 | ham = hamm(48000) 15 | 16 | ham_dft = np.fft.fft(ham) 17 | 18 | plt.plot(ham_dft) 19 | plt.show() 20 | -------------------------------------------------------------------------------- /knishida/chapter01/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import soundfile as sf 3 | 4 | A = 1 5 | f = 440.0 6 | f_2 = 660.0 7 | sec = 3.0 8 | fs = 16000 9 | writefilename = "04_out.wav" 10 | 11 | t = np.arange(0, sec, 1 / fs) 12 | y = A * np.sin(2 * np.pi * f * t) 13 | y_2 = A * np.sin(2 * np.pi * f_2 * t) 14 | 15 | 16 | wav = np.array([y, y_2]) 17 | wav = wav.T 18 | 19 | sf.write(writefilename, wav, fs, "PCM_16") 20 | -------------------------------------------------------------------------------- /knishida/chapter03/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def frequency_responce(a, b, f, fs): 5 | N = len(a) 6 | M = len(b) 7 | tmpa = 0 8 | tmpb = 0 9 | omega = 2 * np.pi * f / fs 10 | for k in range(1, N): 11 | tmpa += a[k] * np.exp(-1j * omega * k) 12 | for k in range(M): 13 | tmpb += b[k] * np.exp(-1j * omega * k) 14 | H = tmpb / (1 + tmpa) 15 | return H 16 | -------------------------------------------------------------------------------- /smotoyama/chapter03/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def eq(a, b, f, fs): 5 | N = len(a) 6 | M = len(b) 7 | omega = 2 * np.pi * f / fs 8 | a_sum = 1 9 | b_sum = 0 10 | for i in range(1, N): 11 | a_sum += a[i] * np.exp(-1j * omega * i) 12 | for k in range(M): 13 | b_sum += b[k] * np.exp(-1j * omega * k) 14 | 15 | H = b_sum / a_sum 16 | 17 | return H 18 | -------------------------------------------------------------------------------- /ttakahashi/chapter01/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | A = 1 # 振幅 5 | f = 440 # 周波数 Hz 6 | sec = 3.0 # 信号の長さ s 7 | sr = 16000 # サンプリング周波数 Hz 8 | 9 | t = np.arange(sec * sr) / sr # サンプリング点の生成 10 | y = A * np.sin(2 * np.pi * f * t) # 正弦波の生成 11 | 12 | plt.xlim(0, 0.03) 13 | plt.plot(t, y) 14 | plt.savefig("outputs/01.pdf") 15 | plt.show() 16 | 17 | print("success!") 18 | -------------------------------------------------------------------------------- /ynakamura/chapter03/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def freq_response(a, b, fs, f): 5 | w = 2 * np.pi * f / fs 6 | W = np.exp(1j * w) 7 | 8 | sum1 = 0 9 | for k in range(1, a.shape[0]): 10 | sum1 += a[k] * np.exp(W * -k) 11 | 12 | sum2 = 0 13 | for k in range(b.shape[0]): 14 | sum2 += b[k] * np.exp(W * -k) 15 | 16 | H = sum2 / (1 + sum1) 17 | return H 18 | -------------------------------------------------------------------------------- /kkazama/chapter01/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | A = 1 #振幅 5 | f = 440 6 | sec = 3 #信号の長さ 7 | fs = 16000 #サンプリング周波数 8 | 9 | x = 2 * A * (np.random.rand(round(fs*sec)) - 0.5) #ホワイトノイズの生成 10 | 11 | t = np.arange(0, sec, 1/fs) #時間インデックス 12 | 13 | s = A * np.sin(2 * np.pi * f * t) #正弦波 14 | 15 | plt.plot(t, s) 16 | plt.plot(t, x) 17 | plt.xlim(0, 0.03) #軸範囲 18 | plt.show() 19 | -------------------------------------------------------------------------------- /muesaka/chapter04/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def synthetic_window(S, w): 5 | L = len(w) 6 | Q = int(L / S) 7 | w_s = np.empty(L, dtype=float) 8 | for i in range(0, L - 1): 9 | w_s[i] = 0 10 | for m in range(-(Q - 1), Q - 1): 11 | if i - m * S <= L - 1: 12 | w_s[i] += w[i - m * S] ** 2 13 | w_s[i] = w[i] / w_s[i] 14 | 15 | return w_s 16 | -------------------------------------------------------------------------------- /ykuriki/chapter03/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def circul_conv(x, h): 6 | """x[n]とh[n]の巡回たたみこみを計算する(周期的) 7 | 8 | Args: 9 | x (ndarray): 信号(1次元配列) 10 | h (ndarray): インパルス応答(1次元配列) 11 | 12 | Returns: 13 | y (ndarray): 畳み込み結果(1次元配列) 14 | 15 | """ 16 | X = np.fft.fft(x) 17 | H = np.fft.fft(h) 18 | y = np.fft.ifft(X * H) 19 | 20 | return y 21 | -------------------------------------------------------------------------------- /ynakamura/chapter03/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def linear_conv(x, h): 5 | N = x.shape[0] 6 | z = np.array([]) 7 | for n in range(2 * N - 1): 8 | sum = 0 9 | for k in range(N): 10 | if n - k >= 0 and n - k <= N - 1: 11 | sum += x[k] * h[n - k] 12 | else: 13 | sum += 0 14 | z = np.append(z, sum) 15 | return z 16 | -------------------------------------------------------------------------------- /tkawamura2/chapter03/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def mov_ave(x, M): 6 | N = x.size 7 | y = np.zeros(N) 8 | for n in range(N): 9 | for i in range(M): 10 | if n-i >= 0: 11 | y[n] = y[n] + x[n-i]/M 12 | return y 13 | 14 | 15 | x = np.zeros(10) 16 | x[0] = 1 17 | 18 | y = mov_ave(x, 5) 19 | 20 | plt.stem(y) 21 | plt.show() 22 | -------------------------------------------------------------------------------- /dsugawara/chapter01/02.py: -------------------------------------------------------------------------------- 1 | # 1.で作成した正弦波を 16bit PCM フォーマットで wav ファイルとして保存せよ. 2 | 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | import soundfile 6 | 7 | 8 | A = 1.0 9 | f = 440.0 10 | sf = 16000 11 | sec = 3.0 12 | 13 | t = np.arange(0, sec, 1 / sf) 14 | 15 | y = A * np.sin(2 * np.pi * f * t) 16 | 17 | soundfile.write( 18 | file="02py_sin.wav", data=y, samplerate=sf, format="WAV", subtype="PCM_16" 19 | ) 20 | -------------------------------------------------------------------------------- /ynakamura/chapter03/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def dif_equation(x, a, b): 5 | L = x.shape[0] 6 | y = np.zeros((L)) 7 | for n in range(L): 8 | sum1 = 0 9 | sum2 = 0 10 | for k in range(1, a.shape[0]): 11 | sum1 += a[k] * y[n - k] 12 | for k in range(b.shape[0]): 13 | sum2 += b[k] * x[n - k] 14 | y[n] = 1 / a[0] * (-sum1 + sum2) 15 | return y 16 | -------------------------------------------------------------------------------- /muesaka/chapter03/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def calculate_frequency_responce(a, b, f, fs): 5 | N = len(a) 6 | M = len(b) 7 | tmp_a = 0 8 | tmp_b = 0 9 | omega = 2 * np.pi * f / fs 10 | 11 | for k in range(0, M): 12 | tmp_b += b[k] * np.exp(-1j * omega * k) 13 | for k in range(1, N): 14 | tmp_a += a[k] * np.exp(-1j * omega * k) 15 | H = tmp_b / (1 + tmp_a) 16 | 17 | return H 18 | -------------------------------------------------------------------------------- /tkawamura2/chapter05/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import librosa 3 | 4 | 5 | def spa_corr(X): 6 | X_ = X.transpose(1, 0, 2) 7 | X_conj = np.conj(X_.transpose(0, 2, 1)) 8 | return np.einsum("nft, ntk -> nfk", X_, X_conj) 9 | 10 | 11 | white_noise = np.random.randn(2, 16000 * 5) 12 | print(white_noise.shape) 13 | WN = librosa.stft(white_noise, n_fft=512, hop_length=256, window="hann") 14 | R = spa_corr(WN) 15 | print(R[100]) 16 | -------------------------------------------------------------------------------- /ttakahashi/chapter02/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # Function to calculate DFT 5 | def dft(x): 6 | N = len(x) 7 | n = np.arange(N) 8 | X = np.zeros(N, dtype="complex64") 9 | for k in range(N): 10 | X[k] = np.sum(x[n] * np.exp(-2j * np.pi * k * n / N)) 11 | return X 12 | 13 | 14 | delta = np.array([1, 0, 0, 0, 0, 0, 0, 0]) 15 | delta_dft = dft(delta) 16 | print(delta_dft) 17 | 18 | print("success!") 19 | -------------------------------------------------------------------------------- /ynakamura/chapter02/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def hamming(N): 6 | n = np.arange(N) 7 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 8 | return w 9 | 10 | 11 | fs = 16000 12 | sec = 3 13 | fin = 440 14 | t = np.arange(sec * fs) / fs 15 | 16 | x = np.sin(2.0 * np.pi * fin * t) 17 | 18 | w = hamming(x.shape[0]) 19 | x_ham = x * w 20 | 21 | plt.plot(t, x_ham) 22 | plt.show() 23 | -------------------------------------------------------------------------------- /muesaka/chapter03/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | if __name__ == "__main__": 6 | x = np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]) 7 | N = len(x) 8 | y = np.zeros(N) 9 | 10 | for n in range(N): 11 | for k in range(5): 12 | if (k <= n) and (n - k <= 4): 13 | y[n] = 0.2 * x[n - k] 14 | 15 | plt.stem(y) 16 | plt.tight_layout() 17 | plt.show() 18 | -------------------------------------------------------------------------------- /muesaka/chapter03/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | if __name__ == "__main__": 5 | x = np.array([1, 0, 0, 0, 0, 0, 0, 0, 0, 0]) 6 | N = len(x) 7 | y = np.zeros(N) 8 | 9 | for n in range(N): 10 | if n == 0: 11 | y[n] = 0.4 * x[n] 12 | else: 13 | y[n] = 0.3 * y[n - 1] + 0.4 * x[n] 14 | 15 | plt.stem(y) 16 | plt.tight_layout() 17 | plt.show() 18 | -------------------------------------------------------------------------------- /tkawamura2/chapter01/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from scipy.io.wavfile import read, write 4 | 5 | sec = 3.0 # 信号の長さ s 6 | fs = 16000 # サンプリング周波数 Hz 7 | 8 | t = np.arange(0, sec, 1/fs) 9 | white = np.random.randn(round(fs*sec)) 10 | fs, si = read("660_440.wav") 11 | y_1 = white + si[:,0] 12 | y_2 = white + si[:,1] 13 | 14 | plt.plot(t, y_1) 15 | plt.plot(t, y_2) 16 | plt.xlim([0, 0.03]) 17 | plt.show() 18 | -------------------------------------------------------------------------------- /ynakamura/chapter02/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | fs = 16000 6 | sec = 3 7 | fin = 440 8 | t = np.arange(sec * fs) / fs 9 | 10 | x = np.sin(2.0 * np.pi * fin * t) 11 | 12 | X = np.fft.fft(x) 13 | 14 | # 振幅スペクトルの出力 15 | plt.stem(20 * np.log10(np.abs(X))) 16 | plt.title("amp") 17 | plt.show() 18 | 19 | # 位相スペクトルの出力 20 | plt.stem(20 * np.log10(np.angle(X))) 21 | plt.title("angle") 22 | plt.show() 23 | -------------------------------------------------------------------------------- /tkawamura2/chapter02/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def hamming(N): 6 | w = 0.54 - 0.46 * np.cos(2 * np.pi * np.arange(N) / (N - 1)) 7 | return w 8 | 9 | 10 | a = 1 # 振幅 11 | fs = 16000 # サンプリング周波数 12 | f = 440 # 周波数 13 | sec = 1 # 秒 3→1 14 | 15 | x = np.arange(sec * fs) / fs 16 | y = np.sin(2 * x * np.pi * f) 17 | 18 | y_win = y*hamming(len(y)) 19 | 20 | plt.plot(x, y_win) 21 | plt.show() 22 | -------------------------------------------------------------------------------- /muesaka/chapter02/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def dft(x): 5 | N = len(x) 6 | n = np.arange(N) 7 | dft_x = np.zeros(N, dtype=complex) 8 | for k in range(N): 9 | dft_x[k] = np.sum(x[n] * np.exp(-1j * 2 * np.pi * k * n / N)) 10 | return dft_x 11 | 12 | 13 | if __name__ == "__main__": 14 | delta = np.array([1, 0, 0, 0, 0, 0, 0, 0]) # インパルス信号 15 | 16 | dft_delta = dft(delta) 17 | 18 | print(dft_delta) 19 | -------------------------------------------------------------------------------- /tkawamura2/chapter03/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def dif_equ(x, a, b): 5 | L = x.size 6 | N = a.size 7 | M = b.size 8 | y = np.zeros(L) 9 | 10 | for n in range(L): 11 | for k1 in np.arange(1, N): 12 | if n-k1 >= 0: 13 | y[n] = y[n] - a[k1]*y[n-k1] 14 | for k2 in range(M): 15 | if n-k2 >= 0: 16 | y[n] = y[n] + b[k2]*x[n-k2] 17 | 18 | return y/a[0] 19 | -------------------------------------------------------------------------------- /ttakahashi/chapter01/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import soundfile as sf 3 | 4 | A = 1 # 振幅 5 | f = 660 # 周波数 Hz 6 | sec = 3.0 # 信号の長さ s 7 | sr = 16000 # サンプリング周波数 Hz 8 | 9 | t = np.arange(sec * sr) / sr # サンプリング点の生成 10 | y1 = A * np.sin(2 * np.pi * f * t) # 正弦波の生成 11 | 12 | y2, sr = sf.read("outputs/02.wav") 13 | s = np.vstack((y1, y2)).T 14 | 15 | sf.write("outputs/04.wav", s, sr, format="WAV", subtype="PCM_16") 16 | 17 | print("success!") 18 | -------------------------------------------------------------------------------- /ykuriki/chapter02/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def ham_wnd(N): 6 | """ハミング窓を生成する 7 | 8 | Args: 9 | N (int): 窓の長さ 10 | 11 | Returns: 12 | w (ndarray): ハミング窓(float型1次元配列) 13 | 14 | """ 15 | n = np.arange(N) 16 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 17 | 18 | return w 19 | 20 | 21 | fs = 16000 22 | sec = 3 23 | 24 | wnd = ham_wnd(fs * sec) 25 | 26 | fft_wnd = np.fft.fft(wnd) 27 | -------------------------------------------------------------------------------- /ynakamura/chapter05/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def calc_linear_vector(d, M, thete, f): 5 | c = 334 6 | 7 | u = np.array([np.sin(thete), np.cos(thete), 0]) 8 | a = np.zeros((M)).astype(complex) 9 | for m in range(M): 10 | pm = np.array([((m - 1) - (M - 1) / 2) * d, 0, 0]).T 11 | a[m] = np.exp(1j * 2 * np.pi * f / c * u @ pm) 12 | 13 | return a 14 | 15 | 16 | print(calc_linear_vector(0.05, 3, 45, 1000)) 17 | -------------------------------------------------------------------------------- /kkazama/chapter03/06.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | 6 | def f(x): 7 | y = np.zeros(len(x)) 8 | for k in range(len(x)): 9 | if k == 0: 10 | y[k] = 0.4 * x[k] 11 | else: 12 | y[k] = 0.3 * y[k - 1] + 0.4 * x[k] 13 | return y 14 | 15 | 16 | x = np.zeros(10) 17 | x[0] = 1 18 | y = f(x) 19 | 20 | plt.stem(y) 21 | 22 | plt.show() 23 | -------------------------------------------------------------------------------- /kkazama/chapter04/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | def zero_pad(x, L, S): 4 | x = np.pad(x, L-S, L-S) 5 | a = len(x) % S 6 | if a != 0: 7 | x = np.pad(x, (0, S-a)) 8 | 9 | return x 10 | 11 | def sep_frame(x, L, S): 12 | x = zero_pad(x, L, S) 13 | T = len(x) / S 14 | xt = np.zeros((T, L)) 15 | for t in range(T): 16 | for l in range(L): 17 | xt[t] = x[t * S + l] 18 | 19 | return xt 20 | 21 | -------------------------------------------------------------------------------- /knishida/chapter03/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def linear_convolution(x, h): 5 | K = len(x) 6 | z = np.zeros(2 * K - 2) 7 | for n in range(2 * K - 2): 8 | for k in range(K): 9 | if n - k >= 0 and n - k <= K - 1: 10 | z[n] += x[k] * h[n - k] 11 | return z 12 | 13 | 14 | if __name__ == "__main__": 15 | x = [4, 3, 2, 1] 16 | y = [1, 0, -1, 0] 17 | 18 | print(linear_convolution(x, y)) 19 | -------------------------------------------------------------------------------- /smotoyama/chapter03/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def eq(x): 6 | N = len(x) 7 | y = np.zeros(N) 8 | for i in range(N): 9 | if i - 1 < 0: 10 | y[i] = 0.4 * x[i] 11 | else: 12 | y[i] = 0.3 * y[i - 1] + 0.4 * x[i] 13 | return y 14 | 15 | 16 | if __name__ == "__main__": 17 | x = np.zeros(10) 18 | x[0] = 1 19 | 20 | plt.stem(eq(x)) 21 | plt.show() 22 | -------------------------------------------------------------------------------- /ttakahashi/chapter01/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import soundfile as sf 4 | 5 | sec = 3.0 # 信号の長さ s 6 | sr = 16000 # サンプリング周波数 Hz 7 | 8 | t = np.arange(sec * sr) / sr # サンプリング点の生成 9 | x = np.random.rand(round(sec * sr)) # ホワイトノイズの生成 10 | 11 | y, sr = sf.read("outputs/02.wav") 12 | s = x + y 13 | 14 | plt.xlim(0, 0.03) 15 | plt.plot(t, s) 16 | plt.savefig("outputs/06.pdf") 17 | plt.show() 18 | 19 | print("success!") 20 | -------------------------------------------------------------------------------- /ykuriki/chapter01/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import wave 3 | 4 | 5 | # パラメータ 6 | fs = 16000 7 | f = 440 8 | r = 1 9 | time = 3 10 | 11 | t = np.arange(0, fs * time) / fs 12 | sig = r * np.sin(f * 2 * np.pi * t) 13 | 14 | # 音声ファイル出力 15 | output = (sig * np.iinfo(np.int16).max).astype(np.int16) 16 | wav = wave.open("./02.wav", "wb") 17 | wav.setnchannels(1) 18 | wav.setsampwidth(2) 19 | wav.setframerate(fs) 20 | wav.writeframes(output) 21 | wav.close() 22 | -------------------------------------------------------------------------------- /dsugawara/chapter01/06.py: -------------------------------------------------------------------------------- 1 | # 1.で作成した正弦波と 4.で作成したホワイトノイズと正弦波を混合してプロットせよ. 2 | 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | import soundfile 6 | 7 | 8 | y, y_samplerate = soundfile.read("02py_sin.wav") 9 | white_noise, white_noise_samplerate = soundfile.read("05py_whitenoise.wav") 10 | 11 | t = np.arange(0, 3, 1 / y_samplerate) 12 | 13 | y_mixed = y + white_noise 14 | plt.xlim(0, 0.03) 15 | plt.plot(t, y_mixed) 16 | plt.savefig("06py_mixed.png") 17 | -------------------------------------------------------------------------------- /kkazama/chapter01/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | #SNR計算 4 | def cal_sn(s, x): 5 | snr = 10 * np.log10(np.sum(s * s) / np.sum(x * x)) 6 | 7 | return snr 8 | 9 | #確認用 10 | A = 1 #振幅 11 | f = 440 12 | sec = 3 #信号の長さ 13 | fs = 16000 #サンプリング周波数 14 | 15 | x = 2 * A * (np.random.rand(round(fs*sec)) - 0.5) #ホワイトノイズの生成 16 | 17 | t = np.arange(0, sec, 1/fs) #時間インデックス 18 | 19 | s = A * np.sin(2 * np.pi * f * t) #正弦波 20 | 21 | print(cal_sn(s, x)) 22 | 23 | -------------------------------------------------------------------------------- /kkazama/chapter03/05.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | 6 | def f(x): 7 | y = np.zeros(len(x)) 8 | for n in range(len(x)): 9 | for k in range(5): 10 | m = n - k 11 | if m >= 0: 12 | y[n] += 0.2 * x[m] 13 | 14 | return y 15 | 16 | 17 | x = np.zeros(10) 18 | x[0] = 1 19 | y = f(x) 20 | 21 | plt.stem(y) 22 | 23 | plt.show() 24 | -------------------------------------------------------------------------------- /knishida/chapter02/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def DFT(x): 5 | N = len(x) 6 | n = np.arange(N) 7 | X = np.zeros(N) 8 | for k in range(N): 9 | X[k] = np.sum(x * np.exp(-1j * 2 * np.pi * k * n / N)) 10 | return X 11 | 12 | 13 | def IDFT(X): 14 | N = len(X) 15 | k = np.arange(N) 16 | x = np.zeros(N) 17 | for n in range(N): 18 | x[n] = np.sum(X * np.exp(1j * 2 * np.pi * k * n / N)) / N 19 | return x 20 | -------------------------------------------------------------------------------- /kyamada/Chapter03/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def y_recursive(x): 6 | y = np.zeros_like(x) 7 | y[0] = 0.4 * x[0] 8 | for i in range(1, x.size): 9 | y[i] = 0.3 * y[i-1] + 0.4 * x[i] 10 | return y 11 | 12 | 13 | if __name__ == "__main__": 14 | x = np.zeros(10) 15 | x[0] = 1 16 | y = y_recursive(x) 17 | print(y) 18 | 19 | plt.stem(y) 20 | plt.grid() 21 | plt.show() 22 | -------------------------------------------------------------------------------- /kyamada/chapter01/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def create_white_noise( 6 | fs=16000, 7 | time=3 8 | ): 9 | """ 10 | Create a white noise 11 | """ 12 | t = np.arange(0, time, 1/fs) 13 | x = np.random.randn(len(t)) 14 | return t, x 15 | 16 | 17 | if __name__ == '__main__': 18 | np.random.seed(0) # シード値 19 | t, x = create_white_noise() 20 | plt.plot(t, x) 21 | plt.show() 22 | -------------------------------------------------------------------------------- /ykuriki/chapter03/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | length = 10 6 | x = np.array([1]) 7 | a = 0.3 8 | b = 0.4 9 | y = np.zeros(length) 10 | 11 | # n = 0 12 | y[0] += b * x[0] 13 | 14 | # n > 0 15 | for n in range(1, length): 16 | y[n] += a * y[n - 1] 17 | 18 | 19 | # プロット 20 | plt.stem(np.arange(length), y) 21 | plt.xlabel("n") 22 | plt.ylabel("amplitude") 23 | plt.grid() 24 | plt.tight_layout() 25 | 26 | plt.show() 27 | -------------------------------------------------------------------------------- /ykuriki/chapter04/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def zero_pad(x, L, S): 6 | """x[n]に零詰めする 7 | 8 | Args: 9 | x (ndarray): 信号(1次元配列) 10 | L (int): 窓幅 11 | S (int): シフト幅 12 | 13 | Returns: 14 | ndarray: 零詰めされた信号 15 | 16 | """ 17 | x = np.block([np.zeros(L - S), x, np.zeros(L - S)]) 18 | 19 | if len(x) % S != 0: 20 | x = np.block([x, np.zeros(S - (len(x) % S))]) 21 | 22 | return x 23 | -------------------------------------------------------------------------------- /knishida/chapter03/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | if __name__ == "__main__": 5 | N = 10 6 | x = np.zeros(N + 4) 7 | x[4] = 1 8 | y = np.zeros(N) 9 | for n in range(N): 10 | y[n] = ( 11 | 0.2 * x[n + 4] 12 | + 0.2 * x[n + 3] 13 | + 0.2 * x[n + 2] 14 | + 0.2 * x[n + 1] 15 | + 0.2 * x[n] 16 | ) 17 | 18 | plt.stem(y) 19 | plt.show() 20 | -------------------------------------------------------------------------------- /kkazama/chapter03/02.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | # 巡回畳み込み 6 | def circular_conv(x, h): 7 | N = len(x) # 信号の長さ 8 | y = np.zeros(N) 9 | 10 | for n in range(len(y)): 11 | y[n] = 0 # 初期化 12 | for k in range(len(x)): 13 | m = n - k 14 | if (m >= 0) and (m <= N - 1): 15 | y[n] += x[k] * h[m % N] 16 | 17 | return y 18 | 19 | -------------------------------------------------------------------------------- /tkawamura2/chapter02/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def dft(x): 5 | N = len(x) 6 | X = np.empty(N, dtype=np.complex128) 7 | for k in range(N): 8 | X[k] = np.sum(x * np.exp(-2j * np.pi * k * np.arange(N) / N)) 9 | return X 10 | 11 | 12 | def idft(X): 13 | N = len(X) 14 | x = np.empty(N, dtype=np.float64) 15 | for n in range(N): 16 | x[n] = np.sum(X * np.exp(2j * np.pi * n * np.arange(N) / len(x))) / N 17 | return X 18 | -------------------------------------------------------------------------------- /dsugawara/chapter01/05.py: -------------------------------------------------------------------------------- 1 | # ホワイトノイズをサンプリング周波数 16kHz で 3 秒分作成しプロットせよ. 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import soundfile 5 | 6 | 7 | A = 1.0 8 | sec = 3.0 9 | sf = 16000 10 | t = np.arange(0, 3, 1 / sf) 11 | 12 | white_noise = A * np.random.rand(round(sf * sec)) 13 | 14 | soundfile.write(file="05py_whitenoise.wav", data=white_noise, samplerate=sf) 15 | 16 | plt.plot(t, white_noise) 17 | plt.xlim(0, 0.03) 18 | plt.savefig("05py_whitenoise.png") 19 | -------------------------------------------------------------------------------- /smotoyama/chapter02/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def Hamming(N): 6 | n = np.arange(N) 7 | win = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 8 | return win 9 | 10 | 11 | if __name__ == "__main__": 12 | A = 1 13 | f = 440 14 | fs = 16000 15 | s = 3 16 | 17 | t = np.arange(0, s, 1 / fs) 18 | x = A * np.sin(2 * np.pi * f * t) 19 | 20 | win = Hamming(len(x)) 21 | 22 | Win = np.fft.fft(win) 23 | -------------------------------------------------------------------------------- /tkawamura2/chapter01/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from scipy.io.wavfile import read, write 4 | 5 | a = 1 #振幅 6 | fs = 16000 #サンプリング周波数 7 | f = 660 #周波数 8 | sec = 3 #秒 9 | 10 | x = np.arange(0, sec, 1/fs) 11 | y_1 = np.sin(2*x*np.pi*f) 12 | 13 | fs, y_2 = read("440.wav") 14 | 15 | y_1 = y_1.reshape([len(y_1), 1]) 16 | y_2 = y_2.reshape([len(y_2), 1]) 17 | 18 | y = np.concatenate([y_1, y_2], axis=1) 19 | 20 | write("660_440.wav", fs, y) 21 | -------------------------------------------------------------------------------- /tkawamura2/chapter02/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def hamming(N): 6 | w = 0.54 - 0.46 * np.cos(2 * np.pi * np.arange(N) / (N - 1)) 7 | return w 8 | 9 | 10 | a = 1 # 振幅 11 | fs = 16000 # サンプリング周波数 12 | f = 440 # 周波数 13 | sec = 1 # 秒 3→1 14 | 15 | x = np.arange(sec * fs) / fs 16 | y = np.sin(2 * x * np.pi * f) 17 | 18 | w = hamming(len(y)) 19 | W = np.fft.fft(w) 20 | 21 | plt.plot(x, 20 * np.log10(np.abs(W))) 22 | plt.show() 23 | -------------------------------------------------------------------------------- /ykuriki/chapter01/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | # パラメータ 6 | fs = 16000 7 | f = 440 8 | r = 1 9 | time = 3 10 | 11 | np.random.seed(2286) 12 | 13 | t = np.arange(0, fs * time) / fs 14 | sig = r * np.sin(f * 2 * np.pi * t) 15 | noise = np.random.rand(fs * time) 16 | x = sig + noise 17 | 18 | # プロット 19 | plt.plot(t, x) 20 | plt.xlim([0, 0.03]) 21 | plt.xlabel("time [s]") 22 | plt.ylabel("amplitude") 23 | plt.grid() 24 | 25 | plt.show() 26 | -------------------------------------------------------------------------------- /kyamada/chapter01/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def calcrate_snr(x, s): 5 | """ 6 | Calculate SNR 7 | Input: 8 | x: wave data 9 | s: noise data 10 | Output: 11 | snr: SNR 12 | """ 13 | snr = 10 * np.log10(np.sum(x ** 2)/np.sum(s ** 2)) 14 | return snr 15 | 16 | 17 | if __name__ == '__main__': 18 | x = 100 * np.sin(2 * np.pi * 400) 19 | s = 1 * np.sin(2 * np.pi * 880) 20 | snr = calcrate_snr(x, s) 21 | print(snr) 22 | -------------------------------------------------------------------------------- /ttakahashi/chapter01/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | A = 1 # 振幅 4 | f = 440 # 周波数 Hz 5 | sec = 3.0 # 信号の長さ s 6 | sr = 16000 # サンプリング周波数 Hz 7 | 8 | t = np.arange(sec * sr) / sr # サンプリング点の生成 9 | y = A * np.sin(2 * np.pi * f * t) # 正弦波の生成 10 | x = np.random.rand(round(sr)) # ホワイトノイズの生成 11 | 12 | 13 | # Function to calculate SNR 14 | def calc_snr(s, x): 15 | return 10 * np.log10(np.sum(s**2) / np.sum(x**2)) 16 | 17 | 18 | print(calc_snr(y, x)) 19 | 20 | print("success!") 21 | -------------------------------------------------------------------------------- /kkazama/chapter02/02.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | # DFT 6 | def dft(x): 7 | N = len(x) # 配列の長さ 8 | X = [] # 値代入用 9 | 10 | for n in range(N): 11 | a = -1j * 2 * np.pi * np.arange(N) * n 12 | X.append(np.sum(x[n] * np.exp(a / N))) 13 | 14 | return X 15 | 16 | 17 | # インパルス信号 18 | N = 8 19 | delta = np.zeros(N) 20 | delta[0] = 1 21 | 22 | plt.stem(dft(delta)) 23 | plt.show() 24 | -------------------------------------------------------------------------------- /kkazama/chapter03/01.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | # 線形畳み込み 6 | def conv(x, h): 7 | N = len(x) 8 | y = np.zeros(len(x) + len(h) - 1) 9 | for n in range(len(y)): # n = 0,..., 2N-2 10 | y[n] = 0 #初期化 11 | for k in range(len(x)): 12 | m = n - k 13 | if (n - k >= 0) and (n - k <= N - 1): 14 | y[n] += x[k] * h[m] 15 | 16 | return y 17 | 18 | -------------------------------------------------------------------------------- /smotoyama/chapter02/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def Hamming(N): 6 | n = np.arange(N) 7 | win = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 8 | return win 9 | 10 | 11 | if __name__ == "__main__": 12 | A = 1 13 | f = 440 14 | fs = 16000 15 | s = 3 16 | 17 | t = np.arange(0, s, 1 / fs) 18 | x = A * np.sin(2 * np.pi * f * t) 19 | 20 | win = Hamming(len(x)) 21 | 22 | plt.plot(win) 23 | plt.show() 24 | -------------------------------------------------------------------------------- /dsugawara/chapter01/10.py: -------------------------------------------------------------------------------- 1 | # 9.の信号に対して 5 点移動平均フィルタを適用した結果と元の信号をプロットせよ. 2 | 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | import soundfile 6 | 7 | x, x_samplerate = soundfile.read("09py_mixed.wav") 8 | 9 | y = np.zeros(len(x)) 10 | M = 5 11 | for i in range(M, len(y)): 12 | y[i] = np.mean(x[i - M : i]) 13 | 14 | sf = 16000 15 | sec = 1.0 16 | t = np.arange(0, sec, 1 / sf) 17 | 18 | plt.plot(t, x) 19 | plt.plot(t, y) 20 | plt.xlim(0, 0.03) 21 | plt.savefig("10py_filter.png") 22 | -------------------------------------------------------------------------------- /ttakahashi/chapter02/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # Function to calculate DFT 5 | def dft(x): 6 | N = len(x) 7 | n = np.arange(N) 8 | X = np.zeros(N, dtype="complex64") 9 | for k in range(N): 10 | X[k] = np.sum(x[n] * np.exp(-2j * np.pi * k * n / N)) 11 | return X 12 | 13 | 14 | delta = np.array([1, 0, 0, 0, 0, 0, 0, 0]) 15 | delta_dft = dft(delta) 16 | delta_fft = np.fft.fft(delta) 17 | print(np.array_equal(delta_dft, delta_fft)) 18 | 19 | print("success!") 20 | -------------------------------------------------------------------------------- /ttakahashi/chapter02/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def hamming(N): 6 | n = np.arange(N) 7 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 8 | return w 9 | 10 | 11 | sec = 3.0 # 信号の長さ s 12 | sr = 16000 # サンプリング周波数 Hz 13 | 14 | N = sec * sr 15 | w = hamming(N) 16 | 17 | plt.plot(w) 18 | plt.xlabel("time [sec]") 19 | plt.ylabel("amplitude") 20 | plt.grid() 21 | plt.savefig("outputs/07.pdf") 22 | plt.show() 23 | 24 | print("success!") 25 | -------------------------------------------------------------------------------- /dsugawara/chapter04/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def padding(L, S, x): 5 | 6 | x = np.pad(x, [L - S, L - S]) 7 | temp = S - (x.size % S) # 末尾に詰める0の個数を求める 8 | x = np.pad(x, [0, temp]) 9 | return x 10 | 11 | 12 | def frame_cut(L, S, x): 13 | N = x.size 14 | T = ((N - L) // S) + 1 # フレーム数.後で確認 15 | x_cut = np.empty([T, L]) # 出力 16 | x_pad = padding(L, S, x) 17 | for t in range(0, T): 18 | x_cut[t] = x_pad[t * S : t * S + L] 19 | return x_cut 20 | -------------------------------------------------------------------------------- /dsugawara/chapter03/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def Frequency_response(a, b, f, sf): 5 | 6 | omega = 2 * np.pi * f / sf 7 | N = a.size 8 | M = b.size 9 | sigma_a = 0j 10 | sigma_b = 0j 11 | 12 | # a[N]を含むシグマ計算 13 | for k in range(1, N): 14 | sigma_a += a[k] * np.e ** (-1j * omega * k) 15 | # b[M]を含むシグマ計算 16 | for k in range(0, M): 17 | sigma_b += b[k] * np.e ** (-1j * omega * k) 18 | 19 | H = sigma_b / (1 + sigma_a) 20 | 21 | return H 22 | -------------------------------------------------------------------------------- /tkawamura2/chapter04/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import math 3 | 4 | 5 | def pad(x, L, S): 6 | x_ = np.pad(x, [L - S, L - S]) 7 | a = np.mod(x_.size, S) 8 | if a != 0: 9 | x_ = np.pad(x_, [0, S - a]) 10 | return x_ 11 | 12 | 13 | def frame_div(x, L, S): 14 | x_ = pad(x, L, S) 15 | N_ = x_.size 16 | T = math.floor((N_ - L) / S) + 1 17 | x_t = np.empty([T, L], dtype=np.float64) 18 | for t in range(T): 19 | x_t[t] = x_[t * S: t * S + L] 20 | return x_t 21 | -------------------------------------------------------------------------------- /dsugawara/chapter02/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | # Humming窓 6 | def Humming(N): 7 | w = [] 8 | for n in range(N): 9 | a = 0.54 - 0.46 * np.cos(2*np.pi*n/(N-1)) 10 | w.append(a) 11 | return w 12 | 13 | 14 | # 第1章1. の信号 15 | A = 1.0 16 | f = 440.0 17 | sf = 16000 18 | sec = 3.0 19 | 20 | t = np.arange(0, sec, 1/sf) 21 | 22 | y = A * np.sin(2*np.pi*f*t) 23 | 24 | 25 | # 窓関数 26 | win = Humming(len(y)) 27 | 28 | dft_win = np.fft.fft(win) 29 | -------------------------------------------------------------------------------- /ykuriki/chapter03/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | length = 10 6 | x = np.array([1]) 7 | b = np.array([0.2, 0.2, 0.2, 0.2, 0.2]) 8 | y = np.zeros(length) 9 | 10 | for n in range(length): 11 | for k in range(len(b)): 12 | if k <= n and n - k < len(x): 13 | y[n] += b[k] * x[n - k] 14 | 15 | 16 | # プロット 17 | plt.stem(np.arange(length), y) 18 | plt.xlabel("n") 19 | plt.ylabel("amplitude") 20 | plt.grid() 21 | plt.tight_layout() 22 | 23 | plt.show() 24 | -------------------------------------------------------------------------------- /dsugawara/chapter03/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | x = np.zeros(10) 5 | x[0] = 1 6 | 7 | y = np.zeros(10) 8 | y[0] = 0.2 * x[0] 9 | y[1] = 0.2 * x[1] + 0.2 * x[0] 10 | y[2] = 0.2 * x[2] + 0.2 * x[1] + 0.2 * x[0] 11 | y[3] = 0.2 * x[3] + 0.2 * x[2] + 0.2 * x[1] + 0.2 * x[0] 12 | for i in range(4, 10): 13 | y[i] = ( 14 | 0.2 * x[i] + 0.2 * x[i - 1] + 0.2 * x[i - 2] + 0.2 * x[i - 3] + 0.2 * x[i - 4] 15 | ) 16 | 17 | 18 | plt.stem(y) 19 | plt.savefig("05py_sabun.png") 20 | -------------------------------------------------------------------------------- /kyamada/chapter02/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def Myhamming(N): 6 | """ 7 | My hamming window 8 | Input: 9 | x: input array 10 | Output: 11 | x: output array 12 | """ 13 | n = np.arange(N) 14 | # for n in range(N): 15 | win = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N-1)) 16 | return win 17 | 18 | 19 | if __name__ == '__main__': 20 | 21 | x = Myhamming(16000) 22 | plt.plot(x) 23 | plt.grid() 24 | plt.show() 25 | -------------------------------------------------------------------------------- /muesaka/chapter02/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def hamming(N): 5 | n = np.arange(N) 6 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 7 | return w 8 | 9 | 10 | if __name__ == "__main__": 11 | a = 1.0 # 振幅 12 | sec = 3 # 信号長 13 | sr = 16000 # サンプリング周波数 14 | f = 440 # 周波数 15 | 16 | t = np.arange(0, sec * sr) / sr 17 | signal = a * np.sin(2 * np.pi * f * t) 18 | window = hamming(len(signal)) 19 | dft_window = np.fft.fft(window) 20 | 21 | print(dft_window) 22 | -------------------------------------------------------------------------------- /muesaka/chapter02/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def dft(x): 5 | N = len(x) 6 | n = np.arange(N) 7 | dft_x = np.zeros(N, dtype=complex) 8 | for k in range(N): 9 | dft_x[k] = np.sum(x[n] * np.exp(-1j * 2 * np.pi * k * n / N)) 10 | return dft_x 11 | 12 | 13 | def idft(x): 14 | N = len(x) 15 | k = np.arange(N) 16 | idft_x = np.zeros(N, dtype=complex) 17 | for n in range(N): 18 | idft_x[n] = 1 / N * np.sum(x[k] * np.exp(1j * 2 * np.pi * k * n / N)) 19 | return idft_x 20 | -------------------------------------------------------------------------------- /smotoyama/chapter03/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def eq(x): 6 | N = len(x) 7 | y = np.zeros(N) 8 | for i in range(N): 9 | y[i] = ( 10 | 0.2 * x[i] 11 | + 0.2 * x[i - 1] 12 | + 0.2 * x[i - 2] 13 | + 0.2 * x[i - 3] 14 | + 0.2 * x[i - 4] 15 | ) 16 | return y 17 | 18 | 19 | if __name__ == "__main__": 20 | x = np.zeros(10) 21 | x[0] = 1 22 | 23 | plt.stem(eq(x)) 24 | plt.show() 25 | -------------------------------------------------------------------------------- /tkawamura2/chapter03/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def freq_res(y, a, b, fs): 5 | omega = np.arange(0, fs)/fs 6 | F = omega.size 7 | N = a.size 8 | M = b.size 9 | H = np.zeros(F) 10 | 11 | for o, f in enumerate(omega): 12 | a_sum = 1 13 | b_sum = 0 14 | for k1 in np.arange(1, N): 15 | a_sum = a_sum + a[k1]*np.exp(-1j*o*k1) 16 | for k2 in range(M): 17 | b_sum = b_sum + b[k2]*np.exp(-1j*o*k2) 18 | H[f] = b_sum/a_sum 19 | return H 20 | -------------------------------------------------------------------------------- /tkawamura2/chapter05/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def liner_amvector(d, M, theta, f): 5 | c = 334 # 音速 6 | 7 | p = (np.arange(M) - (M - 1) / 2) * d 8 | p = p.reshape([1, p.size]) 9 | zero = np.zeros(p.size) 10 | zero = zero.reshape([1, zero.size]) 11 | p_m = np.concatenate([p, zero, zero]).T 12 | 13 | u = np.array([np.sin(theta), np.cos(theta), 0]) 14 | 15 | tmp = 2 * np.pi * f / c * u * p_m 16 | return np.exp(tmp*1j) 17 | 18 | 19 | print(liner_amvector(0.05, 3, np.pi/4, 1000)) 20 | -------------------------------------------------------------------------------- /knishida/chapter01/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def SNratio(s, x): 5 | ssum = 0.0 6 | xsum = 0.0 7 | for i in range(len(s)): 8 | ssum += s[i] ** 2 9 | xsum += x[i] ** 2 10 | return 10 * np.log10(ssum / xsum) 11 | 12 | 13 | if __name__ == "__main__": 14 | A = 1 15 | f = 440.0 16 | sec = 3.0 17 | fs = 16000 18 | 19 | t = np.arange(0, sec, 1 / fs) 20 | y = A * np.sin(2 * np.pi * f * t) 21 | n = np.random.rand(round(fs * sec)) 22 | 23 | print(SNratio(y, n)) 24 | -------------------------------------------------------------------------------- /ykuriki/chapter03/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def line_conv(x, h): 6 | """線形たたみこみを行う 7 | 8 | Args: 9 | x (ndarray): 信号(1次元配列) 10 | h (ndarray): インパルス応答(1次元配列) 11 | 12 | Returns: 13 | y (ndarray): 畳み込みの実行結果(1次元配列) 14 | 15 | """ 16 | x_size = len(x) 17 | h_size = len(h) 18 | y_size = x_size + h_size - 1 19 | 20 | y = np.zeros(y_size) 21 | 22 | # 纏めて計算 23 | for i in range(x_size): 24 | y[i : i + h_size] += x[i] * h 25 | 26 | return y 27 | -------------------------------------------------------------------------------- /knishida/chapter02/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def Hamming(N): 6 | n = np.arange(N) 7 | win = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 8 | return win 9 | 10 | 11 | if __name__ == "__main__": 12 | A = 1 13 | f = 440.0 14 | sec = 3.0 15 | fs = 16000 16 | 17 | t = np.arange(0, sec * fs) / fs 18 | y = A * np.sin(2 * np.pi * f * t) 19 | 20 | win_ham = Hamming(len(y)) 21 | y_ham = y * win_ham 22 | 23 | plt.plot(t, y_ham) 24 | plt.show() 25 | -------------------------------------------------------------------------------- /ykuriki/chapter03/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def circul_conv_pad(x, h): 6 | """x[n]とh[n]の巡回たたみこみを計算する(非周期的) 7 | 8 | Args: 9 | x (ndarray): 信号(1次元配列) 10 | h (ndarray): インパルス応答(1次元配列) 11 | 12 | Returns: 13 | y (ndarray): 畳み込み結果(1次元配列) 14 | 15 | """ 16 | x_size = len(x) 17 | h_size = len(h) 18 | 19 | X = np.fft.fft(np.block([x, np.zeros(h_size)])) 20 | H = np.fft.fft(np.block([h, np.zeros(x_size)])) 21 | y = np.fft.ifft(X * H) 22 | 23 | return y[:-1] 24 | -------------------------------------------------------------------------------- /dsugawara/chapter02/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | # impulse 6 | delta = np.zeros(8) 7 | delta[0] = 1 8 | 9 | dft_delta = np.fft.fft(delta) 10 | 11 | # スペクトルを求める 12 | amp = np.abs(dft_delta) 13 | angle = np.angle(dft_delta) 14 | 15 | # plot 16 | fig = plt.figure() 17 | ax1 = fig.add_subplot(2, 1, 1) 18 | ax2 = fig.add_subplot(2, 1, 2) 19 | ax1.stem(amp) 20 | ax2.stem(angle) 21 | ax1.set_title("amplitude") 22 | ax2.set_title("angle") 23 | fig.tight_layout() 24 | plt.savefig("05py_spectrum.png") 25 | -------------------------------------------------------------------------------- /ykuriki/chapter04/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def syn_wnd(w, S): 6 | """合成窓を生成する 7 | 8 | Args: 9 | w (ndarray): 窓関数 10 | S (int): シフト幅 11 | 12 | Returns: 13 | ws (ndarray): 合成窓(float型1次元配列) 14 | 15 | """ 16 | L = len(w) 17 | Q = L//S 18 | ws = np.zeros(L) 19 | 20 | for l in range(L): 21 | tmp = 0 22 | for m in range(-Q+1,Q): 23 | if l-m*S < L: 24 | tmp += w[l-m*S] ** 2 25 | ws[l] = w[l] / tmp 26 | 27 | return ws -------------------------------------------------------------------------------- /dsugawara/chapter03/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def Difference_equation(x, a, b): 5 | N = a.size 6 | M = b.size 7 | L = x.sixe 8 | y = np.zeros(L) 9 | sigma_a = 0 10 | sigma_b = 0 11 | 12 | for n in range(0, L): 13 | # a[N]を含むシグマ計算 14 | for k in range(1, N): 15 | sigma_a += a[k] * y[n - k] 16 | # b[M]を含むシグマ計算 17 | for k in range(0, M): 18 | sigma_b += b[k] * x[n - k] 19 | 20 | y[n] = 1 / a[0] * (-1 * sigma_a + sigma_b) 21 | 22 | return y 23 | -------------------------------------------------------------------------------- /knishida/chapter02/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def Hamming(N): 6 | n = np.arange(N) 7 | win = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 8 | return win 9 | 10 | 11 | if __name__ == "__main__": 12 | A = 1 13 | f = 440.0 14 | sec = 3.0 15 | fs = 16000 16 | 17 | t = np.arange(0, sec * fs) / fs 18 | y = A * np.sin(2 * np.pi * f * t) 19 | 20 | win_ham = Hamming(len(y)) 21 | ham_dft = np.fft.fft(win_ham) 22 | 23 | plt.plot(ham_dft) 24 | plt.show() 25 | -------------------------------------------------------------------------------- /muesaka/chapter03/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def calculate_difference(a, b, x, y_size): 5 | N = len(a) 6 | M = len(b) 7 | L = len(x) 8 | y = np.zeros(y_size) 9 | 10 | for n in range(y_size): 11 | ay = 0 12 | bx = 0 13 | for k in range(1, N): 14 | if n >= k: 15 | ay -= a[k] * y[n - k] 16 | for k in range(0, M): 17 | if (n >= k) and (n - k < L): 18 | bx += b[k] * x[n - k] 19 | y[n] = (ay + bx) / a[0] 20 | 21 | return y 22 | -------------------------------------------------------------------------------- /smotoyama/chapter03/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def eq(a, b, x): 5 | N = len(a) 6 | M = len(b) 7 | L = len(x) 8 | y = np.zeros(L) 9 | for i in range(L): 10 | a_sum = 0 11 | b_sum = 0 12 | for j in range(1, N + 1): 13 | if i - j >= 0: 14 | a_sum += a[j] * y[i - j] 15 | for k in range(M + 1): 16 | if i - k > 0 and i - j <= L: 17 | b_sum += b[k] * x[i - k] 18 | 19 | y[i] = (-1 * a_sum + b_sum) / a[0] 20 | 21 | return y 22 | -------------------------------------------------------------------------------- /kyamada/Chapter03/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def difference_equation_05(x): 6 | """ 7 | Input 8 | x : input signal 9 | Output 10 | y: output signal 11 | """ 12 | y = 0.2 * x[0:-4] + 0.2 * x[1:-3] + 0.2 * \ 13 | x[2:-2] + 0.2 * x[3:-1] + 0.2 * x[4:] 14 | return y 15 | 16 | 17 | if __name__ == "__main__": 18 | x = np.zeros(10) 19 | x[0] = 1 20 | 21 | y = difference_equation_05(x) 22 | print(y) 23 | 24 | plt.stem(y) 25 | plt.show() 26 | -------------------------------------------------------------------------------- /smotoyama/chapter02/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from scipy import signal 3 | import matplotlib.pyplot as plt 4 | 5 | 6 | def Hamming(N): 7 | n = np.arange(N) 8 | win = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 9 | return win 10 | 11 | 12 | if __name__ == "__main__": 13 | A = 1 14 | f = 440 15 | fs = 16000 16 | s = 3 17 | 18 | t = np.arange(0, fs * s) / fs 19 | x = A * np.sin(2 * np.pi * f * t) 20 | 21 | win = Hamming(len(x)) 22 | x_win = x * win 23 | 24 | plt.plot(t, x_win) 25 | plt.show() 26 | -------------------------------------------------------------------------------- /dsugawara/chapter02/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | # Humming窓 6 | def Humming(N): 7 | w = [] 8 | for n in range(N): 9 | a = 0.54 - 0.46 * np.cos(2*np.pi*n/(N-1)) 10 | w.append(a) 11 | return w 12 | 13 | 14 | # 第1章1. の信号 15 | A = 1.0 16 | f = 440.0 17 | sf = 16000 18 | sec = 3.0 19 | 20 | t = np.arange(0, sec, 1/sf) 21 | 22 | y = A * np.sin(2*np.pi*f*t) 23 | 24 | y_humming = y * Humming(len(y)) 25 | 26 | 27 | # plot 28 | plt.plot(t, y_humming) 29 | plt.savefig("08py_humming.png") 30 | -------------------------------------------------------------------------------- /kkazama/chapter02/08.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | def hamm(N): 6 | w = np.zeros(N) 7 | for n in range(N): 8 | w[n] = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 9 | return w 10 | 11 | A = 1 #振幅 12 | f = 440 #周波数 13 | sec = 3 #長さ 14 | fs = 16000 #サンプリング周波数 15 | 16 | t = np.arange(0, sec, 1/fs) #時間インデックス 17 | 18 | xt = A * np.sin(2 * np.pi * f * t) #正弦波 19 | 20 | #窓関数 21 | ham = hamm(fs * sec) 22 | 23 | xt = xt * ham 24 | 25 | plt.plot(xt) 26 | plt.show() -------------------------------------------------------------------------------- /smotoyama/chapter01/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import soundfile as sf 3 | 4 | 5 | def create_sound(A, f, t): 6 | x = A * np.sin(2 * np.pi * f * t) 7 | return x 8 | 9 | 10 | if __name__ == "__main__": 11 | A = 1 12 | f_1 = 440 13 | f_2 = 660 14 | fs = 16000 15 | s = 3 16 | 17 | t = np.arange(0, s, 1 / fs) 18 | x_1 = A * np.sin(2 * np.pi * f_1 * t) 19 | x_2 = A * np.sin(2 * np.pi * f_2 * t) 20 | 21 | x = np.array([x_1, x_2]) 22 | 23 | sf.write("sound_2ch.wav", data=x.T, samplerate=fs, subtype="PCM_16") 24 | -------------------------------------------------------------------------------- /ttakahashi/chapter03/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def frequency_response(a, b, f, fs): 5 | """ 6 | 周波数応答を計算する 7 | Args: 8 | a (ndarray): y の係数 9 | b (ndarray): x の係数 10 | f (float): 周波数 11 | fs (float): サンプリング周波数 12 | Returns: 13 | H (ndarray): 周波数応答 14 | """ 15 | omega = 2 + np.pi * f / fs 16 | sum_a = np.sum(a * np.exp(-1j * omega * np.arange(1, len(a)))) 17 | sum_b = np.sum(b * np.exp(-1j * omega * np.arange(0, len(b)))) 18 | H = sum_b / (1 + sum_a) 19 | return H 20 | -------------------------------------------------------------------------------- /muesaka/chapter01/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import better_exceptions 3 | import soundfile as sf 4 | better_exceptions.MAX_LENGTH = None 5 | 6 | 7 | def create_signal(a, sec, sr, f): # 信号生成 8 | t = np.linspace(0, sec, int(sr * sec)) 9 | s = a * np.sin(2 * np.pi * f * t) 10 | return t, s 11 | 12 | 13 | if __name__ == "__main__": 14 | a = 1.0 # 振幅 15 | sec = 3.0 # 信号長 16 | sr = 16000 # サンプリング周波数 17 | f = 440 # 周波数 18 | 19 | signal = create_signal(a, sec, sr, f) 20 | sf.write('02.wav', signal[1], sr, 'PCM_16') 21 | -------------------------------------------------------------------------------- /ykuriki/chapter01/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import wave 3 | 4 | 5 | # パラメータ 6 | fs = 16000 7 | f1 = 440 8 | f2 = 660 9 | r = 1 10 | time = 3 11 | 12 | t = np.arange(0, fs * time) / fs 13 | sig1 = r * np.sin(f1 * 2 * np.pi * t) 14 | sig2 = r * np.sin(f2 * 2 * np.pi * t) 15 | sig = np.array([sig1, sig2]).T.reshape(-1) 16 | 17 | # 音声ファイル出力 18 | output = (sig * np.iinfo(np.int16).max).astype(np.int16) 19 | wav = wave.open("./03.wav", "wb") 20 | wav.setnchannels(2) 21 | wav.setsampwidth(2) 22 | wav.setframerate(fs) 23 | wav.writeframes(output) 24 | wav.close() 25 | -------------------------------------------------------------------------------- /ykuriki/chapter01/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def Add_noise(s, snr): 5 | """SN比がn[dB]になるようにホワイトノイズを加える 6 | 7 | Args: 8 | s (ndarray): 元の信号 (float型1次元配列) 9 | snr (float): SN比 10 | 11 | Returns: 12 | x (ndarray): ノイズを加えた信号 (float型1次元配列) 13 | 14 | """ 15 | np.random.seed(2286) 16 | 17 | alpha = 1 / (10 ** (snr / 20)) 18 | noise = np.random.rand(len(s)) 19 | noise /= np.sqrt(np.sum(noise @ noise)) 20 | noise *= alpha * np.sqrt(np.sum(s @ s)) 21 | 22 | x = s + noise 23 | 24 | return x 25 | -------------------------------------------------------------------------------- /ynakamura/chapter04/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def zero_pad(x, L, S): 5 | x_pad = np.pad(x, L - S) 6 | if x_pad.shape[0] % S != 0: 7 | x_pad = np.pad(x_pad, ((0, S - (x_pad.shape[0] % S)))) 8 | return x_pad 9 | 10 | 11 | def frame_div(x, L, S): 12 | x_pad = zero_pad(x, L, S) 13 | T = (x_pad.shape[0] - L) // S 14 | x_div = [] 15 | for nT in range(T): 16 | x_t = [] 17 | for nL in range(L): 18 | x_t.append(x_pad[nT * S + nL]) 19 | x_div.append(x_t) 20 | return np.array(x_div) 21 | -------------------------------------------------------------------------------- /dsugawara/chapter01/04.py: -------------------------------------------------------------------------------- 1 | # 振幅 1, 周波数 660 Hz の正弦波をサンプリング周波数 16kHz で 3 秒分作成し,1.で作成した信号と合わせて 2ch の wav ファイルとして保存せよ. 2 | 3 | import numpy as np 4 | import matplotlib.pyplot as plt 5 | import soundfile 6 | 7 | 8 | y1, samplerate = soundfile.read("02py_sin.wav") 9 | 10 | 11 | # 振幅 1, 周波数 660 Hz の正弦波をサンプリング周波数 16kHz で 3 秒分作成 12 | A = 1.0 13 | f = 660 14 | sf = 16000 15 | sec = 3.0 16 | t = np.arange(0, 3, 1 / sf) 17 | y2 = A * np.sin(2 * np.pi * f * t) 18 | 19 | y = np.array([y1, y2]) 20 | y = y.T 21 | 22 | soundfile.write(file="04py_sin2ch.wav", data=y, samplerate=sf) 23 | -------------------------------------------------------------------------------- /ttakahashi/chapter01/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import soundfile as sf 3 | 4 | sec = 3.0 # 信号の長さ s 5 | sr = 16000 # サンプリング周波数 Hz 6 | 7 | t = np.arange(sec * sr) / sr # サンプリング点の生成 8 | x = np.random.rand(round(sec * sr)) # ホワイトノイズの生成 9 | 10 | y, sr = sf.read("outputs/02.wav") 11 | 12 | 13 | # Function to adjust the amplitude of white noise to match the SNR 14 | def adjust_snr(s, x, snr): 15 | return (x / np.sqrt(np.sum(x**2))) * np.sqrt(np.sum(s**2)) * 10 ** (-snr / 20) 16 | 17 | 18 | print(adjust_snr(y, x, 1.7)) 19 | print(x) 20 | 21 | print("success!") 22 | -------------------------------------------------------------------------------- /kkazama/chapter03/03.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | # ゼロ詰め巡回畳み込み 6 | def pad_circular_conv(x, h): 7 | N = len(x) # 信号の長さ 8 | y = np.zeros(len(x) + len(h) - 1) 9 | 10 | x = np.pad(x, (0, len(h) - 1)) 11 | h = np.pad(h, (0, len(x) - 1)) 12 | 13 | for n in range(len(y)): 14 | y[n] = 0 # 初期化 15 | for k in range(len(x)): 16 | m = n - k 17 | if (m >= 0) and (m <= N - 1): 18 | y[n] += x[k] * h[m % N] 19 | 20 | return y 21 | -------------------------------------------------------------------------------- /kkazama/chapter02/06.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | A = 1 # 振幅 6 | f = 440 # 周波数 7 | sec = 3 # 長さ 8 | fs = 16000 # サンプリング周波数 9 | 10 | t = np.arange(0, sec * fs) / fs # 時間インデックス 11 | 12 | xt = A * np.sin(2 * np.pi * f * t) # 正弦波 13 | 14 | Xk = np.fft.fft(xt) 15 | 16 | amp = 20 * np.log10(np.abs(Xk)) 17 | angle = 20 * np.log(np.angle(Xk)) 18 | 19 | # 振幅スペクトル 20 | plt.subplot(2, 1, 1) 21 | plt.stem(amp) 22 | 23 | # 位相スペクトル 24 | plt.subplot(2, 1, 2) 25 | plt.stem(angle) 26 | 27 | plt.show() 28 | -------------------------------------------------------------------------------- /kkazama/chapter02/05.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | # DFT 6 | def dft(x): 7 | N = len(x) # 配列の長さ 8 | X = [] # 値代入用 9 | 10 | for n in range(N): 11 | a = -1j * 2 * np.pi * np.arange(N) * n 12 | X.append(np.sum(x[n] * np.exp(a / N))) 13 | 14 | return X 15 | 16 | 17 | # インパルス信号 18 | N = 8 19 | delta = np.zeros(N) 20 | delta[0] = 1 21 | 22 | plt.subplot(2, 1, 1) 23 | plt.stem(dft(delta)) 24 | 25 | plt.subplot(2, 1, 2) 26 | plt.stem(np.fft.fft(delta)) 27 | 28 | plt.show() 29 | -------------------------------------------------------------------------------- /kyamada/chapter02/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def dft(x): 6 | """ 7 | DFT 8 | Input: 9 | x: input array 10 | Output: 11 | X: output DFT array 12 | """ 13 | N = x.size 14 | X = np.zeros(N, dtype=complex) 15 | for k in range(N): 16 | for n in range(N): 17 | X[k] += x[n] * np.exp((-2j * np.pi * k * n) / N) 18 | return X 19 | 20 | 21 | if __name__ == '__main__': 22 | x = np.array([1, 0, 0, 0, 0, 0, 0, 0]) 23 | X = dft(x) 24 | plt.stem(X) 25 | plt.show() 26 | -------------------------------------------------------------------------------- /ttakahashi/chapter03/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def difference_equation(x): 6 | """ 7 | Args: 8 | x (ndarray): 入力信号 9 | Returns: 10 | y (ndarray): 出力信号 11 | """ 12 | y = np.zeros(len(x)) 13 | y[0] = 0.4 * x[0] 14 | for i in range(1, len(x)): 15 | y[i] = 0.3 * y[i - 1] + 0.4 * x[i] 16 | return y 17 | 18 | 19 | x = np.zeros(10) 20 | x[0] = 1 21 | y = difference_equation(x) 22 | plt.stem(y) 23 | plt.grid() 24 | plt.savefig("outputs/06.pdf") 25 | plt.show() 26 | 27 | print("success!") 28 | -------------------------------------------------------------------------------- /ynakamura/chapter02/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def dft(x): 5 | N = x.shape[0] 6 | X = np.array([]) 7 | for k in range(N): 8 | sum = 0 9 | for n in range(N): 10 | sum += x[n] * np.exp(-1j * 2 * np.pi * k * n / N) 11 | X = np.append(X, sum) 12 | return X 13 | 14 | 15 | def idft(X): 16 | N = X.shape[0] 17 | x = np.array([]) 18 | for n in range(N): 19 | sum = 0 20 | for k in range(N): 21 | sum += X[k] * np.exp(1j * 2 * np.pi * k * n / N) 22 | x = np.append(x, sum / N) 23 | return x 24 | -------------------------------------------------------------------------------- /kkazama/chapter02/01.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | # DFT 6 | def dft(x): 7 | N = len(x) # 配列の長さ 8 | X = [] # 値代入用 9 | 10 | for n in range(N): 11 | a = -1j * 2 * np.pi * np.arange(N) * n 12 | X.append(np.sum(x[n] * np.exp(a / N))) 13 | 14 | return X 15 | 16 | 17 | # IDFT 18 | def idft(X): 19 | N = len(X) 20 | x = [] 21 | for n in range(N): 22 | a = 1j * 2 * np.pi * np.arange(N) * n 23 | x.append(1 / N * np.sum(X[n] * np.exp(a / N))) 24 | 25 | return x 26 | -------------------------------------------------------------------------------- /muesaka/chapter02/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def dft(x): 5 | N = len(x) 6 | n = np.arange(N) 7 | dft_x = np.zeros(N, dtype=complex) 8 | for k in range(N): 9 | dft_x[k] = np.sum(x[n] * np.exp(-1j * 2 * np.pi * k * n / N)) 10 | return dft_x 11 | 12 | 13 | if __name__ == "__main__": 14 | delta = np.array([1, 0, 0, 0, 0, 0, 0, 0]) # インパルス信号 15 | 16 | dft_delta = dft(delta) 17 | fft_delta = np.fft.fft(delta) 18 | 19 | print(np.array_equiv(dft_delta, fft_delta)) # 全体が等しいかチェック 20 | print(np.isclose(dft_delta, fft_delta)) # 要素ごとにおおよそ等しいかチェック 21 | -------------------------------------------------------------------------------- /kyamada/Chapter03/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def frequency_response(a, b, f, fs): 5 | """ 6 | 周波数応答を計算する 7 | Input: 8 | a : coefficient of y 9 | b : coefficient of x 10 | f: 周波数 11 | fs: サンプリング周波数 12 | Output: 13 | H: 周波数応答 14 | """ 15 | N = a.size 16 | M = b.size 17 | 18 | omega = 2 + np.pi * f / fs 19 | # a についての処理 20 | sum_a = np.sum(a * np.exp(-1j * omega * np.arange(1, N))) 21 | # b についての処理 22 | sum_b = np.sum(b * np.exp(-1j * omega * np.arange(0, M))) 23 | H = sum_b / (1 + sum_a) 24 | return H 25 | -------------------------------------------------------------------------------- /ykuriki/chapter02/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def clc_dft(x): 6 | """x[n]の離散フーリエ変換(DFT)を計算する 7 | 8 | Args: 9 | x (ndarray): 信号 (float型1次元配列) 10 | 11 | Returns: 12 | X (ndarray): 信号のDFT (complex型1次元配列) 13 | 14 | """ 15 | N = len(x) 16 | n = np.arange(N) 17 | X = np.zeros(N, dtype="complex64") 18 | 19 | for k in range(N): 20 | X[k] = np.sum(x[n] * np.exp(-2j * np.pi * k * n / N)) 21 | 22 | return X 23 | 24 | 25 | delta = np.array([1, 0, 0, 0, 0, 0, 0, 0]) 26 | 27 | dft_delta = clc_dft(delta) 28 | 29 | print(dft_delta) 30 | -------------------------------------------------------------------------------- /ykuriki/chapter03/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def clc_freq_resp(a, b, f, fs): 6 | """周波数応答を計算する 7 | 8 | Args: 9 | a (ndarray): yの係数(1次元配列) 10 | b (ndarray): xの係数(1次元配列) 11 | f (double): 周波数 12 | fs (double): サンプリング周波数 13 | 14 | 15 | Returns: 16 | H (complex): 周波数応答 17 | 18 | """ 19 | N = len(a) 20 | M = len(b) 21 | omega = 2 * np.pi * f / fs 22 | 23 | H = np.sum(b * np.exp(-1j * omega * np.arange(M))) / ( 24 | 1 + np.sum(a[1:] * np.exp(-1j * omega * np.arange(1, N))) 25 | ) 26 | 27 | return H 28 | -------------------------------------------------------------------------------- /tkawamura2/chapter02/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def dft(x): 5 | N = len(x) 6 | X = np.empty(N, dtype=np.complex128) 7 | for k in range(N): 8 | X[k] = np.sum(x * np.exp(-2j * np.pi * k * np.arange(N) / N)) 9 | return X 10 | 11 | 12 | def idft(X): 13 | N = len(X) 14 | x = np.empty(N, dtype=np.float64) 15 | for n in range(N): 16 | x[n] = np.sum(X * np.exp(2j * np.pi * n * np.arange(N) / len(x))) / N 17 | return X 18 | 19 | 20 | delta = np.zeros(8, dtype=np.float64) 21 | delta[0] = 1 22 | Delta = dft(delta) 23 | delta_ = idft(Delta) 24 | print(delta_) 25 | -------------------------------------------------------------------------------- /ynakamura/chapter05/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def calc_circular_vector(r, M, thete, f): 5 | c = 334 6 | 7 | u = np.array([np.sin(thete), np.cos(thete), 0]) 8 | a = np.zeros((M)).astype(complex) 9 | for m in range(M): 10 | pm = np.array( 11 | [ 12 | r * np.sin(2 * np.pi / M * (m - 1)), 13 | r * np.cos(2 * np.pi / M * (m - 1)), 14 | 0, 15 | ] 16 | ).T 17 | a[m] = np.exp(1j * 2 * np.pi * f / c * u @ pm) 18 | 19 | return a 20 | 21 | 22 | print(calc_circular_vector(0.05, 3, 45, 1000)) 23 | -------------------------------------------------------------------------------- /kkazama/chapter02/04.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | # DFT 6 | def dft(x): 7 | N = len(x) # 配列の長さ 8 | X = [] # 値代入用 9 | 10 | for n in range(N): 11 | a = -1j * 2 * np.pi * np.arange(N) * n 12 | X.append(np.sum(x[n] * np.exp(a / N))) 13 | 14 | return X 15 | 16 | 17 | # インパルス信号 18 | N = 8 19 | delta = np.zeros(N) 20 | delta[0] = 1 21 | x_dft = dft(delta) # DFT 22 | 23 | plt.subplot(2, 1, 1) 24 | plt.stem(np.abs(x_dft)) 25 | plt.subplot(2, 1, 2) 26 | plt.stem(np.angle(x_dft)) 27 | 28 | plt.show() 29 | -------------------------------------------------------------------------------- /knishida/chapter02/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def DFT(x): 5 | N = len(x) 6 | n = np.arange(N) 7 | X = np.zeros(N) 8 | for k in range(N): 9 | X[k] = np.sum(x * np.exp(-1j * 2 * np.pi * k * n / N)) 10 | return X 11 | 12 | 13 | def IDFT(X): 14 | N = len(X) 15 | k = np.arange(N) 16 | x = np.zeros(N) 17 | for n in range(N): 18 | x[n] = np.sum(X * np.exp(1j * 2 * np.pi * k * n / N)) / N 19 | return x 20 | 21 | 22 | if __name__ == "__main__": 23 | delta = [1, 0, 0, 0, 0, 0, 0, 0] 24 | delta_dft = DFT(delta) 25 | 26 | print(f"{delta_dft}") 27 | -------------------------------------------------------------------------------- /smotoyama/chapter01/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import soundfile as sf 4 | 5 | 6 | def noize_SNR(x, snr): 7 | sigma = np.sqrt(np.mean(x ** 2) / 10 ** (snr / 10)) 8 | noize = np.random.normal(0, sigma, len(x)) 9 | return noize 10 | 11 | 12 | if __name__ == "__main__": 13 | A = 1 14 | f = 440 15 | s = 3 16 | fs = 8000 17 | 18 | t = np.arange(0, s, 1 / fs) 19 | y, sr = sf.read("sound_re.wav") 20 | 21 | y_avefil = np.convolve(y, np.ones(5), mode="same") / 5 22 | 23 | plt.plot(t, y_avefil) 24 | plt.xlim([0, 0.03]) 25 | plt.show() 26 | -------------------------------------------------------------------------------- /tkawamura2/chapter05/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def circ_amvector(r, M, theta, f): 5 | c = 334 # 音速 6 | 7 | p = 2 * np.pi / M * np.arange(M) 8 | p_1 = r * np.sin(p) 9 | p_1 = p_1.reshape([1, p_1.size]) 10 | p_2 = r * np.cos(p) 11 | p_2 = p_2.reshape([1, p_2.size]) 12 | zero = np.zeros(p.size) 13 | zero = zero.reshape([1, zero.size]) 14 | p_m = np.concatenate([p_1, p_2, zero]).T 15 | 16 | u = np.array([np.sin(theta), np.cos(theta), 0]) 17 | 18 | tmp = 2 * np.pi * f / c * u * p_m 19 | return np.exp(tmp*1j) 20 | 21 | 22 | print(circ_amvector(0.05, 3, np.pi/4, 1000)) 23 | -------------------------------------------------------------------------------- /smotoyama/chapter02/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def DFT(x): 5 | N = len(x) 6 | c = np.zeros(N) * 1j 7 | 8 | for i in range(N): 9 | c[i] = np.sum(x * np.exp(-1j * 2 * np.pi * i * np.arange(N) / N)) 10 | 11 | return c 12 | 13 | 14 | def IDFT(X): 15 | N = len(X) 16 | c = np.zeros(N) * 1j 17 | 18 | for i in range(N): 19 | c[i] = (1 / N) * np.sum(X * np.exp(1j * 2 * np.pi * i * np.arange(N) / N)) 20 | 21 | return c 22 | 23 | 24 | if __name__ == "__main__": 25 | N = 8 26 | delta = np.zeros(N) 27 | delta[0] = 1 28 | 29 | c = DFT(delta) 30 | 31 | print(c) 32 | -------------------------------------------------------------------------------- /tkawamura2/chapter01/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from scipy.io.wavfile import read, write 4 | 5 | #from 08 import set_snr 6 | def set_snr(s, snr): 7 | A_s = np.linalg.norm(s) 8 | x = np.random.randn(round(len(s))) 9 | x = A_s * x /(10**(snr/20)*np.linalg.norm(x)) 10 | return x 11 | 12 | sec = 3.0 # 信号の長さ s 13 | sf = 16000 # サンプリング周波数 Hz 14 | snr = 6 15 | 16 | fs, si = read("660_440.wav") 17 | 18 | t = np.arange(0, sec, 1/fs) 19 | y_1 = si[:,0] + set_snr(si[:,0], snr) 20 | y_2 = si[:,1] + set_snr(si[:,1], snr) 21 | 22 | plt.plot(t, y_1) 23 | plt.plot(t, y_2) 24 | plt.xlim([0, 0.03]) 25 | plt.show() -------------------------------------------------------------------------------- /dsugawara/chapter02/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | # 第1章1. の信号 6 | A = 1.0 7 | f = 440.0 8 | sf = 16000 9 | sec = 3.0 10 | 11 | t = np.arange(0, sec * sf) / sf 12 | 13 | y = A * np.sin(2 * np.pi * f * t) 14 | 15 | Y = np.fft.fft(y) 16 | 17 | 18 | # スペクトルを求める 19 | amp = 20 * np.log10(np.abs(Y)) 20 | angle = 20 * np.log10(np.angle(Y)) 21 | 22 | 23 | # plot 24 | fig = plt.figure() 25 | ax1 = fig.add_subplot(2, 1, 1) 26 | ax2 = fig.add_subplot(2, 1, 2) 27 | ax1.stem(amp) 28 | ax2.stem(angle) 29 | ax1.set_title("amplitude") 30 | ax2.set_title("angle") 31 | fig.tight_layout() 32 | plt.savefig("06py_spectrum.png") 33 | -------------------------------------------------------------------------------- /ttakahashi/chapter02/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def hamming(N): 6 | n = np.arange(N) 7 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 8 | return w 9 | 10 | 11 | A = 1 # 振幅 12 | f = 440 # 周波数 Hz 13 | sec = 3.0 # 信号の長さ s 14 | sr = 16000 # サンプリング周波数 Hz 15 | 16 | t = np.arange(sec * sr) / sr # サンプリング点の生成 17 | y = A * np.sin(2 * np.pi * f * t) # 正弦波の生成 18 | 19 | N = sec * sr 20 | w = hamming(N) 21 | y_wnd = y * w 22 | 23 | plt.plot(y_wnd) 24 | plt.xlabel("time [sec]") 25 | plt.ylabel("amplitude") 26 | plt.grid() 27 | plt.savefig("outputs/08.pdf") 28 | plt.show() 29 | 30 | print("success!") 31 | -------------------------------------------------------------------------------- /muesaka/chapter02/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def hamming(N): 6 | n = np.arange(N) 7 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 8 | return w 9 | 10 | 11 | if __name__ == "__main__": 12 | a = 1.0 # 振幅 13 | sec = 3 # 信号長 14 | sr = 16000 # サンプリング周波数 15 | f = 440 # 周波数 16 | 17 | t = np.arange(0, sec * sr) / sr 18 | signal = a * np.sin(2 * np.pi * f * t) 19 | window = hamming(len(signal)) 20 | window_signal = signal * window 21 | 22 | plt.plot(t, window_signal) 23 | plt.xlabel("time") 24 | plt.ylabel("amplitude") 25 | plt.tight_layout() 26 | plt.show() 27 | -------------------------------------------------------------------------------- /ttakahashi/chapter03/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def difference_equation(x): 6 | """ 7 | Args: 8 | x (ndarray): 入力信号 9 | Returns: 10 | x (ndarray): 出力信号 11 | """ 12 | x[0] = 0.2 * x[0] 13 | for i in range(1, 4): 14 | for j in range(i): 15 | x[i] += 0.2 * x[j] 16 | for i in range(4, len(x)): 17 | for j in range(5): 18 | x[i] += 0.2 * x[i - j] 19 | return x 20 | 21 | 22 | x = np.zeros(10) 23 | x[0] = 1 24 | y = difference_equation(x) 25 | plt.stem(y) 26 | plt.grid() 27 | plt.savefig("outputs/05.pdf") 28 | plt.show() 29 | 30 | print("success!") 31 | -------------------------------------------------------------------------------- /ykuriki/chapter01/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import wave 3 | 4 | 5 | # 音声データの読み取り 6 | wavfile = "./08.wav" 7 | wav = wave.open(wavfile, "rb") 8 | ch = wav.getnchannels() 9 | wid = wav.getsampwidth() 10 | sfrq = wav.getframerate() 11 | siglen = wav.getnframes() 12 | sig = wav.readframes(siglen) 13 | sig = np.frombuffer(sig, dtype=np.int16) 14 | sig = sig / np.iinfo(np.int16).max 15 | wav.close() 16 | 17 | 18 | # パラメータ 19 | fs = 8000 20 | 21 | # 音声ファイル出力 22 | output = (sig[::2] * np.iinfo(np.int16).max).astype(np.int16) 23 | wav = wave.open("./09.wav", "wb") 24 | wav.setnchannels(ch) 25 | wav.setsampwidth(wid) 26 | wav.setframerate(fs) 27 | wav.writeframes(output) 28 | wav.close() 29 | -------------------------------------------------------------------------------- /smotoyama/chapter02/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def Hamming(N): 6 | n = np.arange(N) 7 | win = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 8 | return win 9 | 10 | 11 | if __name__ == "__main__": 12 | A = 1 13 | f = 440 14 | fs = 16000 15 | s = 3 16 | 17 | t = np.arange(0, s, 1 / fs) 18 | x = A * np.sin(2 * np.pi * f * t) 19 | 20 | win = Hamming(len(x)) 21 | 22 | Win = np.fft.fft(win) 23 | X = np.fft.fft(x) 24 | 25 | X = np.pad(X, (X.size // 2, X.size // 2 - 1)) 26 | 27 | x_win = np.fft.ifft(np.convolve(X, Win, mode="valid")) 28 | 29 | plt.plot(x_win.real) 30 | plt.show() 31 | -------------------------------------------------------------------------------- /tkawamura2/chapter02/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def dft(x): 6 | N = len(x) 7 | X = np.empty(N, dtype=np.complex128) 8 | for k in range(N): 9 | X[k] = np.sum(x * np.exp(-2j * np.pi * k * np.arange(N) / N)) 10 | return X 11 | 12 | 13 | def idft(X): 14 | N = len(X) 15 | x = np.empty(N, dtype=np.float64) 16 | for n in range(N): 17 | x[n] = np.sum(X * np.exp(2j * np.pi * n * np.arange(N) / len(x))) / N 18 | return X 19 | 20 | 21 | delta = np.zeros(8, dtype=np.float64) 22 | delta[0] = 1 23 | Delta = dft(delta) 24 | delta_ = idft(Delta) 25 | 26 | plt.stem(range(len(delta_)), delta_) 27 | plt.show() 28 | -------------------------------------------------------------------------------- /knishida/chapter02/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def DFT(x): 5 | N = len(x) 6 | n = np.arange(N) 7 | X = np.zeros(N) 8 | for k in range(N): 9 | X[k] = np.sum(x * np.exp(-1j * 2 * np.pi * k * n / N)) 10 | return X 11 | 12 | 13 | def IDFT(X): 14 | N = len(X) 15 | k = np.arange(N) 16 | x = np.zeros(N) 17 | for n in range(N): 18 | x[n] = np.sum(X * np.exp(1j * 2 * np.pi * k * n / N)) / N 19 | return x 20 | 21 | 22 | if __name__ == "__main__": 23 | delta = [1, 0, 0, 0, 0, 0, 0, 0] 24 | delta_dft = DFT(delta) 25 | delta_np = np.fft.fft(delta) 26 | 27 | print(f"DFT :{delta_dft}") 28 | print(f"numpy:{delta_np}") 29 | -------------------------------------------------------------------------------- /ykuriki/chapter02/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def clc_dft(x): 6 | """x[n]の離散フーリエ変換(DFT)を計算する 7 | 8 | Args: 9 | x (ndarray): 信号 (float型1次元配列) 10 | 11 | Returns: 12 | X (ndarray): 信号のDFT (complex型1次元配列) 13 | 14 | """ 15 | N = len(x) 16 | n = np.arange(N) 17 | X = np.zeros(N, dtype="complex64") 18 | 19 | for k in range(N): 20 | X[k] = np.sum(x[n] * np.exp(-2j * np.pi * k * n / N)) 21 | 22 | return X 23 | 24 | 25 | delta = np.array([1, 0, 0, 0, 0, 0, 0, 0]) 26 | 27 | fft_delta = np.fft.fft(delta) 28 | 29 | # 2. の結果 30 | dft_delta = clc_dft(delta) 31 | 32 | # 比較 33 | print(np.array_equal(fft_delta, dft_delta)) 34 | -------------------------------------------------------------------------------- /smotoyama/chapter02/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def DFT(x): 5 | N = len(x) 6 | c = np.zeros(N) * 1j 7 | 8 | for i in range(N): 9 | c[i] = np.sum(x * np.exp(-1j * 2 * np.pi * i * np.arange(N) / N)) 10 | 11 | return c 12 | 13 | 14 | def IDFT(X): 15 | N = len(X) 16 | c = np.zeros(N) * 1j 17 | 18 | for i in range(N): 19 | c[i] = (1 / N) * np.sum(X * np.exp(1j * 2 * np.pi * i * np.arange(N) / N)) 20 | return c 21 | 22 | 23 | if __name__ == "__main__": 24 | N = 8 25 | x = np.random.randn(N) 26 | c = DFT(x) 27 | X = np.fft.fft(x) 28 | x_ifft = np.fft.ifft(c) 29 | c_i = IDFT(c) 30 | 31 | print(c) 32 | print(X) 33 | -------------------------------------------------------------------------------- /ynakamura/chapter02/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def dft(x): 5 | N = x.shape[0] 6 | X = np.array([]) 7 | for k in range(N): 8 | sum = 0 9 | for n in range(N): 10 | sum += x[n] * np.exp(-1j * 2 * np.pi * k * n / N) 11 | X = np.append(X, sum) 12 | return X 13 | 14 | 15 | def idft(X): 16 | N = X.shape[0] 17 | x = np.array([]) 18 | for n in range(N): 19 | sum = 0 20 | for k in range(N): 21 | sum += X[k] * np.exp(1j * 2 * np.pi * k * n / N) 22 | x = np.append(x, sum / N) 23 | return x 24 | 25 | 26 | N = 8 27 | delta = np.zeros(N) 28 | delta[0] = 1 29 | Delta = dft(delta) 30 | 31 | print(Delta) 32 | -------------------------------------------------------------------------------- /knishida/chapter02/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def DFT(x): 6 | N = len(x) 7 | n = np.arange(N) 8 | X = np.zeros(N) 9 | for k in range(N): 10 | X[k] = np.sum(x * np.exp(-1j * 2 * np.pi * k * n / N)) 11 | return X 12 | 13 | 14 | def IDFT(X): 15 | N = len(X) 16 | k = np.arange(N) 17 | x = np.zeros(N) 18 | for n in range(N): 19 | x[n] = np.sum(X * np.exp(1j * 2 * np.pi * k * n / N)) / N 20 | return x 21 | 22 | 23 | if __name__ == "__main__": 24 | delta = [1, 0, 0, 0, 0, 0, 0, 0] 25 | delta_dft = DFT(delta) 26 | delta_idft = IDFT(delta_dft) 27 | 28 | plt.stem(delta_idft) 29 | plt.show() 30 | -------------------------------------------------------------------------------- /ynakamura/chapter04/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def composite_win(S, win): 5 | L = win.shape[0] 6 | Q = L // S 7 | for nL in range(L): 8 | sum = 0 9 | for nQ in range(Q): 10 | sum += win[nL - nQ * S] ** 2 11 | win_s = win / sum 12 | return np.array(win_s) 13 | 14 | 15 | def istft(X, S): 16 | F, T = X.shape 17 | N = 2 * (F - 1) 18 | M = S * (T - 1) + N 19 | x = np.zeros((M)) 20 | z = np.zeros((T, N)) 21 | w_s = composite_win(S, win=np.hamming(N)) 22 | for t in range(T): 23 | for n in range(N): 24 | z[t, n] = np.fft.irfft(X[:, t])[n] 25 | x[t * S + n] = x[t * S + n] + w_s[n] * z[t, n] 26 | return x 27 | -------------------------------------------------------------------------------- /dsugawara/chapter01/09.py: -------------------------------------------------------------------------------- 1 | # 8.で作成した関数を用いて,6.と同様にホワイトノイズと正弦波の混合信号を作成せよ.ただし,SN 比が 6dB となるようにすること. 2 | 3 | import numpy as np 4 | import soundfile 5 | 6 | 7 | def make_noise(signal, sf, is_SNR): 8 | noise = np.random.randn(sf) 9 | 10 | noise = noise / np.sqrt(np.sum(noise**2)) 11 | noise = noise * np.sqrt(np.sum(signal**2)) 12 | noise = noise * 10 ** (-1 * is_SNR / 20) 13 | 14 | return noise 15 | 16 | 17 | A = 1.0 18 | f = 440.0 19 | sf = 16000 20 | sec = 1.0 21 | t = np.arange(0, sec, 1 / sf) 22 | signal = A * np.sin(2 * np.pi * f * t) 23 | 24 | white_noise = make_noise(signal, sf, 6.0) 25 | 26 | x = signal + white_noise 27 | 28 | 29 | soundfile.write(file="09py_mixed.wav", data=x, samplerate=sf) 30 | -------------------------------------------------------------------------------- /knishida/chapter02/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | if __name__ == "__main__": 5 | A = 1 6 | f = 440.0 7 | sec = 3.0 8 | fs = 16000 9 | 10 | t = np.arange(0, sec * fs) / fs 11 | y = A * np.sin(2 * np.pi * f * t) 12 | 13 | y_npfft = np.fft.fft(y) 14 | 15 | y_amp = 20 * np.log10(np.abs(y_npfft)) 16 | y_angle = 20 * np.log10(np.angle(y_npfft)) 17 | 18 | fig = plt.figure() 19 | fig_amp = fig.add_subplot(2, 1, 1) 20 | fig_angle = fig.add_subplot(2, 1, 2) 21 | fig_amp.plot(y_amp) 22 | fig_angle.plot(y_angle) 23 | fig_amp.set_title("Amplitude") 24 | fig_angle.set_title("Angle") 25 | fig.tight_layout() 26 | plt.show() 27 | -------------------------------------------------------------------------------- /smotoyama/chapter02/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def DFT(x): 6 | N = len(x) 7 | c = np.zeros(N) * 1j 8 | 9 | for i in range(N): 10 | c[i] = np.sum(x * np.exp(-1j * 2 * np.pi * i * np.arange(N) / N)) 11 | 12 | return c 13 | 14 | 15 | def IDFT(X): 16 | N = len(X) 17 | c = np.zeros(N) * 1j 18 | 19 | for i in range(N): 20 | c[i] = (1 / N) * np.sum(X * np.exp(1j * 2 * np.pi * i * np.arange(N) / N)) 21 | 22 | return c 23 | 24 | 25 | if __name__ == "__main__": 26 | N = 8 27 | delta = np.zeros(N) 28 | delta[0] = 1 29 | 30 | c = DFT(delta) 31 | c_idft = IDFT(c) 32 | 33 | plt.stem(c_idft) 34 | plt.show() 35 | -------------------------------------------------------------------------------- /tkawamura2/chapter05/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def set_snr(s, snr): 6 | A_s = np.linalg.norm(s) 7 | x = np.random.randn(round(len(s))) 8 | x = A_s * x / (10 ** (snr / 20) * np.linalg.norm(x)) 9 | return x 10 | 11 | 12 | a = 1 # 振幅 13 | fs = 16000 # サンプリング周波数 14 | f = 440 # 周波数 15 | sec = 1 # 秒 16 | snr = 10 17 | 18 | x = np.arange(sec * fs) / fs 19 | y = np.sin(2 * x * np.pi * f) 20 | 21 | white_noise = set_snr(y, snr) 22 | 23 | x1 = y + white_noise 24 | x2 = np.pad(y[:-9], (9, 0)) + white_noise 25 | x3 = np.pad(y[:-19], (19, 0)) + white_noise 26 | 27 | plt.plot(x1) 28 | plt.plot(x2) 29 | plt.plot(x3) 30 | plt.xlim(0, 0.01 * fs) 31 | plt.show() 32 | -------------------------------------------------------------------------------- /ttakahashi/chapter01/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import soundfile as sf 3 | 4 | 5 | # Function to calculate SNR 6 | def calc_snr(s, x): 7 | return 10 * np.log10(np.sum(s**2) / np.sum(x**2)) 8 | 9 | 10 | # Function to adjust the amplitude of white noise to match the SNR 11 | def adjust_snr(s, x, snr): 12 | return (x / np.sqrt(np.sum(x**2))) * np.sqrt(np.sum(s**2)) * 10 ** (-snr / 20) 13 | 14 | 15 | sec = 3.0 # 信号の長さ s 16 | sr = 16000 # サンプリング周波数 Hz 17 | 18 | t = np.arange(sec * sr) / sr # サンプリング点の生成 19 | x = np.random.rand(round(sec * sr)) # ホワイトノイズの生成 20 | 21 | y, sr = sf.read("outputs/02.wav") 22 | s = x + y 23 | 24 | adj_x = adjust_snr(s, x, 6) 25 | print(calc_snr(s, adj_x)) 26 | 27 | print("success!") 28 | -------------------------------------------------------------------------------- /smotoyama/chapter01/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import soundfile as sf 3 | 4 | 5 | def cul_SNR(y, x): 6 | snr = 10 * np.log10(np.sum(y ** 2) / np.sum(x ** 2)) 7 | return snr 8 | 9 | 10 | def adj_SNR(x, snr): 11 | n = np.random.rand(round(len(x))) 12 | sigma = np.sqrt(np.sum(x ** 2) / np.sum(n ** 2) * 10 ** (-snr / 10)) 13 | noize = sigma * n 14 | y = x + noize 15 | return y 16 | 17 | 18 | if __name__ == "__main__": 19 | A = 1 20 | f = 440 21 | s = 3 22 | fs = 16000 23 | SN = 6 24 | 25 | t = np.arange(0, s, 1 / fs) 26 | x = A * np.sin(2 * np.pi * f * t) 27 | 28 | y = adj_SNR(x, SN) 29 | 30 | sf.write("sound_noize.wav", data=y, samplerate=fs, subtype="PCM_16") 31 | -------------------------------------------------------------------------------- /ykuriki/chapter05/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def clc_sp_mat(X): 6 | """空間相関行列を計算する 7 | 8 | Args: 9 | X (ndarray): M個のF×T複素数行列 10 | 11 | 12 | Returns: 13 | R (ndarray): 空間相関行列 14 | 15 | """ 16 | M, F, T = X.shape 17 | R = np.zeros((F, M, M), dtype="complex") 18 | 19 | for f in range(F): 20 | for t in range(T): 21 | x_ft = X[:, f, t] 22 | R[f] += np.outer(x_ft, np.conjugate(x_ft)) 23 | R[f] /= T 24 | 25 | return R 26 | 27 | 28 | X1 = np.array([[1, -1j, -1, 1j], [2, -2j, -2, 2j], [3, -3j, -3, 3j]]) 29 | X2 = np.array([[4, -2j, 1, 0], [2, -1j, 0, 0], [1, -1j, 1, 0]]) 30 | X = np.array([X1, X2]) 31 | 32 | print(clc_sp_mat(X)) 33 | -------------------------------------------------------------------------------- /kkazama/chapter01/04.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | import soundfile as sf 5 | 6 | A = 1 #振幅 7 | f1 = 440 #周波数 8 | f2 = 660 9 | sec = 3 #長さ 10 | fs = 16000 #サンプリング周波数 11 | 12 | t = np.arange(0, sec, 1/fs) #時間インデックス 13 | 14 | x1 = A * np.sin(2 * np.pi * f1 * t) #正弦波 15 | x2 = A * np.sin(2 * np.pi * f2 * t) 16 | 17 | y = np.array([x1, x2]) 18 | y = y.T 19 | 20 | sf.write("stereo.wav", y, fs, subtype="PCM_16") 21 | 22 | #波形確認 23 | plt.subplot(3, 1, 1) 24 | plt.plot(t, x1) 25 | plt.xlim(0, 0.03) #軸範囲 26 | 27 | plt.subplot(3, 1, 2) 28 | plt.plot(t, x2) 29 | plt.xlim(0, 0.03) 30 | 31 | plt.subplot(3, 1, 3) 32 | plt.plot(t, y) 33 | plt.xlim(0, 0.03) 34 | 35 | plt.show() -------------------------------------------------------------------------------- /kkazama/chapter03/07.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | 6 | def differ_eq(x, a, b): 7 | L = len(x) 8 | N = len(a) 9 | M = len(b) 10 | y = np.zeros(L) 11 | 12 | for n in range(L): 13 | 14 | for k in range(1, N): 15 | if n - k >= 0: 16 | y[n] -= a[k] * y[n - k] 17 | 18 | for l in range(M): 19 | if n - l >= 0 and n - l < L: 20 | y[n] += b[l] * x[n - l] 21 | 22 | y = y / a[0] 23 | 24 | return y 25 | 26 | 27 | x = np.zeros(10) 28 | x[0] = 1 29 | a = np.zeros(10) + 0.3 30 | b = np.zeros(10) + 0.4 31 | y = differ_eq(x, a, b) 32 | 33 | plt.stem(y) 34 | 35 | plt.show() 36 | -------------------------------------------------------------------------------- /ynakamura/chapter05/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def calc_vector(x, y, thete, f): 5 | c = 334 6 | assert x.shape[0] == y.shape[0], "The sizes of x and y must be equal" 7 | M = x.shape[0] 8 | 9 | u = np.array([np.sin(thete), np.cos(thete), 0]) 10 | a = np.zeros((M)).astype(complex) 11 | for m in range(M): 12 | pm = np.array([x[m], y[m], 0]).T 13 | a[m] = np.exp(1j * 2 * np.pi * f / c * u @ pm) 14 | 15 | return a 16 | 17 | 18 | # 1.の問題設定 19 | x_1 = np.array([0.1, 0.05, 0.0]) 20 | y_1 = np.array([0, 0, 0]) 21 | print(calc_vector(x_1, y_1, 45, 1000)) 22 | 23 | # 2.の問題設定 24 | x_2 = np.array([-0.0433, 0, 0.0433]) 25 | y_2 = np.array([-0.025, 0.05, -0.025]) 26 | print(calc_vector(x_2, y_2, 45, 1000)) 27 | -------------------------------------------------------------------------------- /kkazama/chapter02/03.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | # DFT 6 | def dft(x): 7 | N = len(x) # 配列の長さ 8 | X = [] # 値代入用 9 | 10 | for n in range(N): 11 | a = -1j * 2 * np.pi * np.arange(N) * n 12 | X.append(np.sum(x[n] * np.exp(a / N))) 13 | 14 | return X 15 | 16 | 17 | # IDFT 18 | def idft(X): 19 | N = len(X) 20 | x = [] 21 | for n in range(N): 22 | a = 1j * 2 * np.pi * np.arange(N) * n 23 | x.append(1 / N * np.sum(X[n] * np.exp(a / N))) 24 | 25 | return x 26 | 27 | 28 | # インパルス信号 29 | N = 8 30 | delta = np.zeros(N) 31 | delta[0] = 1 32 | 33 | x_dft = dft(delta) 34 | 35 | plt.stem(idft(x_dft)) 36 | plt.show() 37 | -------------------------------------------------------------------------------- /ykuriki/chapter02/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | # 関数 6 | def ham_wnd(N): 7 | """ハミング窓を生成する 8 | 9 | Args: 10 | N (int): 窓の長さ 11 | 12 | Returns: 13 | w (ndarray): ハミング窓(float型1次元配列) 14 | 15 | """ 16 | n = np.arange(N) 17 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 18 | 19 | return w 20 | 21 | 22 | fs = 16000 23 | f = 440 24 | r = 1 25 | sec = 3 26 | 27 | t = np.arange(0, fs * sec) / fs 28 | sig = r * np.sin(f * 2 * np.pi * t) 29 | 30 | wnd = ham_wnd(len(sig)) 31 | 32 | wnd_sig = sig * wnd 33 | 34 | 35 | # プロット 36 | plt.plot(t, wnd_sig) 37 | plt.xlabel("time [sec]") 38 | plt.ylabel("amplitude") 39 | plt.grid() 40 | plt.tight_layout() 41 | 42 | plt.show() 43 | -------------------------------------------------------------------------------- /dsugawara/chapter02/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # DFT 5 | def dft(f): 6 | N = len(f) 7 | X = np.zeros(N) * 1j 8 | for k in range(N): 9 | x = 0j 10 | for n in range(N): # シグマ計算 11 | a = (2 * np.pi * n * k) / N 12 | x += f[n] * np.e ** (-1j * a) 13 | X[k] = x 14 | return X 15 | 16 | 17 | # IDFT 18 | def idft(F): 19 | N = len(F) 20 | x = np.zeros(N) * 1j 21 | for k in range(N): 22 | X = 0j 23 | for n in range(N): 24 | a = (2 * np.pi * n * k) / N 25 | X += 1 / N * F[n] * np.e ** (1j * a) 26 | x[k] = X 27 | return x 28 | 29 | 30 | # impulse 31 | delta = np.zeros(8) 32 | delta[0] = 1 33 | DELTA = dft(delta) 34 | 35 | print(DELTA) 36 | -------------------------------------------------------------------------------- /ttakahashi/chapter03/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def linear_conv(x, h): 5 | """ 6 | x[n]とh[n]の線形畳み込みを計算する 7 | Args: 8 | x (ndarray): 信号(1次元配列) 9 | h (ndarray): インパルス応答(1次元配列) 10 | Returns: 11 | y (ndarray): 畳み込み結果(1次元配列) 12 | """ 13 | y = np.zeros(len(h) + len(x) - 1, dtype=np.float32) 14 | for k in range(0, len(y)): 15 | y[k] = 0.0 16 | for m in range(0, len(x)): 17 | n = k - m 18 | if n >= 0 and n < len(h): 19 | y[k] += x[m] * h[n] 20 | return y 21 | 22 | 23 | impulse = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 24 | kernel = [4, 3, 2, 1, 0, 0] 25 | print(linear_conv(impulse, kernel)) 26 | print(np.convolve(impulse, kernel)) 27 | 28 | print("success!") 29 | -------------------------------------------------------------------------------- /kkazama/chapter03/09.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | 6 | def freq_res(a, b, f, fs): 7 | N = len(a) 8 | M = len(b) 9 | omega = (2 * np.pi * f) / fs 10 | 11 | sum_a = np.sum(a * np.exp(-1j * omega * np.arange(N))) - a[0] 12 | sum_b = np.sum(b * np.exp(-1j * omega * np.arange(M))) 13 | 14 | r = sum_b / (1 + sum_a) 15 | 16 | return r 17 | 18 | 19 | N = 10 20 | fs = 16000 21 | H = np.zeros(N) 22 | a = np.zeros(N) 23 | b = np.zeros(N) + 0.2 24 | 25 | for n in range(N): 26 | f = n * fs / N 27 | H[n] = freq_res(a, b, f, fs) 28 | 29 | plt.subplot(2, 1, 1) 30 | plt.stem(np.abs(H)) 31 | 32 | plt.subplot(2, 1, 2) 33 | plt.stem(np.angle(H)) 34 | 35 | plt.show() 36 | -------------------------------------------------------------------------------- /tkawamura2/chapter04/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def syn_win(win, S): 5 | L = win.size 6 | Q = int(np.floor(L / S)) 7 | swin = np.empty(L, dtype=np.float64) 8 | for i in range(L): 9 | k = i - (Q - 1) * S 10 | if k < 0: 11 | k = 0 12 | swin[i] = win[i] / np.sum(win[k: i + (Q - 1) * S]**2) 13 | return swin 14 | 15 | 16 | def my_istft(X, S, win): 17 | T = X.shape[1] 18 | N = 2 * (X.shape[0] - 1) 19 | M = S * (T - 1) + N 20 | swin = syn_win(win, S) 21 | x = np.zeros(M, dtype=np.float64) 22 | z = np.empty([T, N], dtype=np.float64) 23 | for t in range(T): 24 | z[t] = np.fft.irfft(X[:, t]) 25 | x[t * S: t * S + N] = x[t * S: t * S + N] + swin * z[t] 26 | return x 27 | -------------------------------------------------------------------------------- /dsugawara/chapter04/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def optimal_window(S, win): 5 | 6 | L = win.size 7 | Q = L // S 8 | for l in range(0, L): 9 | sigma = 0 10 | for m in range(0, Q): 11 | sigma += win[l - m * S] ** 2 12 | win_s = win / sigma 13 | 14 | return win_s 15 | 16 | 17 | def istft(S, X): 18 | F = X.shape[0] 19 | T = X.shape[1] 20 | N = 2 * (F - 1) 21 | M = S * (T - 1) + N 22 | win = np.hamming(N) 23 | win_opt = optimal_window(S, win) 24 | x = np.zeros(M) 25 | z = np.empty((T, N)) 26 | 27 | for t in range(0, T): 28 | for n in range(0, N): 29 | z[t, n] = np.fft.irfft(X[:, t])[n] 30 | x[t * S + n] += win_opt[n] * z[t, n] 31 | 32 | return x 33 | -------------------------------------------------------------------------------- /kkazama/chapter04/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def mix_win(w, S): 5 | L = len(w) 6 | Q = L // S 7 | ws = np.zeros(L) 8 | for l in range(L): 9 | a = l - (Q - 1) 10 | if a >= 0: 11 | ws[l] = w[l] / np.sum(w[a:Q] ** 2) 12 | return ws 13 | 14 | 15 | def istft(X, S): 16 | F = X.shape[0] 17 | T = X.shape[1] 18 | 19 | N = 2 * (F - 1) 20 | M = S * (T - 1) + N 21 | 22 | x = np.zeros(M, dtype=complex) 23 | z = np.zeros((T, N), dtype=complex) 24 | 25 | w = np.hamming(S) 26 | w = mix_win(w, S) 27 | 28 | for t in range(T): 29 | for n in range(N): 30 | z[t][n] = np.fft.irfft(X[:, t])[n] 31 | x[t * S + n] = x[t * S + n] + w[n] * z[t][n] 32 | 33 | return x 34 | -------------------------------------------------------------------------------- /muesaka/chapter04/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def padding(L, S, x): 5 | zeros = np.zeros(L - S, dtype=float) 6 | x_pad = np.concatenate((zeros, x, zeros)) 7 | x_pad_mod = len(x_pad) % S 8 | if x_pad_mod != 0: 9 | zeros = np.zeros(x_pad_mod, dtype=float) 10 | x_pad = np.concatenate((x_pad, zeros)) 11 | 12 | return x_pad 13 | 14 | 15 | def divide_frame(L, S, x): 16 | x_pad = padding(L, S, x) 17 | T = (len(x_pad) - L) // S + 1 18 | x_div = np.empty([T, L], dtype=float) 19 | for t in range(T): 20 | x_t = np.zeros(L, dtype=float) 21 | for i in range(L): 22 | if len(x_pad) > (t * S + i): 23 | x_t[i] = x_pad[t * S + i] 24 | x_div[t] = x_t 25 | 26 | return x_div 27 | -------------------------------------------------------------------------------- /kkazama/chapter01/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | #SNR計算 5 | def cal_sn(s, x): 6 | snr = 10 * np.log10(np.sum(s * s) / np.sum(x * x)) 7 | 8 | return snr 9 | 10 | #ノイズの振幅調整 11 | def cal_adj_amp(s, x, snr): 12 | x = x / np.sqrt(sum(x * x)) 13 | x = x * np.sqrt(sum(s * s)) 14 | x = x * 10 ** (-snr / 20) 15 | 16 | return x 17 | 18 | A = 1 #信号の振幅 19 | f = 440 20 | sec = 3 #信号の長さ 21 | fs = 16000 #サンプリング周波数 22 | 23 | #調整前 24 | r = np.random.rand(round(fs*sec)) 25 | x = 2 * A * (r - 0.5) #ホワイトノイズの生成 26 | 27 | t = np.arange(0, sec, 1/fs) #時間インデックス 28 | s = A * np.sin(2 * np.pi * f * t) #正弦波 29 | 30 | #調整後 31 | x = cal_adj_amp(s, x, 6.0) 32 | 33 | plt.plot(t, s) 34 | plt.plot(t, x) 35 | plt.xlim(0, 0.03) #軸範囲 36 | plt.show() -------------------------------------------------------------------------------- /kkazama/chapter03/10.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | 6 | def freq_res(a, b, f, fs): 7 | N = len(a) 8 | M = len(b) 9 | omega = (2 * np.pi * f) / fs 10 | 11 | sum_a = np.sum(a * np.exp(-1j * omega * np.arange(N))) - a[0] 12 | sum_b = np.sum(b * np.exp(-1j * omega * np.arange(M))) 13 | 14 | r = sum_b / (1 + sum_a) 15 | 16 | return r 17 | 18 | 19 | N = 10 20 | fs = 16000 21 | H = np.zeros(N) 22 | a = np.zeros(N) - 0.3 23 | b = np.zeros(N) + 0.4 24 | 25 | for n in range(N): 26 | f = n * fs / N 27 | H[n] = freq_res(a, b, f, fs) 28 | 29 | plt.subplot(2, 1, 1) 30 | plt.stem(np.abs(H)) 31 | 32 | plt.subplot(2, 1, 2) 33 | plt.stem(np.angle(H)) 34 | 35 | plt.show() 36 | -------------------------------------------------------------------------------- /kkazama/chapter04/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | #ゼロ詰め 4 | def zero_pad(x, L, S): 5 | x = np.pad(x, L-S, L-S) 6 | a = len(x) % S 7 | if a != 0: 8 | x = np.pad(x, (0, S-a)) 9 | 10 | return x 11 | 12 | #フレーム分割 13 | def sep_frame(x, L, S): 14 | x = zero_pad(x, L, S) 15 | T = len(x) / S 16 | xt = np.zeros((T, L)) 17 | for t in range(T): 18 | for l in range(L): 19 | xt[t] = x[t * S + l] 20 | 21 | return xt 22 | 23 | # フーリエ変換 24 | def sig_stft(x, L, S): 25 | xt = sep_frame(x, L, S) 26 | w = np.hamming(L) 27 | T = xt.shape[0] 28 | X = np.zeros((L // 2 + 1, T), dtype=complex) 29 | for t in range(T): 30 | xt[t] = xt[t] * w 31 | X[:, t] = np.fft.rfft(xt[t]) 32 | 33 | return X 34 | 35 | -------------------------------------------------------------------------------- /knishida/chapter03/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def linear_convolution(x, h): 5 | K = len(x) 6 | z = np.zeros(2 * K - 2) 7 | for n in range(2 * K - 2): 8 | for k in range(K): 9 | if n - k >= 0 and n - k <= K - 1: 10 | z[n] += x[k] * h[n - k] 11 | return z 12 | 13 | 14 | def circular_convolution(x, h): 15 | N = len(x) 16 | z = np.zeros(N) 17 | for n in range(N): 18 | for k in range(N): 19 | z[n] += x[k] * h[(n - k) % N] 20 | return z 21 | 22 | 23 | if __name__ == "__main__": 24 | x = [4, 3, 2, 1] 25 | y = [1, 0, -1, 0] 26 | N = len(x) 27 | 28 | print(f"circ:{circular_convolution(np.pad(x,((0,N-2))),np.pad(y,((0,N-2))))}") 29 | print(f"linr:{linear_convolution(x,y)}") 30 | -------------------------------------------------------------------------------- /muesaka/chapter02/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def dft(x): 6 | N = len(x) 7 | n = np.arange(N) 8 | dft_x = np.zeros(N, dtype=complex) 9 | for k in range(N): 10 | dft_x[k] = np.sum(x[n] * np.exp(-1j * 2 * np.pi * k * n / N)) 11 | return dft_x 12 | 13 | 14 | if __name__ == "__main__": 15 | delta = np.array([1, 0, 0, 0, 0, 0, 0, 0]) # インパルス信号 16 | 17 | dft_delta = dft(delta) 18 | amp_dft_delta = np.abs(dft_delta) 19 | phase_dft_delta = np.angle(dft_delta) 20 | 21 | fig, ax = plt.subplots(nrows=2, ncols=1) 22 | ax[0].stem(amp_dft_delta) 23 | ax[0].set_title("amplitude") 24 | ax[1].stem(phase_dft_delta) 25 | ax[1].set_title("phase") 26 | plt.tight_layout() 27 | plt.show() 28 | -------------------------------------------------------------------------------- /muesaka/chapter01/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import better_exceptions 4 | better_exceptions.MAX_LENGTH = None 5 | 6 | 7 | def create_noise(sec, sr): # 白色雑音生成 8 | t = np.linspace(0, sec, int(sr*sec)) 9 | s = np.random.random(len(t)) 10 | return t, s 11 | 12 | 13 | def plot_signal(t, s, right, filename): # 単一信号プロット 14 | plt.plot(t, s) 15 | plt.xlim(0, right) 16 | plt.xlabel('Time') 17 | plt.ylabel('Amplitude') 18 | plt.savefig(filename) 19 | plt.show() 20 | 21 | 22 | if __name__ == "__main__": 23 | a = 1.0 # 振幅 24 | sec = 3.0 # 信号長 25 | sr = 16000 # サンプリング周波数 26 | plot_right = 0.03 # x軸プロット上限 27 | 28 | noise = create_noise(sec, sr) 29 | plot_signal(*noise, plot_right, '05.pdf') 30 | -------------------------------------------------------------------------------- /ttakahashi/chapter02/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # Function to calculate DFT 5 | def dft(x): 6 | N = len(x) 7 | n = np.arange(N) 8 | X = np.zeros(N, dtype="complex64") 9 | for k in range(N): 10 | X[k] = np.sum(x[n] * np.exp(-2j * np.pi * k * n / N)) 11 | return X 12 | 13 | 14 | # Function to calculate IDFT 15 | def idft(X): 16 | N = len(X) 17 | k = np.arange(N) 18 | x = np.zeros(N, dtype="float64") 19 | for n in range(N): 20 | x[n] = np.sum(X[k] * np.exp(2j * np.pi * k * n / N)) / N 21 | return x 22 | 23 | 24 | N = 8 25 | x = np.random.rand(round(N)) 26 | x_dft = dft(x) 27 | x_fft = np.fft.fft(x) 28 | x_idft = idft(x_dft) 29 | x_ifft = np.fft.ifft(x_fft) 30 | print(x_idft) 31 | print(x_ifft) 32 | 33 | print("success!") 34 | -------------------------------------------------------------------------------- /ynakamura/chapter02/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def dft(x): 6 | N = x.shape[0] 7 | X = np.array([]) 8 | for k in range(N): 9 | sum = 0 10 | for n in range(N): 11 | sum += x[n] * np.exp(-1j * 2 * np.pi * k * n / N) 12 | X = np.append(X, sum) 13 | return X 14 | 15 | 16 | def idft(X): 17 | N = X.shape[0] 18 | x = np.array([]) 19 | for n in range(N): 20 | sum = 0 21 | for k in range(N): 22 | sum += X[k] * np.exp(1j * 2 * np.pi * k * n / N) 23 | x = np.append(x, sum / N) 24 | return x 25 | 26 | 27 | N = 8 28 | delta = np.zeros(N) 29 | delta[0] = 1 30 | Delta = dft(delta) 31 | 32 | print(idft(Delta)) 33 | plt.stem(idft(Delta)) 34 | plt.show() 35 | -------------------------------------------------------------------------------- /dsugawara/chapter04/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def padding(L, S, x): 5 | 6 | x = np.pad(x, [L - S, L - S]) 7 | temp = S - (x.size % S) # 末尾に詰める0の個数を求める 8 | x = np.pad(x, [0, temp]) 9 | return x 10 | 11 | 12 | def frame_cut(L, S, x): 13 | N = x.size 14 | T = ((N - L) // S) + 1 # フレーム数.後で確認 15 | x_cut = np.empty([T, L]) # 出力 16 | x_pad = padding(L, S, x) 17 | for t in range(0, T): 18 | x_cut[t] = x_pad[t * S : t * S + L] 19 | return x_cut 20 | 21 | 22 | def stft(L, S, x): 23 | x_cut = frame_cut(L, S, x) 24 | T = x_cut.shape[0] 25 | win = np.hamming(L) 26 | X = np.empty((L // 2 + 1, T), dtype="complex") 27 | 28 | for t in range(0, T): 29 | X[:, t] = np.fft.rfft(win * x_cut[t, :]) 30 | 31 | return X 32 | -------------------------------------------------------------------------------- /tkawamura2/chapter02/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def dft(x): 6 | N = len(x) 7 | X = np.empty(N, dtype=np.complex128) 8 | for k in range(N): 9 | X[k] = np.sum(x * np.exp(-2j * np.pi * k * np.arange(N) / N)) 10 | return X 11 | 12 | 13 | def idft(X): 14 | N = len(X) 15 | x = np.empty(N, dtype=np.float64) 16 | for n in range(N): 17 | x[n] = np.sum(X * np.exp(2j * np.pi * n * np.arange(N) / len(x))) / N 18 | return X 19 | 20 | 21 | delta = np.zeros(8, dtype=np.float64) 22 | delta[0] = 1 23 | Delta = dft(delta) 24 | delta_ = idft(Delta) 25 | 26 | plt.stem(range(len(Delta)), 20 * np.log10(np.abs(Delta))) 27 | plt.show() 28 | 29 | plt.stem(range(len(Delta)), 20 * np.log10(np.angle(Delta))) 30 | plt.show() 31 | -------------------------------------------------------------------------------- /ynakamura/chapter03/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def freq_response(a, b, fs, f): 6 | w = 2 * np.pi * f / fs 7 | W = np.exp(1j * w) 8 | 9 | sum1 = 0 10 | for k in range(1, a.shape[0]): 11 | sum1 += a[k] * np.exp(W * -k) 12 | 13 | sum2 = 0 14 | for k in range(b.shape[0]): 15 | sum2 += b[k] * np.exp(W * -k) 16 | 17 | H = sum2 / (1 + sum1) 18 | return H 19 | 20 | 21 | a = np.array([1]) 22 | b = np.array([0.2] * 5) 23 | fs = 16000 24 | N = 10 25 | f = np.array([]) 26 | for n in range(N): 27 | f = np.append(f, n / N * fs) 28 | H = freq_response(a, b, fs, f) 29 | 30 | 31 | plt.stem(np.abs(H)) 32 | plt.title("Amp") 33 | plt.show() 34 | 35 | plt.stem(np.angle(H)) 36 | plt.title("Phase") 37 | plt.show() 38 | -------------------------------------------------------------------------------- /knishida/chapter03/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def difference_equation(a, b, x, plots): 6 | N = len(a) 7 | M = len(b) 8 | X = len(x) 9 | y = np.zeros(plots) 10 | 11 | for p in range(plots): 12 | tmpa = 0 13 | tmpb = 0 14 | for n in range(1, N): 15 | if p - n >= 0: 16 | tmpa += a[n] * y[p - n] 17 | for m in range(M): 18 | if p - m >= 0 and p - m < X: 19 | tmpb += b[m] * x[p - m] 20 | y[p] = -tmpa + tmpb 21 | 22 | y /= a[0] 23 | return y 24 | 25 | 26 | if __name__ == "__main__": 27 | a = [1, -0.3] 28 | b = [0.4] 29 | x = [1] 30 | 31 | y = difference_equation(a, b, x, 10) 32 | 33 | plt.stem(y) 34 | plt.show() 35 | -------------------------------------------------------------------------------- /tkawamura2/chapter04/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import math 3 | 4 | 5 | def pad(x, L, S): 6 | x_ = np.pad(x, [L - S, L - S]) 7 | a = np.mod(x_.size, S) 8 | if a != 0: 9 | x_ = np.pad(x_, [0, S - a]) 10 | return x_ 11 | 12 | 13 | def frame_div(x, L, S): 14 | x_ = pad(x, L, S) 15 | N_ = x_.size 16 | T = math.floor((N_ - L) / S) + 1 17 | x_t = np.empty([T, L], dtype=np.float64) 18 | for t in range(T): 19 | x_t[t] = x_[t * S: t * S + L] 20 | return x_t 21 | 22 | 23 | def my_stft(x, L, S): 24 | x_t = frame_div(x, L, S) 25 | T = x_t.shape[0] 26 | win = np.hamming(L) 27 | X = np.empty([int(np.floor(L / 2)) + 1, T], dtype=np.complex128) 28 | for t in range(T): 29 | X[:, t] = np.fft.rfft(win * x_t[t, :]) 30 | return X 31 | -------------------------------------------------------------------------------- /ynakamura/chapter03/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def freq_response(a, b, fs, f): 6 | w = 2 * np.pi * f / fs 7 | W = np.exp(1j * w) 8 | 9 | sum1 = 0 10 | for k in range(1, a.shape[0]): 11 | sum1 += a[k] * np.exp(W * -k) 12 | 13 | sum2 = 0 14 | for k in range(b.shape[0]): 15 | sum2 += b[k] * np.exp(W * -k) 16 | 17 | H = sum2 / (1 + sum1) 18 | return H 19 | 20 | 21 | a = np.array([1, -0.3]) 22 | b = np.array([0.4]) 23 | fs = 16000 24 | N = 10 25 | f = np.array([]) 26 | for n in range(N): 27 | f = np.append(f, n / N * fs) 28 | H = freq_response(a, b, fs, f) 29 | 30 | 31 | plt.stem(np.abs(H)) 32 | plt.title("Amp") 33 | plt.show() 34 | 35 | plt.stem(np.angle(H)) 36 | plt.title("Phase") 37 | plt.show() 38 | -------------------------------------------------------------------------------- /knishida/chapter01/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def SNratio(s, x): 5 | ssum = 0.0 6 | xsum = 0.0 7 | for i in range(len(s)): 8 | ssum += s[i] ** 2 9 | xsum += x[i] ** 2 10 | return 10 * np.log10(ssum / xsum) 11 | 12 | 13 | def ajust_SN(s, x, SN): 14 | ssum = 0.0 15 | xsum = 0.0 16 | for i in range(len(s)): 17 | ssum += s[i] ** 2 18 | xsum += x[i] ** 2 19 | return np.sqrt(ssum / xsum * 10 ** (-SN / 10)) 20 | 21 | 22 | if __name__ == "__main__": 23 | A = 1 24 | f = 440.0 25 | sec = 3.0 26 | fs = 16000 27 | SN = 12 28 | 29 | t = np.arange(0, sec, 1 / fs) 30 | y = A * np.sin(2 * np.pi * f * t) 31 | n = np.random.rand(round(fs * sec)) 32 | 33 | X = ajust_SN(y, n, SN) 34 | 35 | print(SNratio(y, X * n)) 36 | -------------------------------------------------------------------------------- /muesaka/chapter01/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import better_exceptions 3 | import soundfile as sf 4 | better_exceptions.MAX_LENGTH = None 5 | 6 | 7 | def create_signal(a, sec, sr, f): # 信号生成 8 | t = np.linspace(0, sec, int(sr * sec)) 9 | s = a * np.sin(2 * np.pi * f * t) 10 | return t, s 11 | 12 | 13 | def generate_stereo_signal(s1, s2): # ステレオ信号合成 14 | return [[s1[i], s2[i]] for i in range(len(s1))] 15 | 16 | 17 | if __name__ == "__main__": 18 | a = 1.0 # 振幅 19 | sec = 3.0 # 信号長 20 | sr = 16000 # サンプリング周波数 21 | f1 = 440 # 周波数1(440 Hz) 22 | f2 = 660 # 周波数2(660 Hz) 23 | 24 | s1 = create_signal(a, sec, sr, f1) 25 | s2 = create_signal(a, sec, sr, f2) 26 | s3 = generate_stereo_signal(s1[1], s2[1]) 27 | sf.write('04.wav', s3[1], sr, 'PCM_16') 28 | -------------------------------------------------------------------------------- /smotoyama/chapter03/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def linear_conv_cir(x, h): 5 | N = len(x) 6 | x = np.pad(x, [0, N - 2]) 7 | h = np.pad(h, [0, N - 2]) 8 | z = np.zeros(2 * N - 2) 9 | for n in range(z.size): 10 | for k in range(z.size): 11 | z[n] += x[k] * h[(n - k) % z.size] 12 | return z 13 | 14 | 15 | def linear_conv(x, h): 16 | N = len(x) 17 | z = np.zeros(2 * N - 2) 18 | for n in range(z.size): 19 | for k in range(N): 20 | if n - k >= 0 and n - k <= N - 1: 21 | z[n] += x[k] * h[n - k] 22 | return z 23 | 24 | 25 | if __name__ == "__main__": 26 | x = [4, 3, 2, 1] 27 | y = [1, 0, -1, 0] 28 | 29 | z_lin = linear_conv(x, y) 30 | z_cir = linear_conv_cir(x, y) 31 | 32 | print(z_lin, z_cir) 33 | -------------------------------------------------------------------------------- /ttakahashi/chapter03/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def circle_conv(x, h): 5 | """ 6 | x[n]とh[n]の巡回畳み込みを計算する 7 | Args: 8 | x (ndarray): 信号(1次元配列) 9 | h (ndarray): インパルス応答(1次元配列) 10 | Returns: 11 | y (ndarray): 畳み込み結果(1次元配列) 12 | """ 13 | y = np.zeros(len(h) + len(x) - 1, dtype=np.float32) 14 | for k in range(0, len(y)): 15 | y[k] = 0.0 16 | for m in range(0, len(x)): 17 | n = k - m 18 | if n >= 0 and n < len(h): 19 | index = (k - m) % len(x) 20 | y[k] += x[m] * h[index] 21 | return y 22 | 23 | 24 | impulse = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 25 | kernel = [4, 3, 2, 1, 0, 0] 26 | print(circle_conv(impulse, kernel)) 27 | print(np.convolve(impulse, kernel)) 28 | 29 | print("success!") 30 | -------------------------------------------------------------------------------- /ykuriki/chapter05/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def clc_amv_line(d, M, theta, f): 6 | """アレイマニフォールドベクトルを計算する 7 | 8 | Args: 9 | d (double): マイク間隔 [m] 10 | M (int): マイク数 11 | theta (int/double): 音源方向 [deg] 12 | f (int/double): 周波数 [Hz] 13 | 14 | 15 | Returns: 16 | a (ndarray): アレイマニフォールドベクトル 17 | 18 | """ 19 | c = 334 20 | theta = np.radians(theta) 21 | a = np.zeros(M, dtype="complex") 22 | u = np.array([np.sin(theta), np.cos(theta), 0]) 23 | 24 | for m in range(M): 25 | p_m = np.array([(m - (M - 1) / 2) * d, 0, 0]) 26 | a[m] = np.exp(1j * 2 * np.pi * f / c * u @ p_m) 27 | 28 | return a 29 | 30 | 31 | d = 0.05 32 | M = 3 33 | theta = 45 34 | f = 1000 35 | 36 | print(clc_amv_line(d, M, theta, f)) 37 | -------------------------------------------------------------------------------- /ykuriki/chapter03/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def clc_diff_eq(a, b, x, length): 6 | """差分方程式を計算する 7 | 8 | Args: 9 | a (ndarray): yの係数(1次元配列) 10 | b (ndarray): xの係数(1次元配列) 11 | x (ndarray): 信号(1次元配列) 12 | length (int): yの長さ 13 | 14 | 15 | Returns: 16 | y (ndarray): 結果(1次元配列) 17 | 18 | """ 19 | N = len(a) 20 | M = len(b) 21 | L = len(x) 22 | y = np.zeros(length) 23 | 24 | # bxを計算しておく 25 | for n in range(length): 26 | for k in range(M): 27 | if k <= n and n - k < L: 28 | y[n] += b[k] * x[n - k] 29 | 30 | for n in range(length): 31 | for k in range(1, N): 32 | if k <= n: 33 | y[n] -= a[k] * y[n - k] 34 | y[n] /= a[0] 35 | 36 | return y 37 | -------------------------------------------------------------------------------- /ynakamura/chapter01/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def adjust_amp(x, snr, fs=16000, sec=3): 6 | whitenoise = np.random.rand(int(fs * sec)) * 2 - 1 7 | a = np.sqrt(np.sum(x**2) / np.sum(whitenoise**2) * (10 ** (-snr / 10))) 8 | whitenoise = a * np.random.rand(int(fs * sec)) * 2 - 1 9 | return whitenoise 10 | 11 | 12 | fs = 16000 13 | sec = 3 14 | fin = 440 15 | t = np.linspace(0.0, sec, int(fs * sec)) 16 | 17 | x = np.sin(2.0 * np.pi * fin * t) 18 | 19 | whitenoise = adjust_amp(x, 20) 20 | mix = x + whitenoise 21 | 22 | plt.plot(t, mix) 23 | plt.xlim(0, 0.03) 24 | plt.show() 25 | 26 | 27 | # SNR確認用コード 28 | def calculate_snr(signal, noise): 29 | return 10 * np.log(np.sum(signal**2) / np.sum(noise**2)) 30 | 31 | 32 | print(calculate_snr(x, whitenoise)) 33 | -------------------------------------------------------------------------------- /kyamada/chapter01/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def create_sinusoid( 6 | fs=16000, 7 | f=440, 8 | r=1, 9 | theta=0, 10 | time=3, 11 | ): 12 | """ 13 | Create a sinusoid wave 14 | Input: 15 | fs: sampling frequency 16 | f: frequency 17 | r: amplitude 18 | theta: phase 19 | time: time 20 | Output: 21 | t: time_data 22 | x: wave data 23 | """ 24 | t = np.arange(0, time, 1/fs) 25 | x = r * np.sin(2 * np.pi * f * t + theta) 26 | 27 | return t, x 28 | 29 | 30 | 31 | 32 | 33 | if __name__ == '__main__': 34 | fs = 16000 35 | t, x = create_sinusoid(fs=fs) 36 | print(int(0.003*fs)) 37 | plt.plot(t, x) 38 | plt.xlim([0, 0.03]) 39 | plt.show() 40 | 41 | 42 | -------------------------------------------------------------------------------- /muesaka/chapter02/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | if __name__ == "__main__": 5 | a = 1.0 # 振幅 6 | sec = 3 # 信号長 7 | sr = 16000 # サンプリング周波数 8 | f = 440 # 周波数 9 | 10 | t = np.arange(0, sec * sr) / sr 11 | signal = a * np.sin(2 * np.pi * f * t) 12 | fft_signal = np.fft.fft(signal) 13 | amp_fft_signal = 20 * np.log10(np.abs(fft_signal)) 14 | phase_fft_signal = 20 * np.log10(np.angle(fft_signal)) 15 | freq_index = np.arange(len(fft_signal)) / len(fft_signal) * sr # プロットのインデックス用 16 | 17 | fig, ax = plt.subplots(nrows=2, ncols=1) 18 | ax[0].plot(freq_index, amp_fft_signal) 19 | ax[0].set_title("amplitude") 20 | ax[1].plot(freq_index, phase_fft_signal) 21 | ax[1].set_title("phase") 22 | plt.tight_layout() 23 | plt.show() 24 | -------------------------------------------------------------------------------- /tkawamura/chapter01/02.py: -------------------------------------------------------------------------------- 1 | # WAV ファイルの作成(モノラル): 1.で作成した正弦波を 16bit PCM フォーマットで wav ファイルとして保存せよ. 2 | 3 | import numpy as np 4 | import soundfile as sf 5 | 6 | 7 | def main(): 8 | amp = 1 # amplitude of sine wave 9 | freq = 440 # frequency of sine wave (Hz) 10 | fs = 16000 # sampling frequency (Hz) 11 | sig_len = 3 # signal length (sec.) 12 | 13 | filename = "sin.wav" 14 | 15 | t = np.arange(fs * sig_len) / fs # time 16 | sin_wave = amp * np.sin(2 * np.pi * freq * t) # Asin(2πft) 17 | 18 | # save 19 | sf.write(filename, sin_wave, fs, "PCM_16") 20 | 21 | # 確認方法 (soundfile.info: https://pysoundfile.readthedocs.io/en/latest/#soundfile.info) 22 | # dtype2 = sf.info(filename) 23 | # print(f"information : {dtype2}") 24 | 25 | 26 | if __name__ == "__main__": 27 | main() 28 | -------------------------------------------------------------------------------- /dsugawara/chapter02/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # DFT 5 | def dft(f): 6 | N = len(f) 7 | X = np.zeros(N) * 1j 8 | for k in range(N): 9 | x = 0j 10 | for n in range(N): # シグマ計算 11 | a = (2 * np.pi * n * k) / N 12 | x += f[n] * np.e ** (-1j * a) 13 | X[k] = x 14 | return X 15 | 16 | 17 | # IDFT 18 | def idft(F): 19 | N = len(F) 20 | x = np.zeros(N) * 1j 21 | for k in range(N): 22 | X = 0j 23 | for n in range(N): 24 | a = (2 * np.pi * n * k) / N 25 | X += 1 / N * F[n] * np.e ** (1j * a) 26 | x[k] = X 27 | return x 28 | 29 | 30 | # 確認 31 | N = 8 32 | x = np.random.randn(N) 33 | c = dft(x) 34 | X = np.fft.fft(x) 35 | x_ifft = np.fft.ifft(c) 36 | c_i = idft(c) 37 | 38 | print(c) 39 | print(X) 40 | -------------------------------------------------------------------------------- /tkawamura2/chapter02/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def dft(x): 6 | N = len(x) 7 | X = np.empty(N, dtype=np.complex128) 8 | for k in range(N): 9 | X[k] = np.sum(x * np.exp(-2j * np.pi * k * np.arange(N) / N)) 10 | return X 11 | 12 | 13 | def idft(X): 14 | N = len(X) 15 | x = np.empty(N, dtype=np.float64) 16 | for n in range(N): 17 | x[n] = np.sum(X * np.exp(2j * np.pi * n * np.arange(N) / len(x))) / N 18 | return X 19 | 20 | 21 | a = 1 # 振幅 22 | fs = 16000 # サンプリング周波数 23 | f = 440 # 周波数 24 | sec = 1 # 秒 3→1 25 | 26 | x = np.arange(sec * fs) / fs 27 | y = np.sin(2 * x * np.pi * f) 28 | 29 | Y = dft(y) 30 | 31 | plt.plot(x, 20 * np.log10(np.abs(Y))) 32 | plt.show() 33 | 34 | plt.plot(x, 20 * np.log10(np.angle(Y))) 35 | plt.show() 36 | -------------------------------------------------------------------------------- /ynakamura/chapter02/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def hamming(N): 6 | n = np.arange(N) 7 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 8 | return w 9 | 10 | 11 | def convolve(a, v): 12 | a_len = a.shape[0] 13 | v_len = v.shape[0] 14 | sum = np.array([]) 15 | for i in range(a_len - v_len + 1): 16 | sum = np.append(sum, np.sum(a[i : v_len + i] * np.flip(v))) 17 | return sum 18 | 19 | 20 | fs = 16000 21 | sec = 3 22 | fin = 440 23 | t = np.arange(sec * fs) / fs 24 | 25 | x = np.sin(2.0 * np.pi * fin * t) 26 | w = hamming(x.shape[0]) 27 | 28 | X = np.fft.fft(x) 29 | W = np.fft.fft(w) 30 | 31 | X = np.pad(X, (X.shape[0] // 2, X.shape[0] // 2 - 1)) 32 | 33 | x_ham = np.fft.ifft(convolve(X, W)) 34 | 35 | plt.plot(t, x_ham) 36 | plt.show() 37 | -------------------------------------------------------------------------------- /kkazama/chapter02/10.py: -------------------------------------------------------------------------------- 1 | from wsgiref.headers import tspecials 2 | import numpy as np 3 | from matplotlib import pyplot as plt 4 | 5 | 6 | def hamm(N): 7 | w = np.zeros(N) 8 | for n in range(N): 9 | w[n] = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 10 | return w 11 | 12 | 13 | def conv(x, h): 14 | X = np.fft.fft(x) 15 | H = np.fft.fft(h) 16 | y = np.fft.ifft(X * H) 17 | 18 | return y 19 | 20 | 21 | A = 1 # 振幅 22 | f = 440 # 周波数 23 | sec = 3 # 長さ 24 | fs = 16000 # サンプリング周波数 25 | 26 | t = np.arange(0, sec, 1 / fs) # 時間インデックス 27 | 28 | xt = A * np.sin(2 * np.pi * f * t) # 正弦波 29 | Xk = np.fft.fft(xt) 30 | 31 | # 窓関数 32 | ham = hamm(len(xt)) 33 | ham_dft = np.fft.fft(ham) 34 | 35 | # IDFT 36 | ham_sig = np.fft.ifft(conv(Xk, ham_dft)) 37 | 38 | 39 | plt.plot(ham_sig) 40 | plt.show() 41 | -------------------------------------------------------------------------------- /muesaka/chapter01/01.py: -------------------------------------------------------------------------------- 1 | import matplotlib.pyplot as plt 2 | import numpy as np 3 | import better_exceptions 4 | better_exceptions.MAX_LENGTH = None 5 | 6 | 7 | def create_signal(a, sec, sr, f): # 信号生成 8 | t = np.linspace(0, sec, int(sr * sec)) 9 | s = a * np.sin(2 * np.pi * f * t) 10 | return t, s 11 | 12 | 13 | def plot_signal(t, s, right, filename): # 単一信号プロット 14 | plt.plot(t, s) 15 | plt.xlim(0, right) 16 | plt.xlabel('Time') 17 | plt.ylabel('Amplitude') 18 | plt.savefig(filename) 19 | plt.show() 20 | 21 | 22 | if __name__ == "__main__": 23 | a = 1.0 # 振幅 24 | sec = 3.0 # 信号長 25 | sr = 16000 # サンプリング周波数 26 | f = 440 # 周波数 27 | plot_right = 0.03 # x軸プロット上限 28 | 29 | signal = create_signal(a, sec, sr, f) 30 | plot_signal(*signal, plot_right, '01.pdf') 31 | -------------------------------------------------------------------------------- /kyamada/chapter02/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def dft(x): 6 | """ 7 | DFT 8 | Input: 9 | x: input array 10 | Output: 11 | X: output DFT array 12 | """ 13 | N = len(x) 14 | X = np.zeros(N, dtype=complex) 15 | for k in range(N): 16 | for n in range(N): 17 | X[k] += x[n] * np.exp((-2j * np.pi * k * n) / N) 18 | return X 19 | 20 | 21 | if __name__ == '__main__': 22 | x = np.array([1, 0, 0, 0, 0, 0, 0, 0]) 23 | X = dft(x) 24 | X_amp = 20 * np.log10(np.abs(X)) 25 | X_phase = 20 * np.log10(np.angle(X)) 26 | plt.subplot(2, 1, 1) 27 | plt.stem(X_amp) 28 | plt.grid() 29 | plt.title("Amplitude") 30 | plt.subplot(2, 1, 2) 31 | plt.stem(X_phase) 32 | plt.grid() 33 | plt.title("Phase") 34 | plt.show() 35 | -------------------------------------------------------------------------------- /tkawamura2/chapter01/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | from scipy.io.wavfile import read, write 4 | 5 | #from 08 import set_snr 6 | def set_snr(s, snr): 7 | A_s = np.linalg.norm(s) 8 | x = np.random.randn(round(len(s))) 9 | x = A_s * x /(10**(snr/20)*np.linalg.norm(x)) 10 | return x 11 | 12 | sec = 3.0 # 信号の長さ s 13 | sf = 16000 # サンプリング周波数 Hz 14 | snr = 6 15 | 16 | fs, si = read("660_440.wav") 17 | 18 | t = np.arange(0, sec, 1/fs) 19 | y_1 = si[:,0] + set_snr(si[:,0], snr) 20 | y_2 = si[:,1] + set_snr(si[:,1], snr) 21 | 22 | y_1 = np.convolve(y_1,np.ones(5)/5, mode='valid') 23 | y_2 = np.convolve(y_2,np.ones(5)/5, mode='valid') 24 | 25 | y_1 = np.pad(y_1, (0,len(t)-len(y_1))) 26 | y_2 = np.pad(y_2, (0,len(t)-len(y_2))) 27 | 28 | plt.plot(t, y_1) 29 | plt.plot(t, y_2) 30 | plt.xlim([0, 0.03]) 31 | plt.show() -------------------------------------------------------------------------------- /ynakamura/chapter04/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def zero_pad(x, L, S): 5 | x_pad = np.pad(x, L - S) 6 | if x_pad.shape[0] % S != 0: 7 | x_pad = np.pad(x_pad, ((0, S - (x_pad.shape[0] % S)))) 8 | return x_pad 9 | 10 | 11 | def frame_div(x, L, S): 12 | x_pad = zero_pad(x, L, S) 13 | T = (x_pad.shape[0] - L) // S 14 | x_div = [] 15 | for nT in range(T): 16 | x_t = [] 17 | for nL in range(L): 18 | x_t.append(x_pad[nT * S + nL]) 19 | x_div.append(x_t) 20 | return np.array(x_div) 21 | 22 | 23 | def stft(x, L, S, win): 24 | x_div = frame_div(x, L, S) 25 | T = x_div.shape[0] 26 | X = [] 27 | for t in range(T): 28 | tmp = x_div[t, :] 29 | tmp = tmp * win 30 | tmp = np.fft.rfft(tmp) 31 | X.append(tmp) 32 | return np.array(X).T 33 | -------------------------------------------------------------------------------- /muesaka/chapter02/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def dft(x): 6 | N = len(x) 7 | n = np.arange(N) 8 | dft_x = np.zeros(N, dtype=complex) 9 | for k in range(N): 10 | dft_x[k] = np.sum(x[n] * np.exp(-1j * 2 * np.pi * k * n / N)) 11 | return dft_x 12 | 13 | 14 | def idft(x): 15 | N = len(x) 16 | k = np.arange(N) 17 | idft_x = np.zeros(N, dtype=complex) 18 | for n in range(N): 19 | idft_x[n] = 1 / N * np.sum(x[k] * np.exp(1j * 2 * np.pi * k * n / N)) 20 | return idft_x 21 | 22 | 23 | if __name__ == "__main__": 24 | delta = np.array([1, 0, 0, 0, 0, 0, 0, 0]) # インパルス信号 25 | 26 | dft_delta = dft(delta) 27 | idft_delta = idft(dft_delta) 28 | 29 | plt.stem(idft_delta.real) 30 | plt.xlabel("index") 31 | plt.ylabel("amplitude") 32 | plt.show() 33 | -------------------------------------------------------------------------------- /muesaka/chapter04/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def synthetic_window(S, w): 5 | L = len(w) 6 | Q = int(L / S) 7 | w_s = np.empty(L, dtype=float) 8 | for i in range(L): 9 | w_s[i] = 0 10 | for m in range(-(Q - 1), Q): 11 | if i - m * S < L: 12 | w_s[i] += w[i - m * S] ** 2 13 | w_s[i] = w[i] / w_s[i] 14 | return w_s 15 | 16 | 17 | def istft(S, X): 18 | F, T = X.shape 19 | N = 2 * (F - 1) 20 | M = S * (T - 1) + N 21 | x_hat = np.zeros(M, dtype=complex) 22 | z = np.zeros((T, N), dtype=complex) 23 | w = np.hamming(N) 24 | w_s = synthetic_window(S, w) 25 | 26 | for t in range(T): 27 | for n in range(N): 28 | z[t, n] = np.fft.irfft(X[:, t])[n] 29 | x_hat[t * S + n] = x_hat[t * S + n] + w_s[n] * z[n] 30 | 31 | return x_hat 32 | -------------------------------------------------------------------------------- /tkawamura2/chapter05/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def amvector(mic_arg, theta, f): 5 | c = 334 # 音速 6 | 7 | u = np.array([np.sin(theta), np.cos(theta), 0]) 8 | 9 | tmp = 2 * np.pi * f / c * u * mic_arg 10 | return np.exp(tmp*1j) 11 | 12 | 13 | d = 0.05 14 | M = 3 15 | 16 | p = (np.arange(M) - (M - 1) / 2) * d 17 | p = p.reshape([1, p.size]) 18 | zero = np.zeros(p.size) 19 | zero = zero.reshape([1, zero.size]) 20 | p_m = np.concatenate([p, zero, zero]).T 21 | print(amvector(p_m, np.pi/4, 1000)) 22 | 23 | r = 0.05 24 | 25 | p = 2 * np.pi / M * np.arange(M) 26 | p_1 = r * np.sin(p) 27 | p_1 = p_1.reshape([1, p_1.size]) 28 | p_2 = r * np.cos(p) 29 | p_2 = p_2.reshape([1, p_2.size]) 30 | zero = np.zeros(p.size) 31 | zero = zero.reshape([1, zero.size]) 32 | p_m = np.concatenate([p_1, p_2, zero]).T 33 | print(amvector(p_m, np.pi/4, 1000)) 34 | -------------------------------------------------------------------------------- /ttakahashi/chapter02/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | A = 1 # 振幅 5 | f = 440 # 周波数 Hz 6 | sec = 3.0 # 信号の長さ s 7 | sr = 16000 # サンプリング周波数 Hz 8 | 9 | t = np.arange(sec * sr) / sr # サンプリング点の生成 10 | y = A * np.sin(2 * np.pi * f * t) # 正弦波の生成 11 | y_fft = np.fft.fft(y) 12 | amp = 20 * np.log10(np.abs(y_fft)) 13 | phase = 20 * np.log10(np.angle(y_fft)) 14 | 15 | fig = plt.figure() 16 | ax1 = fig.add_subplot(2, 1, 1) 17 | ax2 = fig.add_subplot(2, 1, 2) 18 | ax1.plot(amp) 19 | ax2.plot(phase) 20 | ax1.set_title("amplitude") 21 | ax2.set_title("phase") 22 | ax1.set_xlabel("k") 23 | ax2.set_xlabel("k") 24 | ax1.set_ylabel("amplitude spectrum [db]") 25 | ax2.set_ylabel("phase spectrum [db]") 26 | ax1.grid(True) 27 | ax2.grid(True) 28 | fig.tight_layout() 29 | plt.savefig("outputs/06.pdf") 30 | plt.show() 31 | 32 | print("success!") 33 | -------------------------------------------------------------------------------- /kyamada/chapter01/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def create_snr_noise( 6 | x, 7 | snr 8 | ): 9 | """ 10 | Create a sinusoid wave with SNR 11 | Input: 12 | x: wave data 13 | snr: SNR 14 | Output: 15 | noise: noise data 16 | """ 17 | # ホワイトノイズの作成 18 | noise = np.random.randn(len(t)) 19 | # 混合をする 20 | noise = noise / np.sqrt(np.sum(noise ** 2)) # エネルギーの正規化 21 | noise = noise * np.sqrt(np.sum(x ** 2)) 22 | noise = noise * 10 ** (-snr / 20) 23 | 24 | return noise 25 | 26 | 27 | if __name__ == '__main__': 28 | np.random.seed(0) # シード値 29 | time = 0.01 30 | fs = 16000 31 | t = np.arange(0, time, 1/fs) 32 | x = 1000 * np.sin(2 * np.pi * 440 * t) 33 | noise = create_snr_noise(x, snr=10) 34 | plt.plot(t, noise) 35 | plt.show() 36 | -------------------------------------------------------------------------------- /tkawamura2/chapter02/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def convolve(x, y): 6 | N = x.size 7 | out = np.zeros(N + y.size, dtype=np.complex128) 8 | for n in range(y.size): 9 | out[n: n + N] += x * y[n] 10 | return out[:N] 11 | 12 | 13 | def hamming(N): 14 | w = 0.54 - 0.46 * np.cos(2 * np.pi * np.arange(N) / (N - 1)) 15 | return w 16 | 17 | 18 | a = 1 # 振幅 19 | fs = 16000 # サンプリング周波数 20 | f = 440 # 周波数 21 | sec = 1 # 秒 3→1 22 | 23 | x = np.arange(sec * fs) / fs 24 | y = np.sin(2 * x * np.pi * f) 25 | 26 | Y = np.fft.fft(y) 27 | w = hamming(len(x)) 28 | W = np.fft.fft(w) 29 | 30 | Y = np.pad(Y, [int(len(Y) / 2), int(len(Y) / 2 - 1)]) 31 | 32 | Y_win = np.convolve(Y, W, mode="valid") 33 | # Y_win = convolve(Y, W) 34 | y_win = np.fft.ifft(Y_win) 35 | 36 | plt.plot(x, y_win.real) 37 | plt.show() 38 | -------------------------------------------------------------------------------- /ynakamura/chapter05/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def calc_spatial_correlation_matrix(X): 5 | M, F, T = X.shape 6 | 7 | x = [0] * M 8 | for m in range(M): 9 | x[m] = X[m, :, :] 10 | x = np.array(x) 11 | 12 | R = [] 13 | for f in range(F): 14 | sum = np.zeros((M, M)).astype(np.complex128) 15 | for t in range(T): 16 | tmp1 = x[:, f, t].reshape(-1, 1) 17 | tmp2 = np.conj(x[:, f, t]).reshape(-1, 1).T 18 | tmp3 = tmp1 @ tmp2 19 | sum += tmp3 20 | R.append(1 / T * sum) 21 | R = np.array(R) 22 | 23 | return R 24 | 25 | 26 | X_1 = np.array([[1, -1j, -1, 1j], [2, -2j, -2, 2j], [3, -3j, -3, 3j]]) 27 | X_2 = np.array([[4, -2 * 1j, 1, 0], [2, -1j, 0, 0], [1, -1j, 1, 0]]) 28 | X = np.stack([X_1, X_2]) 29 | 30 | R = calc_spatial_correlation_matrix(X) 31 | print(R) 32 | -------------------------------------------------------------------------------- /ykuriki/chapter05/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def clc_amv_cir(r, M, theta, f): 6 | """アレイマニフォールドベクトルを計算する 7 | 8 | Args: 9 | r (double): アレイ半径 [m] 10 | M (int): マイク数 11 | theta (int/double): 音源方向 [deg] 12 | f (int/double): 周波数 [Hz] 13 | 14 | 15 | Returns: 16 | a (ndarray): アレイマニフォールドベクトル 17 | 18 | """ 19 | c = 334 20 | theta = np.radians(theta) 21 | a = np.zeros(M, dtype="complex") 22 | u = np.array([np.sin(theta), np.cos(theta), 0]) 23 | 24 | for m in range(M): 25 | p_m = np.array( 26 | [r * np.sin(2 * np.pi / M * m), r * np.cos(2 * np.pi / M * m), 0] 27 | ) 28 | a[m] = np.exp(1j * 2 * np.pi * f / c * u @ p_m) 29 | 30 | return a 31 | 32 | 33 | r = 0.05 34 | M = 3 35 | theta = 45 36 | f = 1000 37 | 38 | print(clc_amv_cir(r, M, theta, f)) 39 | -------------------------------------------------------------------------------- /kyamada/Chapter03/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def circle_convolution(x, h): 5 | """ 6 | Circle convolution 7 | Inputs: 8 | x: input signal 9 | h: convolution signal 10 | Returns: 11 | z: output signal 12 | """ 13 | N = x.size # 配列のサイズ 14 | n = x.size + h.size - 1 # 全体のサイズ 15 | z = np.zeros(n) # 出力配列 16 | for i in range(0, n): 17 | z[i] = 0 18 | for k in range(0, N): 19 | index = (i - k) % N 20 | print(x[k], h[index]) 21 | z[i] += x[k] * h[index] 22 | return z 23 | 24 | 25 | if __name__ == "__main__": 26 | x = np.array([4, 3, 2, 1]) 27 | h = np.array([1, -1, 1, -1]) 28 | np_convolution = np.convolve(x, h) 29 | print("Numpy convolution:", np_convolution) 30 | convolution = circle_convolution(x, h) 31 | print("My convolution:", convolution) 32 | -------------------------------------------------------------------------------- /kyamada/Chapter03/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def linear_convolution(x, h): 5 | """ 6 | Linear convolution 7 | Inputs: 8 | x: input signal 9 | h: convolution signal 10 | Returns: 11 | z: output signal 12 | """ 13 | N = x.size # 配列のサイズ 14 | n = x.size + h.size - 1 # 全体のサイズ 15 | z = np.zeros(n) # 出力配列 16 | for i in range(0, n): 17 | z[i] = 0 18 | for k in range(0, N): 19 | if (i - k) >= 0 and (i - k) < N: # 入力配列のサイズを超えないようにする 20 | z[i] += x[k] * h[(i - k)] 21 | return z 22 | 23 | 24 | if __name__ == "__main__": 25 | x = np.array([4, 3, 2, 1]) 26 | h = np.array([1, -1, 1, -1]) 27 | np_convolution = np.convolve(x, h) 28 | print("Numpy convolution:", np_convolution) 29 | convolution = linear_convolution(x, h) 30 | print("My convolution:", convolution) 31 | -------------------------------------------------------------------------------- /dsugawara/chapter02/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | # DFT 6 | def dft(f): 7 | N = len(f) 8 | X = np.zeros(N) * 1j 9 | for k in range(N): 10 | x = 0j 11 | for n in range(N): # シグマ計算 12 | a = (2 * np.pi * n * k) / N 13 | x += f[n] * np.e ** (-1j * a) 14 | X[k] = x 15 | return X 16 | 17 | 18 | # IDFT 19 | def idft(F): 20 | N = len(F) 21 | x = np.zeros(N) * 1j 22 | for k in range(N): 23 | X = 0j 24 | for n in range(N): 25 | a = (2 * np.pi * n * k) / N 26 | X += 1 / N * F[n] * np.e ** (1j * a) 27 | x[k] = X 28 | return x 29 | 30 | 31 | # impulse 32 | delta = np.zeros(8) 33 | delta[0] = 1 34 | DELTA = dft(delta) 35 | idft_delta = idft(DELTA) 36 | 37 | 38 | # plot 39 | plt.stem(idft_delta) 40 | plt.savefig("03py_impulse_idft.png") 41 | -------------------------------------------------------------------------------- /smotoyama/chapter03/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def eq(a, b, f, fs): 6 | N = len(a) 7 | M = len(b) 8 | omega = 2 * np.pi * f / fs 9 | a_sum = 1 10 | b_sum = 0 11 | for i in range(1, N): 12 | a_sum += a[i] * np.exp(-1j * omega * i) 13 | for k in range(M): 14 | b_sum += b[k] * np.exp(-1j * omega * k) 15 | 16 | H = b_sum / a_sum 17 | 18 | return H 19 | 20 | 21 | if __name__ == "__main__": 22 | fs = 16000 23 | f = np.arange(fs)/ fs 24 | 25 | a = [1, 0.3] 26 | b = [0.4] 27 | 28 | H = eq(a, b, f, fs) 29 | 30 | fig = plt.figure() 31 | ax1 = fig.add_subplot(2, 1, 1) 32 | ax2 = fig.add_subplot(2, 1, 2) 33 | ax1.stem(np.abs(H)) 34 | ax2.stem(np.angle(H)) 35 | ax1.set_title("Amplitude") 36 | ax2.set_title("Angle") 37 | fig.tight_layout() 38 | plt.show() 39 | -------------------------------------------------------------------------------- /ykuriki/chapter02/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | fs = 16000 6 | f = 440 7 | r = 1 8 | sec = 3 9 | 10 | t = np.arange(0, fs * sec) / fs 11 | sig = r * np.sin(f * 2 * np.pi * t) 12 | 13 | fft_sig = np.fft.fft(sig) 14 | 15 | amp = 20 * np.log10(np.abs(fft_sig)) 16 | phase = 20 * np.log10(np.angle(fft_sig)) 17 | 18 | freq = np.arange(len(fft_sig)) / len(fft_sig) * fs 19 | 20 | 21 | # プロット 22 | fig, ax = plt.subplots(nrows=2, ncols=1, figsize=(6, 5)) 23 | 24 | p0 = ax[0].plot(freq, amp) 25 | ax[0].set_xlabel("frequency [Hz]") 26 | ax[0].set_ylabel("amplitude spectrum [dB]") 27 | ax[0].set_title("Amplitude spectrum") 28 | ax[0].grid() 29 | 30 | p0 = ax[1].plot(freq, phase) 31 | ax[1].set_xlabel("frequency [Hz]") 32 | ax[1].set_ylabel("phase spectrum [dB]") 33 | ax[1].set_title("Phase spectrum") 34 | ax[1].grid() 35 | 36 | plt.tight_layout() 37 | 38 | plt.show() 39 | -------------------------------------------------------------------------------- /kkazama/chapter01/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | #ノイズの振幅調整 5 | def cal_adj_amp(s, x, snr): 6 | x = x / np.sqrt(sum(x * x)) 7 | x = x * np.sqrt(sum(s * s)) 8 | x = x * 10 ** (-snr / 20) 9 | 10 | return x 11 | 12 | A = 1 #信号の振幅 13 | f = 440 14 | sec = 3 #信号の長さ 15 | fs = 16000 #サンプリング周波数 16 | 17 | #調整前 18 | r = np.random.rand(round(fs*sec)) 19 | x = 2 * A * (r - 0.5) #ホワイトノイズの生成 20 | 21 | t = np.arange(0, sec, 1/fs) #時間インデックス 22 | s = A * np.sin(2 * np.pi * f * t) #正弦波 23 | 24 | #調整後 25 | x = cal_adj_amp(s, x, 6.0) 26 | 27 | plt.subplot(2, 1, 1) 28 | plt.plot(t, s) 29 | plt.plot(t, x) 30 | plt.xlim(0, 0.03) #軸範囲 31 | 32 | #移動平均 33 | y = np.zeros(x.size) 34 | M = 5 35 | 36 | for i in range(M+1, y.size - M): 37 | y[i] = np.mean(x[i - M : i + M]) 38 | 39 | plt.subplot(2, 1, 2) 40 | plt.plot(t, s) 41 | plt.plot(t, y) 42 | plt.xlim(0, 0.03) 43 | plt.show() -------------------------------------------------------------------------------- /knishida/chapter01/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def SNratio(s, x): 6 | ssum = 0.0 7 | xsum = 0.0 8 | for i in range(len(s)): 9 | ssum += s[i] ** 2 10 | xsum += x[i] ** 2 11 | return 10 * np.log10(ssum / xsum) 12 | 13 | 14 | def ajust_SN(s, x, SN): 15 | ssum = 0.0 16 | xsum = 0.0 17 | for i in range(len(s)): 18 | ssum += s[i] ** 2 19 | xsum += x[i] ** 2 20 | return np.sqrt(ssum / xsum * 10 ** (-SN / 10)) 21 | 22 | 23 | if __name__ == "__main__": 24 | A = 1 25 | f = 440.0 26 | sec = 3.0 27 | fs = 16000 28 | SN = 6 29 | 30 | t = np.arange(0, sec, 1 / fs) 31 | y = A * np.sin(2 * np.pi * f * t) 32 | n = np.random.rand(round(fs * sec)) 33 | 34 | X = ajust_SN(y, n, SN) 35 | 36 | obs = y + X * n 37 | plt.plot(t, obs) 38 | plt.xlim([0, 0.03]) 39 | plt.show() 40 | -------------------------------------------------------------------------------- /smotoyama/chapter03/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def eq(a, b, f, fs): 6 | N = len(a) 7 | M = len(b) 8 | omega = 2 * np.pi * f / fs 9 | a_sum = 1 10 | b_sum = 0 11 | for i in range(1, N): 12 | a_sum += a[i] * np.exp(-1j * omega * i) 13 | for k in range(M): 14 | b_sum += b[k] * np.exp(-1j * omega * k) 15 | 16 | H = b_sum / a_sum 17 | 18 | return H 19 | 20 | 21 | if __name__ == "__main__": 22 | fs = 16000 23 | f = np.arange(fs)/ fs 24 | 25 | a = [1] 26 | b = [0.2, 0.2, 0.2, 0.2, 0.2] 27 | 28 | H = eq(a, b, f, fs) 29 | 30 | fig = plt.figure() 31 | ax1 = fig.add_subplot(2, 1, 1) 32 | ax2 = fig.add_subplot(2, 1, 2) 33 | ax1.stem(np.abs(H)) 34 | ax2.stem(np.angle(H)) 35 | ax1.set_title("Amplitude") 36 | ax2.set_title("Angle") 37 | fig.tight_layout() 38 | plt.show() 39 | -------------------------------------------------------------------------------- /ykuriki/chapter02/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def clc_dft(x): 6 | """x[n]の離散フーリエ変換(DFT)を計算する 7 | 8 | Args: 9 | x (ndarray): 信号 (float型1次元配列) 10 | 11 | Returns: 12 | X (ndarray): 信号のDFT (complex型1次元配列) 13 | 14 | """ 15 | N = len(x) 16 | n = np.arange(N) 17 | X = np.zeros(N, dtype="complex64") 18 | 19 | for k in range(N): 20 | X[k] = np.sum(x[n] * np.exp(-2j * np.pi * k * n / N)) 21 | 22 | return X 23 | 24 | 25 | def clc_idft(X): 26 | """X[n]の逆離散フーリエ変換(IDFT)を計算する 27 | 28 | Args: 29 | X (ndarray): 信号 (complex型1次元配列) 30 | 31 | Returns: 32 | x (ndarray): 信号のIDFT (float型1次元配列) 33 | 34 | """ 35 | N = len(X) 36 | k = np.arange(N) 37 | x = np.zeros(N, dtype="float64") 38 | 39 | for n in range(N): 40 | x[n] = np.sum(X[k] * np.exp(2j * np.pi * k * n / N)) / N 41 | 42 | return x 43 | -------------------------------------------------------------------------------- /ykuriki/chapter04/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def zero_pad(x, L, S): 6 | """x[n]に零詰めする 7 | 8 | Args: 9 | x (ndarray): 信号(1次元配列) 10 | L (int): 窓幅 11 | S (int): シフト幅 12 | 13 | Returns: 14 | ndarray: 零詰めされた信号 15 | 16 | """ 17 | x = np.block([np.zeros(L - S), x, np.zeros(L - S)]) 18 | 19 | if len(x) % S != 0: 20 | x = np.block([x, np.zeros(S - (len(x) % S))]) 21 | 22 | return x 23 | 24 | 25 | def frame_div(x, L, S): 26 | """x[n]をフレーム分割する 27 | 28 | Args: 29 | x (ndarray): 信号(1次元配列) 30 | L (int): 窓幅 31 | S (int): シフト幅 32 | 33 | Returns: 34 | ndarray: フレーム分割された信号 35 | 36 | """ 37 | x = zero_pad(x, L, S) 38 | T = int(np.floor((len(x) - L) / S)) + 1 39 | xt = np.zeros((T, L)) 40 | 41 | for t in range(T): 42 | xt[t] = x[t * S : t * S + L] 43 | 44 | return xt 45 | -------------------------------------------------------------------------------- /tkawamura2/chapter03/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def freq_res(a, b, fs): 6 | omega = np.arange(0, fs)/fs 7 | F = omega.size 8 | N = a.size 9 | M = b.size 10 | H = np.zeros(F, dtype=np.complex128) 11 | 12 | for f, o in enumerate(omega): 13 | a_sum = 1 + 0j 14 | b_sum = 0 + 0j 15 | for k1 in np.arange(1, N): 16 | a_sum = a_sum + a[k1]*np.exp(-1j*o*k1) 17 | for k2 in range(M): 18 | b_sum = b_sum + b[k2]*np.exp(-1j*o*k2) 19 | H[f] = b_sum/a_sum 20 | return H 21 | 22 | 23 | fs = 16000 24 | b = np.array([0.4, 0], dtype=np.complex128) 25 | a = np.array([0, 0.3], dtype=np.complex128) 26 | 27 | H = freq_res(a, b, fs) 28 | 29 | plt.plot(np.abs(H)) 30 | # plt.plot(20*np.log10(np.abs(H))) 31 | plt.show() 32 | 33 | plt.plot(np.angle(H)) 34 | # plt.plot(20*np.log10(np.angle(H))) 35 | plt.show() 36 | -------------------------------------------------------------------------------- /ttakahashi/chapter02/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | # Function to calculate DFT 6 | def dft(x): 7 | N = len(x) 8 | n = np.arange(N) 9 | X = np.zeros(N, dtype="complex64") 10 | for k in range(N): 11 | X[k] = np.sum(x[n] * np.exp(-2j * np.pi * k * n / N)) 12 | return X 13 | 14 | 15 | # Function to calculate IDFT 16 | def idft(X): 17 | N = len(X) 18 | k = np.arange(N) 19 | x = np.zeros(N, dtype="float64") 20 | for n in range(N): 21 | x[n] = np.sum(X[k] * np.exp(2j * np.pi * k * n / N)) / N 22 | return x 23 | 24 | 25 | delta = np.array([1, 0, 0, 0, 0, 0, 0, 0]) 26 | delta_dft = dft(delta) 27 | delta_idft = idft(delta_dft) 28 | 29 | plt.stem(np.arange(len(delta_idft)), delta_idft) 30 | plt.xlabel("n") 31 | plt.ylabel("amplitude") 32 | plt.grid() 33 | plt.savefig("outputs/03.pdf") 34 | plt.show() 35 | 36 | print("success!") 37 | -------------------------------------------------------------------------------- /ttakahashi/chapter03/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def difference_equation(x, a, b): 6 | """ 7 | 一般化差分方程式 8 | Args: 9 | x (ndarray): 入力信号 10 | a (ndarray): y の係数 11 | b (ndarray): x の係数 12 | Returns: 13 | y (ndarray): 出力信号 14 | """ 15 | y = np.zeros(len(x)) 16 | for n in range(0, len(x)): 17 | for k in range(1, len(a)): 18 | y[n] -= a[k] * y[n - k] 19 | for k in range(0, len(b)): 20 | if (n - k) >= 0 or (n - k) < len(a) or n > len(a): # 入力配列のサイズを超えないようにする 21 | y[n] += b[k] * x[n - k] 22 | y = y / a[0] 23 | return y 24 | 25 | 26 | x = np.zeros(10) 27 | x[0] = 1 28 | a = np.array([1, -0.3]) 29 | b = np.array([0.4]) 30 | y = difference_equation(x, a, b) 31 | plt.stem(y) 32 | plt.grid() 33 | plt.savefig("outputs/07.pdf") 34 | plt.show() 35 | 36 | print("success!") 37 | -------------------------------------------------------------------------------- /ynakamura/chapter02/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def dft(x): 6 | N = x.shape[0] 7 | X = np.array([]) 8 | for k in range(N): 9 | sum = 0 10 | for n in range(N): 11 | sum += x[n] * np.exp(-1j * 2 * np.pi * k * n / N) 12 | X = np.append(X, sum) 13 | return X 14 | 15 | 16 | def idft(X): 17 | N = X.shape[0] 18 | x = np.array([]) 19 | for n in range(N): 20 | sum = 0 21 | for k in range(N): 22 | sum += X[k] * np.exp(1j * 2 * np.pi * k * n / N) 23 | x = np.append(x, sum / N) 24 | return x 25 | 26 | 27 | N = 8 28 | delta = np.zeros(N) 29 | delta[0] = 1 30 | Delta = dft(delta) 31 | 32 | # 振幅スペクトルの出力 33 | plt.stem(20 * np.log10(np.abs(Delta))) 34 | plt.title("amp") 35 | plt.show() 36 | 37 | # 位相スペクトルの出力 38 | plt.stem(20 * np.log10(np.angle(Delta))) 39 | plt.title("angle") 40 | plt.show() 41 | -------------------------------------------------------------------------------- /ynakamura/chapter05/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def adjust_amp(x, snr, fs=16000, sec=3): 6 | whitenoise = np.random.rand(int(fs * sec)) * 2 - 1 7 | a = np.sqrt(np.sum(x**2) / np.sum(whitenoise**2) * (10 ** (-snr / 10))) 8 | whitenoise = a * np.random.rand(int(fs * sec)) - (a / 2) 9 | return whitenoise 10 | 11 | 12 | fs = 16000 13 | sec = 1 14 | A = 1 15 | f = 440 16 | t = np.linspace(0, sec, fs * sec) 17 | N = fs * sec 18 | 19 | s = A * np.cos(2 * np.pi * f * t) 20 | e = adjust_amp(s, 10, fs, sec) 21 | 22 | x_1 = s + e 23 | 24 | x_2 = np.zeros((N)) 25 | for n in range(N): 26 | x_2[n] = s[n - 10] + e[n] 27 | 28 | 29 | x_3 = np.zeros((N)) 30 | for n in range(N): 31 | x_3[n] = s[n - 20] + e[n] 32 | 33 | plt.plot(t, x_1, label="x1") 34 | plt.plot(t, x_2, label="x2") 35 | plt.plot(t, x_3, label="x3") 36 | plt.xlim(0, 0.01) 37 | plt.legend() 38 | plt.show() 39 | -------------------------------------------------------------------------------- /ttakahashi/chapter03/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def circle_conv_pad(x, h): 5 | """ 6 | x[n]とh[n]の巡回畳み込みを零詰めを行って計算する 7 | Args: 8 | x (ndarray): 信号(1次元配列) 9 | h (ndarray): インパルス応答(1次元配列) 10 | Returns: 11 | y (ndarray): 畳み込み結果(1次元配列) 12 | """ 13 | y = np.zeros(len(h) + len(x) - 1, dtype=np.float32) 14 | x = np.pad(x, (0, len(x) - 1), "constant") 15 | h = np.pad(h, (0, len(x) - 1), "constant") 16 | for k in range(0, len(y)): 17 | y[k] = 0.0 18 | for m in range(0, len(x)): 19 | n = k - m 20 | if n >= 0 and n < len(h): 21 | index = (k - m) % len(x) 22 | y[k] += x[m] * h[index] 23 | return y 24 | 25 | 26 | impulse = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] 27 | kernel = [4, 3, 2, 1, 0, 0] 28 | print(circle_conv_pad(impulse, kernel)) 29 | print(np.convolve(impulse, kernel)) 30 | 31 | print("success!") 32 | -------------------------------------------------------------------------------- /tkawamura2/chapter03/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def freq_res(a, b, fs): 6 | omega = np.arange(0, fs)/fs 7 | F = omega.size 8 | N = a.size 9 | M = b.size 10 | H = np.zeros(F, dtype=np.complex128) 11 | 12 | for f, o in enumerate(omega): 13 | a_sum = 1 + 0j 14 | b_sum = 0 + 0j 15 | for k1 in np.arange(1, N): 16 | a_sum = a_sum + a[k1]*np.exp(-1j*o*k1) 17 | for k2 in range(M): 18 | b_sum = b_sum + b[k2]*np.exp(-1j*o*k2) 19 | H[f] = b_sum/a_sum 20 | return H 21 | 22 | 23 | fs = 16000 24 | b = np.array([0.2, 0.2, 0.2, 0.2, 0.2], dtype=np.complex128) 25 | a = np.array([0, 0], dtype=np.complex128) 26 | 27 | H = freq_res(a, b, fs) 28 | 29 | plt.plot(np.abs(H)) 30 | # plt.plot(20*np.log10(np.abs(H))) 31 | plt.show() 32 | 33 | plt.plot(np.angle(H)) 34 | # plt.plot(20*np.log10(np.angle(H))) 35 | plt.show() 36 | -------------------------------------------------------------------------------- /dsugawara/chapter02/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | # Humming窓 6 | def Humming(N): 7 | w = [] 8 | for n in range(N): 9 | a = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 10 | w.append(a) 11 | return w 12 | 13 | 14 | # 第1章1. の信号 15 | A = 1.0 16 | f = 440.0 17 | sf = 16000 18 | sec = 3.0 19 | 20 | t = np.arange(0, sec, 1 / sf) 21 | 22 | y = A * np.sin(2 * np.pi * f * t) 23 | 24 | dft_y = np.fft.fft(y) 25 | 26 | 27 | # 窓関数 28 | win = Humming(len(y)) 29 | 30 | dft_win = np.fft.fft(win) 31 | 32 | 33 | # 畳み込み 34 | N = len(y) 35 | n = np.arange(0, N) 36 | Z = np.empty(0) 37 | dft_y = np.pad(dft_y, [int(len(dft_y) / 2), int(len(dft_y) / 2 - 1)]) 38 | for k in range(0, N): 39 | temp = np.sum(dft_y[n] * dft_win[(k - n) % N]) 40 | Z = np.append(Z, temp) 41 | 42 | idft_z = np.fft.ifft(Z) 43 | 44 | plt.plot(t, idft_z) 45 | plt.savefig("10py_circle_convolution.png") 46 | -------------------------------------------------------------------------------- /ttakahashi/chapter02/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | # Function to calculate DFT 6 | def dft(x): 7 | N = len(x) 8 | n = np.arange(N) 9 | X = np.zeros(N, dtype="complex64") 10 | for k in range(N): 11 | X[k] = np.sum(x[n] * np.exp(-2j * np.pi * k * n / N)) 12 | return X 13 | 14 | 15 | delta = np.array([1, 0, 0, 0, 0, 0, 0, 0]) 16 | delta_dft = dft(delta) 17 | amp = np.abs(delta_dft) 18 | phase = np.angle(delta_dft) 19 | 20 | fig = plt.figure() 21 | ax1 = fig.add_subplot(2, 1, 1) 22 | ax2 = fig.add_subplot(2, 1, 2) 23 | ax1.stem(amp) 24 | ax2.stem(phase) 25 | ax1.set_title("amplitude") 26 | ax2.set_title("phase") 27 | ax1.set_xlabel("k") 28 | ax2.set_xlabel("k") 29 | ax1.set_ylabel("amplitude spectrum") 30 | ax2.set_ylabel("phase spectrum") 31 | ax1.grid(True) 32 | ax2.grid(True) 33 | fig.tight_layout() 34 | plt.savefig("outputs/04.pdf") 35 | plt.show() 36 | 37 | print("success!") 38 | -------------------------------------------------------------------------------- /dsugawara/chapter05/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def linear_array_vector(d, M, theta, f): 5 | 6 | """直線状アレイのアレイマニフォールドベクトルを求める 7 | 8 | Args: 9 | d (double): アレイ間隔 10 | M (int): マイク数 11 | theta (int): 音源方向(y軸から反時計回りが正の向き) 12 | f (int): 周波数 13 | 14 | Return: 15 | ndarray: アレイマニフォールドベクトル 16 | 17 | """ 18 | c = 334.0 19 | theta = np.pi / 2 + 2 * np.pi * (theta / 360) 20 | u = np.array([np.sin(theta), np.cos(theta), 0]).T 21 | 22 | p = np.zeros([M, 3]) 23 | for m in range(1, M + 1): 24 | idx_m = m - 1 25 | p[idx_m, 0] = ((m - 1) - (M - 1) / 2) * d 26 | p = p.T 27 | a = np.empty([u.T.shape[0], p.shape[1]], dtype="complex") #  a[1, M]になるはず 28 | a = np.exp(2j * np.pi * f / c * np.dot(u.T, p)) 29 | 30 | return a 31 | 32 | 33 | # main 34 | d = 0.05 35 | M = 3 36 | theta = 45 37 | f = 1000 38 | 39 | 40 | print(linear_array_vector(d, M, theta, f)) 41 | -------------------------------------------------------------------------------- /kyamada/chapter02/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def dft(x): 6 | """ 7 | DFT 8 | Input: 9 | x: input array 10 | Output: 11 | X: output DFT array 12 | """ 13 | N = len(x) 14 | X = np.zeros(N, dtype=complex) 15 | for k in range(N): 16 | for n in range(N): 17 | X[k] += x[n] * np.exp((-2j * np.pi * k * n) / N) 18 | return X 19 | 20 | 21 | def idft(x): 22 | """ 23 | IDFT 24 | Input: 25 | x: input array 26 | Output: 27 | X: output DFT array 28 | """ 29 | N = len(x) 30 | X = np.zeros(N, dtype=complex) 31 | for k in range(N): 32 | for n in range(N): 33 | X[k] += x[n] * np.exp((2j * np.pi * k * n) / N) / N 34 | return X 35 | 36 | 37 | if __name__ == '__main__': 38 | x = np.array([1, 0, 0, 0, 0, 0, 0, 0]) 39 | X = dft(x) 40 | y = idft(X) 41 | plt.stem(y) 42 | plt.show() 43 | -------------------------------------------------------------------------------- /muesaka/chapter03/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def calculate_frequency_responce(a, b, f, fs): 6 | N = len(a) 7 | M = len(b) 8 | tmp_a = 0 9 | tmp_b = 0 10 | omega = 2 * np.pi * f / fs 11 | 12 | for k in range(0, M): 13 | tmp_b += b[k] * np.exp(-1j * omega * k) 14 | for k in range(1, N): 15 | tmp_a += a[k] * np.exp(-1j * omega * k) 16 | H = tmp_b / (1 + tmp_a) 17 | 18 | return H 19 | 20 | 21 | if __name__ == "__main__": 22 | a = np.array([1, -0.3]) 23 | b = np.array([0.4]) 24 | N = 16 25 | H = np.zeros(N, dtype="complex") 26 | fs = 16000 27 | f = np.zeros(N) 28 | 29 | for i in range(N): 30 | f[i] = i * fs / N 31 | H[i] = calculate_frequency_responce(a, b, f[i], fs) 32 | 33 | fig, ax = plt.subplots(nrows=2, ncols=1) 34 | ax[0].stem(f, np.abs(H)) 35 | ax[1].stem(f, np.angle(H)) 36 | plt.tight_layout() 37 | plt.show() 38 | -------------------------------------------------------------------------------- /tkawamura/chapter01/03.py: -------------------------------------------------------------------------------- 1 | # WAV ファイルの作成(モノラル): 1.で作成した正弦波を 16bit PCM フォーマットで wav ファイルとして保存せよ. 2 | 3 | import numpy as np 4 | import soundfile as sf 5 | 6 | 7 | def main(): 8 | amp = 1 # amplitude of sine wave 9 | freq1 = 440 # frequency of sine wave (Hz) 10 | freq2 = 660 11 | fs = 16000 # sampling frequency (Hz) 12 | sig_len = 3 # signal length (sec.) 13 | 14 | filename = "sin-stereo.wav" 15 | 16 | t = np.arange(fs * sig_len) / fs # time 17 | sin_wave1 = amp * np.sin(2 * np.pi * freq1 * t) # Asin(2πft) 18 | sin_wave2 = amp * np.sin(2 * np.pi * freq2 * t) 19 | sin_wave = np.stack((sin_wave1, sin_wave2), axis=1) # stereo 20 | 21 | # save 22 | sf.write(filename, sin_wave, fs, "PCM_16") 23 | 24 | # 確認方法 (soundfile.info: https://pysoundfile.readthedocs.io/en/latest/#soundfile.info) 25 | # dtype2 = sf.info(filename) 26 | # print(f"information : {dtype2}") 27 | 28 | 29 | if __name__ == "__main__": 30 | main() 31 | -------------------------------------------------------------------------------- /muesaka/chapter03/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def calculate_frequency_responce(a, b, f, fs): 6 | N = len(a) 7 | M = len(b) 8 | tmp_a = 0 9 | tmp_b = 0 10 | omega = 2 * np.pi * f / fs 11 | 12 | for k in range(0, M): 13 | tmp_b += b[k] * np.exp(-1j * omega * k) 14 | for k in range(1, N): 15 | tmp_a += a[k] * np.exp(-1j * omega * k) 16 | H = tmp_b / (1 + tmp_a) 17 | 18 | return H 19 | 20 | 21 | if __name__ == "__main__": 22 | a = np.array([1]) 23 | b = np.array([0.2, 0.2, 0.2, 0.2, 0.2]) 24 | N = 16 25 | H = np.zeros(N, dtype="complex") 26 | fs = 16000 27 | f = np.zeros(N) 28 | 29 | for i in range(N): 30 | f[i] = i * fs / N 31 | H[i] = calculate_frequency_responce(a, b, f[i], fs) 32 | 33 | fig, ax = plt.subplots(nrows=2, ncols=1) 34 | ax[0].stem(f, np.abs(H)) 35 | ax[1].stem(f, np.angle(H)) 36 | plt.tight_layout() 37 | plt.show() 38 | -------------------------------------------------------------------------------- /knishida/chapter03/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def frequency_responce(a, b, f, fs): 6 | N = len(a) 7 | M = len(b) 8 | tmpa = 0 9 | tmpb = 0 10 | omega = 2 * np.pi * f / fs 11 | for k in range(1, N): 12 | tmpa += a[k] * np.exp(-1j * omega * k) 13 | for k in range(M): 14 | tmpb += b[k] * np.exp(-1j * omega * k) 15 | H = tmpb / (1 + tmpa) 16 | return H 17 | 18 | 19 | if __name__ == "__main__": 20 | a = [1, 0.3] 21 | b = [0.4] 22 | fs = 16000 23 | N = 16 24 | H = np.zeros(N) 25 | f = 0 26 | 27 | for i in range(N): 28 | f = i / N * fs 29 | H[i] = frequency_responce(a, b, f, fs) 30 | 31 | fig = plt.figure() 32 | ax1 = fig.add_subplot(2, 1, 1) 33 | ax2 = fig.add_subplot(2, 1, 2) 34 | ax1.stem(np.abs(H)) 35 | ax2.stem(np.angle(H)) 36 | ax1.set_title("Amplitude") 37 | ax2.set_title("Angle") 38 | fig.tight_layout() 39 | plt.show() 40 | -------------------------------------------------------------------------------- /dsugawara/chapter05/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def scm(X): 5 | 6 | """空間相関行列(spatial correation matrix)を求める 7 | 8 | Args: 9 | X (ndarray): 複素数行列(3次元配列) 10 | 11 | Return: 12 | ndarray: 空間相関行列 13 | 14 | """ 15 | M = X.shape[0] 16 | F = X.shape[1] 17 | T = X.shape[2] 18 | 19 | R = np.empty([F, M, M], dtype="complex") 20 | x = np.empty(M, dtype="complex") 21 | for f in range(0, F): 22 | for t in range(0, T): 23 | sigma = 0 24 | for m in range(1, M + 1): 25 | idx_m = m - 1 26 | x[idx_m] = X[idx_m, f, t] 27 | x[idx_m].T 28 | x_H = np.conjugate(x.T) 29 | sigma += np.dot(x, x_H) 30 | R[f] = 1 / T * sigma 31 | 32 | return R 33 | 34 | 35 | # main 36 | X1 = np.array([[1, -1j, -1, 1j], [2, -2j, -2, 2j], [3, -3j, -3, 3j]]) 37 | X2 = np.array([[4, -2j, 1, 0], [2, -1j, 0, 0], [1, -1j, 1, 0]]) 38 | X = np.array([X1, X2]) 39 | 40 | print(scm(X)) 41 | -------------------------------------------------------------------------------- /smotoyama/chapter02/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def DFT(x): 6 | N = len(x) 7 | c = np.zeros(N) * 1j 8 | 9 | for i in range(N): 10 | c[i] = np.sum(x * np.exp(-1j * 2 * np.pi * i * np.arange(N) / N)) 11 | 12 | return c 13 | 14 | 15 | def IDFT(X): 16 | N = len(X) 17 | c = np.zeros(N) * 1j 18 | 19 | for i in range(N): 20 | c[i] = (1 / N) * np.sum(X * np.exp(1j * 2 * np.pi * i * np.arange(N) / N)) 21 | 22 | return c 23 | 24 | 25 | if __name__ == "__main__": 26 | N = 8 27 | delta = np.zeros(N) 28 | delta[0] = 1 29 | 30 | X = DFT(delta) 31 | 32 | X_amp = 20 * np.log10(np.abs(X)) 33 | X_ang = 20 * np.log10(np.angle(X)) 34 | 35 | fig = plt.figure() 36 | ax1 = fig.add_subplot(2, 1, 1) 37 | ax2 = fig.add_subplot(2, 1, 2) 38 | ax1.stem(X_amp) 39 | ax2.stem(X_ang) 40 | ax1.set_title("Amplitude") 41 | ax2.set_title("Angle") 42 | fig.tight_layout() 43 | plt.show() 44 | -------------------------------------------------------------------------------- /knishida/chapter03/09.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def frequency_responce(a, b, f, fs): 6 | N = len(a) 7 | M = len(b) 8 | tmpa = 0 9 | tmpb = 0 10 | omega = 2 * np.pi * f / fs 11 | for k in range(1, N): 12 | tmpa += a[k] * np.exp(-1j * omega * k) 13 | for k in range(M): 14 | tmpb += b[k] * np.exp(-1j * omega * k) 15 | H = tmpb / (1 + tmpa) 16 | return H 17 | 18 | 19 | if __name__ == "__main__": 20 | a = [1] 21 | b = [0.2, 0.2, 0.2, 0.2, 0.2] 22 | fs = 16000 23 | N = 16 24 | H = np.zeros(N) 25 | f = 0 26 | 27 | for i in range(N): 28 | f = i / N * fs 29 | H[i] = frequency_responce(a, b, f, fs) 30 | 31 | fig = plt.figure() 32 | ax1 = fig.add_subplot(2, 1, 1) 33 | ax2 = fig.add_subplot(2, 1, 2) 34 | ax1.stem(np.abs(H)) 35 | ax2.stem(np.angle(H)) 36 | ax1.set_title("Amplitude") 37 | ax2.set_title("Angle") 38 | fig.tight_layout() 39 | plt.show() 40 | -------------------------------------------------------------------------------- /dsugawara/chapter05/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def circular_array_vector(r, M, theta, f): 5 | 6 | """円状アレイのアレイマニフォールドベクトルを求める 7 | 8 | Args: 9 | r (double): アレイ半径 10 | M (int): マイク数 11 | theta (int): 音源方向(y軸から反時計回りが正の向き) 12 | f (int): 周波数 13 | 14 | Return: 15 | ndarray: アレイマニフォールドベクトル 16 | 17 | """ 18 | c = 334.0 19 | theta = np.pi / 2 + 2 * np.pi * (theta / 360) 20 | u = np.array([np.sin(theta), np.cos(theta), 0]).T 21 | 22 | p = np.zeros([M, 3]) 23 | for m in range(1, M + 1): 24 | idx_m = m - 1 25 | p[idx_m, 0] = r * np.sin(2 * np.pi / M * (m - 1)) 26 | p[idx_m, 1] = r * np.cos(2 * np.pi / M * (m - 1)) 27 | p = p.T 28 | a = np.empty([u.T.shape[0], p.shape[1]], dtype="complex") #  a[1, M]になるはず 29 | a = np.exp(2j * np.pi * f / c * np.dot(u.T, p)) 30 | 31 | return a 32 | 33 | 34 | # main 35 | r = 0.05 36 | M = 3 37 | theta = 45 38 | f = 1000 39 | 40 | print(circular_array_vector(r, M, theta, f)) 41 | -------------------------------------------------------------------------------- /kyamada/Chapter03/07.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def difference_equation(x, a, b): 6 | """ 7 | 差分方程式 8 | Input 9 | x : input signal 10 | a : coefficient of y 11 | b : coefficient of x 12 | Output 13 | y: output signal 14 | """ 15 | 16 | N = a.size 17 | M = b.size 18 | L = x.size 19 | y = np.zeros(L) 20 | 21 | for n in range(0, L): 22 | # a についての処理 23 | for k in range(1, N): 24 | y[n] -= a[k] * y[n - k] 25 | # b についての処理 26 | for k in range(0, M): 27 | if (n - k) >= 0 or (n - k) < N or n > N: # 入力配列のサイズを超えないようにする 28 | y[n] += b[k] * x[n - k] 29 | 30 | y = y / a[0] 31 | return y 32 | 33 | 34 | if __name__ == "__main__": 35 | x = np.zeros(10) 36 | x[0] = 1 37 | a = np.array([1, -0.3]) 38 | b = np.array([0.4]) 39 | y = difference_equation(x, a, b) 40 | 41 | plt.stem(y) 42 | plt.grid() 43 | plt.show() 44 | -------------------------------------------------------------------------------- /ttakahashi/chapter01/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | import soundfile as sf 4 | 5 | 6 | # Function to calculate SNR 7 | def calc_snr(s, x): 8 | return 10 * np.log10(np.sum(s**2) / np.sum(x**2)) 9 | 10 | 11 | # Function to adjust the amplitude of white noise to match the SNR 12 | def adjust_snr(s, x, snr): 13 | return (x / np.sqrt(np.sum(x**2))) * np.sqrt(np.sum(s**2)) * 10 ** (-snr / 20) 14 | 15 | 16 | sec = 3.0 # 信号の長さ s 17 | sr = 16000 # サンプリング周波数 Hz 18 | 19 | t = np.arange(sec * sr) / sr # サンプリング点の生成 20 | x = np.random.rand(round(sec * sr)) # ホワイトノイズの生成 21 | 22 | y, sr = sf.read("outputs/02.wav") 23 | s = x + y 24 | adj_x = adjust_snr(s, x, 6) 25 | 26 | M = 5 27 | maf_x = [] 28 | for i in range(len(adj_x) - M): 29 | maf_x.append(np.mean(adj_x[i : i + M])) 30 | for i in range(M): 31 | maf_x.append(maf_x[-1]) 32 | 33 | plt.xlim(0, 0.03) 34 | plt.plot(t, adj_x) 35 | plt.plot(t, maf_x) 36 | plt.savefig("outputs/10.pdf") 37 | plt.show() 38 | 39 | print("success!") 40 | -------------------------------------------------------------------------------- /dsugawara/chapter05/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def make_noise(signal, is_SNR): 6 | 7 | """SNRを調整した雑音を生成する 8 | 9 | Args: 10 | signal (ndarray): 信号(1次元配列) 11 | is_SNR (double): SNR 12 | 13 | Return: 14 | ndarray: 雑音 15 | 16 | """ 17 | noise = np.random.randn(len(signal)) 18 | 19 | noise = noise / np.sqrt(np.sum(noise**2)) 20 | noise = noise * np.sqrt(np.sum(signal**2)) 21 | noise = noise * 10 ** (-1 * is_SNR / 20) 22 | 23 | return noise 24 | 25 | 26 | # main 27 | A = 1.0 28 | fs = 16000 29 | sec = 1 30 | f = 440 31 | t = np.arange(0, fs * sec) / fs 32 | 33 | s = A * np.sin(2 * np.pi * f * t) 34 | 35 | noise = make_noise(s, 10) 36 | 37 | x1 = s + noise 38 | s2 = np.pad(s, [10, 0]) 39 | x2 = s2[0 : len(s2) - 10] + noise 40 | s3 = np.pad(s, [20, 0]) 41 | x3 = s3[0 : len(s3) - 20] + noise 42 | 43 | # plot 44 | plt.xlim(0, fs * 0.01) 45 | plt.plot(x1) 46 | plt.plot(x2) 47 | plt.plot(x3) 48 | plt.savefig("06py_signal.png") 49 | -------------------------------------------------------------------------------- /kyamada/chapter02/01.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def dft(x): 5 | """ 6 | DFT 7 | Input: 8 | x: input array 9 | Output: 10 | X: output DFT array 11 | """ 12 | N = len(x) 13 | X = np.zeros(N, dtype=complex) 14 | for k in range(N): 15 | # for n in range(N): 16 | # X[k] += x[n] * np.exp((-2j * np.pi * k * n) / N) 17 | 18 | X = np.sum(x * np.exp(-1j * 2 * np.pi * k * np.arange(N) / N)) 19 | return X 20 | 21 | 22 | def idft(x): 23 | """ 24 | IDFT 25 | Input: 26 | x: input array 27 | Output: 28 | X: output DFT array 29 | """ 30 | N = x.size 31 | X = np.zeros(N, dtype=complex) 32 | for k in range(N): 33 | x = (1 / N) * np.sum(X * np.exp(1j * 2 * np.pi * k * np.arange(N) / N)) 34 | return x 35 | 36 | 37 | if __name__ == '__main__': 38 | x = np.array([1, 0, 0, 0, 0, 0, 0, 0]) 39 | X = dft(x) 40 | X_fft = np.fft.fft(x) 41 | print(X) 42 | print(X_fft) 43 | y = idft(X_fft) 44 | 45 | -------------------------------------------------------------------------------- /ykuriki/chapter01/08.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import wave 3 | 4 | 5 | # 関数 6 | def Add_noise(s, snr): 7 | """SN比がn[dB]になるようにホワイトノイズを加える 8 | 9 | Args: 10 | s (ndarray): 元の信号 (float型1次元配列) 11 | snr (float): SN比 12 | 13 | Returns: 14 | x (ndarray): ノイズを加えた信号 (float型1次元配列) 15 | 16 | """ 17 | np.random.seed(2286) 18 | 19 | alpha = 1 / (10 ** (snr / 20)) 20 | noise = np.random.rand(len(s)) 21 | noise /= np.sqrt(np.sum(noise @ noise)) 22 | noise *= alpha * np.sqrt(np.sum(s @ s)) 23 | 24 | x = s + noise 25 | 26 | return x 27 | 28 | 29 | # パラメータ 30 | fs = 16000 31 | f = 440 32 | r = 1 33 | time = 3 34 | snr = 6 35 | 36 | t = np.arange(0, fs * time) / fs 37 | sig = r * np.sin(f * 2 * np.pi * t) 38 | x = Add_noise(sig, snr) 39 | 40 | 41 | # 音声ファイル出力 42 | output = (x * np.iinfo(np.int16).max).astype(np.int16) 43 | wav = wave.open("./08.wav", "wb") 44 | wav.setnchannels(1) 45 | wav.setsampwidth(2) 46 | wav.setframerate(fs) 47 | wav.writeframes(output) 48 | wav.close() 49 | -------------------------------------------------------------------------------- /ynakamura/chapter01/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def adjust_amp(x, snr, fs=16000, sec=3): 6 | whitenoise = np.random.rand(int(fs * sec)) * 2 - 1 7 | a = np.sqrt(np.sum(x**2) / np.sum(whitenoise**2) * (10 ** (-snr / 10))) 8 | whitenoise = a * np.random.rand(int(fs * sec)) * 2 - 1 9 | return whitenoise 10 | 11 | 12 | fs = 16000 13 | sec = 3 14 | fin = 440 15 | t = np.linspace(0.0, sec, int(fs * sec)) 16 | 17 | x = np.sin(2.0 * np.pi * fin * t) 18 | 19 | whitenoise = adjust_amp(x, 6) 20 | mix = x + whitenoise 21 | 22 | mix_filtered = np.array([]) 23 | for i in range(mix.shape[0] - 4): 24 | tmp = 0 25 | for j in range(5): 26 | tmp += mix[i + j] 27 | mix_filtered = np.append(mix_filtered, tmp / 5) 28 | 29 | plt.plot(t[: mix_filtered.shape[0]], mix[: mix_filtered.shape[0]], label="original") 30 | # plt.plot(t, mix_filtered, label="original") 31 | plt.plot(t[: mix_filtered.shape[0]], mix_filtered, label="filtered") 32 | plt.xlim(0, 0.03) 33 | plt.legend() 34 | plt.show() 35 | -------------------------------------------------------------------------------- /knishida/chapter02/04.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def DFT(x): 6 | N = len(x) 7 | n = np.arange(N) 8 | X = np.zeros(N) 9 | for k in range(N): 10 | X[k] = np.sum(x * np.exp(-1j * 2 * np.pi * k * n / N)) 11 | return X 12 | 13 | 14 | def IDFT(X): 15 | N = len(X) 16 | k = np.arange(N) 17 | x = np.zeros(N) 18 | for n in range(N): 19 | x[n] = np.sum(X * np.exp(1j * 2 * np.pi * k * n / N)) / N 20 | return x 21 | 22 | 23 | if __name__ == "__main__": 24 | delta = [1, 0, 0, 0, 0, 0, 0, 0] 25 | delta_dft = DFT(delta) 26 | delta_idft = IDFT(delta_dft) 27 | 28 | delta_amp = np.abs(delta_idft) 29 | delta_angle = np.angle(delta_idft) 30 | 31 | fig = plt.figure() 32 | fig_amp = fig.add_subplot(2, 1, 1) 33 | fig_angle = fig.add_subplot(2, 1, 2) 34 | fig_amp.stem(delta_amp) 35 | fig_angle.stem(delta_angle) 36 | fig_amp.set_title("Amplitude") 37 | fig_angle.set_title("Angle") 38 | fig.tight_layout() 39 | plt.show() 40 | -------------------------------------------------------------------------------- /kyamada/chapter01/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def create_white_noise( 6 | fs=16000, 7 | time=3 8 | ): 9 | """ 10 | Create a white noise 11 | """ 12 | t = np.arange(0, time, 1/fs) 13 | x = np.random.randn(len(t)) 14 | return t, x 15 | 16 | 17 | def create_sinusoid( 18 | fs=16000, 19 | f=440, 20 | r=1, 21 | theta=0, 22 | time=3, 23 | ): 24 | """ 25 | Create a sinusoid wave 26 | Input: 27 | fs: sampling frequency 28 | f: frequency 29 | r: amplitude 30 | theta: phase 31 | time: time 32 | Output: 33 | t: time_data 34 | x: wave data 35 | """ 36 | t = np.arange(0, time, 1/fs) 37 | x = r * np.sin(2 * np.pi * f * t + theta) 38 | 39 | return t, x 40 | 41 | 42 | if __name__ == '__main__': 43 | np.random.seed(0) # シード値 44 | t, noise = create_white_noise() 45 | t, x_sinusoid = create_sinusoid() 46 | x = noise + x_sinusoid 47 | plt.plot(t, x) 48 | plt.show() 49 | -------------------------------------------------------------------------------- /muesaka/chapter04/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def padding(L, S, x): 5 | zeros = np.zeros(L - S, dtype=float) 6 | x_pad = np.concatenate((zeros, x, zeros)) 7 | x_pad_mod = len(x_pad) % S 8 | if x_pad_mod != 0: 9 | zeros = np.zeros(x_pad_mod, dtype=float) 10 | x_pad = np.concatenate((x_pad, zeros)) 11 | 12 | return x_pad 13 | 14 | 15 | def divide_frame(L, S, x): 16 | x_pad = padding(L, S, x) 17 | T = (len(x_pad) - L) // S + 1 18 | x_div = np.empty([T, L], dtype=float) 19 | for t in range(T): 20 | x_t = np.zeros(L, dtype=float) 21 | for i in range(L): 22 | if len(x_pad) > (t * S + i): 23 | x_t[i] = x_pad[t * S + i] 24 | x_div[t] = x_t 25 | 26 | return x_div 27 | 28 | 29 | def stft(L, S, x): 30 | x_div = divide_frame(L, S, x) 31 | win = np.hamming(L) 32 | T = len(x_div) 33 | x_stft = np.empty([int(L / 2 + 1), T], dtype=complex) 34 | for t in range(T): 35 | x_stft[:, t] = np.fft.rfft(win * x_div[t, :]) 36 | 37 | return x_stft 38 | -------------------------------------------------------------------------------- /muesaka/chapter02/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def hamming(N): 6 | n = np.arange(N) 7 | w = 0.54 - 0.46 * np.cos(2 * np.pi * n / (N - 1)) 8 | return w 9 | 10 | 11 | def circular_convolution(x, y): 12 | N = max(len(x), len(y)) 13 | n = np.arange(N) 14 | z = np.zeros(N, dtype="complex") 15 | 16 | for k in range(N): 17 | z[k] = np.sum(x[n] * y[(k - n) % N]) 18 | return z / N 19 | 20 | 21 | if __name__ == "__main__": 22 | a = 1.0 # 振幅 23 | sec = 3 # 信号長 24 | sr = 16000 # サンプリング周波数 25 | f = 440 # 周波数 26 | 27 | t = np.arange(0, sec * sr) / sr 28 | signal = a * np.sin(2 * np.pi * f * t) 29 | window = hamming(len(signal)) 30 | x = np.fft.fft(signal) 31 | y = np.fft.fft(window) 32 | z = circular_convolution(x, y) 33 | idft_z = np.fft.ifft(z) 34 | window_signal = window * signal 35 | 36 | fig, ax = plt.subplots(nrows=2, ncols=1) 37 | ax[0].plot(idft_z.real) 38 | ax[1].plot(window_signal) 39 | plt.tight_layout() 40 | plt.show() 41 | -------------------------------------------------------------------------------- /ykuriki/chapter05/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | # 関数 5 | def clc_amv(p, theta, f): 6 | """アレイマニフォールドベクトルを計算する 7 | 8 | Args: 9 | p (ndarray): アレイの座標 10 | theta (int/double): 音源方向 [deg] 11 | f (int/double): 周波数 [Hz] 12 | 13 | 14 | Returns: 15 | a (ndarray): アレイマニフォールドベクトル 16 | 17 | """ 18 | c = 334 19 | M = len(p) 20 | theta = np.radians(theta) 21 | a = np.zeros(M, dtype="complex") 22 | u = np.array([np.sin(theta), np.cos(theta), 0]) 23 | 24 | for m in range(M): 25 | a[m] = np.exp(1j * 2 * np.pi * f / c * u @ p[m]) 26 | 27 | return a 28 | 29 | 30 | d = 0.05 31 | r = 0.05 32 | M = 3 33 | theta = 45 34 | f = 1000 35 | 36 | p_l = np.vstack([[-d, 0, 0], [0, 0, 0], [d, 0, 0]]) 37 | print(clc_amv(p_l, theta, f)) 38 | 39 | p_c = np.vstack( 40 | [ 41 | [r * np.sin(0), r * np.cos(0), 0], 42 | [r * np.sin(2 * np.pi / M), r * np.cos(2 * np.pi / M), 0], 43 | [r * np.sin(4 * np.pi / M), r * np.cos(4 * np.pi / M), 0], 44 | ] 45 | ) 46 | print(clc_amv(p_c, theta, f)) 47 | -------------------------------------------------------------------------------- /knishida/chapter01/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def SNratio(s, x): 6 | ssum = 0.0 7 | xsum = 0.0 8 | for i in range(len(s)): 9 | ssum += s[i] ** 2 10 | xsum += x[i] ** 2 11 | return 10 * np.log10(ssum / xsum) 12 | 13 | 14 | def ajust_SN(s, x, SN): 15 | ssum = 0.0 16 | xsum = 0.0 17 | for i in range(len(s)): 18 | ssum += s[i] ** 2 19 | xsum += x[i] ** 2 20 | return np.sqrt(ssum / xsum * 10 ** (-SN / 10)) 21 | 22 | 23 | if __name__ == "__main__": 24 | A = 1 25 | f = 440.0 26 | sec = 3.0 27 | fs = 16000 28 | SN = 6 29 | 30 | t = np.arange(0, sec, 1 / fs) 31 | y = A * np.sin(2 * np.pi * f * t) 32 | n = np.random.rand(round(fs * sec)) 33 | 34 | X = ajust_SN(y, n, SN) 35 | 36 | obs = y + X * n 37 | mov_ave = np.convolve(obs, np.ones(5), mode="same") / 5 38 | plt.plot(t, obs, label="obs") 39 | plt.plot(t, mov_ave, label="mov_ave") 40 | plt.xlim([0, 0.03]) 41 | plt.legend() 42 | plt.tight_layout() 43 | plt.show() 44 | -------------------------------------------------------------------------------- /kyamada/chapter02/06.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def create_sinusoid( 6 | fs=16000, 7 | f=440, 8 | r=1, 9 | theta=0, 10 | time=3, 11 | ): 12 | """ 13 | Create a sinusoid wave 14 | Input: 15 | fs: sampling frequency 16 | f: frequency 17 | r: amplitude 18 | theta: phase 19 | time: time 20 | Output: 21 | t: time_data 22 | x: wave data 23 | """ 24 | t = np.arange(0, time, 1/fs) 25 | x = r * np.sin(2 * np.pi * f * t + theta) 26 | 27 | return t, x 28 | 29 | 30 | if __name__ == '__main__': 31 | fs = 16000 32 | t, x = create_sinusoid(fs=fs) 33 | 34 | X = np.fft.fft(x) 35 | 36 | X_amp = 20 * np.log10(np.abs(X)) 37 | X_phase = 20 * np.log10(np.angle(X)) 38 | 39 | 40 | plt.subplot(2, 1, 1) 41 | plt.stem(X_amp) 42 | plt.grid() 43 | plt.title("Amplitude") 44 | plt.subplot(2, 1, 2) 45 | plt.stem(X_phase) 46 | plt.grid() 47 | plt.title("Phase") 48 | plt.tight_layout() 49 | plt.show() 50 | -------------------------------------------------------------------------------- /tkawamura/chapter01/04.py: -------------------------------------------------------------------------------- 1 | # 白色雑音の生成: ホワイトノイズをサンプリング周波数 16000 Hz で 3 秒分作成しプロットせよ. 2 | 3 | import matplotlib.pyplot as plt 4 | import numpy as np 5 | 6 | 7 | def main(): 8 | fs = 16000 # sampling frequency (Hz) 9 | sig_len = 3 # signal length (sec.) 10 | 11 | t = np.arange(fs * sig_len) / fs # time 12 | white_noise = np.random.rand(fs * sig_len) # ホワイトノイズ 13 | 14 | # plot 15 | plt.title("White noise") 16 | plt.plot(t, white_noise) 17 | plt.ylabel("Amplitude") 18 | plt.xlabel("Time (sec.)") 19 | plt.show() 20 | 21 | # 確認方法 (スペクトルのパワーがフラットになることを確認) 22 | # sig_len_sample = fs * sig_len # signal length (sample) 23 | # white_noise_spec = np.fft.rfft(white_noise) # ホワイトノイズのスペクトル 24 | # power = 10 * np.log10(np.abs(white_noise_spec) ** 2) 25 | # freq = np.arange(sig_len_sample // 2 + 1) / sig_len_sample * fs 26 | # plt.title("White noise (Freqency domain)") 27 | # plt.plot(freq, power) 28 | # plt.ylabel("Power") 29 | # plt.xlabel("Freq. (Hz)") 30 | # plt.show() 31 | 32 | 33 | if __name__ == "__main__": 34 | main() 35 | -------------------------------------------------------------------------------- /kyamada/Chapter03/03.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def circle_convolution(x, h): 5 | """ 6 | Circle convolution with padding 7 | 8 | Inputs: 9 | x: input signal 10 | h: convolution signal 11 | Returns: 12 | z: output signal 13 | """ 14 | N = x.size # 配列のサイズ 15 | n_size = x.size + h.size - 1 # 全体のサイズ 16 | x = np.pad(x, (0, N-1), 'constant') # zero-padding 17 | h = np.pad(h, (0, N-1), 'constant') # zero-padding 18 | N = x.size 19 | n = x.size + h.size - 1 # 全体のサイズ 20 | z = np.zeros(n) # 出力配列 21 | for i in range(0, n): 22 | z[i] = 0 23 | for k in range(0, N): 24 | index = (i - k) % N 25 | print(x[k], h[index]) 26 | z[i] += x[k] * h[index] 27 | return z[:n_size] 28 | 29 | 30 | if __name__ == "__main__": 31 | x = np.array([4, 3, 2, 1]) 32 | h = np.array([1, -1, 1, -1]) 33 | np_convolution = np.convolve(x, h) 34 | print("Numpy convolution:", np_convolution) 35 | convolution = circle_convolution(x, h) 36 | print("My convolution:", convolution) 37 | -------------------------------------------------------------------------------- /tkawamura/chapter01/05.py: -------------------------------------------------------------------------------- 1 | # 信号の混合: 1.で作成した正弦波と 4.で作成したホワイトノイズと正弦波を混合してプロットせよ. 2 | import matplotlib.pyplot as plt 3 | import numpy as np 4 | 5 | 6 | def main(): 7 | # 2つの信号に共通の全パラメータ 8 | fs = 16000 # sampling frequency (Hz) 9 | sig_len = 3 # signal length (sec.) 10 | 11 | # 1.で作成した正弦波のパラメータ 12 | amp = 1 # amplitude of sine wave 13 | freq = 440 # frequency of sine wave (Hz) 14 | 15 | t = np.arange(fs * sig_len) / fs # time 16 | sin_wave = amp * np.sin(2 * np.pi * freq * t) # 1.で作成した正弦波 17 | white_noise = np.random.rand(fs * sig_len) # 4.で作成したホワイトノイズ 18 | mixture = sin_wave + white_noise 19 | 20 | # plot 21 | plt.title("Mixture") 22 | plt.plot(t, mixture, label="Mixture") 23 | # plt.plot(t, sin_wave, marker=".", label="Sine wave") # 確認用 24 | # plt.plot(t, white_noise, marker=".", label="White noise") # 確認用 25 | plt.ylabel("Amplitude") 26 | plt.xlabel("Time (sec.)") 27 | # plt.xlim(0, 10 / fs) # 確認方法: 10サンプル分だけ表示 28 | # plt.grid() 29 | plt.legend() 30 | plt.show() 31 | 32 | 33 | if __name__ == "__main__": 34 | main() 35 | -------------------------------------------------------------------------------- /smotoyama/chapter02/05.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def DFT(x): 6 | N = len(x) 7 | c = np.zeros(N) * 1j 8 | 9 | for i in range(N): 10 | c[i] = np.sum(x * np.exp(-1j * 2 * np.pi * i * np.arange(N) / N)) 11 | print(i) 12 | 13 | return c 14 | 15 | 16 | def IDFT(X): 17 | N = len(X) 18 | c = np.zeros(N) * 1j 19 | 20 | for i in range(N): 21 | c[i] = (1 / N) * np.sum(X * np.exp(1j * 2 * np.pi * i * np.arange(N) / N)) 22 | 23 | return c 24 | 25 | 26 | if __name__ == "__main__": 27 | A = 1 28 | f = 440 29 | fs = 16000 30 | s = 3 31 | 32 | t = np.arange(0, s, 1 / fs) 33 | x = A * np.sin(2 * np.pi * f * t) 34 | 35 | X = DFT(x) 36 | 37 | X_amp = 20 * np.log10(np.abs(X)) 38 | X_ang = 20 * np.log10(np.angle(X)) 39 | 40 | fig = plt.figure() 41 | ax1 = fig.add_subplot(2, 1, 1) 42 | ax2 = fig.add_subplot(2, 1, 2) 43 | ax1.plot(X_amp) 44 | ax2.plot(X_ang) 45 | ax1.set_title("Amplitude Spectrum") 46 | ax2.set_title("Phase Spectrum") 47 | fig.tight_layout() 48 | plt.show() 49 | -------------------------------------------------------------------------------- /ttakahashi/chapter03/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def frequency_response(a, b, f, fs): 6 | """ 7 | 周波数応答を計算する 8 | Args: 9 | a (ndarray): y の係数 10 | b (ndarray): x の係数 11 | f (float): 周波数 12 | fs (float): サンプリング周波数 13 | Returns: 14 | H (ndarray): 周波数応答 15 | """ 16 | omega = 2 + np.pi * f / fs 17 | sum_a = np.sum(a * np.exp(-1j * omega * np.arange(1, len(a)))) 18 | sum_b = np.sum(b * np.exp(-1j * omega * np.arange(0, len(b)))) 19 | H = sum_b / (1 + sum_a) 20 | return H 21 | 22 | 23 | fs = 16000 24 | a = np.array([1, 0.3]) 25 | b = np.array([0.4]) 26 | N = 10 27 | H = np.zeros(N, dtype="complex") 28 | for i in range(N): 29 | f = (i / N) * fs 30 | H[i] = frequency_response(a, b, f, fs) 31 | 32 | fig = plt.figure() 33 | ax1 = fig.add_subplot(2, 1, 1) 34 | ax2 = fig.add_subplot(2, 1, 2) 35 | ax1.stem(np.abs(H), label="Amplitude") 36 | ax2.stem(np.angle(H), label="Angle") 37 | ax1.grid(True) 38 | ax2.grid(True) 39 | fig.tight_layout() 40 | plt.savefig("outputs/10.pdf") 41 | plt.show() 42 | 43 | print("success!") 44 | -------------------------------------------------------------------------------- /kyamada/chapter01/02.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import wave 3 | 4 | 5 | def create_sinusoid( 6 | fs=16000, 7 | f=440, 8 | r=1, 9 | theta=0, 10 | time=3, 11 | ): 12 | """ 13 | Create a sinusoid wave 14 | Input: 15 | fs: sampling frequency 16 | f: frequency 17 | r: amplitude 18 | theta: phase 19 | time: time 20 | Output: 21 | t: time_data 22 | x: wave data 23 | """ 24 | t = np.arange(0, time, 1/fs) 25 | x = r * np.sin(2 * np.pi * f * t + theta) 26 | 27 | return t, x 28 | 29 | 30 | def save_wave_file(filename, framerate, x): 31 | """ 32 | Save a wave file 33 | Input: 34 | filename: wave file name 35 | x: wave data 36 | """ 37 | wf = wave.open(f'{filename}.wav', 'w') 38 | wf.setnchannels(1) 39 | wf.setsampwidth(2) 40 | wf.setframerate(framerate) 41 | wf.writeframes(x.astype(np.int16).tostring()) 42 | wf.close() 43 | 44 | 45 | if __name__ == '__main__': 46 | t, x = create_sinusoid(r=1000) 47 | # save wave file 48 | save_wave_file('kyamada/chapter01/sinusoid', 16000, x) 49 | -------------------------------------------------------------------------------- /dsugawara/chapter03/10.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import matplotlib.pyplot as plt 3 | 4 | 5 | def Frequency_response(a, b, f, sf): 6 | 7 | omega = 2 * np.pi * f / sf 8 | N = a.size 9 | M = b.size 10 | sigma_a = 0j 11 | sigma_b = 0j 12 | 13 | # a[N]を含むシグマ計算 14 | for k in range(1, N): 15 | sigma_a += a[k] * np.e ** (-1j * omega * k) 16 | # b[M]を含むシグマ計算 17 | for k in range(0, M): 18 | sigma_b += b[k] * np.e ** (-1j * omega * k) 19 | 20 | H = sigma_b / (1 + sigma_a) 21 | 22 | return H 23 | 24 | 25 | # main 26 | N = 16 27 | a = np.array([1, 0.3]) 28 | b = np.array([0.4]) 29 | sf = 16000 30 | H = np.zeros(N, dtype="complex") 31 | 32 | for i in range(0, N): 33 | f = i / N * sf 34 | H[i] = Frequency_response(a, b, f, sf) 35 | 36 | H_amp = np.abs(H) 37 | H_angle = np.angle(H) 38 | 39 | # plot 40 | fig = plt.figure() 41 | ax1 = fig.add_subplot(2, 1, 1) 42 | ax2 = fig.add_subplot(2, 1, 2) 43 | ax1.stem(H_amp) 44 | ax2.stem(H_angle) 45 | ax1.set_title("Amplitude characteristics") 46 | ax2.set_title("Angle characteristics") 47 | fig.tight_layout() 48 | plt.savefig("10py_characteristics") 49 | --------------------------------------------------------------------------------