From 41c0c6bda0511cc8227c198e006c5bcda2be9996 Mon Sep 17 00:00:00 2001
From: Stuart Layton <slayton@mit.edu>
Date: Wed, 14 Mar 2012 16:54:24 -0400
Subject: [PATCH] fixed bug in SpikeObject that was causing a buffer over run
 when spikes were generated

---
 .../Processors/Visualization/SpikeObject.cpp  | 90 +++++++++----------
 Source/Processors/Visualization/SpikeObject.h |  2 +-
 2 files changed, 43 insertions(+), 49 deletions(-)

diff --git a/Source/Processors/Visualization/SpikeObject.cpp b/Source/Processors/Visualization/SpikeObject.cpp
index 7182fb534..7994ecf1e 100644
--- a/Source/Processors/Visualization/SpikeObject.cpp
+++ b/Source/Processors/Visualization/SpikeObject.cpp
@@ -89,9 +89,7 @@ bool unpackSpike(SpikeObject *s, char* buffer, int bufferSize){
 
 	if (idx>=bufferSize)
 		std::cout<<"Buffer Overrun! More data extracted than was given!"<<std::endl;
-
-
-
+	
 }
 
 // Checks the validity of the buffer, this should be run before unpacking and after packing the buffer
@@ -135,60 +133,56 @@ void makeBufferValid(char *buffer, int bufferSize){
 
 }
 
-SpikeObject generateSimulatedSpike(uint64_t timestamp, int noise, bool sineWave)
+void generateSimulatedSpike(SpikeObject *s, uint64_t timestamp, int noise)
 {
-
-	uint16_t realSpikeWave[32] =
+	uint16_t trace[32] =
 	{ 1880,	 	1900,	1940,	2040,	2290,	2790,	3475,	3995, 	4110, 	3890,
       3505,		3090,	2720,	2410, 	2155,  	1945,	1775,	1635,	1520, 	1420,
       1340,		1265,	1205,	1155,	1115,	1080,	1050,	1034,	1010, 	1001,
       1000, 	1000};
 
-    uint16_t sineSpikeWave[32] = 
-    {	78, 	90, 	101, 	111,	120,	126,	129,	130,
-		129,	126,	120,	111,	101,	90,		78,		65,
-		52,		40,		29,		19,		11,		5,		2,		1,
-		2,		5,		11,		19,		29,		40,		52,		65};
-
-    uint16_t *trace;
-
-    uint16_t gain;
-
-
-    if(sineWave){
-    	trace = sineSpikeWave;
-    	gain = 100;
-    }
-    else{
-    	trace = realSpikeWave;
-    	gain = 5;
-    }
-
-
-    SpikeObject s;
-    s.timestamp = timestamp;
-    s.source = 0;
-    s.nChannels = 4;
-    s.nSamples = 32;
-
+  //   uint16_t sineSpikeWave[32] = 
+  //   {	78, 	90, 	101, 	111,	120,	126,	129,	130,
+		// 129,	126,	120,	111,	101,	90,		78,		65,
+		// 52,		40,		29,		19,		11,		5,		2,		1,
+		// 2,		5,		11,		19,		29,		40,		52,		65};
+
+    // uint16_t trace[32] = {0};
+
+    uint16_t gain = 5;
+
+    // if(sineWave){
+    // 	memcpy(trace, sineSpikeWave, 64);
+    // 	gain = 100;
+    // }
+    // else{
+    // 	memcpy(trace, realSpikeWave, 64);
+    // 	gain = 5;
+    // }
+
+    s->timestamp = timestamp;
+    s->source = 0;
+    s->nChannels = 4;
+    s->nSamples = 32;
     int idx=0;
-    for (int i=0; i<s.nSamples; i++)
+    
+
+    for (int i=0; i<4; i++)
     {
-            s.gain[i] = gain;
-            s.threshold[i] = 12000;
-
-			for (int j=0; j<s.nChannels; j++){
-				
-				int n = 0;
-				if (noise>0){
-					n = rand() % noise - noise/2;
-				}
-
-                s.data[idx++] = (trace[i]*gain) + n;
-            }
+        s->gain[i] = gain;
+        s->threshold[i] = 12000;
+
+		for (int j=0; j<32; j++){
+			
+			int n = 0;
+			if (noise>0){
+				n = rand() % noise - noise/2;
+			}
+
+            s->data[idx] = (trace[j] + n) * gain;
+            idx = idx+1;
+        }
     }
-
-    return s;
 }
 
 
diff --git a/Source/Processors/Visualization/SpikeObject.h b/Source/Processors/Visualization/SpikeObject.h
index 3f301d015..dc9bd0f30 100644
--- a/Source/Processors/Visualization/SpikeObject.h
+++ b/Source/Processors/Visualization/SpikeObject.h
@@ -71,7 +71,7 @@ void makeBufferValid(char *buffer, int bufferLength);
 // Help function for generating fake spikes in the absence of a real spike source. 
 // Can be used to generate a sign wave with a fixed Frequency of 1000 hz or a basic spike waveform
 // Additionally noise can be added to the waveform for help in diagnosing projection plots
-SpikeObject generateSimulatedSpike(uint64_t timestamp, int noise, bool sineWave);
+void generateSimulatedSpike(SpikeObject *s, uint64_t timestamp, int noise);
 
 // Define the << operator for the SpikeObject
 // std::ostream& operator<<(std::ostream &strm, const SpikeObject s);
-- 
GitLab