00001 /* 00002 * buffer_spike_detector.cpp 00003 * Copyright 2013 University of Massachusetts Lowell 00004 * Author: Jonathan Hasenzahl 00005 */ 00006 00007 #include "burst_calc/buffer_spike_detector.h" 00008 #include <cstdio> 00009 #include <cmath> 00010 00014 BufferSpikeDetector::BufferSpikeDetector() 00015 { 00016 dishes_received_ = 0; 00017 00018 for (int i = 0; i < 60; i++) 00019 { 00020 sums_[i] = 0.0; 00021 sum_squares_[i] = 0.0; 00022 min_volts_[i] = 1.0; 00023 max_volts_[i] = -1.0; 00024 } 00025 } 00026 00034 void BufferSpikeDetector::init(int buffer_size, double stdev_mult) 00035 { 00036 buffer_size_ = buffer_size; 00037 stdev_mult_ = stdev_mult; 00038 } 00039 00044 void BufferSpikeDetector::add(const neuro_recv::dish_state& d) 00045 { 00046 dishes_received_++; 00047 00048 // Update min/max and add to sum 00049 for (int i = 0; i < 60; i++) 00050 { 00051 if (d.samples[i] > max_volts_[i]) 00052 max_volts_[i] = d.samples[i]; 00053 else if (d.samples[i] < min_volts_[i]) 00054 min_volts_[i] = d.samples[i]; 00055 00056 sums_[i] += d.samples[i]; 00057 sum_squares_[i] += d.samples[i] * d.samples[i]; 00058 } 00059 00060 // Calculate the baselines and thresholds if there are enough dish states 00061 if (isBuffered()) 00062 calculate(); 00063 } 00064 00071 void BufferSpikeDetector::calculate() 00072 { 00073 for (int i = 0; i < 60; i++) 00074 { 00075 baselines_[i] = sums_[i] / dishes_received_; 00076 variances_[i] = (sum_squares_[i] - ((sums_[i] * sums_[i]) / dishes_received_)) / (dishes_received_ - 1); 00077 stdevs_[i] = sqrt(variances_[i]); 00078 thresholds_[i] = stdevs_[i] * stdev_mult_ + baselines_[i]; 00079 printf("Min: %+.5f Max: %+.5f Base: %+.5f StDev: %+.5f Thresh: %+.5f\n", 00080 min_volts_[i], max_volts_[i], baselines_[i], stdevs_[i], 00081 thresholds_[i]); 00082 } 00083 } 00084 00092 burst_calc::ranges BufferSpikeDetector::getRanges() 00093 { 00094 burst_calc::ranges ranges; 00095 for (int i = 0; i < 60; i++) 00096 { 00097 ranges.baselines[i] = baselines_[i]; 00098 ranges.thresholds[i] = thresholds_[i]; 00099 ranges.min_volts[i] = min_volts_[i]; 00100 ranges.max_volts[i] = max_volts_[i]; 00101 } 00102 return ranges; 00103 }