# Baseline Calibration Processor, Version 3.0
# (C) 2016 Kevan Hashemi, Open Source Instruments Inc.
#
# [12-FEB-16] This calibration processor uses the amplitude of the signal after
# filtering to a frequency range called the "event band" using the discrete
# fourier transform of the interval. The calibration power is in units of ADC
# counts, which we can convert to volts using the gain of the recording device
# (for example, 0.4 uV/count for A3028 all existing version). Note that we take
# the fourier transform after applying the glitch filter, but we don't set the
# glitch filter with this processor. Be sure to set the glitch threshold to
# something like 200 before performing calibration. Use this processor with "Reset
# Baselines on Playback Start" and "Write Baselines to Metadata on Playback
# Finish" in the Neuroarchiver's Calibration panel, and it will write the minimun
# interval power measure for he archive into the archive's metadata. If you set
# the "Name for All Metadata Reads and Writes" to "BCP3" you will later be able to
# select its calibration from any others you may have written to the metadata.
#
# Because we are going to use the fourier transform, we must make sure we
# configure the Neuroarchiver to calculate the transform even when the
# amplitude versus frequency plot is disabled.
set config(af_calculate) 0
# Define the limits of frequency band in which we will determine the
# upon which we will determine baseline power.
set event_lo 2
set event_hi 80.0
# Maximum signal loss we can tolerate and still consider the interval
# an event.
set max_loss 20.0
# Calculate the standard deviation of the signal, as well as the average,
# maximum, minimum, and mean absolute deviation, just because we can. We
# are going to print out the standard deviation of the signal so we can
# compare it to the standard deviation of the signal filtered by the
# discrete fourier transform.
scan [lwdaq ave_stdev $info(values)] %f%f%f%f%f ave stdev max min mad
# Obtain the event-band power. We take all the components of the discrete
# fourier transform that lie in our event band, add the squares of their
# amplitudes together, divide by two, and take the square root. This gives
# us the amplitude of the signal we would obtain if we took the inverse
# transform of the event-band components alone. If we want to see what
# the event-band filtered signal looks like, we set the first flag after
# the event_hi parameter in the line below to 1. The filtered signal will
# appear in a different color in the value versus time plot. The calculation
# of the inverse transform takes time, so turn off the signal display when
# processing bulk datat: set the flag after event_hi to 0.
set event_pwr [expr sqrt([Neuroarchiver_band_power $event_lo $event_hi 0 0]/2.0)]
# Look for a minimum event power value. If we find one, and it's not
# a redundant interval with zero power, and furthermore reception is
# adequate, we set the baseline power for this channel equal to the
# new minimum.
upvar #0 Neuroarchiver_info(bp_$info(channel_num)) baseline_pwr
if {($event_pwr < $baseline_pwr) && ($event_pwr > 0.0) && ($info(loss) < $max_loss)} {
set baseline_pwr [format %.1f $event_pwr]
}
# Our characteristics consist of the baseline power and the interval
# standard deviation. We have the channel number, the current baseline
# power, the event-band power of this interval, and the standard deviation
# of this interval. If we set the event band range to 0.001 (a value just
# above zero to eliminate the zero-frequency component) and 1000.0 (or any
# value higher than the half the sample frequency) we expect to get an
# event band power slightly lower than the standard deviation. The event
# band power will not be identical to the standard deviation, because we
# apply a window function to the signal before taking the fourier transform,
# and this window function removes some of the variation in the signal
# by bringing the signal at the start and finish of the interval to the
# same value, which is the average value of the signal before applying the
# window function.
append result "$info(channel_num)\
$baseline_pwr\
[format %.1f $event_pwr]\
[format %.1f $stdev] "