Setting the baseline of a smoothed waveform to zero

Discussions and questions about topics not covered above
ariberk
Posts: 5
Joined: 04 Aug 2021, 19:31
Contact:

Setting the baseline of a smoothed waveform to zero

Unread postby ariberk » 05 Aug 2021, 22:09

I am a new user of Spike2, so I apologize if this is a naive question. We record bursts of nerve action potentials and need to quantify the sizes of these bursts. In the past, using other software, we would routinely rectify, smooth, and then measure the area under the curve of each burst. If the baseline of the rectified and smoothed signal was not zero, we could simply subtract a voltage amount that would set this baseline to zero before measuring the area under the curve. If we did not do this, it would artificially add that DC offset to each burst measurement.

In Spike2, I have tried doing the same sequence of operations, but smoothing creates a positive DC offset and DC remove does not then move the baseline to zero; instead, it apparently makes the average of the signal zero (over a specified period of time), so that the curves of the rectified and smoothed bursts go above and below zero, rather than bottoming out at zero. If I then computed Area, it would not include the below-zero portions of each burst, as I understand it. Is there another function to use in Spike2 to make the baseline zero after smoothing, instead of DC remove? If I can't use such a function, I am concerned that the Area function will then calculate the entire area above zero for each burst, even though some of that is an artifactual elevation of the baseline.

User avatar
Greg Smith
Major contributor
Posts: 1634
Joined: 19 Jun 2008, 12:27
Software used: Spike2 and Signal
1401 type: Many 1401 types
Location: Cambridge, England
Contact:

Re: Setting the baseline of a smoothed waveform to zero

Unread postby Greg Smith » 10 Aug 2021, 16:11

DC Remove is a high-pass filter, so does not do what you want.

Is it possible that the Curve Area measurement does what you need? See Cursor menu->Vertical Cursors->Cursor Regions in the online Help for an explanation.

If this is no use to you and you need to subtract a defined mean level from your result view data before you perform your measurement, you can do this with a short script.

In the result view average set cursor 1 and 2 to mark the region to average to form a baseline. The following script will subtract the mean level from the result view:

Code: Select all

var chan% := 1;     'The channel number in the result view
var rh% := View();  'Get the current view handle
if ViewKind(rh%)<>4 then Message("Make the result view the current view first"); halt endif;
if (Cursor(1) < 0) or (Cursor(2) < 0) then Message("Position cursors 1 and 2 around the baseline area"); halt endif;
if Cursor(2) <= Cursor(1) then Message("Position cursor 1 to the left of cursor 2"); halt endif;
var mean := ChanMeasure(chan%, 2, BinToX(cursor(1)), BinToX(Cursor(2)));
ArrSub(View(rh%,chan%).[], mean); 'Remove the mean level
Optimise(chan%);     'Set y axis range to show resulting data


Save this as RemoveMean.s2s. You can associate this with a button by using the Script menu Script Bar list command to Add this script, then display the Script bar. Each time you click the button to run the script, it will remove the DC level for you.

What the script does
The script checks that the current view (window) is a result view, then checks that cursors 1 and 2 are visible and in the right order. The chan% variable is the number of the channel to analyse in the result view.
The ChanMeasure(...) command works out the mean level and the ArrSub() command subtracts the mean level from the internal array that holds the channel data, which is accessed with the View(rh%, chan%).[] syntax.

If you are in doubt as to what something does in the script, click on the word in the script and press the F1 key to bring up the online Help.

You could extend the script to measure the area between two other cursors and print this to the Log view (or a file), should you need to do this.
Greg Smith Cambridge Electronic Design

ariberk
Posts: 5
Joined: 04 Aug 2021, 19:31
Contact:

Re: Setting the baseline of a smoothed waveform to zero

Unread postby ariberk » 10 Aug 2021, 23:48

Thanks, Greg!!

That looks like just the thing. I will try it. I wasn't sure the Area function would do the right thing because I thought it might add the positive offset to the total, but this should do what I want.

User avatar
Tim Bergel
Site Admin
Posts: 2381
Joined: 19 Jun 2008, 14:40
Location: Cambridge, England
Contact:

Re: Setting the baseline of a smoothed waveform to zero

Unread postby Tim Bergel » 12 Aug 2021, 14:23

I fear that Greg's solution will not work for you - you don't say but his solution only works on result views whereas your data is probably a sampled data file.

The DC remove option should work better than you say - if you use DC remove first, then rectify and then smooth you should get what you want - the DC remove will make the average value zero, the rectify then forces everything positive and the smooth smooths. I would suggest you try this first - it certainly looks like it does the correct thing here.

Beyond that you can use a virtual channel with an expression like "ch(1)-0.12" to generate a waveform channel with any arbitrary offset added or subtracted. This can then be rectified and smoothed as before. The advantage of this is that you are simply offsetting the data rather than the filtering done by DC remove (though as long as you keep the time constant high, say 1 second, it should be fine), the disadvantage is that you have to determine the offset value yourself, and that if there is DC drift during the recording then the offset will only be correct at one point and elsewhere you will still have the DC level. The only way to deal with drift, if present, is to use the DCRemove processing.
Tim Bergel Cambridge Electronic Design

ariberk
Posts: 5
Joined: 04 Aug 2021, 19:31
Contact:

Re: Setting the baseline of a smoothed waveform to zero

Unread postby ariberk » 16 Aug 2021, 18:25

The problem is that the positive offset apparently occurs as a result of rectifying, then smoothing, even if there is no DC offset in the original data. If I understand correctly, this is because the rectifying makes all values positive and then the smoothing is a kind of averaging that will inevitably create a positive offset of the baseline from these all-positive inputs. So the question I have then is whether this positive offset is included within the measurement performed by Area. But I suppose I can do what you suggest, except create the virtual channel AFTER the rectify and smooth operations.That looks like it is doing the right thing. Then I can use Area on the virtual channel.

User avatar
Tim Bergel
Site Admin
Posts: 2381
Joined: 19 Jun 2008, 14:40
Location: Cambridge, England
Contact:

Re: Setting the baseline of a smoothed waveform to zero

Unread postby Tim Bergel » 17 Aug 2021, 13:10

Yes indeed, rectification will cause a positive offset in the resulting signal, as it must.

Your method using a virtual channel to remove this 'background' offset will work. A simpler solution is to use the Curve area measurement rather than the simple Area. Area measures the area above the zero level while Curve area measures the area above a line joining the start and end points. This method also has the advantage that you do not have to decide what the background offset to be subtracted is, and that it will still work if the background offset changes during the recording.

ariberk
Posts: 5
Joined: 04 Aug 2021, 19:31
Contact:

Re: Setting the baseline of a smoothed waveform to zero

Unread postby ariberk » 17 Aug 2021, 14:27

That sounds good. I gather that Curve area makes use of a horizontal cursor to define the baseline. I will try that.

User avatar
Tim Bergel
Site Admin
Posts: 2381
Joined: 19 Jun 2008, 14:40
Location: Cambridge, England
Contact:

Re: Setting the baseline of a smoothed waveform to zero

Unread postby Tim Bergel » 20 Aug 2021, 18:12

I gather that Curve area makes use of a horizontal cursor to define the baseline

I don't think that is correct. The baseline used is a (normally) sloping line drawn between the waveform level at the start of the measurement and the waveform level at the end - see the diagrams in the help for Cursor region measurements. I don't know any simple way to measure the area above a horizontal cursor though of course it can be done with a script.
Tim Bergel Cambridge Electronic Design

ariberk
Posts: 5
Joined: 04 Aug 2021, 19:31
Contact:

Re: Setting the baseline of a smoothed waveform to zero

Unread postby ariberk » 20 Aug 2021, 21:01

But this should work as long as the two vertical cursors I am measuring between intersect the (same) baseline, so that the (otherwise possibly diagonal) line ends up being horizontal, right? I did try this with my data and compare the Curve area measurement to the area measurement I got by subtracting an appropriate offset from the signal and then measuring Area, and the two numbers were very similar (same to 3 significant digits).

User avatar
Tim Bergel
Site Admin
Posts: 2381
Joined: 19 Jun 2008, 14:40
Location: Cambridge, England
Contact:

Re: Setting the baseline of a smoothed waveform to zero

Unread postby Tim Bergel » 01 Oct 2021, 07:20

Yes, as long as the start and end levels are the same the two measurement methods will match as you see.
Tim Bergel Cambridge Electronic Design


Return to “General”

Who is online

Users browsing this forum: No registered users and 1 guest

cron