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 high frequency, the upper is matlab result, the below is R result: