I am new to signal processing and I am sure this is a very rudimentary question. However I have done various searches and I cannot find an explanation (possibly because my knowledge is insufficient to even ask the question).
I have a light intensity signal, which is sampled at 2hz. I would like to apply 0.1hz lowpass fir filter. Using the gsignal package, I create the filter with the following code:
fs = 2
h <- gsignal::fir1(20, 0.1/(fs / 2), "low")
I am using a value of 20 for the order, because I am trying to emulate a software that employs "Lowpass Fir Filter 20 Order Hamming".
Edit: The coefficients of the filter are as follows:
[1] 0.0001146817 0.0017498427 0.0056848703 0.0138813218
[5] 0.0275824834 0.0467267921 0.0696939138 0.0934923321
[9] 0.1143658981 0.1286667204 0.1337545106 0.1286667204
[13] 0.1143658981 0.0934923321 0.0696939138 0.0467267921
[17] 0.0275824834 0.0138813218 0.0056848703 0.0017498427
[21] 0.0001146817
/edit
Then I employ the filter as follows:
xf <- gsignal::filter(h, x1)
The issue I have is visible in the plot below:
The black line is the original signal and the red line is the "filtered" signal. As can be seen, after applying the filter, all of the values have been increased by what seems to be a constant value. This appears to be affected by the order value. As such, I am not even sure that the filter is doing anything at all.
After googling to try to understand this, I have tried using the filtfilt() function without success. I have also managed to set initial values for my fir filter, so the "drop" at the beginning of the signal does not happen.
I am willing to study and learn more about this to solve my issue. If someone could point my in the right direction as to what I am not understanding, I would be very appreciative.
Thanks.
Edit #2: I believe the issue might be solved now, thanks to a comment below. I will add my working code for anyone with a similar question in the future:
fs = 2
h <- gsignal::fir1(20, 0.1/(fs / 2), "low") # create filter
hn <- h / sum(h) # scale the filter by dividing by the sum of coefficients
myzi <- gsignal::filter_zi(hn) # create the starting point values
# apply filter with initial value
x2 <- gsignal::filter(hn, x1, myzi * x1[1])
# find the filter delay so it can be compensated for
gd <- grpdelay(hn)
npts <- length(x1)
delay <- mean(gd$gd)
x3 <- c(x2$y[(delay +1):npts], rep(NA, delay)) # filtered data with delay compensation applied
# plot the data for visual inpection
plot(t, x1, type = "l", xlab ="", ylab = "")
lines(t, x2$y, col = "blue")
lines(t, x3, col = "red")
This results in the following , where the black line is the original signal, blue line is filtered signal and red line is filtered signal with delay compensation: