Pretty much all of Fall 2024
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import numpy as np
|
||||
import sounddevice as sd
|
||||
#import sounddevice as sd
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
def normalize_signal(signal):
|
||||
@@ -10,12 +10,37 @@ def normalize_signal(signal):
|
||||
normalized_signal -= 1
|
||||
return normalized_signal
|
||||
|
||||
snr = 10
|
||||
|
||||
f = 466.16
|
||||
f_s = 16000
|
||||
T_0 = 1/f
|
||||
|
||||
t = np.arange(0,0.01,1/f_s)
|
||||
t = np.arange(0,T_0,1/f_s)
|
||||
s = 0.5 * np.sin(2*np.pi*f*t)
|
||||
|
||||
sd.play(normalize_signal(s), samplerate=f_s, blocking=True)
|
||||
# Convert signal covariance
|
||||
var_s = np.cov(s)
|
||||
|
||||
# Calculate required noise variance
|
||||
var_snr_10 = var_s/(10**(10/10))
|
||||
var_snr_20 = var_s/(10**(20/10))
|
||||
var_snr_30 = var_s/(10**(30/10))
|
||||
|
||||
# Genearate noise
|
||||
noise_snr_10 = (var_snr_10**0.5) * np.random.randn(len(s))
|
||||
noise_snr_20 = (var_snr_20**0.5) * np.random.randn(len(s))
|
||||
noise_snr_30 = (var_snr_30**0.5) * np.random.randn(len(s))
|
||||
|
||||
# Add signal and noise
|
||||
m_snr_10 = s+noise_snr_10
|
||||
m_snr_20 = s+noise_snr_20
|
||||
m_snr_30 = s+noise_snr_30
|
||||
|
||||
plt.plot(t,s, label="Pure A$\sharp$ Tone")
|
||||
plt.plot(t,m_snr_10, label="Corrupted A$\sharp$ Tone (SNR=10dB)")
|
||||
plt.plot(t,m_snr_20, label="Corrupted A$\sharp$ Tone (SNR=20dB)")
|
||||
plt.plot(t,m_snr_30, label="Corrupted A$\sharp$ Tone (SNR=30dB)")
|
||||
plt.legend()
|
||||
plt.title("Pure and Corrupted A$\sharp$ Tones")
|
||||
plt.xlabel("Time (s)")
|
||||
plt.show()
|
||||
#sd.play(normalize_signal(s), samplerate=f_s, blocking=True)
|
||||
|
||||
Reference in New Issue
Block a user