MATLAB: Zeroeing baseline & measuring noise in noisy signal

signal processing

I am analysing voltage signal acquired at 40KHz sampling frequency, my aim is to detect an event at an exact time, I attach example. I have 2 questions regarding signal processing.
1. What is the most appropriate way to zero the signal?
I tried subtracting the mean measured in a window of variable lengths – anything from 40 to 4000 points. But as you can see the signal is quite noisy with lots of spontaneous events, so at times if the window happens to include lots of events the mean will be affected and the zeroing will not work as well.
2. How to best measure the standard deviation of baseline noise?
To establish if anything occurred at my timepoint of interest, I would like to measure baseline noise standard deviation, and set a criterion that if the peak of the event of interest is larger than 4*standard deviation of the noise then the event has occurred. However, again, because the signal is so noisy, I am not sure how to approach measuring baseline noise.
I am new to signal processing, and I want to make sure I am not making any silly mistakes, any input will be much appreciated!

Best Answer

There is noise everywhere - on the low valleys and even on the high peaks. If you want you can just set a threshold and zero out the signal if it falls below some threshold:
signal(signal < someValue) = 0;
or you can try to smooth the whole thing with sgolayfilt(). See attached demo.