why is this butterworth filter presenting different results in R and Matlab?

a 夏天 提交于 2020-12-13 12:25:21

问题


I'm trying to use a 20Hz low pass filter on data in R, but when I use the filtfilt function, the plot is different from the matlab.

I'm using the following code in R:

fc<-20
fs<-100
Wn<-pi*fc/(2*fs)
testar<- butter(5, Wn, type="low")
L2<- signal::filtfilt(testar,Tabela$posicao)
plot(Tabela$tempo, L2, type = "l", col="red")

The matlab code is:

fc=20;
fs=100;
Wn=pi*fc/(2*fs);
[b,a] = butter(5,Wn,'low');
posfilt= filtfilt(b,a,Tabela.posicao);

The plot in matlab is:

The R one:

why the R one is presenting those variation in the begin and in the end of the graph?


回答1:


I have hunch that the difference is in how each version handles end-effect transients.

Your signal has a large DC-offset (~875). If you think of the signal as being zero 0 before and after the recording. The jump at the start of the signal gets processed by the filter and is seen as an artifact or end-effect. These end-effects are what you see in the R version of the filtered signal.

From the R documentation from filtfilt this version is old and likely doesn't minimize the end transients (R 'filtfilt' docs). On the other hand the MATLAB version of filtfilt does; Quoting from the MATLAB documentation:

"filtfilt minimizes start-up and ending transients by matching initial conditions. Do not use 'filtfilt' with differentiator and Hilbert FIR filters, because the operation of these filters depends heavily on their phase response." FILTFILT Documentation



来源:https://stackoverflow.com/questions/53950001/why-is-this-butterworth-filter-presenting-different-results-in-r-and-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!