Skip to content

Commit 58e7390

Browse files
committed
課題を解いた
1 parent dc0c51b commit 58e7390

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-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()

0 commit comments

Comments
 (0)