I am trying to add a Gaussian noise, normal distributed to a signal I have simulated (sig_noiseFree), to get a noisy signal (sig_noisy). lease see the code.
I wrote the function my self according to what I understood. Can please anyone confirm that I am doing this right ? especially the noise addition and SNR calculation ?
I have a second question : I understand that the variable "k" controls the noise level, how can I set the SNR and generate the suitable k so I get the noisy signal with the SNR wanted ? Thank you in advance.
def SNR(sig, noise,dt):
Signal = np.sum(np.abs(np.fft.fft(sig)*dt)**2)/len(np.fft.fft(sig))
Noise = np.sum(np.abs(np.fft.fft(noise)*dt)**2)/len(np.fft.fft(noise))
return (10 * np.log10(Signal/Noise))
def Gauss_noise(k,sig):
return np.random.normal(scale=k*np.max(sig), size=len(sig))
def noisy_sig(sig,k, dt):
return np.fft.ifft(np.fft.fft(sig)*dt+np.fft.fft(np.random.normal(scale= k*np.max(sig), size=len(sig)))*dt)/dt
def main(argv):
k = 2e-2
noise_g = Gauss_noise(k,sig_noiseFree)
sig_noisy = noisy_sig(sig_noiseFree,k, dt)
if __name__ == "__main__":
main(sys.argv)