How to add White Gaussian Noise to Signal using MATLAB ?

16 Aug, 2021
In this article, we are going to discuss the addition of “White Gaussian Noise” to signals like sine, cosine, and square wave using MATLAB. The white Gaussian noise can be added to the signals using MATLAB/GNU-Octave inbuilt function awgn(). Here, “AWGN” stands for “Additive White Gaussian Noise”.
AWGN is a very basic noise model commonly used in the communication system, signal processing, and information theory to imitate the effect of random processes that occur in nature. 
We normally have different syntaxes for awgn() function depending on the number and type of parameters passed to it. But here, we will study only two syntaxes of it which are most commonly used in the communication system and signal processing.
Syntax:
awgn(input_signal, snr)
This syntax will add the white Gaussian noise to the passed input_signal and maintains the passed SNR (signal to noise ratio) in dB. By default this syntax considers the power of the input_signal as 0 dBW (decibel watt).
awgn(input_signal, snr, signal_power)
This syntax will do the same thing as the first one but the only difference is, here the power of the input_signal is not considered as zero rather it has to be passed as one of the arguments along with the input_signal and snr.
Note: Signal power can be passed as “measured” or some scalar value to set the signal level of the input_signal, according to which the appropriate noise level is determined based on the value of snr.
Let’s understand the implementation with the help of an example where we will add the gaussian white noise to the sine waves.
Step 1: Define the required parameters
Matlab
% sampling frequencyfs = 1000; % time sampling with step% size = 0.001 (1/fs)t = 0:1/fs:1; % frequency of input signalf = 20; % SNR to be maintainedsnr = 10;
  
Step 2: Define the input signal and plot 
Matlab
% input message signal (sine wave)% generates a sine wave of frequency fst = sin(2 * pi * f * t); % plot the input signal (sine wave)% 'b' gives blue colour plotplot(t, st, 'b', 'Linewidth', 2);xlabel('Time');ylabel('Amplitude');title('Input Message Signal');grid on;
Output:
Input Signal (Sine Wave)
 
Step 3: Add white Gaussian noise to signal and plot
Matlab
% signal with white Gaussian noise% adds White Gaussian Noise to the signalst_nn = awgn(st, snr, 'measured'); % plot the noisy signal% 'r' gives red colour plotplot(t, st_nn, 'r', 'Linewidth', 2);xlabel('Time');ylabel('Amplitude');title('Signal After Addition of Noise');grid on;
Output:
Noisy Signal (With white Gaussian noise)
 
Let’s see another example Addition of white Gaussian noise to square wave. We have to follow the same three steps as above to add the white Gaussian noise to the square wave. But this time we will plot both the input signal and the noisy signal simultaneously in the same figure to analyze the changes carefully.
Matlab
% sampling frequencyfs = 1000; % time sampling with step size% 0.001 (1/fs)t = 0:1/fs:1; % frequency of input signalf = 10; % SNR to be maintained snr = 5; % Input Message Signal% generates a square wave of% frequency fst = square(2 * pi * f * t);subplot(2,1,1); % 'b' gives blue colour plotplot(t,st,'b','Linewidth',2);xlabel('Time'); ylabel('Amplitude');title('Input Message Signal');grid on;  % signal with white Gaussian% noise adds White Gaussian% Noise to the signalst_nn = awgn(st,snr,'measured'); % gives a blue colour plot of% square wavesubplot(2,1,2); plot(t, st, 'b', 'Linewidth', 2); % puts the next graph on the% same plothold on; % gives a red colour plot of the% AWGN noiseplot(t, st_nn, 'r');xlabel('Time');ylabel('Amplitude');title('Signal After Addition of Noise');grid on;hold off;
Output:
Output Plots
kapoorsagar226
rajeev0719singh
MATLAB
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.