# Implementing a finite impulse respone (FIR) filter for computing the derivative of a discrete signal

I am trying to reimplement an algorithm on my own. In the description of the implementation, it's written that they compute the derivate of a series of value using a [-1/2, +1/2] finite impulse response filter for obtaining an array of the original length.

Basically, a FIR filter should be a convolution but I don't understand this description applied to this case.

I've tried to develop a simple algorithm using an interpolation:

import numpy as np
from scipy.interpolate import interp1d
x = np.arange(len(signal))
signal  = [2,2,2,3,4,5,5,5,3,4,2,2,2]
# classical differentiation
diff = np.diff(signal)

f = interp1d(x, signal,fill_value='extrapolate')
newx = np.arange(-0.5, len(signal)+0.5)
ynew = f(newx)
intdiff = np.diff(ynew)


And to compare it with the classical differentiation:

import matplotlib.pyplot as plt
plt.plot(x,signal,marker='x',color='g',label='original signal')
plt.plot(x[:-1]+0.5,diff,marker='x',color='r',label='differenciated')
plt.plot(x,intdiff,marker='x',label='interpolation differenciated')
plt.legend()


I am wondering how to implement a derivate of a series of value using a [-1/2, +1/2] FIR filter? And what are the advantages compared to a derivative compute using I[n+1] - I[n].

FIR filter with impulse response $$h[n] = {-\frac{1}{2}, \frac{1}{2}}$$ means that : $$y[n] = \frac{1}{2}x[n-1] - \frac{1}{2}x[n]$$

This is in some sense a mirror operation of Moving Average of two samples. This is Moving Difference (samples reversed). A High Pass Filter.

So, each output sample is the difference of current input sample with previous input sample. And, this is the result of convolution operation between $$x[n]$$ and $$h[n]$$.

But I am not sure that this is will correctly give the Derivative, since for a positively increasing discrete ramp, you would expect the discrete derivative to give positive result. Because for a increasing discrete ramp $$x[n]>x[n-1]$$. And that FIR filter will give negative result and in addition to negative slope it will also scale the result by $$\frac{1}{2}$$, which is unwanted.

Correct definition of a Discrete Derivative would be in my understanding, the following: $$\dot{x} = x[n] - x[n-1]$$

And this can be implemented using FIR filter with impulse response $$h[n] = 1,-1$$

• So this would be like using: conv = np.convolve(signal,[0.5,-0.5]). I am also a little bit confused regarding the effectiveness. – G M Jun 24 '20 at 13:01
• @GM Yes, the way to implement FIR filtering would be convolving with $h[n]$. The question about effectiveness : What is it that you want to do? If you want to calculate derivative, then this is the way. – DSP Rookie Jun 24 '20 at 13:27
• Thanks for your comment. The output is used for edge detection of images, the implementers use the FIR but mentioned also the classical method, I was also thinking to use second order accurate central differences. That I think could be more accurate. – G M Jun 24 '20 at 13:46
• @GM If the application is to detect edges in an image, then Moving Difference is fine. Because the edges in an image are perceived due to the fast changing intensities i.e. high frequency components. And, since Moving Difference is a HPF, so, the output images will have suppressed low frequency components and that will expose the edges. So, I guess Moving difference or discrete derivative both will give you good results, Discrete Derivative result will be twice the Moving Difference result. – DSP Rookie Jun 24 '20 at 14:36