import numpy as np
import matplotlib.pyplot as plt
#高斯白噪声,x是数据,snr是信噪比
def wgn(x, snr):
Ps = np.sum(np.power(x, 2)) / np.prod(x.shape)
Pn = Ps / (np.power(10, snr / 10))
noise = np.random.randn(x.shape[0],x.shape[1]) * np.sqrt(Pn)
return x + noise
#下面是举例
T = 100
x = np.zeros((1, T)) + 35.
snr = 50
x_noise = wgn(x, snr)
plt.plot(x_noise[0])
plt.plot(x[0])
plt.ylim([30, 40])
plt.show()