\documentclass[a4paper,12pt,english]{article}
\usepackage{fullpage}
\begin{document}
\section{Loading data into \texttt{R}}
The data recorded from 4 electrodes result from a preprocessing briefly described in the main text and are stored as signed integer coded on 4 Bytes. They must be mutiplied by 0.12715626 on channels 1 and 4 and by 0.01271439 on channels 2 and 3 to get voltage in $\mu$V. They are sampled at 500 Hz and 600 second are stored.
We can then read the data from our web repository and assign them to variable \texttt{Data\_raw} of our \texttt{work space}:
<<load-data>>=
reposName <- "http://www.biomedicale.univ-paris5.fr/physcerv/C_Pouzat/Data_folder/"
dN <- paste("SpinalCordMouseEmbryo_CH",1:4,".dat",sep="")
fullN <- paste(reposName,dN,sep="")
nSamples <- 500 * 600
Data_raw <- sapply(fullN, readBin, n=nSamples, what="integer")
Data_raw <- t(t(Data_raw)*c(0.12715626,0.01271439,0.01271439,0.12715626))
@ 
For readers unfamiliar with \texttt{R}, the assignment operator ``<-'' can be replaced by the usual symbol ``=''. \texttt{R} users can always get the documentation of native and user contributed functions with:
<<get-doc-on-readBin, eval=FALSE>>=
?readBin
@ 

The time derivatives of the measurements are simply obtained using a difference equation whose precision is $o(\delta^2)$:
\begin{displaymath}
  f'(x) = \frac{f(x+\delta) - f(x-\delta)}{2 \, \delta}
\end{displaymath}
<<Data_derivative>>=
Data_derivative <- apply(Data_raw,2, function(x) c(0,diff(x,2)/2,0)*500/1000)
@ 
Here the unit of \texttt{Data\_derivative} is  $\mu$V / ms.

\section{LFP detection}

We are going to detect minima on each channel whose amplitudes are below a \emph{user set} multiple of the channel standard deviation. We start by computing this quantity for each of the two versions of the data we might choose to work with, ``raw'' of ``derivative'':
<<SD>>=
SD_raw <- apply(Data_raw, 2, sd)
SD_derivative <- apply(Data_derivative, 2, sd)
@ 
Here \texttt{SD\_raw} and \texttt{SD\_derivative} are \emph{vectors} with as many elements as \texttt{Data\_raw} and \texttt{Data\_derivative} have columns, that is, as many elements as recording channels.

We are going to use a threshold of 4 times the standard deviation on each channel:
<<threshold-on-derivative>>=
factor <- 4
@ 
A inquiring reader could easily make another choice like using a threshold of 3.5:

<<threshold-on-raw, eval=FALSE>>=
factor <- 3.5
@ 

As explained in the main text we \emph{decided} to identify events as minima exceeding (in absolute value) a threshold on 3 channels simultaneously. To this end we define a variable, \texttt{activeElecNumber}, which contains our number of required active channels. The value of this variable can easily be changed:
<<activeElecNumber>>=
activeElecNumber <- 3
@ 
The detection can now proceed:
<<detect-LFPs>>=
Times <- (1:dim(Data_derivative)[1])/500
timeLFP <- Times[apply(t(t(Data_derivative)/SD_derivative) < -factor,1,sum) >= 3]
@ 


We decide moreover to keep only detected events which are more than 100 ms apart. When ``too close'' events are found, the second one is discarded. This elimination is done recursively starting with the second event:
<<keep-far-apart-events>>=
timeLFP2 <- timeLFP
nbLFP <- length(timeLFP)
last <- 1; new <- 2
while (new <= nbLFP) {
 tDiff <- timeLFP[new]-timeLFP2[last]
 if (tDiff >= 0.1) {
   last <- last+1
   timeLFP2[last] <- timeLFP[new]
 }
 new <- new+1
}
timeLFP <- timeLFP2[1:last]
rm(timeLFP2)
@ 

We can now produce our summary Fig.~\ref{fig:detectionOnDerivativeData} with:
<<make-figure,eval=FALSE>>=
Data_derivativeN <- Data_derivative/diff(range(Data_derivative))
Data_derivativeN.min <- min(Data_derivativeN)
Data_derivativeN <- Data_derivativeN - Data_derivativeN.min
Data_derivativeN <- t(t(Data_derivativeN)-c(0,1,2,3))
thresh <- -factor*SD_derivative/diff(range(Data_derivative)) - Data_derivativeN.min - c(0,1,2,3)
lwr <- 0-Data_derivativeN.min - c(0,1,2,3)
upr <- 2/diff(range(Data_derivative)) - Data_derivativeN.min - c(0,1,2,3)
plot(0,0,type="n",xlab="Time (s)",ylab="",xlim=c(0,600),ylim=c(-3,1),axes=FALSE)
sapply(1:4, function(i) lines(Times,Data_derivativeN[,i],lwd=1))
sapply(1:4,function(i) text(550,2-i,paste("Channel",i)))
abline(h=thresh,col="red",lty=2,lwd=3)
axis(1,at=(0:6)*100,lwd=3)
rug(timeLFP,col="red",lwd=5)
segments(-5,lwr,-5,upr,lwd=5)
@ 
\begin{figure*}
  \centering
<<print-figure, echo=FALSE, results=hide>>=
## We want a png output instead of the default pdf of Sweave (the latter give a very large file)
## This is why we build this figure using two successive code chunks
png(file="detectionOnDerivativeDataRnw.png",width=3000,height=3000)
par(cex=5)
<<make-figure>>
dev.off()  
@ 
\includegraphics[width=1.0\textwidth]{detectionOnDerivativeDataRnw}
\caption{\textbf{Detection of LFP activity on the first
      time-derivative of the data.} The same scale applies to all channels (vertical bar: 200 $\mu$V / ms). Channel specific detection threshold shown in dashed red. Detected events are shown as vertical red bar at bottom.}
  \label{fig:detectionOnDerivativeData}
\end{figure*}

\end{document}
