Butterworth Filter 巴特沃斯滤波器的python代码应用

I have already got the data from the real robot, but the data is too wavy.
So I choose a filter same as the paper by my supervisor to obtain a smoother curve.
I use python to implement because my learning machine ELM also uses python, so after the data are filtered, they will directly run in ELM.

Define

scipy.signal.butter(N, Wn, btype, analog, output, fs)

Input

  • N
    the order
  • Wn
    the normalization of cutoff frequency
    Wn = 2*cutoff_frequency/sampling_frequency
    note that 0
  • btype='low'
    type of filter
    {‘lowpass’, ‘highpass’, ‘bandpass’, ‘bandstop’}
  • analog=False
    True -> return an analog filter
    False -> return a digital filter
  • output='ba'
    type of output
    {numerator/denominator (‘ba’), pole-zero (‘zpk’), or second-order sections (‘sos’)}

Default is ‘ba’ for backwards compatibility, but ‘sos’ should be used for general-purpose filtering.

  • fs=None
    sampling frequency of the digital system

Return

corresponds to the type of output by input output='ba'

  • b, a
    Numerator (b) and denominator (a) polynomials of the IIR filter.

  • z, p
    Zeros, poles, and system gain of the IIR filter transfer function.

  • sos
    Second-order sections representation of the IIR filter.

scipy.signal.filtfilt(b, a, x, axis, padtype, padlen, method, irlen)
scipy.signal.filtfilt(b, a, x, axis=-1, padtype='odd', padlen=None, method='pad', irlen=None)

Input

b: 滤波器的分子系数向量

a: 滤波器的分母系数向量

x: 要过滤的数据数组。(array型)

axis: 指定要过滤的数据数组x的轴

padtype: 必须是“奇数”、“偶数”、“常数”或“无”。这决定了用于过滤器应用的填充信号的扩展类型。{‘odd’, ‘even’, ‘constant’, None}

padlen:在应用滤波器之前在轴两端延伸X的元素数目。此值必须小于要滤波元素个数- 1。(int型或None)

method:确定处理信号边缘的方法。当method为“pad”时,填充信号;填充类型padtype和padlen决定,irlen被忽略。当method为“gust”时,使用古斯塔夫森方法,而忽略padtype和padlen。{“pad” ,“gust”}

irlen:当method为“gust”时,irlen指定滤波器的脉冲响应的长度。如果irlen是None,则脉冲响应的任何部分都被忽略。对于长信号,指定irlen可以显著改善滤波器的性能。(int型或None)

Return

y: 滤波后的数据数组

Python code

Define the function

from scipy import signal

def butter_lowpass_filtfilt(self, data, order=4, cutoff, fs):
		
		wn = 2*cutoff/fs
		b, a = signal.butter(order, wn, 'lowpass', analog = False)
		output = signal.filtfilt(b, a, data, axis=0)

		return output 

Call the function

###### get input and output data ####### 
	X, T = robot.loadData(file_name_list)
######## filter output data ############
	file_T = robot.butter_lowpass_filtfilt(T.tolist())
# plot
	plot_figure(T,file_T,joint_i)

Figure

Filter and ELM

figure!!!

Zoom in the result of filter

Butterworth Filter 巴特沃斯滤波器的python代码应用_第1张图片

Note

I use Butterworth Filter to filter my noise torque from the robot.
One important parameter is cutoff frequency.
too big -> overfitting
too small -> too smooth

Because the range of each joint is different, after I plot the image and check the accuracy, I set the cutoff of 7 joints as [3, 4, 4, 3, 2, 2, 2]

References

  1. [开发技巧]·Python实现信号滤波(基于scipy)
  2. scipy.signal.butter

你可能感兴趣的:(Python,机器学习)