0

For a year time series, I extract the high frequency and low frequency of the 10-year cycle. The results obtained by R language and Matlab are different (at the leftmost of the x-axis)

This is Matlab code

x=[0.95,1.31,1.46,1.54,1.51,1.38,1.19,1.09,0.86,1.02,...
    0.82,0.94,0.92,0.90,1.22,0.82,0.79,0.94,0.76,0.79,0.96,...
    0.90,0.97,1.02,0.96,0.86,0.70,0.76,0.86,0.82];
cutyear = 10;
sample_frequency = 1;
nyquist_frequency = 0.5*sample_frequency;
cut = 1/cutyear/nyquist_frequency;
order = 4;
[b_high,a_high] = butter(order,cut,'high');
[b_low,a_low] = butter(order,cut,'low');
H = filtfilt(b_high,a_high,x);
L = filtfilt(b_low,a_low,x);
plot(L)

This is R code

library(signal)
x<-c(0.95,1.31,1.46,1.54,1.51,1.38,1.19,1.09,0.86,1.02,0.82,0.94,0.92,0.90,1.22,0.82,0.79,0.94,0.76,0.79,0.96,0.90,0.97,1.02,0.96,0.86,0.70,0.76,0.86,0.82)
EndEffect <- function(filt,x) {
  signal::filtfilt(filt,c(rev(x),x,rev(x)))[(length(x) + 1):(2 * length(x))]
}
cutyear <- 10
n <- 4
sample_frequency <- 1;
nyquist_frequency <- 0.5*sample_frequency
cut = 1/cutyear/nyquist_frequency

bf_low <- butter(n, cut, "low")
bf_high <- butter(n, cut, "high")
y_low <- EndEffect(bf_low,x)
y_high <- EndEffect(bf_high,x)
plot(y_high,type='l')

I used the function provided by Matt Summersgill (why is this butterworth filter presenting different results in R and Matlab?). It is very helpful to resolve the difference at the rightmost x-axis, but at the leftmost, they are still different.

This is low frequency, the upper is matlab result, the below is R result:

Although there are some differences overall, the differences in most places are acceptable, but the difference on the left is a bit big.

The uppper is an example with x data of length of 30.

The below is an data of length of 307.

This is low frequency, the upper is matlab result, the below is R result: This is low frequency, the upper is matlab reslut, the below is R result.

This is high frequency, the upper is matlab result, the below is R result: This is high frequency, the upper is matlab reslut, the below is R result.

Hao Wu
  • 1
  • 1
  • 2
    We do not have enough info to evaluate. Most likely it's a either a different way each set of code rounds values, or the two systems return a different resolution (number of sample locations). There could even be problems with integer vs. double values; MATLAB is really bad at doing coercion properly. Or it could be exactly what's mentioned in the page you linked. – Carl Witthoft Mar 25 '23 at 16:30
  • 1
    Oh, and a procedural rule: any time you invoke an `R` function from a library, please post the library you are using. I don't know but suspect there is more than one `butter` Butterworh function in different libraries. – Carl Witthoft Mar 25 '23 at 16:34
  • Have you checked intermediate values (i.e. the results of `butter()`) to see that you're getting consistent answers between R and Matlab, i.e. to figure out whether the difference is in the construction of the filter or in its application ... ? – Ben Bolker Mar 25 '23 at 18:13
  • If the differences happen only on the left end of the output, it is a boundary condition issue. The filter is applied left to right, at the start (left side) the system needs to be initialized. Each implementation makes a different choice there. – Cris Luengo Mar 26 '23 at 13:23

0 Answers0