diff --git a/src/streamchannel.cpp b/src/streamchannel.cpp --- a/src/streamchannel.cpp +++ b/src/streamchannel.cpp @@ -17,9 +17,10 @@ along with serialplot. If not, see . */ +#include #include "streamchannel.h" -StreamChannel::StreamChannel(unsigned i, const FrameBuffer* x, +StreamChannel::StreamChannel(unsigned i, const XFrameBuffer* x, FrameBuffer* y, ChannelInfoModel* info) { _index = i; @@ -37,8 +38,37 @@ unsigned StreamChannel::index() const {r QString StreamChannel::name() const {return _info->name(_index);}; QColor StreamChannel::color() const {return _info->color(_index);}; bool StreamChannel::visible() const {return _info->isVisible(_index);}; -const FrameBuffer* StreamChannel::xData() const {return _x;} +const XFrameBuffer* StreamChannel::xData() const {return _x;} const FrameBuffer* StreamChannel::yData() const {return _y;} FrameBuffer* StreamChannel::yData() {return _y;} const ChannelInfoModel* StreamChannel::info() const {return _info;} -void StreamChannel::setX(const FrameBuffer* x) {_x = x;}; +void StreamChannel::setX(const XFrameBuffer* x) {_x = x;}; + +double StreamChannel::findValue(double x) const +{ + int index = _x->findIndex(x); + Q_ASSERT(index < (int) _x->size()); + + if (index >= 0) + { + // can't do estimation for last sample + if (index == (int) _x->size() - 1) + { + return _y->sample(index); + } + else + { + // calculate middle of the line + double prev_x = _x->sample(index); + double next_x = _x->sample(index+1); + double ratio = (x - prev_x) / (next_x - prev_x); + double prev_y = _y->sample(index); + double next_y = _y->sample(index+1); + return ratio * (next_y - prev_y) + prev_y; + } + } + else + { + return std::numeric_limits::quiet_NaN(); + } +}