You are here : matlabSignal Processingsnr

snr() - Signal Processing

exampler = snr(x,y) returns
the signal-to-noise ratio (SNR) in decibels of a signal, x,
by computing the ratio of its summed squared magnitude to that of
the noise, y. y must have
the same dimensions as x. Use this form when
the input signal is not necessarily sinusoidal and you have an estimate
of the noise.

exampler = snr(x) returns
the SNR in decibels relative to the carrier (dBc) of a real-valued
sinusoidal input signal, x. The SNR is determined
using a modified periodogram of the same length as the input. The
modified periodogram uses a Kaiser window with β =
38. The result excludes the power of the first six harmonics, including
the fundamental.
exampler = snr(x,fs,n) returns
the SNR in dBc of a real sinusoidal input signal, x,
sampled at a rate fs. The computation excludes
the power contained in the lowest n harmonics,
including the fundamental. The default value of fs is
1. The default value of n is 6.

exampler = snr(pxx,f,'psd') specifies
the input pxx as a one-sided power spectral density
(PSD) estimate. The argument f is a vector of
the frequencies at which the estimates of pxx occur.
The computation of noise excludes the power of the first six harmonics,
including the fundamental.
r = snr(pxx,f,n,'psd') specifies
the number of harmonics, n, to exclude when computing
the SNR. The default value of n is 6 and includes
the fundamental.

exampler = snr(sxx,f,rbw,'power') specifies
the input as a one-sided power spectrum, sxx,
of a real signal. The input rbw is the resolution
bandwidth over which each power estimate is integrated.
r = snr(sxx,f,rbw,n,'power') specifies
the number of harmonics, n, to exclude when computing
the SNR. The default value of n is 6 and includes
the fundamental.

example[r,noisepow]
= snr(___) also returns the total noise power
of the nonharmonic components of the signal.

examplesnr(___) with no output arguments
plots the spectrum of the signal in the current figure window and
labels its main features. It uses different colors to draw the fundamental
component, the DC value and the harmonics, and the noise. The SNR
appears above the plot. This functionality works for all syntaxes
listed above except snr(x,y).


Syntax

r = snr(x,y) exampler = snr(x) exampler = snr(x,fs,n) exampler = snr(pxx,f,'psd') exampler = snr(pxx,f,n,'psd')r = snr(sxx,f,rbw,'power') exampler = snr(sxx,f,rbw,n,'power')[r,noisepow]
= snr(___) examplesnr(___) example


Example

Signal-to-Noise Ratio for Rectangular Pulse with Gaussian NoiseOpen This Example
Compute the signal-to-noise ratio (SNR) of a 20 ms rectangular pulse sampled for 2 s at 10 kHz in the presence of Gaussian noise. Set the random number generator to the default settings for reproducible results.
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)

pulseSNR =

   80.0818

Compare SNR with THD and SINADOpen This Example
Compute and compare the signal-to-noise ratio (SNR), the total harmonic distortion (THD), and the signal to noise and distortion ratio (SINAD) of a signal.
Create a sinusoidal signal sampled at 48 kHz. The signal has a fundamental of frequency 1 kHz and unit amplitude. It additionally contains a 2 kHz harmonic with half the amplitude and additive noise with variance 
. Set the random number generator to the default settings for reproducible results.rng default
fs = 48e3;
t = 0:1/fs:1-1/fs;
A = 1.0; powfund = A^2/2;
a = 0.4; powharm = a^2/2;
s = 0.1; varnoise = s^2;
x = A*cos(2*pi*1000*t) + ...
    a*sin(2*pi*2000*t) + s*randn(size(t));
Verify that SNR, THD, and SINAD agree with their definitions.SNR = snr(x);
defSNR = 10*log10(powfund/varnoise);
SN = [SNR defSNR]

THD = thd(x);
defTHD = 10*log10(powharm/powfund);
TH = [THD defTHD]

SINAD = sinad(x);
defSINAD = 10*log10(powfund/(powharm+varnoise));
SI = [SINAD defSINAD]

SN =

   17.0178   16.9897


TH =

   -7.9546   -7.9588


SI =

    7.4571    7.4473

Noise PowerOpen This Example
Compute the noise power in the sinusoid from the preceding example. Verify that it agrees with the definition. Set the random number generator to the default settings for reproducible results.
rng default
fs = 48e3;
t = 0:1/fs:1-1/fs;
A = 1.0; powfund = A^2/2;
a = 0.4; powharm = a^2/2;
s = 0.1; varnoise = s^2;
x = A*cos(2*pi*1000*t) + ...
    a*sin(2*pi*2000*t) + s*randn(size(t));

[SNR,npow]=snr(x,fs);
compare = [10*log10(powfund)-npow SNR]

compare =

   17.0281   17.0178

Signal-to-Noise Ratio of SinusoidOpen This Example
Compute the SNR of a 2.5 kHz sinusoid sampled at 48 kHz. Add white noise with standard deviation 0.001. Set the random number generator to the default settings for reproducible results.
rng default
Fi = 2500; Fs = 48e3; N = 1024;
x = sin(2*pi*Fi/Fs*(1:N)) + 0.001*randn(1,N);
SNR = snr(x,Fs)

SNR =

   57.7103

SNR of Sinusoid Using PSDOpen This Example
Obtain the periodogram power spectral density (PSD) estimate of a 2.5 kHz sinusoid sampled at 48 kHz. Add white noise with standard deviation 0.00001. Use this value as input to determine the SNR. Set the random number generator to the default settings for reproducible results.
rng default
Fi = 2500;
Fs = 48e3;
N = 1024;
x = sin(2*pi*Fi/Fs*(1:N)) + 0.00001*randn(1,N);

w = kaiser(numel(x),38);
[Pxx, F] = periodogram(x,w,numel(x),Fs);
SNR = snr(Pxx,F,'psd')

SNR =

   97.7446

SNR of Sinusoid Using Power SpectrumOpen This Example
Using the power spectrum, compute the SNR of a 2.5 kHz sinusoid sampled at 48 kHz and embedded in white noise with a standard deviation of 0.00001. Reset the random number generator for reproducible results.
rng default
Fi = 2500;
Fs = 48e3;
N = 1024;
x = sin(2*pi*Fi/Fs*(1:N)) + 0.00001*randn(1,N);

w = kaiser(numel(x),38);
[Sxx, F] = periodogram(x,w,numel(x),Fs,'power');
rbw = enbw(w,Fs);
SNR = snr(Sxx,F,rbw,'power')

SNR =

   97.7446

Plot the spectrum of the signal and annotate the SNR.snr(Sxx,F,rbw,'power');

SNR of Amplified SignalOpen This Example
Generate a sinusoid of frequency 2.5 kHz sampled at 50 kHz. Reset the random number generator. Add Gaussian white noise with standard deviation 0.00005 to the signal. Pass the result through a weakly nonlinear amplifier. Plot the SNR.
rng default

fs = 5e4;
f0 = 2.5e3;
N = 1024;
t = (0:N-1)/fs;

ct = cos(2*pi*f0*t);
cd = ct + 0.00005*randn(size(ct));

amp = [1e-5 5e-6 -1e-3 6e-5 1 25e-3];
sgn = polyval(amp,cd);

snr(sgn,fs);

The DC component and all harmonics, including the fundamental, are excluded from the noise measurement. The fundamental and harmonics are labeled.Related ExamplesAnalyzing Harmonic Distortion


Output / Return Value


Limitations


Alternatives / See Also


Reference