对于那些试图在SNR和numpy生成的普通随机变量之间建立连接的人:
[1]
,其中必须记住p是平均功率。
或分贝:
[2]
在这种情况下,我们已经有一个信号,我们想产生噪声,给我们一个期望的信噪比。
虽然噪声可能来自不同的flavors取决于您正在建模的对象,但一个好的开始(特别是对于这个射电望远镜示例)是Additive White Gaussian Noise (AWGN)。如前一个答案所述,要建立AWGN模型,需要在原始信号中添加一个零均值高斯随机变量。随机变量的方差将影响平均噪声功率。
对于高斯随机变量X,平均功率
,也称为第二个moment,是
[3]
所以对于白噪声,
,平均功率等于方差
。
在python中建模时,您可以
一。根据所需的信噪比和一组现有测量值计算方差,如果您希望您的测量值具有相当一致的振幅值,这将起作用。
2。或者,可以将噪声功率设置为已知级别,以匹配接收器噪声之类的内容。接收噪声可以通过将望远镜指向自由空间并计算平均功率来测量。
不管怎样,重要的是要确保你在信号中加入了噪声,并在线性空间中取平均值,而不是以分贝为单位。
下面是一些生成信号并绘制电压、功率(瓦特)和功率(分贝)的代码:# Signal Generation
# matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(1, 100, 1000)
x_volts = 10*np.sin(t/(2*np.pi))
plt.subplot(3,1,1)
plt.plot(t, x_volts)
plt.title('Signal')
plt.ylabel('Voltage (V)')
plt.xlabel('Time (s)')
plt.show()
x_watts = x_volts ** 2
plt.subplot(3,1,2)
plt.plot(t, x_watts)
plt.title('Signal Power')
plt.ylabel('Power (W)')
plt.xlabel('Time (s)')
plt.show()
x_db = 10 * np.log10(x_watts)
plt.subplot(3,1,3)
plt.plot(t, x_db)
plt.title('Signal Power in dB')
plt.ylabel('Power (dB)')
plt.xlabel('Time (s)')
plt.show()
以下是基于所需信噪比添加AWGN的示例:# Adding noise using target SNR
# Set a target SNR
target_snr_db = 20
# Calculate signal power and convert to dB
sig_avg_watts = np.mean(x_watts)
sig_avg_db = 10 * np.log10(sig_avg_watts)
# Calculate noise according to [2] then convert to watts
noise_avg_db = sig_avg_db - target_snr_db
noise_avg_watts = 10 ** (noise_avg_db / 10)
# Generate an sample of white noise
mean_noise = 0
noise_volts = np.random.normal(mean_noise, np.sqrt(noise_avg_watts), len(x_watts))
# Noise up the original signal
y_volts = x_volts + noise_volts
# Plot signal with noise
plt.subplot(2,1,1)
plt.plot(t, y_volts)
plt.title('Signal with noise')
plt.ylabel('Voltage (V)')
plt.xlabel('Time (s)')
plt.show()
# Plot in dB
y_watts = y_volts ** 2
y_db = 10 * np.log10(y_watts)
plt.subplot(2,1,2)
plt.plot(t, 10* np.log10(y_volts**2))
plt.title('Signal with noise (dB)')
plt.ylabel('Power (dB)')
plt.xlabel('Time (s)')
plt.show()
下面是一个基于已知噪声功率添加AWGN的示例:# Adding noise using a target noise power
# Set a target channel noise power to something very noisy
target_noise_db = 10
# Convert to linear Watt units
target_noise_watts = 10 ** (target_noise_db / 10)
# Generate noise samples
mean_noise = 0
noise_volts = np.random.normal(mean_noise, np.sqrt(target_noise_watts), len(x_watts))
# Noise up the original signal (again) and plot
y_volts = x_volts + noise_volts
# Plot signal with noise
plt.subplot(2,1,1)
plt.plot(t, y_volts)
plt.title('Signal with noise')
plt.ylabel('Voltage (V)')
plt.xlabel('Time (s)')
plt.show()
# Plot in dB
y_watts = y_volts ** 2
y_db = 10 * np.log10(y_watts)
plt.subplot(2,1,2)
plt.plot(t, 10* np.log10(y_volts**2))
plt.title('Signal with noise')
plt.ylabel('Power (dB)')
plt.xlabel('Time (s)')
plt.show()