yang mana persamaan diatas ekuivalen dengan persamaan sebelumnya sehingga SNR dalam dB dapat dituliskan dalam persamaan akhir berikut. Nah, bagaimana listing code-nya dalam Matlab/Octave? Silakan pelajari m-file berikut yang saya rangkum dari link sebelah.
-------------------------------------------------------------------------% SNR demo Fs=2000; % Sampling Frequency Fc=10; % Carrier Frequency t=0:1/Fs:1; % define evaluation time signal=sin(2*pi*Fc*t); % Sample signal waveform SNR=5; % SNR 5 dB will be added to signal % 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 commandsFs=2000; %Sampling Frequency Fc=10; % Carrier Frequency t=0:1/Fs:1; % define evaluation time signal=sin(2*pi*Fc*t); % Sample signal waveform %Generate Scaled signal, noise for SNR = 5dB % Also works well for negative SNR 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']); --------------------------------------------------------------------------------------------
Bila anda sukses anda akan mendapatkan hasil plot seperti dibawah. Untuk mengecek benar tidaknya program yang kita buat dapat dengan melihat SNR (measuredSNR) dimana seharusnya mendekati 5 dB karena di awal program telah ditentukan bahwa noise yang ditambakan ke sinyal sebesar 5 dB. Hasil yang ditunjukkan program tidak eksak sebesar 5 dB (sekitat 4,9xx atau 4,8xx dB) karena random noise yang berubah secara random pula... :-) Selamat Mencoba...
The following matlab code incorporates this concept of scaling the desired signal for the required amount of SNR. It also generates the noise and the noise added signal accordingly. If you have Matlab with Communication toolbox installed then you can use "awgn" function[1].The "awgn" function adds white Gaussian noise to the input vector signal. It also provides the option of measuring the power of the input vector before adding the noise. If you don't want to use the function or if the communication toolbox is not available , then you may use the code given below. Download Code: Matlab code for generating signal with noise for given SNR - genSignalForSNR.m Sample simulation: Fs=2000; %Sampling Frequency Fc=10; % Carrier Frequency t=0:1/Fs:1; % define evaluation time signal=sin(2*pi*Fc*t); % Sample signal waveform
%Generate Scaled signal, noise for SNR = 5dB % Also works well for negative SNR [scaledSignal,noise,measuredSNR]=genSignalForSNR(signal,5); Sample Output: measureSNR = 4.9985dB (measure SNR will be approximately 5dB and the variation is due to the Gaussian random noise generated)
http://gaussianwaves.blogspot.com/2010/02/generating-signal-waveform-with.html