Tal*_*war 5 python signal-processing numpy gaussian
我想向多元数据添加 5% 高斯噪声。这是方法
import numpy as np
mu, sigma = 0, np.std(data)*0.05
noise = np.random.normal(mu, sigma, data.shape)
noise.shape
Run Code Online (Sandbox Code Playgroud)
我认为你是在正确的轨道上,噪声本质上是相加的,如果你看看(SNR)信噪比计算
信噪比 = 20 * log(p_s)/(p_n)
这只不过是
信噪比 = 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()
Run Code Online (Sandbox Code Playgroud)
我注意到的一件有趣的事情是,np.random.normal对于非常小的方差值,主要对正值进行采样,因此最好缩放 5%,即使用较高方差值采样后的方差