# Measure the SNR of a signal in the frequency domain?

How do you measure the SNR of a sinewave in the frequency domain? (Assuming no filtering.)

Suppose: $$x(n) = s(n) + n(n)$$

Create the signal (nCycles of a sinewave) in MATLAB...:

sampleRate = 1024;
f0 = sampleRate/8;

nCycles = 11;
time = 0:1/sampleRate:nCycles/f0;

signal = sin(2*pi*f0*time);


Create the noise and scale it by the desired SNR...

SNR = 10;
noise = randn(size(signal));
% scale the noise to obtain the desired SNR
noise = noise / norm(noise) * norm(signal) / 10^(SNR/20);

x = signal + noise;


Calculate the resulting SNR (in the time domain):

actualSNR = 20*log10(norm(signal)/norm(x - signal));
disp(['SNR = ',num2str(actualSNR),'  dB'])


Plot the noisy signal in time and frequency

subplot(2,1,1)
% plot the signal in time
plot(time,signal)
hold on; grid on
plot(time,x,'r')

% plot the noisy signal in frequency
NFFT = 4096;
X = fftshift(fft(x,NFFT));
X = X/max(abs(X));
f = sampleRate/2*linspace(-1,1,NFFT);
subplot(2,1,2)

plot(f,20*log10(abs(X)))
grid on;

• Specifying a SNR without mention of a bandwidth has little meaning. I.e., for a pure tone, the narrower the bandwidth, the higher the SNR. That's one of the main reasons to use a filter - increase the SNR. Mar 8 '14 at 20:45
• I disagree with you. Since I didn't mention any filters, it's quite clear what the bandwidth of interest is. Mar 10 '14 at 15:43
• Well, maybe in your signal world. Why someone wouldn't use a simple filter to increase the SNR is at least questionable. Nice OP edit. Mar 11 '14 at 1:50

It can be a little tricky. If your sine wave happens to fall at an FFT bin center, things are a little easier. If your sine wave happens to not fall at a bin center, you have to consider spectral leakage. You'll also need to consider how your window function affects your signal.

A simple technique to estimate the signal power would be to sum the squared magnitudes of the FFT bins with most of the signal's energy (i.e. the largest bins). To get the noise energy, sum the squared magnitude of all of the other bins. This will get you close.

If you know the SNR per sample in the time domain (SNRt) the SNR in the frequency domain (SNRf) will be

$$SNR_f = SNR_t \sqrt{N}$$

where N is the number of non zero elements in the basis vector in the Fourier transform. The fastest Fourier transforms (FFT with $n=2^{integer}$) has most zeros and thus lowest SNRf. The highest SNRf comes with $N$ as a prime number with no zeros in the basis vectors, which is also thus the most arithmetically intense choice of sample size. Quality costs. Also pay attention to the fact that the number of zeros in the basis vectors in the FFT depends on the frequency leading to SNRf being frequency dependent. Lowest SNRf is at the middle frequency where every second element of the cosine part is zero.

Now tell me why people still use sample sets of 2^integer when it is the choice of lowest quality? Today we have the means to do the extra computations of a zero free Fourier transform. It would led to better image quality, better sound and higher data rates.

• How would you know the SNR per sample in the time domain? Can you elaborate on "The highest SNRf comes with N as a prime number with no zeros in the basis vectors"? Nov 10 '14 at 4:46
• SNR in time domain is either given, measured, assumed or calculated. I was specifically thinking about the cosine transformation or the even FFT when talking about zero free basis. Check for example the basis vector link which uses samle size 2^4=16 and having half of the components zero. Change that to 17 and get no zeros link Nov 12 '14 at 12:51
• The intent of the question was to determine the SNR given no information about the time domain. Doesn't the FFT become the DFT if N is not a power of 2? Nov 12 '14 at 22:06
• Sure, the inverse is so similar, just a change of sign in the exponent, that the relation holds. So for the inverse Fourier transform, with signal to noise ratio in a specific frequency SNRif, the SNR in the resulting time domain would be SNRit = √N SNRif Nov 23 '14 at 21:03
• Addition. With a properly scaled transformation there is no change in variance or standard deviation in a Fourier transform, but there is an increase in the expected value for the DC signal. It grows as $$µ_f = \sqrt N µ_t$$ this is not dependent on the number of zeros in the basis vectors. Dec 18 '14 at 16:09

Here is a better answer (than the one I gave before) based on properly scaled Fourier transformations where basis vectors are normalized to length 1.

Noise, a normal distribution of a random variable, has the following variance, based on https://en.wikipedia.org/wiki/Variance#Basic_properties for variance of a linear combination with no correlation, applies to both the Fourier transformation and it's inverse

$${Var}\left(\sum _{{i=1}}^{{N}}a_{i}X_{i}\right) = \sum _{{i=1}}^{{N}}a_{i}^{2}\operatorname {Var}(X_{i})$$

The same variance for all Xi means $${Var}\left(\sum _{{i=1}}^{{N}}a_{i}X_{i}\right) = \sum _{{i=1}}^{{N}}a_{i}^{2}\operatorname {Var}(X) = \left(\sum _{{i=1}}^{{N}}a_{i}^{2}\right)\operatorname {Var}(X)$$

Example 1

The first row DC basis vector in a even Fourier transform
$$\left(\frac{1}{\sqrt N } , \frac{1}{\sqrt N } , \frac{1}{\sqrt N } , ... \right)$$ gives a variance of

$$Var(Transform) = N \left( \frac{1}{\sqrt N } \right)^2 Var(X) = Var(X)$$

Example 2

A basis vector in an even fast Fourier transform with a wavelength of 4 samples, a repeating series of

$$\left( \sqrt{\frac{2}{N}} , 0 , -\sqrt{\frac{2}{N}} , 0 , ... \right)$$

This is the basis vector with most zero elements, every second. The variance becomes

$$Var(Transform) = \frac{N}{2} \left( \sqrt{\frac{2}{N}} \right)^2 Var(X) = Var(X)$$

So variance, and thus standard deviation, is independent of basis vectors and zero elements. But what happens to the signal, the expected value? Maybe someone else can show that?

Another solution can be to model the noise using the non-sine frequencies. This relies on having a reasonable parametric model whose parameters can be estimated from the non-sine frequencies. E.g. if you know the noise is white or pink, this is fairly straightforward. Once you've got the parameters estimated, it's also easy to estimate how much noise there is at the sine frequency, and sum up all noise contributions.