# Calculating smoothed derivative of a signal by using difference with larger step=convolving with rectangular window

I have a signal sampled at $\Delta t: fi(ti=i\Delta t)$ where i = 0..n-1. I want to find the first derivative of the signal: f'(t).

My first thought was to estimate this by a central difference:

$f'(t_i) =\frac{f(t_{i+1})−f(t_{i−1})}{2\Delta t}$

However the signal may have a lot of high frequency noise that may cause quick fluctuations in f'. I guess the proper thing might be to smooth the signal by convolving with a window function e.g., Hann and then find the derivative from the differences.

A colleague suggested a faster way of finding a smoothed estimate of the derivative: use a central difference over 2n samples, where n >> 1:

$f'(t_i) =\frac{f(t_{i+n})−f(t_{i−n})}{2n\Delta t}$

This would of course be computationally faster than first convolving with a window function but is it a good solution?

If we form the sum:

$S=2\Delta t[f'(t_{i-n+1})+f'(t_{i-n+2})+..+f'(t_{i+n-1})]$

and expand each derivative by the central difference with step $\Delta t$:

$S=f(t_{i-n+2})-f(t_{i-n})+f(t_{i-n+3})-f(t_{i-n+2})+..+f(t_{i+n})-f(t_{i+n-2})$

all terms except two cancels out:

$S=f(t_{i+n})-f(t_{i-n})=2n\Delta tf'(t_i)$

Therefore:

$f'(t_i)=\frac{1}{n}[f'(t_{i-n+1})+f'(t_{i-n+2})+..+f'(t_{i+n-1})]$

So taking the central difference over 2n samples is equivalent with first convolving by a rectangular window of size 2n - 2 and then taking a central difference over +/- 1 sample.

How "bad" is it to smooth with a rectangular window?

If we take the FFT this will cause "ringing", but we do not need to take the FFT.

This is a hard question to handle generally. Smoothing with a rectangular window is used all the time (often called a "moving average"), so that's not necessarily a problem. I'm not sure what ringing you're referring to, perhaps the sidelobes of the rectangular window's frequency response.

Differentiation is inherently a highpass operation; the ideal continuous-time differentiator has a transfer function of:

$$H(s) = s$$

So that its magnitude response is:

$$|H(j\omega)| = \omega$$

The gain of a differentiator therefore increases monotonically with frequency. If your signal contains high-frequency noise, then it could be amplified by applying a differentiator. To combat this, two approaches are apparent:

• Design a more sophisticated differentiator filter that has the desired linear magnitude response over the portion of the band that covers your signal of interest, then sharply attenuates higher frequencies. You could design such a filter using least-squares methods or the frequency-sampling method, for example.

• Use a cascaded approach where you first suppress all of the high-frequency noise that you can using a lowpass filter, then follow with the differentiator. The frequency coverage of the differentiator need not be as tight since the lowpass filter will eliminate the out-of-band noise.

The methods should be roughly equivalent if you're using linear filters; you could think of the first single-filter approach as just a cascade of a differentiator and a lowpass filter. As you noted, the central-difference approach can be modeled in this way. It's hard for anyone to say that it's "bad" without any knowledge of your application. My main thought would be that it is "bad" if the smoothing operation tangibly attenuates your signal of interest, such that the derivative estimate is no longer useful. However, if the parameters of the signal are such that you can smooth the noise without noticeably distorting the signal (i.e. if the signal is well-oversampled), then it could be a win.