From f73cdf47f5ed5b9bbaa27804c3236f59516e4cef Mon Sep 17 00:00:00 2001
From: jsiegle <jsiegle@mit.edu>
Date: Sat, 15 Jun 2013 14:19:34 -0400
Subject: [PATCH] Threshold slider can now be used to filter spikes

---
 Source/Processors/RecordNode.h                |  4 +-
 .../Visualization/SpikeDisplayCanvas.cpp      | 92 +++++++++++++++++--
 .../Visualization/SpikeDisplayCanvas.h        | 16 +++-
 Source/Processors/Visualization/Visualizer.h  |  2 -
 4 files changed, 98 insertions(+), 16 deletions(-)

diff --git a/Source/Processors/RecordNode.h b/Source/Processors/RecordNode.h
index baadf10e0..4c2094252 100755
--- a/Source/Processors/RecordNode.h
+++ b/Source/Processors/RecordNode.h
@@ -105,11 +105,13 @@ public:
     /** Signals when to create a new data directory when recording starts.*/
     bool newDirectoryNeeded;
 
+    bool isRecording;
+
 private:
 
     /** Keep the RecordNode informed of acquisition and record states.
     */
-    bool isRecording, isProcessing, signalFilesShouldClose;
+    bool isProcessing, signalFilesShouldClose;
 
     /** User-selectable directory for saving data files. Currently
         defaults to the user's home directory.
diff --git a/Source/Processors/Visualization/SpikeDisplayCanvas.cpp b/Source/Processors/Visualization/SpikeDisplayCanvas.cpp
index 022d0805c..a43ed77ac 100755
--- a/Source/Processors/Visualization/SpikeDisplayCanvas.cpp
+++ b/Source/Processors/Visualization/SpikeDisplayCanvas.cpp
@@ -22,6 +22,7 @@
 */
 
 #include "SpikeDisplayCanvas.h"
+#include "../RecordNode.h"
 
 SpikeDisplayCanvas::SpikeDisplayCanvas(SpikeDisplayNode* n) :
     processor(n), newSpike(false)
@@ -118,6 +119,11 @@ void SpikeDisplayCanvas::refresh()
     repaint();
 }
 
+RecordNode* SpikeDisplayCanvas::getRecordNode()
+{
+    return processor->getProcessorGraph()->getRecordNode();
+}
+
 void SpikeDisplayCanvas::processSpikeEvents()
 {
 
@@ -351,10 +357,12 @@ void SpikeDisplay::plotSpike(const SpikeObject& spike, int electrodeNum)
 
 SpikePlot::SpikePlot(SpikeDisplayCanvas* sdc, int elecNum, int p, String name_) :
      canvas(sdc), isSelected(false), electrodeNumber(elecNum),  plotType(p),
-    limitsChanged(true), name(name_)
+    limitsChanged(true), name(name_), isRecording(false)
 
 {
 
+    recordNode = sdc->getRecordNode();
+
     font = Font("Default", 15, Font::plain);
 
     switch (p)
@@ -432,12 +440,49 @@ void SpikePlot::processSpikeObject(const SpikeObject& s)
     //std::cout<<"ElectrodePlot::processSpikeObject()"<<std::endl;
 
     // first, check if it's above threshold
+    bool aboveThreshold = false;
 
     for (int i = 0; i < nWaveAx; i++)
-        wAxes[i]->updateSpikeData(s);
+    {
+        aboveThreshold = aboveThreshold | wAxes[i]->checkThreshold(s);
+    }
+        
+    if (aboveThreshold)
+    {
+        for (int i = 0; i < nWaveAx; i++)
+            wAxes[i]->updateSpikeData(s);
+
+        for (int i = 0; i < nProjAx; i++)
+            pAxes[i]->updateSpikeData(s);
+    }
+    
+
+    // then record it!
+    if (recordNode->isRecording)
+    {
+        if (!isRecording)
+        {
+            // open files
+            isRecording = true;
+            std::cout << "Start recording spikes." << std::endl;
+       
+        }
+
+        if (aboveThreshold)
+        {
+             // write spike to disk
+        }
+
+    } else {
+        
+        if (isRecording)
+        {
+            // close files
+            isRecording = false;
+            std::cout << "Stop recording spikes." << std::endl;
+        }
+    }
 
-    for (int i = 0; i < nProjAx; i++)
-        pAxes[i]->updateSpikeData(s);
 }
 
 void SpikePlot::select()
@@ -752,7 +797,7 @@ void WaveAxes::drawWaveformGrid(Graphics& g)
    
 }
 
-void WaveAxes::updateSpikeData(const SpikeObject& s)
+bool WaveAxes::updateSpikeData(const SpikeObject& s)
 {
     if (!gotFirstSpike)
     {
@@ -761,12 +806,39 @@ void WaveAxes::updateSpikeData(const SpikeObject& s)
 
     SpikeObject newSpike = s;
 
-    spikeIndex++;
-    spikeIndex %= bufferSize;
+    //if (checkThreshold(newSpike))
+    //{
+        spikeIndex++;
+        spikeIndex %= bufferSize;
+
+        spikeBuffer.set(spikeIndex, newSpike); 
+    //    return true;
+
+   // } else {
+   //     return false;
+   // }
 
-    spikeBuffer.set(spikeIndex, newSpike);
 
     
+}
+
+bool WaveAxes::checkThreshold(const SpikeObject& s)
+{
+    int sampIdx = 40*type;
+
+     for (int i = 0; i < s.nSamples-1; i++)
+    {
+        
+        if (float(s.data[sampIdx]-32768)/float(*s.gain)*1000.0f > thresholdLevel)
+        {
+            return true;
+        }
+
+        sampIdx++;
+    }
+
+    return false;
+
 }
 
 void WaveAxes::clear()
@@ -906,7 +978,7 @@ void ProjectionAxes::paint(Graphics& g)
                 0, imageDim-rangeY, rangeX, rangeY);
 }
 
-void ProjectionAxes::updateSpikeData(const SpikeObject& s)
+bool ProjectionAxes::updateSpikeData(const SpikeObject& s)
 {
     if (!gotFirstSpike)
     {
@@ -1034,7 +1106,7 @@ GenericAxes::~GenericAxes()
 
 }
 
-void GenericAxes::updateSpikeData(const SpikeObject& newSpike)
+bool GenericAxes::updateSpikeData(const SpikeObject& newSpike)
 {
     if (!gotFirstSpike)
     {
diff --git a/Source/Processors/Visualization/SpikeDisplayCanvas.h b/Source/Processors/Visualization/SpikeDisplayCanvas.h
index 4facac441..63061ca1e 100755
--- a/Source/Processors/Visualization/SpikeDisplayCanvas.h
+++ b/Source/Processors/Visualization/SpikeDisplayCanvas.h
@@ -57,6 +57,7 @@ class GenericAxes;
 class ProjectionAxes;
 class WaveAxes;
 class SpikePlot;
+class RecordNode;
 
 /**
 
@@ -95,6 +96,8 @@ public:
 
     void buttonClicked(Button* button);
 
+    RecordNode* getRecordNode();
+
 private:
 
     SpikeDisplayNode* processor;
@@ -199,6 +202,8 @@ public:
 
 private:
 
+    bool isRecording;
+
 
     int plotType;
     int nWaveAx;
@@ -221,6 +226,8 @@ private:
 
     Font font;
 
+    RecordNode* recordNode;
+
 };
 
 /**
@@ -239,7 +246,7 @@ public:
 
     virtual ~GenericAxes();
 
-    virtual void updateSpikeData(const SpikeObject& s);
+    virtual bool updateSpikeData(const SpikeObject& s);
 
     void setXLims(double xmin, double xmax);
     void getXLims(double* xmin, double* xmax);
@@ -283,7 +290,8 @@ public:
     WaveAxes(int channel);
     ~WaveAxes() {}
 
-    void updateSpikeData(const SpikeObject& s);
+    bool updateSpikeData(const SpikeObject& s);
+    bool checkThreshold(const SpikeObject& spike);
 
     void paint(Graphics& g);
 
@@ -315,6 +323,8 @@ private:
 
     void drawThresholdSlider(Graphics& g);
 
+    
+
     Font font;
 
     Array<SpikeObject> spikeBuffer;
@@ -347,7 +357,7 @@ public:
     ProjectionAxes(int projectionNum);
     ~ProjectionAxes() {}
 
-    void updateSpikeData(const SpikeObject& s);
+    bool updateSpikeData(const SpikeObject& s);
 
     void paint(Graphics& g);
 
diff --git a/Source/Processors/Visualization/Visualizer.h b/Source/Processors/Visualization/Visualizer.h
index 3825800a6..0e23f3d2c 100755
--- a/Source/Processors/Visualization/Visualizer.h
+++ b/Source/Processors/Visualization/Visualizer.h
@@ -94,8 +94,6 @@ public:
     /** Loads parameters from XML */
     virtual void loadVisualizerParameters(XmlElement* xml) { }
 
-
-
 };
 
 
-- 
GitLab