Skip to content

Commit

Permalink
Merge pull request #10 from kuu8902/main
Browse files Browse the repository at this point in the history
1章を追加
  • Loading branch information
taishi-n authored May 21, 2024
2 parents 4d92e20 + 5e52ea4 commit ed34346
Show file tree
Hide file tree
Showing 11 changed files with 336 additions and 0 deletions.
127 changes: 127 additions & 0 deletions kkoiso/chapter01/chapter01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import numpy as np
import matplotlib.pyplot as plt

#1-1
a = 1.0
freq= 440
samp_rate = 16000
d = 3

t = np.linspace(0, d, int(samp_rate * d), endpoint=False)


sin_wave = a * np.sin(2 * np.pi * freq * t)


plt.plot(t,sin_wave)
plt.title("Sin Wave")
plt.xlabel("Time [s]")
plt.ylabel("Amplitude")
plt.show()

#1-2
import wave


output = 'sin_wave_440Hz.wav'
with wave.open(output, 'w') as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(samp_rate)
wf.writeframes((sin_wave * 32767).astype(np.int16).tobytes())


#1-3
freq2 = 660
sin_wave2 = a * np.sin(2 * np.pi * freq2 * t)


stereo_wave = np.vstack((sin_wave, sin_wave2)).T


output_stereo = 'stereo_sin_waves.wav'
with wave.open(output_stereo, 'w') as wf:
wf.setnchannels(2)
wf.setsampwidth(2)
wf.setframerate(samp_rate)
wf.writeframes((stereo_wave * 32767).astype(np.int16).tobytes())

#1-4
white_noise = np.random.normal(0, 1, len(t))


plt.plot(t, white_noise)
plt.title("White Noise")
plt.xlabel("Time [s]")
plt.ylabel("Amplitude")
plt.show()


#1-5
mixed_signal = sin_wave + white_noise


plt.plot(t, mixed_signal)
plt.title("Mixed Signal (Sin Wave + White Noise)")
plt.xlabel("Time [s]")
plt.ylabel("Amplitude")
plt.show()

#1-6
def calculate_snr(signal, noise):
s_power = np.sum(signal ** 2) / len(signal)
n_power = np.sum(noise ** 2) / len(noise)
snr = 10 * np.log10(s_power / n_power)
return snr

#1-7
def add_noise_with_snr(signal, desired_snr_db):
s_power = np.sum(signal ** 2) / len(signal)
snr_linear = 10 ** (desired_snr_db / 10)
n_power = s_power / snr_linear
noise = np.random.normal(0, np.sqrt(n_power), len(signal))
return signal + noise

#1-8
desired_snr= 6
noise_signal = add_noise_with_snr(sin_wave, desired_snr)


output_noise = 'sin_wave_with_noise_6dB.wav'
with wave.open(output_noise, 'w') as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(samp_rate)
wf.writeframes((noise_signal * 32767).astype(np.int16).tobytes())


#1-9
from scipy.io import wavfile

rate, data = wavfile.read(output_noise)


downsampled_data = data[::2]


output_downsampled = 'downsampled_8kHz.wav'
wavfile.write(output_downsampled, 8000, downsampled_data.astype(np.int16))

#1-10
filtered_signal = np.convolve(downsampled_data, np.ones(5)/5, mode='valid')

plt.figure(figsize=(14, 6))
plt.subplot(2, 1, 1)
plt.plot(downsampled_data[:1000])
plt.title("Original Downsampled Signal (8 kHz)")
plt.xlabel("Sample")
plt.ylabel("Amplitude")

plt.subplot(2, 1, 2)
plt.plot(filtered_signal[:1000])
plt.title("Filtered Signal (5-point Moving Average)")
plt.xlabel("Sample")
plt.ylabel("Amplitude")

plt.tight_layout()
plt.show()
20 changes: 20 additions & 0 deletions kkoiso/chapter01/q01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import numpy as np
import matplotlib.pyplot as plt

#1-1
a = 1.0
freq= 440
samp_rate = 16000
d = 3

t = np.linspace(0, d, int(samp_rate * d), endpoint=False)


sin_wave = a * np.sin(2 * np.pi * freq * t)


plt.plot(t,sin_wave)
plt.title("Sin Wave")
plt.xlabel("Time [s]")
plt.ylabel("Amplitude")
plt.show()
21 changes: 21 additions & 0 deletions kkoiso/chapter01/q02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import numpy as np
import matplotlib.pyplot as plt
#1-2
import wave

a = 1.0
freq= 440
samp_rate = 16000
d = 3

t = np.linspace(0, d, int(samp_rate * d), endpoint=False)


sin_wave = a * np.sin(2 * np.pi * freq * t)
output = 'sin_wave_440Hz.wav'
with wave.open(output, 'w') as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(samp_rate)
wf.writeframes((sin_wave * 32767).astype(np.int16).tobytes())

26 changes: 26 additions & 0 deletions kkoiso/chapter01/q03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import numpy as np
import matplotlib.pyplot as plt
import wave
a = 1.0
freq= 440
samp_rate = 16000
d = 3

t = np.linspace(0, d, int(samp_rate * d), endpoint=False)


sin_wave = a * np.sin(2 * np.pi * freq * t)
#1-3
freq2 = 660
sin_wave2 = a * np.sin(2 * np.pi * freq2 * t)


stereo_wave = np.vstack((sin_wave, sin_wave2)).T


output_stereo = 'stereo_sin_waves.wav'
with wave.open(output_stereo, 'w') as wf:
wf.setnchannels(2)
wf.setsampwidth(2)
wf.setframerate(samp_rate)
wf.writeframes((stereo_wave * 32767).astype(np.int16).tobytes())
23 changes: 23 additions & 0 deletions kkoiso/chapter01/q04.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import numpy as np
import matplotlib.pyplot as plt
import wave

a = 1.0
freq= 440
samp_rate = 16000
d = 3

t = np.linspace(0, d, int(samp_rate * d), endpoint=False)


sin_wave = a * np.sin(2 * np.pi * freq * t)

#1-4
white_noise = np.random.normal(0, 1, len(t))


plt.plot(t, white_noise)
plt.title("White Noise")
plt.xlabel("Time [s]")
plt.ylabel("Amplitude")
plt.show()
23 changes: 23 additions & 0 deletions kkoiso/chapter01/q05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import numpy as np
import matplotlib.pyplot as plt
import wave
a = 1.0
freq= 440
samp_rate = 16000
d = 3

t = np.linspace(0, d, int(samp_rate * d), endpoint=False)


sin_wave = a * np.sin(2 * np.pi * freq * t)

white_noise = np.random.normal(0, 1, len(t))
#1-5
mixed_signal = sin_wave + white_noise


plt.plot(t, mixed_signal)
plt.title("Mixed Signal (Sin Wave + White Noise)")
plt.xlabel("Time [s]")
plt.ylabel("Amplitude")
plt.show()
9 changes: 9 additions & 0 deletions kkoiso/chapter01/q06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import numpy as np
import matplotlib.pyplot as plt
import wave

def calculate_snr(signal, noise):
s_power = np.sum(signal ** 2) / len(signal)
n_power = np.sum(noise ** 2) / len(noise)
snr = 10 * np.log10(s_power / n_power)
return snr
11 changes: 11 additions & 0 deletions kkoiso/chapter01/q07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import numpy as np
import matplotlib.pyplot as plt
import wave

#1-7
def add_noise_with_snr(signal, desired_snr_db):
s_power = np.sum(signal ** 2) / len(signal)
snr_linear = 10 ** (desired_snr_db / 10)
n_power = s_power / snr_linear
noise = np.random.normal(0, np.sqrt(n_power), len(signal))
return signal + noise
33 changes: 33 additions & 0 deletions kkoiso/chapter01/q08.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import numpy as np
import matplotlib.pyplot as plt
import wave


a = 1.0
freq= 440
samp_rate = 16000
d = 3

t = np.linspace(0, d, int(samp_rate * d), endpoint=False)


sin_wave = a * np.sin(2 * np.pi * freq * t)


def add_noise_with_snr(signal, desired_snr_db):
s_power = np.sum(signal ** 2) / len(signal)
snr_linear = 10 ** (desired_snr_db / 10)
n_power = s_power / snr_linear
noise = np.random.normal(0, np.sqrt(n_power), len(signal))
return signal + noise

#1-8
desired_snr= 6
noise_signal = add_noise_with_snr(sin_wave, desired_snr)

output_noise = 'sin_wave_with_noise_6dB.wav'
with wave.open(output_noise, 'w') as wf:
wf.setnchannels(1)
wf.setsampwidth(2)
wf.setframerate(samp_rate)
wf.writeframes((noise_signal * 32767).astype(np.int16).tobytes())
15 changes: 15 additions & 0 deletions kkoiso/chapter01/q09.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import numpy as np
import matplotlib.pyplot as plt
import wave

#1-9
from scipy.io import wavfile
output_noise = 'sin_wave_with_noise_6dB.wav'
rate, data = wavfile.read(output_noise)


downsampled_data = data[::2]


output_downsampled = 'downsampled_8kHz.wav'
wavfile.write(output_downsampled, 8000, downsampled_data.astype(np.int16))
28 changes: 28 additions & 0 deletions kkoiso/chapter01/q10.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import numpy as np
import matplotlib.pyplot as plt
import wave
from scipy.io import wavfile

output_noise = 'sin_wave_with_noise_6dB.wav'
rate, data = wavfile.read(output_noise)


downsampled_data = data[::2]
#1-10
filtered_signal = np.convolve(downsampled_data, np.ones(5)/5, mode='valid')

plt.figure(figsize=(14, 6))
plt.subplot(2, 1, 1)
plt.plot(downsampled_data[:1000])
plt.title("Original Downsampled Signal (8 kHz)")
plt.xlabel("Sample")
plt.ylabel("Amplitude")

plt.subplot(2, 1, 2)
plt.plot(filtered_signal[:1000])
plt.title("Filtered Signal (5-point Moving Average)")
plt.xlabel("Sample")
plt.ylabel("Amplitude")

plt.tight_layout()
plt.show()

0 comments on commit ed34346

Please sign in to comment.