Anda di halaman 1dari 5

Menghitung noise

menghitung SNR dengan Matlab / Octave


SNR- Signal to Noise Ration adalah ukuran untuk membandingkan suatu sinyal dengan noisenya. SNR didefinisikan sebagai perbandingan power signal dengan power noisenya. Gampangannya, kalau power sinyal yang kita inginkan lebih kecil daripada noisenya, maka hasil kita jelek (hasil apapun, entah dalam speech enhacement, source separation, imaga processing atau yang lain). Sehingga, semakin besar nilai SNR tentunya semakin baik kualitas desired signal kita. Sesuai formulanya, SNR dirumuskan sebagai berikut; dimana P merupakan power rata-rata baik dari sinyal maupun noise yang diukur pada t yang sama dan dalam bandwith yang sama. Jika sinyal dan noise memiliki impedansi yang sama, maka SNR dapat dihitung dari kuadrat Amplitudo kedua sinyal dan noise tersebut. dimana A merupakan root mean square (rms) dari amplitudo sinyal. Karena banyak sinyal memiliki dynamic range, maka SNR dapat dinyatakan dalam decibel (dB) sebagai berikut:

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...

Plot dari Sinyal, Noise dan Sinyal+Noise http://bagustris.blogspot.com/2011/10/menghitung-snr-dengan-matlab-octave.html

Generating a signal waveform with required SNR in Matlab


In the process of simulating a communication system in Matlab, often we are stymied by the problem of generating a signal with required amount of SNR. Upon a closer look at the underlying SNR equation, the above mentioned problem which is like a skein of thread can be disentangled easily. By definition SNR is the ratio of signal power to noise power. ( Refer my previous post on calculation of power and energy content of a signal in Matlab - click here ). The power content of any signal can also be calculated from its variance. The noise variance is relatively constant in any communication channel. To get a signal with correct SNR, we need to to scale the desired signal appropriately to get the desired SNR. Equation to calculate SNR : Given the variance of the signal 2S and the variance of the noise 2N SNR can be calculated as,

Rearranging the equation,

All we have to do is to scale the desired signal appropriately as given below

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