agwn function
from numpy import sum,isrealobj,sqrt
from numpy.random import standard_normal
def awgn(s,SNRdB,L=1):
gamma = 10**(SNRdB/10) #SNR to linear scale
if s.ndim==1:# if s is single dimensional vector
P=L*sum(abs(s)**2)/len(s) #Actual power in the vector
else: # multi-dimensional signals like MFSK
P=L*sum(sum(abs(s)**2))/len(s) # if s is a matrix [MxN]
N0=P/gamma # Find the noise spectral density
if isrealobj(s):# check if input is real/complex object type
n = sqrt(N0/2)*standard_normal(s.shape) # computed noise
else:
n = sqrt(N0/2)*(standard_normal(s.shape)+1j*standard_normal(s.shape))
r = s + n
return r