00001 #include "OnlineHistogramGenerator.h" 00002 00003 #include <algorithm> 00004 00005 OnlineHistogramGenerator::OnlineHistogramGenerator::OnlineHistogramGenerator() 00006 {} 00007 00008 OnlineHistogramGenerator::OnlineHistogramGenerator(unsigned numInputPorts, 00009 double minVal, 00010 double maxVal, 00011 unsigned numBins, 00012 unsigned samplingTime) : 00013 n_input_ports(numInputPorts), min_val(minVal), max_val(maxVal), num_bins(numBins), sampling_time(samplingTime) 00014 {} 00015 00016 OnlineHistogramGenerator::~OnlineHistogramGenerator() 00017 {} 00018 00019 00020 int OnlineHistogramGenerator::nAnalogInputPorts() const 00021 { 00022 return n_input_ports; 00023 } 00024 00025 00026 int OnlineHistogramGenerator::nAnalogOutputPorts() const 00027 { 00028 return num_bins; 00029 } 00030 00031 00032 SimObject::PortType OnlineHistogramGenerator::outputPortType(port_t p) const 00033 { 00034 if (p < num_bins) 00035 return analog; 00036 return undefined; 00037 } 00038 00039 SimObject::PortType OnlineHistogramGenerator::inputPortType(port_t p) const 00040 { 00041 if (p < n_input_ports) 00042 return analog; 00043 return undefined; 00044 } 00045 00046 int OnlineHistogramGenerator::reset( double dt ) 00047 { 00048 stepsToWait = 0; 00049 bin_width = (max_val - min_val) / num_bins; 00050 input_counter.resize(num_bins); 00051 out_values.resize(num_bins); 00052 stepsToWait = 0; 00053 return 0; 00054 } 00055 00056 int OnlineHistogramGenerator::advance( AdvanceInfo const& ai) 00057 { 00058 00059 if (!stepsToWait) { 00060 std::transform(input_counter.begin(), input_counter.end(), out_values.begin(), std::bind2nd(std::divides<double>(), (double)n_input_ports)); 00061 std::fill(input_counter.begin(), input_counter.end(), 0u); 00062 stepsToWait = sampling_time; 00063 } 00064 stepsToWait--; 00065 return 0; 00066 } 00067 00068 double OnlineHistogramGenerator::getAnalogOutput(analog_port_id_t port) const 00069 { 00070 return out_values[port]; 00071 } 00072 00073 void OnlineHistogramGenerator::setAnalogInput(double value, analog_port_id_t port) 00074 { 00075 if (!stepsToWait) { 00076 int bin_id = int((value - min_val) / bin_width) ; 00077 if (bin_id < 0) 00078 bin_id = 0; 00079 if ((unsigned)bin_id >= num_bins) 00080 bin_id = num_bins - 1; 00081 input_counter[bin_id]++; 00082 } 00083 } 00084 00085