You are on page 1of 2

Fs=2000; Fc=10; t=0:1/Fs:1; signal=sin(2*pi*Fc*t);

%Sampling Frequency % Carrier Frequency % define evaluation time % Sample signal waveform

%Generate Scaled signal, noise for SNR = 5dB % Also works well for negative SNR [scaledSignal,noise,measuredSNR]=genSignalForSNR(signal,5); ----------------------------------%%%%%%%%%%%%%%%%%%%%% %FileName - genSignalForSNR.m %Author: Mathuranathan %http://www.gaussianWaves.com %Creative Commons - cc by-nc-sa %%%%%%%%%%%%%%%%%%%%% function [signalWithNoise,noise,measuredSNR]= genSignalForSNR(signal,SNR); % Generate Gaussian Noise with zero mean and unit variance noise=randn(1,length(signal)); %Scale the input signal accordingly for the given SNR. scaledSignal = std(noise)/std(signal)*(sqrt(10^(SNR/10)))*signal; %calculate Signal power and noise power signalPower = (norm(scaledSignal)^2)/length(scaledSignal); noisePower = (norm(noise)^2)/length(noise); %Alternative way of calculating Signal and noise power from their variance %signalPower = var(scaledSignal); %noisePower = var(noise); %Calculate Signal to noise ratio for the scaledSignal and generated Noise SNRratio = signalPower/noisePower; measuredSNR=10*log10(SNRratio); %Add the scaled signal with the generated noise signalWithNoise=scaledSignal+noise; %plotting commands subplot(3,1,1); plot(scaledSignal); title('Input Signal'); subplot(3,1,2); plot(noise); title('Generated Noise'); subplot(3,1,3); plot(signalWithNoise); title(['Signal + Noise for SNR= ',num2str(measuredSNR),' dB']); -------------------------------rng default Tpulse = 20e-3; Fs = 10e3; t = -1:1/Fs:1;

x = rectpuls(t,Tpulse); y = 0.00001*randn(size(x)); s = x + y; pulseSNR = snr(x,s-x)