0

I'm new to python and scipy, and i am trying to filter acceleration data taken in 3 dimensions at 25Hz. I'm having a weird problem, after applying the filter the graph of my data is smoothed, however the values seem to be amplified quite a bit depending on the order and cutoff frequencies of the filter. Here is my code:

from scipy import loadtxt 
from scipy import signal
import numpy as np
import matplotlib.pyplot as plt 
my_data = loadtxt("DATA-001.CSV",delimiter=",",skiprows=8)


N, Wn = signal.buttord( [3,11], [.3,18], .1, 10, True)
print N
print Wn
b,a = signal.butter(N, Wn, 'bandpass', analog=True)


filtered_z = signal.filtfilt(a,b,[my_data[1:500,3]],)
filtered_z = np.reshape(filtered_z, (499,))
plt.figure(1)
plt.subplot(411)
plt.plot(my_data[1:500,0],my_data[1:500,3])

plt.subplot(412)
plt.plot(my_data[1:500,0], filtered_z, 'k')
plt.show()

Right now, this code returns this graph:

I'm unsure of how to get rid of this weird gain issue, if anyone has any suggestions? Thank you!

Zacky Balkhy
  • 101
  • 4
  • You have your coefficients the wrong way around in signal.filtfilt. Should be: `filtered_z = signal.filtfilt(b,a,[my_data[1:500,3]],)`. Also, the size of poles and zeros of your filter can lead to amplification. – EngineerCamp May 12 '16 at 01:07
  • @EngineerCamp Your comment should be an answer! – Warren Weckesser May 12 '16 at 01:37
  • @EngineerCamp Thank you for the response! But, when i switch the coefficients all my data turns into "nan" Is there a solution to this? – Zacky Balkhy May 12 '16 at 02:14
  • @ZackyBalkhy Sounds like your filter is unstable so take a look at the coefficients to be sure. Is there a particular reason you want to use a bandpass filter rather than a low pass? – EngineerCamp May 12 '16 at 02:33
  • @EngineerCamp I wanted to be able to filter out high end noise, and low end gravity effects (i read somewhere that this could be done with a filter) but I'll switch it to low pass and then i can algorithmically account for gravity later on. What should my coefficients look like? Thanks for all the help. – Zacky Balkhy May 12 '16 at 02:51

1 Answers1

1

You have your coefficients the wrong way around in signal.filtfilt. Should be:

filtered_z = signal.filtfilt(b,a,[my_data[1:500,3]],)

The size and ratio of the coefficients can result in amplification of the signal.

EngineerCamp
  • 661
  • 1
  • 6
  • 16
  • Thank you for the response! But, when i switch the coefficients all my data turns into "nan" Is there a solution to this? – Zacky Balkhy May 12 '16 at 02:25
  • @ZackyBalkhy can you explain how you came up with your bandpass and stopband frequencies? – EngineerCamp May 12 '16 at 04:15
  • I took frequencies that i had read in literature for accelerometer filtering and tweaked them to try to find the best smoothing. – Zacky Balkhy May 12 '16 at 14:02
  • @ZackyBalkhy filter design is both an art and a science. Take a look at the answer to this post http://stackoverflow.com/questions/12093594/how-to-implement-band-pass-butterworth-filter-with-scipy-signal-butter and try it out with your values. Your current filter design specifies the bandpass to be between 0.5 and 1.75 Hz, I doubt that is really what you want. This is probably also causing your filter to be unstable. See https://www.quora.com/Why-does-a-digital-IIR-band-pass-filter-become-unstable-when-the-cut-off-frequencies-are-specified-well-below-the-sampling-frequency for more details. – EngineerCamp May 13 '16 at 05:29