1
votes

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:

matlab plot

The R one:

R plot

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

1
Can you clarify what Tabela$posicao or Tabela.posicao is? I could not reproduce your problem because its undefined in both MATLAB and RNirvedh Meshram
I user an Excel data called Tabela, and posicao is the column I had to filterMarcos Sírio
Please provide a reproducible example.Cyrus Mohammadian

1 Answers

2
votes

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