# convert the presented binary test-sequence with winner to receptive field
# $P: presented symbol  $W: winner neuron

BEGIN {
  if(qlen <= 0) qlen = 50
  if(moduloprint <= 0) moduloprint = 1e8
  if(depth <=0) depth = moduloprint
  if(P <= 0) P = 1
  if(W <= 0) W = P+1
}

{
  gsub(/\r/,"")

  queue[NR%qlen] = "" $P

  if(NR < qlen) next

  result = ""

  for(i = qlen; i > 0; i--) {

    if(!((ind = (i+NR) % qlen) in queue)) break

    z = queue[ind]

    if(strings[$W] != "")
      if(z != substr(strings[$W], qlen-i+1, 1)) break

    result = result z

  }

  strings[$W] = result
  ++cnt[$W]

  if(NR % depth == 0) {
    sum = len = 0
    for(ind in strings) {
      sum += cnt[ind] * (l = length(strings[ind]))
      len += l
    }
    print "# " sum/NR, len
  }
}

END {

  sum = len = 0
  for(ind in strings) {
    sum += cnt[ind] * (l = length(strings[ind]))
    len += l
  }
  print "# " sum/NR, len

  for(ind in strings) print ind, strings[ind], cnt[ind]/NR
}
