// $Id: spikeclamp_spiketuft.hoc,v 1.3 2007/03/12 22:08:47 ted Exp $

// reads spike waveform and subtracts baseline
// then sets up an SEClamp attached to tuft origin
// and uses this waveform to drive the SEClamp

objref datfil, spvec, tspvec
strdef datfilname
USEDAP = 1  // use the spike from D. Zecevic's lab
  // 0 means use data from Shen et al.
if (USEDAP) {
  datfilname="dapdata.txt"  // from D. Zecevic's lab
} else {
  datfilname="vpri_somastrongdata.txt"  // from Shen et al.
}

proc getspike() { local count, ii  localobj tmp
  tmp = new File()
  tmp.ropen(datfilname)
  // assumes first number in the file is the number of data pairs
  // subsequent values are (t, v) data pairs
  count = tmp.scanvar()
  spvec = new Vector(count)
  tspvec = new Vector(count)
  for ii=0,count-1 {
    tspvec.x[ii] = tmp.scanvar()
    spvec.x[ii] = tmp.scanvar()
  }
  tmp.close()
}

// determine baseline and subtract from spike vector
// for these data, the baseline runs from t = 0 to 1 ms
// three args:  first is v values, second is time vector, third is time that marks end of baseline
proc zerobaseline() { local mean  localobj baselinevec
//  for ii=0,$o2.indwhere(">=", $3) print $o2.x[ii], $o1.x[ii]
  mean = $o1.mean(0, $o2.indwhere(">=", $3))  // baseline is all elements of $o1 for which t <= $3
//  print mean
  $o1.sub(mean)
}

getspike()
zerobaseline(spvec, tspvec, 1)

/*
// show the baseline-zeroed spike
objref gx
gx = new Graph()
spvec.plot(gx, tspvec)
gx.exec_menu("View = plot")
*/

objref probe

proc makesec() {
  apic[1] probe = new SEClamp(1)  // clamp apic[1](1) to spike waveform
  probe.rs = 1e-3
  probe.dur1 = 1e9
  probe.amp1 = 0
  spvec.play(&probe.amp1, tspvec, 1)  // with interpolation
}

makesec()