Why did a digital HPF increase the gain of high-frequency signal? Background: I was trying to remove the DC component of a load cell signal using a high-pass filter. I don't care about that anymore. I'm curious why a digital filter and an analog filter behave differently under the same conditions.

Here (link 1, link 2) is the data I'm working with, and below is my Python code:



import numpy as np
import math as m
from scipy import signal
from scipy import fftpack
from myLib import myFFT
import matplotlib.pyplot as plt

# load data

filename = "raw_data.txt"
fSampling = 1E3
fNyquist = 0.5*fSampling
data = np.loadtxt(filename,skiprows=0)
i1 = 1000
i2 = 5000
t = data[:,0]
x = data[:,3]


# remove DC component

fCutoff = 0.01/fNyquist
bd, ad = signal.butter(4, fCutoff, 'high', analog=False)
xd = signal.filtfilt(bd,ad,x)
ba, aa = signal.butter(4, fCutoff, 'high', analog=True)
xa = signal.filtfilt(ba,aa,x)


# plot results

f, axarr = plt.subplots(3, sharex=True)
axarr[0].plot(t, x)
axarr[0].set_title("raw")
axarr[0].set_ylabel("x [-]")
axarr[1].plot(t, xd)
axarr[1].set_title("digital")
axarr[1].set_ylabel("x [-]")
axarr[2].plot(t, xa)
axarr[2].set_title("analog")
axarr[2].set_xlabel("t [s]")
axarr[2].set_ylabel("x [-]")

plt.show()


Problem The digital filter doubles the values, while the analog filter does not.


Why does the digital filter behave differently? Is something weird about my data, or do digital and analog filters behave differently? I'm new to signals processing (this all originated from me leaving the analog flag with the default "False"), but I thought analog and digital filters were generally different means to the same end.

Edit: I did some research online, and I've learned that I should use a digital filter to filter regularly sampled data. I don't know why yet (digital filters perform better at a known range?), but this is just making even more curious why my digital filter increased the gain of my signal. I analyzed the frequency response of the digital filter, and theoretically I should get a gain of 0:


I want to chalk this up to numerical errors since the digital filter's numerator and denominator are so close, but so are the analog filter's numerator and denominator: 

b digital = [ 0.99991791 -3.99967164  5.99950746 -3.99967164  0.99991791]
a digital = [ 1.         -3.99983581  5.99950745 -3.99950746  0.99983583]
b analog = [ 1.  0.  0.  0.  0.]
a analog = [  1.00000000e+00   5.22625186e-05   1.36568542e-09   2.09050074e-14
   1.60000000e-19]
The following coefficients $a$ and $b$ implement a second order digital DC notch filter : 

b = 0.9900   -1.9800    0.9900

a = 1.0000   -1.9800    0.9801


Whose frequency response is (y-axis in dB):



Given the raw-input signal $x[n]$ as a vector of samples in $x$ in matlab, you would apply the following line to filter it:

y = filter(b,a,x);


Note that the filter is not linear phase, hence you will have some phase distorions especially about DC frequencies... if you need a strictly linear phase DC blocking filter please indicate.

Given the raw data with the following plot:


The resulting filtered signal plot is the following:


It can be seen that initially there is a transient starting from a value of $0.428$ and takes some samples to stabilize to remove the DC. This transient is a direct consequence of long impulse response associated with the IIR filter. 

Shorter impulse responses would provide faster response, but more steady state distortions.

Assuming that a shorter filter would do better, here is another set of DC notch coefficients with shorter impulse response:

b = 0.9100   -1.8200    0.9100
a = 1.0000   -1.8200    0.8281


In which case the output becomes:


Clearly an improvement in dynamic response, but keep in mind that the steadystate response now gives more distortions.

You can even further get a quicker response with the following set:

b = 0.7000   -1.4000    0.7000
a = 1.0000   -1.4000    0.4900


whose result is:


Much faster response, but there could be untolerable distortions on the samples, depending on the application requirements.