// $Id: sense.hoc,v 1.21 2011/11/22 22:55:23 samn Exp $ 

//* proprioceptive inputs
declare("stimnq","o[1]","DPseed",9658,"DPdt",1,"ldp","o[1]","minDPr",0,"maxDPr",1)

rdmthresh_DRSPK = 1

//* mkstimnq([seed]) - make NQS with inputs to DP(proprioceptive) cells
proc mkstimnq () { local ii,se,a localobj xo,rdm,ind,vr
  if(numarg()>0) se=$1 else se=9658
  {nqsdel(stimnq) stimnq=new NQS("eflex","eext","wflex","wext")}
  rdm=new Random() rdm.ACG(se) a=allocvecs(ind,vr)
  ind.indgen(0,PI/2,0.01)
  for ii=0,10 {stimnq.v[0].append(ind) stimnq.v[0].append(ind.c.reverse)}
  stimnq.v[1].copy(stimnq.v[0])
  ind.indgen(PI/4,3*PI/4,0.01) // recording at 1e-3 but play back at 0.3
  for ii=0,10 {stimnq.v[2].append(ind) stimnq.v[2].append(ind.c.reverse)}
  stimnq.v[3].copy(stimnq.v[2])
  for case(&ii,0,2) stimnq.v(ii).apply("sin")
  for case(&ii,1,3) stimnq.v(ii).apply("cos")
  for ii=0,2 stimnq.v(ii).scale(0.1,0.5)
  for ltr(xo,cedp) stimnq.v[xo.zloc].play(&xo.drive, 0.6)
  rdm.poisson(8)
  vr.resize(tstop)
  for ltr(xo,cedp,&ii) {
    vr.setrand(rdm)
    vr.scale(minDPr,maxDPr)
    vr.play(&xo.rand, 0.6)
  }
  dealloc(a)
}

//* setDPrand - set random thresholds for DP cells
proc setDPrand () { local ii,se,a localobj xo,rdm,vr,vec
  if(numarg()>0) se=$1 else se=9658
  if(rdmthresh_DRSPK) {
    ldp=new List() rdm=new Random() rdm.ACG(se) rdm.poisson(8)
  } else {rdm=new Random() rdm.ACG(se)}
  for ltr(xo,cedp,&ii) {
   if(rdmthresh_DRSPK) {
     rdm.normal(25,10)
     ldp.append(vr=new Vector(tstop / DPdt + 1))
     vr.setrand(rdm)
//     vr.scale(minDPr,maxDPr)
     vr.scale(1.5,50)
     vr.play(&xo.drspk.rand,DPdt)
   } else {
     xo.drspk.thresh = xo.drspk.rand = rdm.uniform(minDPr,maxDPr)
   }
  }
}

//* func calls

setDPrand(DPseed)