I have the recorded membrane potential of a neuron, and I want to get a more even baseline by removing certain slow changes from the signal (see figure). The best way to achieve this thus far has been to make a polynomial interpolation of the data, and subtract it from the raw data signal. I divide the data into windows of $20000$ samples, and then apply the filter to each window separately.
However, beside not being a very neat solution, it is by far too slow to be incorporated in the analysis program I am constructing (where the raw data signal is of order of magnitude $10^8$ samples). I have experimented with MATLAB's
designfilt, but so far all the filters I have created with this function distort the small variations with $y$ amplitude around $1$ in the figure, and they have to be kept relatively intact.
I know this is not a question with a single simple answer, but any ideas about how to speed up the execution of my polynomial subtraction filter are most welcome.
I have also included MATLAB code below to generate the displayed figure. If there is a way to upload files I could upload some of the raw data values too.
x = load_x(); % Too large to be uploaded as a text file (even a 20000 snippet will clog the post polynomial_nbr = 18; width_indx = 20000; y = polynomial_filt(x, polynomial_nbr, width_indx); clf t = 1:length(x); plot(t, x, 'b', t, y, 'r') legend('Pre-filtered', 'Post-filtered'); function y = polynomial_filt(x, polynomial_nbr, width_indx) y = nan(size(x)); for i=1:width_indx:length(x) indx = (i-1 + (1:width_indx))'; indx(indx>length(x)) = ; [p, ~, mu] = polyfit(indx, x(indx), polynomial_nbr); y(indx) = x(indx) - polyval(p, indx, , mu); end end