添加高斯白噪音 # 添加高斯白噪音 def gen_gaussian_noise(signal,SNR): #signal: 原始信号,SNR: 添加噪声的信噪比,return: 生成的噪声noise=np.random.randn(*signal.shape) # *signal.shape 获取样本序列的尺寸noise=noise-np.mean(noise)signal_power=(1/signal.shape[0])*np.sum(np.power(signal,2))noise_variance=signal_power/np.power(10,(SNR/10))noise=(np.sqrt(noise_variance)/np.std(noise))*noisereturn noise 验证信噪比 def check_snr(signal,noise):signal_power=(1/signal.shape[0])*np.sum(np.power(signal,2))# 0.5722037noise_power=(1/noise.shape[0])*np.sum(np.power(noise,2)) # 0.90688SNR=10*np.log10(signal_power/noise_power)return SNR 给信号加噪声程序 Noise_data = [] X_train=X_train.reshape(5900,2048).detach().numpy() # tensor转换为ndarray for i in range(X_train.shape[0]):noise = gen_gaussian_noise(X_train[i,:],-2)new_signal = X_train[i,:] + noiseNoise_data.append(new_signal)SNR= check_snr(X_train[i,:],noise)