Search code examples
pythonrandomcomplex-numbersnoise

How to generate complex white noise with specified SNR?


I'm using the following code to generate complex white noise with a specified SNR (signal-to-noise ratio) and zero mean:

import numpy as np

def GenerateNoise(signal, SNR_dB): # desiredSNR [dB]; signal is an array with complex values
   n = np.zeros((len(signal),1), dtype=complex)
   # SNR = var_signal^2 / var_n^2
   snr = 10.0**(SNR_dB/10.0) # Desired linear SNR
   var_signal = signal.var() # Measure power of signal
   var_n = var_signal / snr # Calculate required noise power for desired SNR
   if (var_n == 0): # In case of a null signal
       var_n = snr
   e = np.random.normal(0, np.sqrt(var_n/2), size=(len(signal), 2)).view(np.complex) # Generate noise with calculated power
   for i in range(0, len(signal)):
        n[i,0] = n[i,0] + e[i][0]
   print(10.0*np.log10((np.var(signal)**2)/(np.var(n)**2)))
   return n

The code is inspired from this question. The problem is the SNR displayed is doubled the specified SNR (e.g, with SNR_dB = -10 [dB], the printed value is ~ -20 [dB]).

Does anybody know what is the mistake?


Solution

  • I've figured it out. I wasn't calculating, nor displaying the noise correctly. This is the corrected code, if it is of use to anybody:

    import numpy as np
    def GenerateNoise(signal, SNR_dB): # desiredSNR [dB]; signal is an array with complex values
       n = np.zeros((len(signal),1), dtype=complex)
       # SNR = var_signal / var_n
       snr = 10.0**(SNR_dB/10.0) # Desired linear SNR
       var_signal = signal.var() # Measure power of signal
       var_n = var_signal / snr # Calculate required noise power for desired SNR
       if (var_n == 0): # In case of a null signal
           var_n = snr
       e = np.random.normal(0, np.sqrt(var_n*2.0)/2.0, size=(len(signal), 2)).view(np.complex) # Generate noise with calculated power
       for i in range(0, len(signal)):
           n[i,0] = n[i,0] + e[i][0]
       print(10.0*np.log10((np.var(signal))/(np.var(n))))
       return n