Function Reference
BrainWave.FMP
BrainWave.FMPIterativeWeighted
BrainWave.RMS
BrainWave._centered
BrainWave.averageAverages
BrainWave.averageEpochs
BrainWave.averageEpochsIterativeWeighted
BrainWave.baselineCorrect!
BrainWave.deleteSlice2D
BrainWave.deleteSlice3D
BrainWave.detrendEEG!
BrainWave.epochVariance
BrainWave.fftconvolve
BrainWave.filterContinuous!
BrainWave.findABRPeaks
BrainWave.findArtefactThresh
BrainWave.findExtremum
BrainWave.getACF
BrainWave.getAutocorrelogram
BrainWave.getPhaseSpectrum
BrainWave.getSNR
BrainWave.getSNR2
BrainWave.getSpectrogram
BrainWave.getSpectrum
BrainWave.input
BrainWave.iterativeWeightedAverage
BrainWave.meanERPAmplitude
BrainWave.mergeEventTableCodes!
BrainWave.nextPowTwo
BrainWave.removeEpochs!
BrainWave.removeSpuriousTriggers!
BrainWave.rerefCnt!
BrainWave.segment
BrainWave.simulateRecording
#
BrainWave.FMP
— Method.
FMP(segs)
References
- Elberling, C., & Don, M. (1984). Quality estimation of averaged auditory brainstem responses. Scandinavian Audiology, 13(3), 187–197.
- Ozdamar, O., & Delgado, R. E. (1996). Measurement of signal and noise characteristics in ongoing auditory brainstem response averaging. Annals of Biomedical Engineering, 24(6), 702–715.
#
BrainWave.FMPIterativeWeighted
— Method.
FMPIterativeWeighted(sweeps)
#
BrainWave.RMS
— Method.
RMS(ave, winStart, winStop, preDur, sampRate)
#
BrainWave.averageAverages
— Method.
Perform a weighted average of a list of averages. The weight of each average in the list is determined by the number of segments from which it was obtained.
averageAverages(aveList, nSegments)
Arguments
aveListArray::Array{Dict{String, Array{Real, 2}}}
: The list of averages for each experimental condition.nSegments::Array{Dict{String, Integer}}
: The number of epochs on which each average is based.
Returns
weightedAve::Dict{String,Array{Real,2}}
: The weighted average of the averages in the list.
Examples
epochDur=0.5; preDur=0.15; events=[1,2]; sampRate=256;
rec, evtTab = simulateRecording(dur=120, epochDur=epochDur, preDur=preDur, events=events)
segs, nRaw = segment(rec, evtTab, -preDur, epochDur, sampRate)
ave, nSegs = averageEpochs(segs)
rec2, evtTab2 = simulateRecording(dur=120, epochDur=epochDur, preDur=preDur, events=events)
segs2, nRaw2 = segment(rec2, evtTab2, -preDur, epochDur, sampRate)
ave2, nSegs2 = averageEpochs(segs2)
aveList = [ave, ave2]; nCleanByBlock = [nRaw, nRaw2]
aveAll, nSegsAll = averageAverages(aveList, nCleanByBlock)
#
BrainWave.averageEpochs
— Method.
Average the epochs of a segmented recording.
averageEpochs(rec)
Arguments
rec::Dict{String,Array{T,3}}
: Dictionary containing the segmented recordings for each condition. The segmented recordings consist of 3-dimensional arrays (n_channels x n_samples x n_epochs).
Returns
ave::Dict{String,Array{Real,2}}
: The averaged epochs for each condition.n_segs::Dict{String,Integer}
: The number of epochs averaged for each condition.
Examples
epochDur=0.5; preDur=0.15; events=[1,2]; sampRate=256;
rec, evtTab = simulateRecording(dur=120, epochDur=epochDur, preDur=preDur, events=events)
segs, nRaw = segment(rec, evtTab, -preDur, epochDur, sampRate)
ave, nSegs = averageEpochs(segs)
averageEpochs{T<:Real}(rec::Array{T,3})
#
BrainWave.averageEpochsIterativeWeighted
— Method.
Average the epochs of a segmented recording using iterative weighted averaging algorithm (see References below).
averageEpochsIterativeWeighted(rec)
Arguments
rec::Dict{String,Array{T,3}}
: Dictionary containing the segmented recordings for each condition. The segmented recordings consist of 3-dimensional arrays (n_channels x n_samples x n_epochs).noiseWinStart
::Real: Time in seconds at which the noise estimate should start relative to the start of the epoch.noiseWinStop
::Real: Time in seconds at which the noise estimate should stop relative to the start of the epoch.preDur::Real
: Duration of recording before the experimental event, in seconds.sampRate::Real
: The samplig rate of the EEG recording.noiseEstimate::String
: ifglobal
the estimate of the noise used to weight individual
segments is derived from all the channels. If byChannel
an noise estimate is derived for each channel and segments are weighted differently for each channel depending on the channel noise estimate.
Returns
ave::Dict{String,Array{Real,2}}
: The averaged epochs for each condition.
Examples
epochDur=0.5; preDur=0.15; events=[1,2]; sampRate=256;
rec, evtTab = simulateRecording(dur=120, epochDur=epochDur, preDur=preDur, events=events)
segs, nRaw = segment(rec, evtTab, -preDur, epochDur, sampRate)
ave = averageEpochsIterativeWeighted(segs)
References
- Riedel, H., Granzow, M., & Kollmeier, B. (2001). Single-sweep-based methods to improve the quality of auditory brain stem responses Part II: Averaging methods. Z Audiol, 40(2), 62–85.
#
BrainWave.baselineCorrect!
— Method.
Perform baseline correction by subtracting the average pre-event voltage from each channel of a segmented recording.
baselineCorrect!(rec, baselineStart, preDur, sampRate)
Arguments
rec::Dict{String,Array{T,3}}
: The segmented recording.baselineStart::Real
: Start time of the baseline window relative to the event onset, in seconds. The absolute value ofbaselineStart
cannot be greater thanpreDur
. In practicebaselineStart
allows you to define a baseline window shorter than the time window before the experimental event (preDur
).preDur::Real
: Duration of recording before the experimental event, in seconds.sampRate::Integer
: The samplig rate of the EEG recording.
Examples
epochDur=0.5; preDur=0.15; events=[1,2]; sampRate=256;
rec, evtTab = simulateRecording(dur=120, epochDur=epochDur, preDur=preDur, events=events)
segs, nRaw = segment(rec, evtTab, -preDur, epochDur, sampRate)
#baseline window has the same duration of preDur
baselineCorrect!(segs, -preDur, preDur, sampRate)
#now with a baseline shorter than preDur
baselineCorrect!(segs, -0.15, preDur, sampRate)
#
BrainWave.deleteSlice2D
— Method.
Delete a row or a column from a 2-dimensional array.
deleteSlice2D(x, toRemove, axis)
Arguments
x::AbstractMatrix{T}
: the 2-dimensional array from which rows of columns should be deleted.toRemove::Union{Integer, AbstractVector{Integer}}
: an integer or a list of integers indicating the rows/columns to remove.axis::Integer
: an integer indicating whether rows or columns should be removed. 1 corresponds to rows, 2 corresponds to columns.
Returns
x::AbstractMatrix{T}
: a 2-dimensional array with the selected row or columns removed.
Examples
x = [1 2 3 4;
5 6 7 8;
9 10 11 12;
13 14 15 16
]
#remove first row
isequal(deleteSlice2D(x, 1, 1), x[2:end,:])
#remove rows 1 and 4
isequal(deleteSlice2D(x, [1,4], 1), x[2:3,:])
# remove columns 1 and 4
isequal(deleteSlice2D(x, [1,4], 2), x[:,2:3])
#
BrainWave.deleteSlice3D
— Method.
Delete a slice from a 3-dimensional array.
deleteSlice3D(x, toRemove, axis)
Arguments
x::Array{T, 3}
: the 3-dimensional array from which rows of columns should be deleted.toRemove::Union{Integer, AbstractVector{Integer}}
: an integer or a list of integers indicating the slices to remove.axis::Integer
: an integer indicating the axis along which slices should be removed.
Returns
x::Array{T, 3}
: a 3-dimensional array with the selected row or columns removed.
Examples
x = reshape(collect(1:27), 3,3,3)
deleteSlice3D(x, 2, 1)
deleteSlice3D(x, [2,3], 3)
isequal(deleteSlice3D(x, [2,3], 3), x[:,:, [1]])
#
BrainWave.detrendEEG!
— Method.
Remove the mean value from each channel of an EEG recording.
detrendEEG!(rec)
Arguments
rec::AbstractMatrix{T}
: The EEG recording.
Examples
x = [1 2 3; 4 5 6]
detrendEEG!(x)
#
BrainWave.epochVariance
— Method.
epochVariance(rec, winStart, winStop, preDur, sampRate)
#
BrainWave.fftconvolve
— Method.
Convolve two 1-dimensional arrays using the FFT.
fftconvolve(x, y, mode)
Arguments
x::AbstractVector{T}
: First input.y::AbstractVector{T}
: Second input. Should have the same number of dimensions asx
; if sizes ofx
andy
are not equal thenx
has to be the larger array.-
mode::String
: A string indicating the size of the output:- "full": The output is the full discrete linear convolution of the inputs. (Default)
- "valid": The output consists only of those elements that do not rely on the zero-padding.
- "same": The output is the same size as
x
, centered with respect to the "full" output.
Returns
out::AbstractVector{T}
: An 1-dimensional array containing a subset of the discrete linear convolution ofx
withy
.
Examples
x = rand(1:10, 10)
y = rand(1:10, 10)
fftconvolve(x, y, "same")
#
BrainWave.filterContinuous!
— Method.
Filter a continuous EEG recording.
filterContinuous!(rec, sampRate, filterType, nTaps, cutoffs)
Arguments
rec::AbstractMatrix{Real}
: The nChannelsXnSamples array with the EEG recording.sampRate::Integer
: The EEG recording sampling rate.filterType::String
: The filter type., one of "lowpass", "highpass", or "bandpass".nTaps::Integer
: The number of filter taps.cutoffs::Union{Real, AbstractVector{Real}}
:: The filter cutoffs. If "filterType" is "lowpass" or "highpass" the "cutoffs" array should contain a single value. If "filterType" is bandpass the "cutoffs" array should contain the lower and the upper cutoffs in increasing order.channels::Union{Q, AbstractVector{Q}}
: The channel number, or the list of channels numbers that should be filtered.transitionWidth::Real
: The width of the filter transition region, normalized between 0-1. For a lower cutoff the nominal transition region will go from(1-transitionWidth)*cutoff
tocutoff
. For a higher cutoff the nominal transition region will go from cutoff to(1+transitionWidth)*cutoff
.
Examples
sampRate = 2048; nTaps=512
rec, evtTab = simulateRecording(nChans=4, dur=120, sampRate=sampRate)
filterContinuous!(rec, sampRate, "highpass", nTaps, [30], channels=[1,2,3,4], transitionWidth=0.2)
#using the parallel processing version of the function with a SharedArray
@everywhere using BrainWave
rec, evtTab = simulateRecording(nChans=4, dur=120, sampRate=sampRate);
#to exploit parallel processing you need to call julia with -p `n` parameter, or use addprocs(`n`)
#where n is the number of processes you want to use
#convert recording to SharedArray
sharedRec = convert(SharedArray, rec)
#call the filtering function
filterContinuous!(rec, sampRate, "highpass", nTaps, [30], channels=[1,2,3,4], transitionWidth=0.2)
#convert back to Array type
rec = convert(Array, sharedRec)
#using the parallel processing version of the function with a DistributedArray
@everywhere using BrainWave, DistributedArrays
rec, evtTab = simulateRecording(nChans=4, dur=120, sampRate=sampRate);
#to exploit parallel processing you need to call julia with -p `n` parameter, or use addprocs(`n`)
#where n is the number of processes you want to use
nProcsToUse = min(length(workers()), size(rec, 1))
workersToUse = workers()[1:nProcsToUse]
#convert recording to DistributedArray
dRec = distribute(rec, procs=workersToUse, dist=[nProcsToUse, 1])
#call the filtering function, note that you need to pass the ids of the workers to use
filterContinuous!(dRec, sampRate, "highpass", nTaps, [30], workersToUse, channels=[1,2,3,4], transitionWidth=0.2)
#convert back to Array type
rec[:,:] = convert(Array, dRec)
#
BrainWave.findABRPeaks
— Method.
Attempt to find peaks and troughs of the ABR response for a click of a given level. The algorithm is largely based on Bradley and Wilson (2004).
findABRPeaks(sig, stimLevel, sampRate)
Arguments
sig::Union{AbstractMatrix{T}, AbstractVector{T}}
: the ABR waveform for which the peaks and troughs are sought.stimLevel::Real
: the level of the click used to evoke the ABR response.sampRate::Real
: the sampling rate of the ABR recording.epochStart::Real
: the time, in seconds, at which the epoch starts.- `minInterPeakLatency::Real``: the minimum allowed latency between peaks.
dBRef::String
: whether the stimulus level is specified in dB SPLSPL
, or in dB normal hearing levelnHL
.
Returns
peakPoints::
: array containing the points at which peaks I-V are detectedtroughPoints
peakLatencies
troughLatencies
peakAmps
troughAmps
peakTroughAmps
prms
References
- Bradley, A. P., & Wilson, W. J. (2004). Automated Analysis of the Auditory Brainstem Response. Proceedings of the 2004 Intelligent Sensors, Sensor Networks and Information Processing Conference, 2004., 541–546. http://doi.org/10.1109/ISSNIP.2004.1417519
Examples
#
BrainWave.findArtefactThresh
— Function.
Find epochs with voltage values exceeding a given threshold.
findArtefactThresh(rec, thresh, channels)
findArtefactThresh(rec, thresh)
Arguments
rec::Dict{String, Array{Real, 3}}
: The segmented recording.thresh::Union{Real, AbstractVector{Real}}
: The threshold value(s).chans::AbstractVector{Int}
: The indexes of the channels on which to find artefacts.chanlabels::AbstractVector{String}
: The labels of the channels on which to find artefacts.chanList::AbstractVector{String}
: The names of all the channels.
Returns
segsToReject::Dict{String,Array{Int64,1}}
: dictionary containing the list of segments to reject for each condition.
Notes
If neither channel indexes (chans
) nor channel labels (chanLabels
) for the channels on which to check artefacts are provided, then artefacts will be checked for on all channels. If channel labels (chanLabels
) are given for the channels on which to check for artefacts, then an ordered list containing the names of all available channels (chanList
) must be provided as well.
thresh
should be a list of threshold values, one for each channel to check. If thresh
contains only one value, it is assumed that this is the desired threshold for all of the channels to check. If thresh
contains more than one value, its length must match the number of channels to check.
Examples
epochDur=0.5; preDur=0.15; events=[1,2]; sampRate=256;
rec, evtTab = simulateRecording(dur=120, epochDur=epochDur, preDur=preDur, events=events)
segs, nRaw = segment(rec, evtTab, -preDur, epochDur, sampRate)
# on all channels
badSegs = findArtefactThresh(segs, 65)
# on channels 1 and 2
badSegs = findArtefactThresh(segs, 65, [1,2])
# on channels FP1 and F4 #not run
#findArtefactThresh(segs, 20, ["Fp1", "F4"], chanLabels)
#
BrainWave.findExtremum
— Method.
Find the time point at which a waveform reaches a maximum or a minimum.
findExtremum(wave, searchStart, searchStop, extremumSign, epochStart, sampRate)
Arguments:
wave::Union{AbstractVector{Real}, AbstractMatrix{Real}}
: the waveform for which the extremum should be found.searchStart::Real
: the starting time point, in seconds, of the window in which to search for the extremum.searchStop::Real
: the stopping time point, in seconds, of the window in which to search for the extremum.extremumSign::String
: whether the sought extremum is ofpositive
ornegative
sign.epochStart::Real
: the time, in seconds, at which the epoch starts.samprate::Real
: the sampling rate of the signal.
Returns
extremumPnt::Real
: the sample number at which the extremum occurs.extremumTime::Real
: the time, in seconds, at which the extremum occurs.
Examples
## not run
## P2SearchStart = 0.150
## P2SearchStop = 0.250
## epochStart = -0.150
## sampRate = 8192
## sampPnt, timePnt = findExtremum(wave1, P2SearchStart, P2SearchStop, "positive", epochStart, sampRate)
# contrived example
#using Winston
sampRate = 256
dur = 0.6
epochStart = -0.15
P2SearchStart = 0.150
P2SearchStop = 0.250
nSamp = round(Int, dur*sampRate)
freq = 2
phase = 1/4*(-pi)
tArr = collect(0:nSamp-1)/sampRate + epochStart
wave1 = sin(2*pi*freq*tArr+phase)
wave1[1:round(Int, abs(epochStart)*sampRate)] = 0
sampPnt, timePnt = findExtremum(wave1, P2SearchStart, P2SearchStop, "positive", epochStart, sampRate)
#p = plot(tArr, wave1)
#l1 = LineX(timePnt, color="red")
#add(p, l1)
#display(p)
#
BrainWave.getACF
— Method.
Compute the autocorrelation function of a 1-dimensional signal.
getACF(sig, sampRate, maxLag)
Arguments:
sig::Union{AbstractVector{Real}, AbstractMatrix{Real}}
: the signal for which the autocorrelation should be computed.samprate::Real
: the sampling rate of the signal.maxLag::Real
: the maximum lag (1/f) for which the autocorrelation function should be computed.normalize::Bool
: whether the autocorrelation should be scaled between [-1, 1].window::Function
: The type of window to apply to the signal before computing its ACF (see DSP.jl). Chooserect
if you don't want to apply any window.
Returns
acf::Array{Real,1}
: the autocorrelation function.lags::Array{Real,1}
: the time lags for which the autocorrelation function was computed.
Examples
using DSP
sampRate = 48000
dur = 1
nSamp = round(Int, sampRate*dur)
tArr = collect(0:nSamp-1)/sampRate
freq = 440
sig = sin(2*pi*freq*tArr)
maxLag = 1/200
acf, lags = getACF(sig, sampRate, maxLag, normalize=true, window=hamming)
#
BrainWave.getAutocorrelogram
— Method.
Compute the autocorrelogram of a 1-dimensional array.
getAutocorrelogram(sig, sampRate, winLength, overlap, maxLag)
Arguments
sig::Union{AbstractVector{Real}, AbstractMatrix{Real}}
The signal of which the autocorrelogram should be computed.sampRate::Real
: The sampling rate of the signal.winLength::Real
: The length of the window over which to take the ACF, in seconds.overlap::Real
: The percent of overlap between successive windows (useful for smoothing the autocorrelogram).maxLag::Real
: the maximum lag (1/f) for which the autocorrelation function should be computed.normalize::Bool
: whether the autocorrelation should be scaled between [-1, 1].window::Function
: The type of window to apply to the signal before computing its ACF (see DSP.jl). Chooserect
if you don't want to apply any window.
Returns
acfMatrix::Array{Real,2}
: the autocorrelogram.lags::Array{Real,1}
: the ACF lags.timeArray::Array{Real,1}
: The time axis.
Examples
sig = rand(512)
acg, lags, t = getAutocorrelogram(sig, 256, 0.02, 30, 0.01)
#
BrainWave.getPhaseSpectrum
— Method.
Compute the phase spectrum of a 1-dimensional array.
getPhaseSpectrum(sig, sampRate)
Arguments
sig::Union{AbstractVector{Real}, AbstractMatrix{Real}}
: The signal of which the phase spectrum should be computed.sampRate::Real
: The sampling rate of the signal.window::Function
: The type of window to apply to the signal before computing its FFT (see DSP.jl). Chooserect
if you don't want to apply any window.powerOfTwo::Bool
: Iftrue
sig
will be padded with zeros (if necessary) so that its length is a power of two.
Returns
p::Array{Real,1}
: the phase spectrum of the signal.freqArray::Array{Real,1}
: The FFT frequencies.
Examples
sig = rand(512)
p, f = getPhaseSpectrum(sig, 256)
#
BrainWave.getSNR
— Method.
Compute the signal-to-noise ratio at a given frequency in the power spectrum of a recording.
getSNR(spec, freqArr, sigFreq, nSideComp, nExclude)
Arguments
spec::AbstractVector{Real}
The power spectrum of the recording.freqArr::AbstractVector{Real}
: the FFT frequency array.sigFreq::Real
: the signal frequency of interest.nSideComp::Integer
: the number of components adjacent to the signal used to estimate the noise.nExclude::Integer
: the number of components closest to the signal to exclude from the noise estimate.
Returns
snr::Real
: The signal-to-noise ratio at the target frequency.
Examples
sig = rand(512)
p, f = getSpectrum(sig, 512)
snr = getSNR(p, f, 140, 10, 1)
#
BrainWave.getSNR2
— Method.
Compute the signal-to-noise ratio at a given frequency in the power spectrum of a recording. This function is the same as getSNR
, but it additionaly returns the signal and noise magnitudes separately.
getSNR2(spec, freqArr, sigFreq, nSideComp, nExclude)
Arguments
spec::AbstractVector{Real}
The power spectrum of the recording.freqArr::AbstractVector{Real}
: the FFT frequency array.sigFreq::Real
: the signal frequency of interest.nSideComp::Integer
: the number of components adjacent to the signal used to estimate the noise.nExclude::Integer
: the number of components closest to the signal to exclude from the noise estimate.
Returns
snr::Real
: The signal-to-noise ratio at the target frequency.sigMag::Real
: The signal magnitude.noiseMag::Real
: The noise magnitude.
Examples
sig = rand(512)
p, f = getSpectrum(sig, 512)
snr, sigMag, noiseMag = getSNR2(p, f, 140, 10, 1)
#
BrainWave.getSpectrogram
— Method.
Compute the spectrogram of a 1-dimensional array.
getSpectrogram(sig, sampRate, winLength, overlap)
Arguments
sig::Union{AbstractVector{Real}, AbstractMatrix{Real}}
The signal of which the spectrum should be computed.sampRate::Real
: The sampling rate of the signal.winLength::Real
: The length of the window over which to take the FFTs, in seconds.overlap::Real
: The percent of overlap between successive windows (useful for smoothing the spectrogram).window::Function
: The type of window to apply to the signal before computing its FFT (see DSP.jl). Chooserect
if you don't want to apply any window.powerOfTwo::Bool
: Iftrue
sig
will be padded with zeros (if necessary) so that its length is a power of two.
Returns
powerMatrix::Array{Real, 2}
: the power spectrum for each time window.freqArray::Array{Real, 1}
: The frequency axis.timeArray::Array{Real, 1}
: The time axis.
Notes
If the signal length is not a multiple of the window length it is trucated.
Examples
sig = rand(512)
spec, f, t = getSpectrogram(sig, 256, 0.02, 30)
#
BrainWave.getSpectrum
— Method.
Compute the power spectrum of a 1-dimensional array.
getSpectrum(sig, sampRate)
Arguments
sig::Union{AbstractVector{Real}, AbstractMatrix{Real}}
: The signal of which the spectrum should be computed.sampRate::Real
: The sampling rate of the signal.window::Function
: The type of window to apply to the signal before computing its FFT (see DSP.jl). Chooserect
if you don't want to apply any window.powerOfTwo::Bool
: Iftrue
sig
will be padded with zeros (if necessary) so that its length is a power of two.
Returns
p::Array{Real,1}
: the power spectrum of the signal.freqArray::Array{Real,1}
: The FFT frequencies.
Examples
sig = rand(512)
p, f = getSpectrum(sig, 256)
#
BrainWave.input
— Function.
input(prompt::AbstractString="")
input()
input(prompt)
Read a string from STDIN. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a trailing newline before reading input.
#
BrainWave.iterativeWeightedAverage
— Method.
Compute an iterative weighted average from a matrix of segmented EEG recordings.
iterativeWeightedAverage(sweeps)
Arguments
sweeps
::Array{T,3}`: The matrix containing the segmented recording from which the
average should be computed.
noiseEstimate::String
: ifglobal
the estimate of the noise used to weight individual
segments is derived from all the channels. If byChannel
an noise estimate is derived for each channel and segments are weighted differently for each channel depending on the channel noise estimate.
noiseWinStart
::Real: Time in seconds at which the noise estimate should start relative to the start of the epoch.noiseWinStop
::Real: Time in seconds at which the noise estimate should stop relative to the start of the epoch.preDur::Real
: Duration of recording before the experimental event, in seconds.sampRate::Real
: The samplig rate of the EEG recording.
Returns
weighted_ave
: The weighted average of the segments.
Examples
nChans = 3; nSamp=1000; nSweeps=500 sweeps = rand(nChans, nSamp, nSweeps) iterativeWeightedAverage(sweeps, noiseEstimate="global") iterativeWeightedAverage(sweeps, noiseEstimate="byChannel") iterativeWeightedAverage(sweeps, 0, 0.2, 0.1, 1000, noiseEstimate="global")
References
- Riedel, H., Granzow, M., & Kollmeier, B. (2001). Single-sweep-based methods to improve the quality of auditory brain stem responses Part II: Averaging methods. Z Audiol, 40(2), 62–85.
#
BrainWave.meanERPAmplitude
— Method.
Compute the mean amplitude of an ERP waveform in a time window centered on a given point.
meanERPAmplitude(wave, center, centerType, winLength, sampRate)
Arguments:
wave::Union{AbstractVector{Real}, AbstractMatrix{Real}}
: the waveform for which the mean amplitude should be computed.center::Real
: the center of the window in which the mean amplitude should be computed.center
can be specified either in terms of sample point number, or in terms of time in seconds. If center is specified in terms of time in seconds, the time at which the epoch starts, should be passed as an additional argument to the function.centerType::String
: whether thecenter
is specified in terms of sample point numberpoint
, or interms of time in secondstime
.winLength::String
: the length of the window, in seconds, over which to compute the meam amplitude.samprate::Real
: the sampling rate of the signal.epochStart::Real
: the time, in seconds, at which the epoch starts.
Returns
meanAmp::Real
: the mean amplitude of the waveform in the time window centered at the specified point.
Examples
```julia
## not run
## P2WinLength = 0.050
## centerPoint = 2512
## sampRate = 8192
## meanAmp = meanERPAmplitude(wave, centerPoint, "point", P2WinLength, sampRate)
## epochStart = -0.150
## centerPointTm = 0.156
## meanAmp = meanERPAmplitude(wave, centerPointTm, "time", P2WinLength, sampRate)
# contrived example
#using Winston
sampRate = 256
dur = 0.6
epochStart = -0.15
P2SearchStart = 0.150
P2SearchStop = 0.250
nSamp = round(Int, dur*sampRate)
freq = 2
phase = 1/4*(-pi)
tArr = collect(0:nSamp-1)/sampRate + epochStart
wave1 = sin(2*pi*freq*tArr+phase)
wave1[1:round(Int, abs(epochStart)*sampRate)] = 0
sampPnt, timePnt = findExtremum(wave1, P2SearchStart, P2SearchStop, "positive", epochStart, sampRate)
#p = plot(tArr, wave1)
#l1 = LineX(timePnt, color="red")
#add(p, l1)
#display(p)
meanAmp = meanERPAmplitude(wave1, sampPnt, "point", 0.05, sampRate)
```
#
BrainWave.mergeEventTableCodes!
— Method.
Substitute the event table triggers listed in trigList
with newTrig
mergeEventTableCodes!(eventTable, trigList, newTrig)
Arguments
eventTable::Dict{String,Any}
: The event table.trigList::AbstractVector{Integer}
: The list of triggers to substitute.newTrig::Integer
: The new trigger used to substitute the triggers intrigList
.
Examples
```julia
rec, evtTab= simulateRecording(events=[1,2,3,4])
mergeEventTableCodes!(evtTab, [1,2], 1)
mergeEventTableCodes!(evtTab, [3,4], 3)
```
#
BrainWave.nextPowTwo
— Method.
Find the exponent of the next power of 2 closest to x
.
nextPowTwo(x)
Arguments
x::Real
Examples
```julia
nextPowTwo(6)
nextPowTwo(511)
isequal(2^(nextPowTwo(6)), 2^3)
```
#
BrainWave.removeEpochs!
— Method.
Remove epochs from a segmented recording.
removeEpochs!(rec, toRemove)
Arguments
rec::Dict{String,Array{Real,3}}
: The segmented recordingtoRemove::Dict{String,Array{P,1}}
: List of epochs to remove for each condition
Examples
epochDur=0.5; preDur=0.15; events=[1,2]; sampRate=256;
rec, evtTab = simulateRecording(dur=120, epochDur=epochDur, preDur=preDur, events=events)
segs, nRaw = segment(rec, evtTab, -preDur, epochDur, sampRate)
segsToReject = Dict{String,Array{Int,1}}()
segsToReject["1"] = [3,5]
segsToReject["2"] = [1,2]
#toRemove = @compat Dict("1" => [3,5], "2" => [2])
removeEpochs!(segs, segsToReject)
#
BrainWave.removeSpuriousTriggers!
— Method.
Attempt to remove spurious triggers from an event table.
removeSpuriousTriggers!(eventTable, sentTrigs, minTrigDur, minTrig, maxTrig)
Arguments
-
eventTable
: The event table. A dictionary with three fields- code: trigger codes
- idx: trigger indexes
- dur: trigger durations
sentTrigs
: The triggers that were actually sent.minTrigDur
: The minimum duration of legitimate triggers.minTrig
: The minimum trigger code.maxTrig
: The maximum trigger code.
Returns
-
res_info
: A dictionary with the following fields:lenFound
: The number of event table triggers after removal of spurious triggers.lenSent
: The number of triggers actually sent.match
: Whether the event table triggers match the sent triggers after removal of spurious triggers.
Notes
Spurious triggers may be cause by hardware mulfunction. They may also occurr if the triggers are not sent digitally (e.g. through the parallel port), but are sent through an analogous device (e.g. through the soundcard to synchronize them directly with sound onset). Spurious triggers may have completely different codes (e.g. 177, when the triggers that were actually sent could only take the values of 100 and 120), in which case they are easy to find and remove. However, they may also have the same code as legitimate triggers. In this case they are more difficult to find. This function can find them if they have a shorter duration than legitimate triggers.
Examples
using BrainWave, Compat
sentTrigs = [1,1,1,2,2,1,2,2,1,1] #triggers that were actually sent
evtTab = Dict{String,Any}() #fictitious event table
evtTab["code"] = [1,1,5,1,7,2,5,2,1,2,8,2,1,1,7] #with spurious triggers
evtTab["idx"] = collect(1:500:500*15)
evtTab["dur"] = [0.001 for i=1:15]
res_info = removeSpuriousTriggers!(evtTab, sentTrigs, 0.0004, 192, 254)
println(res_info)
assert(res_info["match"] == true)
#sometimes spurious triggers have the same code as legitimate ones
#but they can still be found if they differ from legitimate
#triggers in duration
sentTrigs = [1,2,3,4,5,6,7,8,9,10] #triggers that were actually sent
evtTab = Dict{String,Any}() #fictitious event table
evtTab["code"] = [1,1,1,2,3,4,4,5,6,7,7,7,8,9,10] #with spurious triggers
evtTab["idx"] = collect(1:500:500*15)
evtTab["dur"] = [0.001 for i=1:15]
evtTab["dur"][[2,3,7,11,12]] = 0.0001 #spurious triggers have shorter duration
res_info = removeSpuriousTriggers!(evtTab, sentTrigs, 0.0004, 192, 254)
println(res_info)
assert(res_info["match"] == true)
#
BrainWave.rerefCnt!
— Method.
Rereference channels in a continuous recording.
rerefCnt!(rec, refChan)
Arguments
rec::AbstractMatrix{Real}
: EEG recording.refChan::Integer
: The reference channel number.channels::Union{P, AbstractVector{P}}
: The channel(s) to be rereferenced.
Examples
rec, evtTab = simulateRecording(nChans=4)
rerefCnt!(rec, 4, channels=[1, 2, 3])
#
BrainWave.segment
— Method.
Segment a continuous EEG recording into discrete event-related epochs.
segment(rec, eventTable, epochStart, epochEnd, sampRate)
Arguments
rec::AbstractMatrix{T<:Real}
: The nChannelsXnSamples array with the EEG data.-
eventTable
: dictionary with the following keyscode::AbstractVector{T<:Integer}
: The list of triggers in the EEG recording.idx::AbstractVector{T<:Integer}
: The indexes oftrigs
in the EEG recording.- epochStart::FloatingPoint`: The time at which the epoch starts relative to the trigger code, in seconds.
- epochEnd::FloatingPoint`: The time at which the epoch ends relative to the trigger code, in seconds.
- sampRate::Integer`: The sampling rate of the EEG recording.
- eventList::AbstractVector{T<:Integer}`: The list of events for which epochs should be extracted. If no list is given epochs will be extracted for all the trigger codes present in the event table.
Returns
segs::Dict{String,Array{T,3}}
: The segmented recording. The dictionary has a key for each condition. The corresponding key value is a 3D array with dimensions nChannels x nSamples x nSegmentsnSegs::Dict{String,Int64}
: The number of segments for each condition.
Examples
epochDur=0.5; preDur=0.2; events=[1,2]; sampRate=256;
rec, evtTab = simulateRecording(dur=120, epochDur=epochDur, preDur=preDur, events=events, sampRate=sampRate)
segs, nSegs = segment(rec, evtTab, -preDur, epochDur, sampRate, eventsList=[1, 2], eventsLabelsList=["cnd1", "cnd2"])
#
BrainWave.simulateRecording
— Method.
Generate a simulated EEG recordings. Not physiologically plausible. Mainly useful for testing purposes.
simulateRecording(; nChans, dur, sampRate, minVolt, maxVolt, events, epochDur, preDur)
Arguments
nChans
: Number of channels.dur
: duration of the recordings, in seconds.sampRate
: Sampling rate.events
: A list of event codes.epochDur
: Duration of an ERP epoch.preDur
: Duration of the pre-stimulus baseline.minVolt
: Minimum possible voltage value.maxVolt
: Maximum possible voltage value.
Returns
rec
: A nChannelsXnSamples data matrix with values randomly drawn from a uniform distribution between minVolt and maxVolts.evtTab
: The event table with event codes and indexes of the samples at which events start.
Examples
rec, evtTab = simulateRecording()
rec, evtTab = simulateRecording(dur=180, events=[1,2,3])
#
BrainWave._centered
— Method.