#include "Data_collection.h" double delta_t_; double buffer_[4]; size_t index_[4]; double running_sum_; double partial_sum_; bool even_; Data_collection::Data_collection( double delta_t ): delta_t_{delta_t}, buffer_{0.0, 0.0, 0.0, 0.0}, index_{0, 1, 2, 3}, running_sum_{0.0}, partial_sum_{0.0}, even_{true} { // Empty constructor } void Data_collection::store_datum( double x ) { size_t tmp = index_[3]; index_[3] = index_[0]; index_[0] = index_[1]; index_[1] = index_[2]; index_[2] = tmp; buffer_[index_[0]] = x; even_ = !even_; if ( even_ ) { // Use running_sum_ += (buffer_[index_[0]] + 4.0*buffer_[index_[1]] + buffer_[index_[2]])/3.0*delta_t; partial_sum_ = 0.0; } else { partial_sum_ = (5.0*buffer_[index_[0]] + 8.0*buffer_[index_[1]] - buffer_[index_[2]])/12.0*delta_t; // The integral must be calculated regardless as to whether or not the // value of the integral is queried. // 5 y(t) + 8 y(t - h) - y(t - 2*h) // running_sum += -------------------------------- h // 12 size_t const last1{(last + (BUFFER_CAPACITY - 1)) % BUFFER_CAPACITY}; size_t const last2{(last + (BUFFER_CAPACITY - 2)) % BUFFER_CAPACITY}; if running_sum += (5*buffer[last] + 8*buffer[last1] - buffer[last2])/12.0*delta_t; }   // 3 y(t) - 4 y(t - h) + y(t – 2h) // Return ------------------------------- // 2 h double Data_collection::derivative() const { size_t const last1{(last + (BUFFER_CAPACITY - 1)) % BUFFER_CAPACITY}; size_t const last2{(last + (BUFFER_CAPACITY - 2)) % BUFFER_CAPACITY}; return (3.0*buffer[last] - 4.0*buffer[last1] + buffer[last2])/(2.0*delta_t); } // 2 y(t) - 5 y(t - h) + 4y(t – 2h) – y(t - 3h) // Return -------------------------------------------- // 2 // h double Data_collection::second_derivative() const { size_t const last1{(last + (BUFFER_CAPACITY - 1)) % BUFFER_CAPACITY}; size_t const last2{(last + (BUFFER_CAPACITY - 2)) % BUFFER_CAPACITY}; size_t const last3{(last + (BUFFER_CAPACITY - 3)) % BUFFER_CAPACITY}; return ( 2.0*buffer[last] - 5.0*buffer[last1] + 4.0*buffer[last2] - buffer[last3] )/(delta_t* delta_t); } // Return the stored value of the integral // - this value will be updated each time a new reading is provided double Data_collection::integral() const { return running_sum; }