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.

enter image description here

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);

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);


  • 1
    $\begingroup$ Sorry I did not get what is your goal, do you want to remove the low frequency components? Have you already tried standard high pass filters? $\endgroup$ – LJSilver Nov 3 '16 at 10:03
  • 2
    $\begingroup$ It is not clear at all what your filter is doing -- for instance, the peak at the right-hand side seems untouched. A filter's action is best described in frequency, so: what is the frequency content of your signal, and what portion of that do you want to remove? $\endgroup$ – MBaz Nov 3 '16 at 13:08
  • 1
    $\begingroup$ This seems extremely inefficient. A simple high pass filter would be better and much, much faster. $\endgroup$ – Hilmar Nov 3 '16 at 14:01
  • 1
    $\begingroup$ @Hilmar Isn't that what OP is asking for? @hamo: What is polynomial_filt? Where did you get it? $\endgroup$ – endolith Nov 3 '16 at 20:12
  • 1
    $\begingroup$ Your answer is y=x-butter(x); or y=x-smooth(x); my friend. Adjust one of these properly for getting what you want. Dont get astonished with another more sofisticated frequency techniques, unless you are very clear with these two i've given to you. The Polinomial Filtering is not what you want. ans as you have realized, the implementation you presented could be VERY resource-eating. Message me if you are ok with that. $\endgroup$ – Brethlosze Nov 4 '16 at 18:16