Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Noise reduction

  • Noise reduction is typically the process of estimating and removing/reducing noise in a time series or spectrum.

    • Measured wind speed: Average wind (over a suitably small time interval) may be more interesting than every little whirl and change.

    • Master thesis on nuclear reactor cracks: 15 second resoultion gives uninteresting variations in the micrometer scale signal.

    • Instruments have a limit to their certified sensitivity: Smoothing sub-sensitivity can make sense.

  • scipy.signal and scipy.ndimage have wide ranges of possibilites.

  • At this stage the general assumption is that noise is uninformative, thus can be removed without harming the signal.

<Figure size 640x480 with 1 Axes>
np.float64(11.428819409529723)

Moving average

  • A window of length n slides along the measured values.

    • Compute the average value of the window

    • Replace the central value.

  • One of the simplest approaches available.

  • Useable on streaming data:

    • No learning, lag equal to window width.

  • Can be tuned:

    • Width of the window.

    • Weighted average, e.g., more weight on the central values.

    • Median instead of mean.

    • Replace the last value instead of the middle value (maybe using different weights).

Simple moving average

An efficient alterantive can be found in the Uniform 1D filter from SciPy.

  • Handles edge effects - which values to where the window doesn’t fit?

  • Can set which value to replace.

<Figure size 640x480 with 1 Axes>
SNR: 24.33 dB

Visualisation of a filter at a given position.

  • The following figure shows a 20 timepoint filter when it reaches the interval 200-219.

<Figure size 640x480 with 1 Axes>

Adding GUI controls

  • ipywidgets is one way of adding controls to a plot.

  • Wrap your plot in a function, give the function as input to interact together with tuples, Booleans, strings, etc. to automatically generate GUI elements.

<Figure size 500x400 with 1 Axes>
Loading...
<function __main__.plot_sma(size)>

Exercise

  1. Modify the interactive code to include choice of edge effect handling.

  2. Modify further to choose between first, middle and last point in the window for origin (replaced value).

Robustifying

  • Instead of a simple average, one can use robust statistics.

  • Median filter - less affected by outliers, but results in a more jagged curve (medians typically change less frequently along a curve). (medfilt and median_filter)

  • Robust mean filter - remove outer 5/10/20% of samples in the window.

np.float64(5.0)
<Figure size 500x400 with 1 Axes>
SNR: 22.18 dB

Gaussian weighting

  • Use a normal distribution to weight the interval.

  • SciPy’s Gaussian 1D filter has several parameters (in addition to edge mode), but most important is:

    • sigma: the standard deviation of the kernel.

  • SciPy’s default is to cut the filter at +/- 4*sigma

<Figure size 800x100 with 2 Axes>
<Figure size 500x400 with 1 Axes>
Loading...
<function __main__.plot_gauss(sigma, show_window, position)>

Savitzky-Golay filters

  • Savitzky and Golay in 1964 made a sliding window smoothing filter using local polynomial fitting.

    • Smoothing parameters:

      • Window length/size/width: typically an odd number from 3 up to length of spectrum.

      • Polynomial order: less than window length, typically 2 or 3.

  • Combined with discrete derivatives it produces smoothed derivative curves.

    • Popular in spectroscopy, enhances certain characteristics of chemical variation.

    • Second derivative popular for its baseline removal effect.

    • Derivative parameter: Degree of derivative, non-negative integer.

  • Edge effects have different defaults from software to software.

<Figure size 500x400 with 1 Axes>

Smoothed signal or residual

  • Some times we may use a smoother to remove a trend.

  • This can be thought of as a high-pass filter (allowing only high frequencies).

<Figure size 500x400 with 1 Axes>

Discussion point

SNR can be estimated from a smoothed signal and its residual.

def SNR(signal, noise):  
    return 10*np.log10(np.sum(signal**2)/np.sum(noise**2))
  • Could we achieve something useful from the previous slide?

  • Will this be a robust estimate?

Derivatives

  • A smoothed derivative shows the trend of the data series rather than the absolute value.

<Figure size 1000x400 with 2 Axes>

Bonus: Whittaker smoother

  • Whittaker in 1923 proposed to replace noisy data by a curve built from a penalized regression.

    • Minimise difference between the curve and data while enforcing smoothing, typically in the form of a second derivative penalty.
      F(y^)=∥y−y^∥+sP(y^)F(\hat{y}) = \|y-\hat{y}\| + sP(\hat{y})

    • ss controls the amount of smoothing.

    • Closely related to Tikhonov Regression and its special case Ridge Regression (L2 penalisation).

    • Python library pybaselines.whittaker.

<Figure size 500x400 with 1 Axes>

Bonus: Baseline estimation

  • Used for baseline estimation/correction, the Whittaker smoother comes in many flavours.

  • Basic version: Asymmetric Least Squares (PHM Eilers 2003) iteratively weights each point along the curve by:

    • 1−p1-p if the curve is below the data,

    • pp if the curve is above the data.

<Figure size 500x400 with 1 Axes>
Loading...
<function __main__.plot_asls(max_iter)>

Bonus: Exponential decay in weights

  • Example of flavours: “Peaked Signal’s Asymmetric Least Squares Algorithm”

  • Documentation:
    “Similar to the asymmetric least squares algorithm, but applies an exponential decay weighting to values greater than the baseline to allow using a higher p value to better fit noisy data.”

  • This version is not symmetric to up and down in the plot as we now in practice have p⋅e−(yi−y^i)/mp\cdot e^{-(y_i-\hat{y}_i)/m} and 1−p1-p, so the code must be adapted to find “topline”. (mm is a parameter controlling the exponential decay.)

<Figure size 500x400 with 1 Axes>

Bonus exercise

  • Fix the pp value at 0.01 and 0.99, respectively, for the upper (U) and lower curve (L).

  • Let max_iter take it’s default value, and assign the power of the smoother (10k10^k) to the slider:
    k∈[0,10]k \in [0,10] with increments of 0.1.

  • Make a side-by-side plot where the left one shows the same as the above (before the exponential smoothing), while the right one shows the data (D) after subtracting the lower curve (D-L) and dividing by the difference between the upper and lower curve (D-L)/(U-L).