Project: engagement_recognition License: BSD Dependencies:
Used by:
None |
engagement_recognition/src/recognition/statistics/window.cppGo to the documentation of this file.00001 /* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2010, Worcester Polytechnic Institute 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Worcester Polytechnic Institute. nor the names 00018 * of its contributors may be used to endorse or promote products 00019 * derived from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 */ 00034 /* 00035 * window.cpp 00036 * 00037 * Created on: Sep 8, 2010 00038 * Author: Brett Ponsler (bponsler (at) wpi (dot) edu) 00039 */ 00040 #include "window.h" 00041 #include <stdio.h> 00042 #include "../recognizers/recognizer_factory.h" 00043 00044 Window::Window() : events_(), earliest_(0) { } 00045 00046 void Window::setWindowSize(double window_size) 00047 { 00048 this->window_size_ = window_size; 00049 } 00050 00051 void Window::updateWindow() 00052 { 00053 this->window_size_ = this->window_size_; 00054 00055 // Compute the start time of the window 00056 unsigned long window_start = CURRENT_TIME - (unsigned long)(this->window_size_ * 1000); 00057 00058 // Traverse from the final event backward until we reach an even that 00059 // has a time below the window size 00060 for (this->earliest_ = this->events_.size(); this->earliest_ > 0 && 00061 this->events_.at(this->earliest_ - 1).getStart() >= window_start; 00062 this->earliest_--); 00063 } 00064 00065 void Window::addEvent(ConnectionEvent event) 00066 { 00067 this->events_.push_back(event); 00068 this->updateWindow(); 00069 } 00070 00071 unsigned long Window::getTimeBetween() 00072 { 00073 int index = this->events_.size(); 00074 00075 // Ensure that two events exist, and return the time between the 00076 // most recent two events 00077 if (index >= 2) 00078 return (this->events_.at(index - 1).getStart() 00079 - this->events_.at(index - 2).getStart()); 00080 else 00081 return 0; 00082 } 00083 00084 unsigned long Window::getRecentMeanDelay() 00085 { 00086 // Keep track of the total recent duration and number of events 00087 unsigned long total = 0; 00088 long events = 0; 00089 00090 // Make sure the earliest index is valid 00091 if (this->earliest_ >= 0 && (unsigned)this->earliest_ < this->events_.size()) 00092 { 00093 // Traverse through the window of connection events 00094 for (unsigned i = this->earliest_; i < this->events_.size(); i++) 00095 { 00096 // As long as this is not a backchannel event add it into the total 00097 // NOTE: Backchannel events do not get added in the delay 00098 if (this->events_.at(i).getType() != 00099 RecognizerFactory::getBackchannelType()) 00100 { 00101 // Add the delay, and increment the number of events 00102 total += this->events_.at(i).getDelay(); 00103 events++; 00104 } 00105 } 00106 } 00107 00108 // Return the mean recent delay 00109 return (events != 0 ? total / events : 0); 00110 } 00111 00112 unsigned long Window::getRecentMaxDelay() 00113 { 00114 // Keep track of the max delay 00115 unsigned long max_delay = 0; 00116 00117 // Make sure the earliest index is valid 00118 if (this->earliest_ >= 0 && (unsigned)this->earliest_ < this->events_.size()) 00119 { 00120 // Traverse through the window of connection events 00121 for (unsigned i = this->earliest_; i < this->events_.size(); i++) 00122 { 00123 // As long as this is not a backchannel event check the delay 00124 // NOTE: Backchannel events do not get added in the delay 00125 if (this->events_.at(i).getType() != 00126 RecognizerFactory::getBackchannelType()) 00127 { 00128 // Grab the delay from the event 00129 unsigned long delay = this->events_.at(i).getDelay(); 00130 00131 // If this delay is a new max delay, store it 00132 if (max_delay < delay) 00133 max_delay = delay; 00134 } 00135 } 00136 } 00137 00138 // Return the max delay 00139 return max_delay; 00140 } 00141 00142 unsigned long Window::getRecentMeanDuration() 00143 { 00144 // Keep track of the total recent duration and number of events 00145 unsigned long total = 0; 00146 int events = 0; 00147 00148 // Make sure the earliest index is valid 00149 if (this->earliest_ >= 0 && (unsigned)this->earliest_ < this->events_.size()) 00150 { 00151 // Traverse through the window of connection events 00152 for (unsigned i = this->earliest_; i < this->events_.size(); i++) 00153 { 00154 // Add the delay, and increment the number of events 00155 total += this->events_.at(i).getDuration(); 00156 events++; 00157 } 00158 } 00159 00160 // Return the mean recent delay 00161 return (events != 0 ? total / events : 0); 00162 } 00163 00164 unsigned long Window::getRecentMaxDuration() 00165 { 00166 // Keep track of the max delay 00167 unsigned long max_duration = 0; 00168 00169 // Make sure the earliest index is valid 00170 if (this->earliest_ >= 0 && (unsigned)this->earliest_ < this->events_.size()) 00171 { 00172 // Traverse through the window of connection events 00173 for (unsigned i = this->earliest_; i < this->events_.size(); i++) 00174 { 00175 // Grab the duration from the event 00176 unsigned long duration = this->events_.at(i).getDuration(); 00177 00178 // If this duration is a new max duration, store it 00179 if (max_duration < duration) 00180 max_duration = duration; 00181 } 00182 } 00183 00184 // Return the max duration 00185 return max_duration; 00186 } 00187 00188 unsigned long Window::getRecentMeanTBCE() 00189 { 00190 // Keep track of the total recent duration and number of events 00191 unsigned long total = 0; 00192 int events = 0; 00193 00194 // Make sure the earliest index is valid 00195 if (this->earliest_ >= 0 && (unsigned)this->earliest_ < this->events_.size()) 00196 { 00197 // Traverse through the window of connection events 00198 for (unsigned i = this->earliest_ + 1; i < this->events_.size(); i++) 00199 { 00200 // Only add the event if it is successful 00201 if (this->events_.at(i).getSuccess()) 00202 { 00203 // Calculate the duration between the connection events 00204 unsigned long between = this->events_.at(i).getStart() 00205 - this->events_.at(i - 1).getStart(); 00206 00207 // Keep track of the total duration, and number of events 00208 total += between; 00209 events++; 00210 } 00211 } 00212 } 00213 00214 // Return the mean recent delay 00215 return (events != 0 ? total / events : 0); 00216 } 00217 00218 unsigned long Window::getRecentMaxTBCE() 00219 { 00220 // Keep track of the maximum time between connection events 00221 unsigned long max_time = 0; 00222 00223 // Make sure the earliest index is valid 00224 if (this->earliest_ >= 0 && (unsigned)this->earliest_ < this->events_.size()) 00225 { 00226 // Traverse through the window of connection events 00227 for (unsigned i = this->earliest_ + 1; i < this->events_.size(); i++) 00228 { 00229 // Only add the event if it is successful 00230 if (this->events_.at(i).getSuccess()) 00231 { 00232 // Calculate the duration between the connection events 00233 unsigned long between = this->events_.at(i).getStart() 00234 - this->events_.at(i - 1).getStart(); 00235 00236 // If this is a new max, store it 00237 if (max_time < between) 00238 max_time = between; 00239 } 00240 } 00241 } 00242 00243 // Return the max time between connection events 00244 return max_time; 00245 } 00246 00247 int Window::getRecentFailureRate() 00248 { 00249 // Keep track of the total recent failed events 00250 int failed = 0; 00251 00252 // Make sure the earliest index is valid 00253 if (this->earliest_ >= 0 && (unsigned)this->earliest_ < this->events_.size()) 00254 { 00255 // Traverse through the window of connection events 00256 for (unsigned i = this->earliest_; i < this->events_.size(); i++) 00257 { 00258 // If the connection event failed, increment the counter 00259 if (!this->events_.at(i).getSuccess()) 00260 failed++; 00261 } 00262 } 00263 00264 // Calculate the number of failed connection events per window 00265 double rate_ms = (this->window_size_ != 0 ? 00266 (double)failed / (double)(this->window_size_ * 1000) 00267 : 0); 00268 00269 // Return the number of failed events per minute of time 00270 return (rate_ms * 60000); 00271 } |