Published: 2016-04-30 | Category: [»] Electricity & Electronics.

Recently I have been investigating square wave signals and their related signal-to-noise ratio (SNR) and discovered some important relation about them. Please note that all the derivations performed here apply not only to square wave signals but also to triangle waves, saw-tooth signals etc. While it is possible to derive the framework presented here to any periodic signal used as a carrier wave for information, the conclusions of this post may differ depending on the exact characteristics of the signal upon study.

Here, I will focus on square wave signals because they are easy to produce on an electrical point of view as they correspond to all-or-none situations. Sine wave signals, Triangle waves or saw-tooth signals are also common in the electronic field but are usually more difficult to obtain.

To fix a framework, I will build up this post based on a theoretical example of a nephelometer instrument. We have seen in a [»] previous post that a nephelometer gives a direct indication of a sample turbidity by measuring the amount of light scattered at a 90° angle. However, the amount of light collected is usually very small and the setup must be carefully protected from ambient light to avoid any offset in the signal levels. While protecting the setup from ambient light is an easy solution to propose, it is also possible to use a square wave modulation of the illumination to act as a carrier wave. That way, the signal collected when the illumination is off will be the ambient light and the signal collected when the illumination is on will be the sum of the ambient light and the signal. By subtracting the two values, or more exactly by looking at the peak-to-peak value of the recorded wave, we get a direct reading of the signal without the ambient light contribution.

A typical signal recorded with such a device will look like the one on Figure 1. I obtained it from simulation using SimuLink by assuming a 10 Hz square wave carrier signal passing through a one-pole transfer function and containing large amount of white noise. Such a signal is a good case-study representing what one would obtain through a first order low pass filter in a noisy environment. Note that the carrier frequency is usually chosen such that it is much faster than the ambient condition change speed. In this example I have assumed a 10 Hz frequency but it might as well be 1 kHz or even 100 kHz.

Figure 1 – Typical experimental square wave signal measured

Extracting the peak-to-peak value from such a signal is not as straightforward as taking the maximum minus minimum because of both the contribution of noise and low-pass effects making a ramping of the signal. The usual state of the art way of processing such a signal is to take the root mean square (RMS) value defined as

which consists of integrating the square of the signal over a period ∆t that is larger or equal to the period of the signal. This formula can be applied in the time domain and in the frequency (Fourier) domain as well since the Parseval theorem states that energy is conserved during the Fourier transform. One condition to get a meaningful value however is that the mean value of the signal must be null otherelse we would offset the measurement by the value of the mean. To do so, one can either subtract the mean value of the signal in the ∆t sampling period or remove the DC term in the Fourier space which will have the same effect.

Before moving on to the interesting part of the post, I first need to make a halt on SNR and discuss it a little bit.

On Figure 1, we can see that the noise levels are quite important and it is likely that if we repeat ‘M’ times the RMS measurement on M different samples we will get M different values that will change slightly depending on this noise. Therefore, outputting a value with an infinite number of digits has no sense because, at some point, the digits will become meaningless in terms of information as they will talk about the noise in the measurement and not about the signal in the measurement. As a consequence, it is always interesting to present a signal measurement with its SNR expressed as a contrast ratio to have an idea on the actual resolution of the data presented. For example, a SNR of 1:1000 will mean that the signal is typically one thousand time stronger than the noise level in the sample.

To define the SNR of a measurement, we first record the noise power using the RMS algorithm by removing the square wave (e.g. by keeping the illumination off during the measurement) and we then record the signal power using the RMS algorithm by using the carrier wave this time. The SNR value is then:

A well-known fact about SNR is that it is possible to increase it by playing with the number of samples used through an averaging process. Imagine we take ‘N’ samples of the same signal plus some random noise and we sum all the measurements. We know that the sum of the signal power will be N times the individual signal power because the signal does not change (we suppose the signal itself is not changing between the measurements). However, the same is not true for the noise because it is a stochastic process and not a deterministic process. In fact, the noise level will only increase as the square root of N leading to a SNR increasing as the square root of the number of samples:

The noise only increases as the square root of the number of samples because we are summing the noise variances and not the standard deviations:

and because all the variances are the same:

If you have trouble understanding this, imagine a variable describing a white noise process. We know that the distribution of such a variable then takes the form of a Gaussian curve Y:

Imagine now the joint probability of two uncorrelated random events Y1 and Y2 occurring, it can be written as:

which has a variance that is the sum of the two events separated variance.

To understand how the SNR of a square wave is related to its RMS value, we need to have to look at the Fourier spectrum of the signal. I have already shown in details in a [»] previous article how square waves can be built from sines waves at discrete values (and vice-versa). To summarize, a square wave of 10 Hz can be seen as an infinite sum of sine waves of frequency n*10 Hz with n odd with amplitudes decreasing as n-1. This can be seen on Figure 2. The peak at 10 Hz will usually be called the fundamental and the other ones the harmonics.

Figure 2 – Frequency spectrum of a square wave signal of 10 Hz

The consequence of this is that the information (or energy) of the square wave is located at discrete points in the frequency spectrum, all the other frequencies being filled up only with noise. This is important because we have seen that integrating more noise will yield larger amount of noise on the sum, increasing as the square root of the number of frequency bins that we integrate while the signal will only increase at the frequency bins ±10 Hz, ±30 Hz, ±50 Hz etc… Adding the frequency bins at other values will then only increase the noise power and not the signal power.

A good way to increase the SNR is therefore to filter the frequency spectrum by applying a mask at the precise frequency bins where information occurs such as to cancel the noise in the other frequency bins. A typical way of doing this is to combine Gaussian curves and multiply the power spectrum of the frequency domain to produces a filtered RMS value. An example of mask is given on Figure 3 with two Gaussian curves with different broadness (full width at half maximum or FWHM). I will come back on the effect of proper FWHM selection later.

Figure [/a]3 – Two Gaussian mask with different FWHM

Where the things come really interesting is when you write the expected SNR dependency with the number of integrated frequency bins. Let us assume that the power of the fundamental peak is P0 and that the noise power corresponding to the integrated frequencies of the mask is ‘d’. The SNR of the fundamental (0th harmonic) is then:

Because the noise is considered white (i.e. equally distributed over the entire spectrum), we know that it will increase by a factor √2 while the power will increase by the power of the first harmonic which is (1/3)2 of the power of the fundamental:

Similarly, the SNR of the sum of the fundamental plus the two first harmonics is:

And we may extend this to N harmonics:

However, this series is bounded to the total energy of the square wave and it is possible to show through the Riemann Zeta function that 80% of the actual energy of the signal is located in the fundamental and only 20% in the harmonics. Because adding any other frequency bin will decrease the signal to 1/√2≈70%, we see that it is impossible to compensate this decrease by selecting any other harmonics.

We could have also directly written

And so the SNR of the combined fundamental and first harmonics will always be lower than the SNR obtained with only the fundamental under the assumption of white noise. This assumption is generally well met for electronics acquisition process but you should also be aware that different kind of noise may apply in different operational conditions. However, most of the noise source will have good white noise approximation under conventional conditions.

The same conclusion can also be drawn for the upper harmonics because the energy is strictly decreasing as we progress in the harmonics order. This was verified through simulation using the sample signal of Figure 1 and the masking process of Figure 3 with increasing number of harmonics. The results are presented on Figure 4.

Figure 4 – SNR change with increasing number of harmonics

The results of Figure 4 then confirms that the best SNR of a square wave signal for RMS measurement is obtained when using only the fundamental frequency. Including any other harmonics or frequency bins will only lower the SNR of the measurement.

An extension of this conclusion is that Fourier-based RMS measurement is therefore far superior than time-based RMS measurement in such case because the time-based algorithm will integrate all the frequencies of the Fourier spectrum, leading to the worst-case SNR measurement. In fact, we can even generalize by saying (still working with square waves and white noise):

This even generalize further with triangle waves or saw-tooth wave because they meet the same harmonics power conditions than the square wave. It is particularly interesting to notice that the information-to-noise content of such carrier signals is actually located in the signal equivalent sine wave and not in their harmonics. I am still wondering what sense to put into this but it seems to be a fundamental law of information in signals.

Finally, I would like to make some technical comments about the influence of FWHM of the Gaussian mask on the SNR. When integrating the masked spectrum, it is important to select an optimal value for the width of the Gaussian curve such as to maximize the SNR. By expanding a little bit using integrals we get

where ‘d’ is the noise density power, supposed constant over the whole frequency range, f0 the frequency of the fundamental peak and 2ln(2)σ the full-width at half-maximum (FWHM) which configures the spreading of the Gaussian mask as illustrated on Figure 3.

Experimentally, if we plot the SNR as a function of the FWHM we will get a result like Figure 5 which shows a strong increase in the SNR as the mask becomes integrating more information and then decrease in a square root way as we integrate more noise over signal. The curve shows then a maximum which is the optimal FWHM size to collect all the signal with the least noise possible.

Figure 5 – Influence of the Gaussian FWHM on the signal SNR

We see that the optimal FWHM size is about 2.5 Hz in this case. To understand a bit more about this we have to analyse the spectrum of the signal, S(f).

Ideally, the spectrum will only consist of pure peaks located a ‘n’ times the fundamental frequency, with n odd. However, in practice, we will measure not pure peak but convoluted sinc functions due to the fact that we are analysing a finite-time sample. Indeed, when sampling for a time ∆t we become unable to resolve frequency changes finer than about ∆t-1. It is possible to see the sample taken as the product of the square wave signal, s(t), and a rectangular window of size ∆t:

whose Fourier spectrum therefore consists of the convolution of the square wave signal and a sinc function for the rectangular window:

If we are only interested in the fundamental peak, we then get:

The power plot of the sinc function is given on Figure 6 as well as the Gaussian corresponding to the best FWHM measured on Figure 5.

Figure 6 – Plot of the sinc function and a Gaussian of σ≈1.1

It can be seen that the “best” Gaussian FHWM obtained on the experiment of Figure 5 is actually much larger than the sinc function. When using smaller values (in particular σ≈0.11 which gives a pretty good estimate of this sinc function) we do not get good SNR. Also, one would have expected that the smaller σ, the better the SNR using the integral formula because it would reduce the noise density to a minimum. Still, I am wondering if this could be due to the size of frequency bins because, without zero padding, the shape of the sinc curve is pretty rough in the Fourier domain. When I get more time, I will try to investigate this a little bit more because it is a tricky behaviour.

Nevertheless, I would then recommend using a value of σ for the mask close to one for the beginning and eventually tune it a little bit based on experimental data.

[⇈] Top of Page

You may also like:

[»] Generating Sine Wave from Square Waves

[»] Introduction to Heterodyne Systems

[»] Sine Wave Oscillator with Fewer Op-Amp

[»] In-line Absorption and Fluorescence Sensor

[»] Generic 8th Order Sallen-Key Filter