0
votes

I have been trying to implement an analog Bessel filter with a cutoff frequency 2kHz using scipy.signal, and I am confused about what value of Wn to set, as the documentation states Wn (for analog filters) should be set to angular frequency (12000 rad/s approximately). But if I implement this to my 1 second of dummy data, with half a second pulse sampled at 500 000 Hz, I get a string of 0s and nans. What is it that I am missing?

import numpy as np
import scipy
import matplotlib.pyplot as plt
import scipy.signal

def make_signal(pulse_length, rate = 500000):
    new_x = np.zeros(rate)
    end_signal = 250000+pulse_length
    new_x[250000:end_signal] = 1
    data = new_x
    print (np.shape(data))
    # pad on both sides
    data=np.concatenate((np.zeros(rate),data,np.zeros(rate)))
    return data 

def conv_time(t):
    pulse_length = t * 500000
    pulse_length = int(pulse_length)
    return pulse_length

def make_data(ti): #give time in seconds
    pulse_length=conv_time(ti)
    print (pulse_length)
    data = make_signal(pulse_length)
    return data
time_scale = np.linspace(0,1,500000)
data = make_data(0.5)    


[b,a] = scipy.signal.bessel(4, 12566.37, btype='low', analog=True, output='ba', norm='phase', fs=None)

output_signal = scipy.signal.filtfilt(b, a, data)
plt.plot(data[600000:800000])

plot of data at midpoint

plt.plot(output_signal[600000:800000])

plot of 'filtered' data

When plotting response using freqs, it doesn't seem that bad to me; where am I making a mistake?

plot of freq and amp

1

1 Answers

1
votes

You are passing an analog filter to a function, scipy.signal.filtfilt, that expects a digital (i.e. discrete time) filter. If you are going to use filtfilt or lfilter, the filter must be digital.

To work with continuous time systems, take a look at the functions

(The 2 versions solve the same mathematical problem as the version without 2 but use a different method. In most cases, the version without 2 is fine and is much faster than the 2 version.)

Other related functions and classes are listed in the section Continuous-Time Linear Systems of the SciPy documentation.

For example, here's a script that plots the impulse and step responses of your Bessel filter:

import numpy as np
from scipy.signal import bessel, step, impulse
import matplotlib.pyplot as plt


order = 4
Wn = 2*np.pi * 2000
b, a = bessel(order, Wn, btype='low', analog=True, output='ba', norm='phase')

# Note: the upper limit for t was chosen after some experimentation.
# If you don't give a T argument to impulse or step, it will choose a
# a "pretty good" time span.
t = np.linspace(0, 0.00125, 2500, endpoint=False)
timp, yimp = impulse((b, a), T=t)
tstep, ystep = step((b, a), T=t)


plt.subplot(2, 1, 1)
plt.plot(timp, yimp, label='impulse response')
plt.legend(loc='upper right', framealpha=1, shadow=True)
plt.grid(alpha=0.25)
plt.title('Impulse and step response of the Bessel filter')

plt.subplot(2, 1, 2)
plt.plot(tstep, ystep, label='step response')
plt.legend(loc='lower right', framealpha=1, shadow=True)
plt.grid(alpha=0.25)
plt.xlabel('t')
plt.show()

The script generates this plot:

plot