MOOSE - Multiscale Object Oriented Simulation Environment
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SpikeRingBuffer.cpp
Go to the documentation of this file.
1 /**********************************************************************
2 ** This program is part of 'MOOSE', the
3 ** Messaging Object Oriented Simulation Environment.
4 ** Copyright (C) 2003-2013 Upinder S. Bhalla. and NCBS
5 ** It is made available under the terms of the
6 ** GNU Lesser General Public License version 2.1
7 ** See the file COPYING.LIB for the full notice.
8 **********************************************************************/
9 
10 #include <math.h>
11 #include <vector>
12 #include <cassert>
13 #include <iostream>
14 #include <stdexcept>
15 using namespace std;
16 #include "SpikeRingBuffer.h"
17 
18 const unsigned int SpikeRingBuffer::MAXBIN = 128;
19 
21  : dt_( 1e-4 ),
22  currTime_( 0 ),
23  currentBin_( 0 ),
24  weightSum_( 20, 0.0 )
25 {;}
26 
27 void SpikeRingBuffer::reinit( double dt, double bufferTime )
28 {
29  dt_ = dt;
30  currTime_ = 0.0;
31  currentBin_ = 0;
32  unsigned int newsize = bufferTime / dt;
33  if ( newsize > MAXBIN ) {
34  cout << "Warning: SpikeRingBuffer::reinit: buffer size too big: " <<
35  newsize << " = " << bufferTime << " / " <<
36  dt << ", using " << MAXBIN << endl;
37  newsize = MAXBIN;
38  }
39  if ( newsize == 0 ) {
40  cout << "Warning: SpikeRingBuffer::reinit: buffer size too small: " <<
41  newsize << " = " << bufferTime << " / " <<
42  dt << ", using " << 10 << endl;
43  newsize = 10;
44  }
45  weightSum_.clear();
46  weightSum_.resize( newsize, 0.0 );
47 }
48 
49 void SpikeRingBuffer::addSpike( double t, double w )
50 {
51  unsigned int bin = round( ( t - currTime_ ) / dt_ );
52 
53  if ( bin > weightSum_.size() ) {
54  // Should do catch-throw here
55  if ( t < currTime_ ) {
56  cout << "Warning: SpikeRingBuffer: handling spike too late: " <<
57  t << " < " << currTime_ << ", using currTime\n";
58  bin = 0;
59  } else if ( bin >= MAXBIN ) {
60  cout << "Warning: SpikeRingBuffer: bin number exceeds limit: "<<
61  "spikeTime = " << t << ", currtime= " << currTime_ <<
62  ", dt = " << dt_ << ", bin = " << bin << " >= " << MAXBIN << ", terminating\n";
63  assert( 0 );
64  } else {
65  weightSum_.resize( bin + 1 );
66  }
67  }
68 
69  // Replace the % with a bitwise operation.
70  weightSum_[ ( bin + currentBin_ ) % weightSum_.size() ] += w;
71 }
72 
73 double SpikeRingBuffer::pop( double currTime )
74 {
75  currTime_ = currTime;
76  if ( currentBin_ == weightSum_.size() )
77  currentBin_ = 0;
78  double ret = weightSum_[ currentBin_ ];
79  weightSum_[ currentBin_++ ] = 0.0;
80  return ret;
81 }
unsigned int currentBin_
double pop(double currTime)
Advances the buffer one step, returns the current weight.
vector< double > weightSum_
void addSpike(double timestamp, double weight)
Adds spike into the buffer.
static const unsigned int MAXBIN
void reinit(double dt, double bufferTime)
Sets up buffer parameters.