巴特沃斯滤波器python_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 0btype='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

a3ac0a8311854146bafb98898b9af904.jpg

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

[开发技巧]·Python实现信号滤波(基于scipy)

scipy.signal.butter

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值