low-pass filtering a signal within a range determined by signal amplitude

Hi all,
I have this problem (see the picture attached). The baseline is noisy, so I have applied a lowpass filter (1 Hz, y=lowpass(I_P,1,6250). The data looks amazing but the spikes are smaller (of course, because I applied a low-pass filter).
So, in short, I want to apply the same low-pass filter (1Hz) exclusively to the baseline, i.e. from -10 pA to + 50 pA, so that the data baseline is improved signal-wise, and the spike amplitude remains untouchable (keeping the real amplitude).
The signal inside the green box is the data I want to filter. The signal inside the red box is the data I do not want to filter.
Thanks

 Accepted Answer

hello Jose
so this is a little code snipset, it uses a windowing technique as low pass filter
the peaks are left as original
you can leave the peks very sharp (one point) or wider if you leave the following line active :
hope it helps !!
%% signal generation
signal = 10*randn(1,samples); % noise
% add some peaks (must NOT be filtered)
signal(100) = 400;
signal(200) = 300;
signal(300) = 200;
signal = myslidingavg(signal, 5); % just a trick to make the peaks a bit wider (optionnal)
%% main code
% sliding avg method
threshold = max(abs(signal))/4;
ind = find(signal< threshold);
out = myslidingavg(signal(ind), 100);
signal_out = signal;
signal_out(ind) = out;
figure(2),
plot(time,signal,'b',time,signal_out,'r');

7 Comments

Greetings Mathieu,
I got and error running your code:
Unrecognized function or variable 'myslidingavg'.
I have looked up this function (I thought it was uploaded at File Exchange). However, there is nothing regarding this function. Also, this warning did not mention any required toolbox to run this function. I guess it is kind of "hand-made" funtion, right?
May you provide me this function?
Thanks!
hi again
it's attached in my first answer
here again
Oh, yes!
My fault. Sorry. I am going to run the code right now.
So, I have some questions regarding this code:
signal = 10*randn(1,samples); % noise
I got an error with samples because it is not defined. Therefore I set sample to 1000. Is it correct?
plot(time,signal,'b',time,signal_out,'r');
What is time and signal? My data? Because if I run this code as it is, I got the same error as mention above (Neither time nor signal are defined).
Last question: I have organized my data as follows: two vectors, Time and Intensity, same length. How can I apply your code to my data set?
Thanks
sorry
the first two lines disappeared when I coppied the first time :
%% signal generation
samples = 1000;
time = (1:samples);
signal = 10*randn(1,samples); % noise
% add some peaks (must NOT be filtered)
signal(100) = 400;
signal(200) = 300;
signal(300) = 200;
signal = myslidingavg(signal, 5); % just a trick to make the peaks a bit wider (optionnal)
%% main code
% sliding avg method
threshold = max(abs(signal))/4;
ind = find(signal< threshold);
out = myslidingavg(signal(ind), 100);
signal_out = signal;
signal_out(ind) = out;
figure(2),
plot(time,signal,'b',time,signal_out,'r');
Dear Mathieu,
At the beginning, I was sceptic about the utility of your code for my needs.
I have been playing around with your code. Now, I understand that this is what I need.
Please, let me encourage you to submit this code to File Exchange. It would be beneficial for everyone in our situation, and you just submitted here in this thread. Then, why don´t you make it more visible, more accessible?
In short, good job and thanks for sharing this code with me.
Best,
Jose
hi again
glad it finally helped you
to be honest with you and with the person you originally developped the function almost 20 years ago, this was available in the FEX under :
I just improved marginaly the function essentially for my own needs, and more recently to help other people
but I wont take credit for someone else work !
there are also alternatives like fast moving_average.m that seems even more powerful - have to try this one

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!