// genesis
// the population of PFs is divided into 128 'beams'
// originally the plan was to apply rectangular gratings 
// as stimuli, but this has been abandone
// rmaex  March 2017


include defaults.g

include sine.g

int number_PF_cells

function make_rectangulargrating (length, spatial_wavelength, temporal_wavelength)

   int cell_number = 128 // 256 // 128
   float  length   // the distance along the sagittal axis to be spanned
   float   spatial_wavelength 
   float   temporal_wavelength
   float step_size, step
   int i

   echo  {length}    {spatial_wavelength} {temporal_wavelength}

   number_PF_cells = cell_number

   // create temporal sinewave

   echo  creating sine

   create sine /sine
   setfield /sine period {temporal_wavelength} amplitude 0.0 background_level 0
   setfield /sine phase {TWOPI / 4}
   useclock /sine 1
  

   // create the spatial sine wave 

   create diffamp /library/rectangulargrating_bipolar
   setfield /library/rectangulargrating_bipolar    saturation 10e10
   createmap   /library/rectangulargrating_bipolar    /  {cell_number + 1} 1

   step_size = {{TWOPI} * {length} / {cell_number} / {spatial_wavelength}}
   echo step_size = {step_size}

   for (i = 0;  {i <= cell_number}; i = i + 1)
        setfield /rectangulargrating_bipolar[{i}] y  {i * length / cell_number}
// this is the only difference with counterphase grating
   	setfield /rectangulargrating_bipolar[{i}] plus {{ round {{sin {{i} * {step_size}}} + 0.5}} * 2 - 1}   
   end 


   // multiply by temporal sine wave

   for (i = 0;  {i <= cell_number}; i = {i + 1})
        addmsg /sine /rectangulargrating_bipolar[{i}]  GAIN output
   end


   // add 1 to get positive output

   create diffamp  /library/rectangulargrating
   setfield /library/rectangulargrating    saturation 10e10 gain {parallel_fiber_firing_rate}
   createmap   /library/rectangulargrating /   {cell_number + 1} 1

   create  neutral   /plusONE
   setfield   /plusONE    x  1

   for (i = 0;  {i <= cell_number}; i = {i + 1})
        setfield   /rectangulargrating[{i}]   y    {i * length / cell_number}
        addmsg /rectangulargrating_bipolar[{i}] /rectangulargrating[{i}] PLUS output
        addmsg   /plusONE   /rectangulargrating[{i}] PLUS x
   end


end