# Is there a difference between filtering a signal before or after differentiating it?

I have a time series and I want to apply:

• a differentiation
• a Butterworth filter

Does the order theoretically (mathematically) make any difference? Does it make any difference in real life when I use numpy?

• Mathematically these two operations commute. Numerically you can get different results. Which order is numerically favourable is hard to predict, but I would probably first filter, then differentiate. – Jazzmaniac Feb 12 '16 at 11:45

So, first of all:

A differentiator is really just a high-pass filter. Digitally (and this is dsp.SE, so I presume this is the case), that typically means a differentiator is really just FIR with taps $[1;-1]$. A butterworth filter being a low-pass IIR, the combination of both does sound like you might want to build a bandpass, or are doing something that doesn't make very much sense; see the comparison of a very relaxed second order butterworth and a differentiator magnitude responses below: now, mathematically, that's identical:

In time domain, you convolve ($*$) the signal $x[n]$ with the filter impulse response:

$$\begin{array}\\ y[n] &= x[n] * h_{diff} * h_{butter}\\ &\text{or, in freq. domain, thanks to the convolution theorem}\\ Y[z] &= X[z] H_{diff} H_{butter}\\ &\text{multiplication is commutative}\\ &= X[z] H_{butter} H_{diff} \\ &\text{but it's also associative}\\ &= X[z](H_{butter}H_{diff})\\ &= X[z] \mathcal Z\{h_{butter}*h_{diff}\} \end{array}$$ which implies you can even without problem combine the differentiator and the low pass filter. To little surprise, you get the discrete derivative of the butterworth filter.

• That's a strange Butterworth filter: its magnitude goes below zero! Also, its magnitude response is not monotonically decreasing as it should. Finally, the red curve is not the product of the other two curves. So something went wrong with that figure ... – Matt L. Feb 12 '16 at 13:40
• @MattL. goood point; I think I forgot to $abs(\cdot)$ the complex response for plotting for some of the lines, and not for others, and the product is really strange. Wish I could fix this now, but I'm not currently equipped to do so; will remove the picture and add it back as soon as I've gotten out the kinks. – Marcus Müller Feb 12 '16 at 14:21
• @MattL. looked at the actual response data before plotting it: yes, took the magnitude response of the HPF, the real part of the LPF response and multiplied the complex responses... Should be better now. – Marcus Müller Feb 12 '16 at 14:27
• Indeed, looks more convincing now ... :) – Matt L. Feb 12 '16 at 14:52
• @Hilmar: I would say that it's not the delay that's the problem with that "differentiator" but the rather crude magnitude approximation at higher frequencies. It's like saying a linear phase low-pass filter is not a good low-pass filter because of its non-zero delay. If it's not a good low-pass filter then that's usually because of its imperfect magnitude response; the delay is often no problem. – Matt L. Feb 12 '16 at 17:18

If you apply a linear differentiation operator, i. e. satisfying Linear system axioms (the standard derivatives are linear, but in some cases, to limit noise influence, for instance in image processing, non-linear derivative schemes are used), and the filter (or kernel) $h$ is linear too, they "should" commute. In other words: $$\frac{\partial}{\partial t} (h \star x) = \left(\frac{\partial}{\partial t} h \right) \star x \,.$$ In practice, doing both operations successively: does the same as performing the two operations simultaneously: yields the same.

As @Jazzmaniac said, an implemetation might yield slight difference, for instance at both ends of the signal, especially with IIR filters (or IIR derivatives). Indeed, they assume what the signal could be on the left and the right. Filtering then differentiating is generally safer. However, when the signal is almost flat on the border, but non-zero, differentiating it might flatten it to zero, providing an effect similar to apodization, resulting in smaller IIR overshoot at the ends.