1 def wgn(x, snr): 2 snr = 10**(snr/10.0) 3 xpower = np.sum(x**2)/len(x) 4 npower = xpower / snr 5 return np.random.randn(len(x)) * np.sqrt(npower)
wgn是獲得原始信號為x,相對於原始信號信噪比是snr dB的高斯噪聲
voice= [......] #voice 為一個聲音序列 n = wgn(voice, 6) voice= np.array(voice) n = np.array(n) voice_withnoise= voice+n plt.plot(voice_withnoise) plt.show()