Skip to content

Commit ed34346

Browse files
authored
Merge pull request #10 from kuu8902/main
1章を追加
2 parents 4d92e20 + 5e52ea4 commit ed34346

File tree

11 files changed

+336
-0
lines changed

11 files changed

+336
-0
lines changed

kkoiso/chapter01/chapter01.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
#1-1
5+
a = 1.0
6+
freq= 440
7+
samp_rate = 16000
8+
d = 3
9+
10+
t = np.linspace(0, d, int(samp_rate * d), endpoint=False)
11+
12+
13+
sin_wave = a * np.sin(2 * np.pi * freq * t)
14+
15+
16+
plt.plot(t,sin_wave)
17+
plt.title("Sin Wave")
18+
plt.xlabel("Time [s]")
19+
plt.ylabel("Amplitude")
20+
plt.show()
21+
22+
#1-2
23+
import wave
24+
25+
26+
output = 'sin_wave_440Hz.wav'
27+
with wave.open(output, 'w') as wf:
28+
wf.setnchannels(1)
29+
wf.setsampwidth(2)
30+
wf.setframerate(samp_rate)
31+
wf.writeframes((sin_wave * 32767).astype(np.int16).tobytes())
32+
33+
34+
#1-3
35+
freq2 = 660
36+
sin_wave2 = a * np.sin(2 * np.pi * freq2 * t)
37+
38+
39+
stereo_wave = np.vstack((sin_wave, sin_wave2)).T
40+
41+
42+
output_stereo = 'stereo_sin_waves.wav'
43+
with wave.open(output_stereo, 'w') as wf:
44+
wf.setnchannels(2)
45+
wf.setsampwidth(2)
46+
wf.setframerate(samp_rate)
47+
wf.writeframes((stereo_wave * 32767).astype(np.int16).tobytes())
48+
49+
#1-4
50+
white_noise = np.random.normal(0, 1, len(t))
51+
52+
53+
plt.plot(t, white_noise)
54+
plt.title("White Noise")
55+
plt.xlabel("Time [s]")
56+
plt.ylabel("Amplitude")
57+
plt.show()
58+
59+
60+
#1-5
61+
mixed_signal = sin_wave + white_noise
62+
63+
64+
plt.plot(t, mixed_signal)
65+
plt.title("Mixed Signal (Sin Wave + White Noise)")
66+
plt.xlabel("Time [s]")
67+
plt.ylabel("Amplitude")
68+
plt.show()
69+
70+
#1-6
71+
def calculate_snr(signal, noise):
72+
s_power = np.sum(signal ** 2) / len(signal)
73+
n_power = np.sum(noise ** 2) / len(noise)
74+
snr = 10 * np.log10(s_power / n_power)
75+
return snr
76+
77+
#1-7
78+
def add_noise_with_snr(signal, desired_snr_db):
79+
s_power = np.sum(signal ** 2) / len(signal)
80+
snr_linear = 10 ** (desired_snr_db / 10)
81+
n_power = s_power / snr_linear
82+
noise = np.random.normal(0, np.sqrt(n_power), len(signal))
83+
return signal + noise
84+
85+
#1-8
86+
desired_snr= 6
87+
noise_signal = add_noise_with_snr(sin_wave, desired_snr)
88+
89+
90+
output_noise = 'sin_wave_with_noise_6dB.wav'
91+
with wave.open(output_noise, 'w') as wf:
92+
wf.setnchannels(1)
93+
wf.setsampwidth(2)
94+
wf.setframerate(samp_rate)
95+
wf.writeframes((noise_signal * 32767).astype(np.int16).tobytes())
96+
97+
98+
#1-9
99+
from scipy.io import wavfile
100+
101+
rate, data = wavfile.read(output_noise)
102+
103+
104+
downsampled_data = data[::2]
105+
106+
107+
output_downsampled = 'downsampled_8kHz.wav'
108+
wavfile.write(output_downsampled, 8000, downsampled_data.astype(np.int16))
109+
110+
#1-10
111+
filtered_signal = np.convolve(downsampled_data, np.ones(5)/5, mode='valid')
112+
113+
plt.figure(figsize=(14, 6))
114+
plt.subplot(2, 1, 1)
115+
plt.plot(downsampled_data[:1000])
116+
plt.title("Original Downsampled Signal (8 kHz)")
117+
plt.xlabel("Sample")
118+
plt.ylabel("Amplitude")
119+
120+
plt.subplot(2, 1, 2)
121+
plt.plot(filtered_signal[:1000])
122+
plt.title("Filtered Signal (5-point Moving Average)")
123+
plt.xlabel("Sample")
124+
plt.ylabel("Amplitude")
125+
126+
plt.tight_layout()
127+
plt.show()

kkoiso/chapter01/q01.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
4+
#1-1
5+
a = 1.0
6+
freq= 440
7+
samp_rate = 16000
8+
d = 3
9+
10+
t = np.linspace(0, d, int(samp_rate * d), endpoint=False)
11+
12+
13+
sin_wave = a * np.sin(2 * np.pi * freq * t)
14+
15+
16+
plt.plot(t,sin_wave)
17+
plt.title("Sin Wave")
18+
plt.xlabel("Time [s]")
19+
plt.ylabel("Amplitude")
20+
plt.show()

kkoiso/chapter01/q02.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
#1-2
4+
import wave
5+
6+
a = 1.0
7+
freq= 440
8+
samp_rate = 16000
9+
d = 3
10+
11+
t = np.linspace(0, d, int(samp_rate * d), endpoint=False)
12+
13+
14+
sin_wave = a * np.sin(2 * np.pi * freq * t)
15+
output = 'sin_wave_440Hz.wav'
16+
with wave.open(output, 'w') as wf:
17+
wf.setnchannels(1)
18+
wf.setsampwidth(2)
19+
wf.setframerate(samp_rate)
20+
wf.writeframes((sin_wave * 32767).astype(np.int16).tobytes())
21+

kkoiso/chapter01/q03.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import wave
4+
a = 1.0
5+
freq= 440
6+
samp_rate = 16000
7+
d = 3
8+
9+
t = np.linspace(0, d, int(samp_rate * d), endpoint=False)
10+
11+
12+
sin_wave = a * np.sin(2 * np.pi * freq * t)
13+
#1-3
14+
freq2 = 660
15+
sin_wave2 = a * np.sin(2 * np.pi * freq2 * t)
16+
17+
18+
stereo_wave = np.vstack((sin_wave, sin_wave2)).T
19+
20+
21+
output_stereo = 'stereo_sin_waves.wav'
22+
with wave.open(output_stereo, 'w') as wf:
23+
wf.setnchannels(2)
24+
wf.setsampwidth(2)
25+
wf.setframerate(samp_rate)
26+
wf.writeframes((stereo_wave * 32767).astype(np.int16).tobytes())

kkoiso/chapter01/q04.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import wave
4+
5+
a = 1.0
6+
freq= 440
7+
samp_rate = 16000
8+
d = 3
9+
10+
t = np.linspace(0, d, int(samp_rate * d), endpoint=False)
11+
12+
13+
sin_wave = a * np.sin(2 * np.pi * freq * t)
14+
15+
#1-4
16+
white_noise = np.random.normal(0, 1, len(t))
17+
18+
19+
plt.plot(t, white_noise)
20+
plt.title("White Noise")
21+
plt.xlabel("Time [s]")
22+
plt.ylabel("Amplitude")
23+
plt.show()

kkoiso/chapter01/q05.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import wave
4+
a = 1.0
5+
freq= 440
6+
samp_rate = 16000
7+
d = 3
8+
9+
t = np.linspace(0, d, int(samp_rate * d), endpoint=False)
10+
11+
12+
sin_wave = a * np.sin(2 * np.pi * freq * t)
13+
14+
white_noise = np.random.normal(0, 1, len(t))
15+
#1-5
16+
mixed_signal = sin_wave + white_noise
17+
18+
19+
plt.plot(t, mixed_signal)
20+
plt.title("Mixed Signal (Sin Wave + White Noise)")
21+
plt.xlabel("Time [s]")
22+
plt.ylabel("Amplitude")
23+
plt.show()

kkoiso/chapter01/q06.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import wave
4+
5+
def calculate_snr(signal, noise):
6+
s_power = np.sum(signal ** 2) / len(signal)
7+
n_power = np.sum(noise ** 2) / len(noise)
8+
snr = 10 * np.log10(s_power / n_power)
9+
return snr

kkoiso/chapter01/q07.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import wave
4+
5+
#1-7
6+
def add_noise_with_snr(signal, desired_snr_db):
7+
s_power = np.sum(signal ** 2) / len(signal)
8+
snr_linear = 10 ** (desired_snr_db / 10)
9+
n_power = s_power / snr_linear
10+
noise = np.random.normal(0, np.sqrt(n_power), len(signal))
11+
return signal + noise

kkoiso/chapter01/q08.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import wave
4+
5+
6+
a = 1.0
7+
freq= 440
8+
samp_rate = 16000
9+
d = 3
10+
11+
t = np.linspace(0, d, int(samp_rate * d), endpoint=False)
12+
13+
14+
sin_wave = a * np.sin(2 * np.pi * freq * t)
15+
16+
17+
def add_noise_with_snr(signal, desired_snr_db):
18+
s_power = np.sum(signal ** 2) / len(signal)
19+
snr_linear = 10 ** (desired_snr_db / 10)
20+
n_power = s_power / snr_linear
21+
noise = np.random.normal(0, np.sqrt(n_power), len(signal))
22+
return signal + noise
23+
24+
#1-8
25+
desired_snr= 6
26+
noise_signal = add_noise_with_snr(sin_wave, desired_snr)
27+
28+
output_noise = 'sin_wave_with_noise_6dB.wav'
29+
with wave.open(output_noise, 'w') as wf:
30+
wf.setnchannels(1)
31+
wf.setsampwidth(2)
32+
wf.setframerate(samp_rate)
33+
wf.writeframes((noise_signal * 32767).astype(np.int16).tobytes())

kkoiso/chapter01/q09.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import wave
4+
5+
#1-9
6+
from scipy.io import wavfile
7+
output_noise = 'sin_wave_with_noise_6dB.wav'
8+
rate, data = wavfile.read(output_noise)
9+
10+
11+
downsampled_data = data[::2]
12+
13+
14+
output_downsampled = 'downsampled_8kHz.wav'
15+
wavfile.write(output_downsampled, 8000, downsampled_data.astype(np.int16))

kkoiso/chapter01/q10.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import wave
4+
from scipy.io import wavfile
5+
6+
output_noise = 'sin_wave_with_noise_6dB.wav'
7+
rate, data = wavfile.read(output_noise)
8+
9+
10+
downsampled_data = data[::2]
11+
#1-10
12+
filtered_signal = np.convolve(downsampled_data, np.ones(5)/5, mode='valid')
13+
14+
plt.figure(figsize=(14, 6))
15+
plt.subplot(2, 1, 1)
16+
plt.plot(downsampled_data[:1000])
17+
plt.title("Original Downsampled Signal (8 kHz)")
18+
plt.xlabel("Sample")
19+
plt.ylabel("Amplitude")
20+
21+
plt.subplot(2, 1, 2)
22+
plt.plot(filtered_signal[:1000])
23+
plt.title("Filtered Signal (5-point Moving Average)")
24+
plt.xlabel("Sample")
25+
plt.ylabel("Amplitude")
26+
27+
plt.tight_layout()
28+
plt.show()

0 commit comments

Comments
 (0)