如何将5%高斯噪声添加到信号数据

时间:2020-09-26 06:44:13

标签: python numpy signal-processing gaussian

我想将5%高斯噪声添加到多变量数据中。 这是方法

import numpy as np 
mu, sigma = 0, np.std(data)*0.05 
noise = np.random.normal(mu, sigma, data.shape)
noise.shape

这是信号。这是添加5%高斯噪声的正确方法吗 enter image description here

1 个答案:

答案 0 :(得分:0)

我认为您走在正确的轨道上,噪声本质上是可加的,如果您查看(SNR)信噪比计算

SNR = 20 * log(p_s)/(p_n)

这不过是

SNR = 20(log(p_s)-log(p_n))

所以我们基本上是在信号中减去噪声的功率

为整个信号添加噪声

我将与您发布的内容相同

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(137)
t = np.linspace(0, 10, 100)
p = np.sin(t)
percentage = 0.05
n = np.random.normal(0, p.std(), t.size) * percentage
pn = p + n

fig = plt.figure()

ax1 = fig.add_subplot(211)
ax1.set_title('Noise added to entire signal')
ax1.plot(t, p, label='pure signal')
ax1.plot(t, pn, label='signal+noise')

ax2 = fig.add_subplot(212)
ax2.plot(t, pn - p, label='added noise', c='r')
plt.legend()

fig = plt.figure()

ax1 = fig.add_subplot(211)
ax1.set_title('Noise added to part of the signal')
ax1.plot(t, p, label='pure signal')
random_indices = np.random.randint(0, t.size, int(t.size*percentage))

pr = p.copy()
pr[random_indices] += n[random_indices]
ax1.plot(t, pr, label='signal+noise')

ax2 = fig.add_subplot(212)
ax2.plot(t, pr - p, label='added noise', c='r')
plt.legend()
plt.show()

enter image description here

enter image description here

注意

我注意到的一件有趣的事是np.random.normal,因为方差很小,主要是对正值进行采样,因此最好缩放5%,即在使用较高方差值进行采样之后对方差进行缩放