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

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

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

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

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

snr(___) 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).

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)

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 0.1^{2}.
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.

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);
[10*log10(powfund)-npow SNR]

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)

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')

Compute the SNR of the sinusoid from the preceding example,
using the power spectrum. 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);
[Sxx, F] = periodogram(x,w,numel(x),Fs,'power');
rbw = enbw(w,Fs);
SNR = snr(Sxx,F,rbw,'power')

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.

Sampling frequency, specified as a positive scalar. The sampling
frequency is the number of samples per unit time. If the unit of time
is seconds, the sampling frequency has units of hertz.

Resolution bandwidth, specified as a positive scalar. The resolution
bandwidth is the product of the frequency resolution of the discrete
Fourier transform and the equivalent noise bandwidth of the window.

Signal-to-noise ratio, expressed in decibels relative to the
carrier (dBc), returned as a real-valued scalar. The SNR is returned
in decibels (dB) if the input signal is not sinusoidal.

The functions thd, sfdr, sinad,
and snr measure the response
of a weakly nonlinear system stimulated by a sinusoid.

When given time-domain input, snr performs
a periodogram using a Kaiser window with large sidelobe attenuation.
To find the fundamental frequency, the algorithm searches the periodogram
for the largest nonzero spectral component. It then computes the central
moment of all adjacent bins that decrease monotonically away from
the maximum. To be detectable, the fundamental should be at least
in the second frequency bin. Higher harmonics are at integer multiples
of the fundamental frequency. If a harmonic lies within the monotonically
decreasing region in the neighborhood of another, its power is considered
to belong to the larger harmonic. This larger harmonic may or may
not be the fundamental.

The function estimates a noise level using the median power
in the regions containing only noise. The DC component is excluded
from the calculation. The noise at each point is the estimated level
or the ordinate of the point, whichever is smaller. The noise is then
subtracted from the values of the signal and the harmonics.

snr fails if the fundamental
is not the highest spectral component in the signal.

Ensure that the frequency components are far enough apart to
accommodate for the sidelobe width of the Kaiser window. If this is
not feasible, you can use the 'power' flag and
compute a periodogram with a different window.