Search code examples
matlabsignal-processingnoise

Add random noise with specific SNR to a signal


I have randomly generated signal for example:

%frequency
f1 = 1000; f2 = 2000;
Fs = 8000; %sampling frequency

%amplitudes
amp1 = 0.75;
amp2 = 0.2;

%time
dt = 1/Fs
stopTime = 0.3;
t = 0:dt:stopTime;

%random noise
noise = randn(1,length(t))

%generated signal
Signal = amp1*sin(2*pi*f1*t) + amp2*sin(2*pi*f1*t) + noise;

Now i need to create two Signals S1, S2 with random noise with specific SNR. Noise added to S1 must be uncorrelated with noise added to S2 Here is what i tried:

%SNR in dB
SNR = [-80,-40,-20,0,20,40,80];

%S1,S2 = Signal + rand noise with SNR -40dB
S1 = awgn(Signal,SNR(2),'measured')
S2 = awgn(Signal,SNR(2),'measured')

Is this correct approach to create random noise with SNR from range -80dB to +80dB? will noise added to S1 be uncorrelated with noise added to S2?


Solution

  • You could just calculate variance of signal and add noise with variance required to produce desired SNR. Here is some python code as an example

    import scipy as sp
    import scipy.fftpack as fft
    
    # frequency
    f1 = 1000.0
    Fs = 8000.0  # sampling frequency
    
    # amplitudes
    amp1 = 0.75
    
    # time
    n = sp.arange(1024)
    
    # Desired SNR in dB
    SNR_dB = 40
    
    # Desired linear SNR
    snr = 10.0**(SNR_dB/10.0)
    print "Linear snr = ", snr
    
    # Measure power of signal
    signal1 = amp1*sp.sin(2*sp.pi*f1/Fs*n)
    p1 = signal1.var()
    print "Power of signal1 = ", p1
    
    # Calculate required noise power for desired SNR
    n = p1/snr
    print "Noise power = ", n
    print "Calculated SNR =  %f dB" % (10*sp.log10(p1/n))
    
    # Generate noise with calculated power
    w = sp.sqrt(n)*sp.randn(1024)
    
    # Add noise to signal
    s1 = signal1 + w
    

    will noise added to S1 be uncorrelated with noise added to S2?

    Yes.