title: How to generate complex white noise with specified SNR?
body: <p>I'm using the following code to generate complex white noise with a specified SNR (signal-to-noise ratio) and zero mean:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The code is inspired from this <a href="https://stackoverflow.com/questions/55700338/how-to-generate-a-complex-gaussian-white-noise-signal-in-pythonor-numpy-scipy">question</a>. The problem is the SNR displayed is doubled the specified SNR (e.g, with SNR_dB = -10 [dB], the printed value is ~ -20 [dB]).</p>
  
  <p>Does anybody know what is the mistake?</p>