QCodeEditor: Update to current cpeditor/QCodeEditor fork, commit ed1196a
[smuview.git] / src / channels / movingavgchannel.cpp
blobd74f955673e602b8de93b7357ec885ec9972e830
1 /*
2 * This file is part of the SmuView project.
4 * Copyright (C) 2019-2022 Frank Stettner <frank-stettner@gmx.net>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <cassert>
21 #include <memory>
22 #include <set>
23 #include <string>
25 #include <QDebug>
27 #include "movingavgchannel.hpp"
28 #include "src/channels/basechannel.hpp"
29 #include "src/channels/mathchannel.hpp"
30 #include "src/data/analogtimesignal.hpp"
31 #include "src/data/datautil.hpp"
32 #include "src/devices/basedevice.hpp"
34 using std::set;
35 using std::string;
37 namespace sv {
38 namespace channels {
40 MovingAvgChannel::MovingAvgChannel(
41 data::Quantity quantity,
42 const set<data::QuantityFlag> &quantity_flags,
43 data::Unit unit,
44 shared_ptr<data::AnalogTimeSignal> signal,
45 uint avg_sample_count,
46 shared_ptr<devices::BaseDevice> parent_device,
47 const set<string> &channel_group_names,
48 const string &channel_name,
49 double channel_start_timestamp) :
50 MathChannel(quantity, quantity_flags, unit,
51 parent_device, channel_group_names, channel_name,
52 channel_start_timestamp),
53 signal_(signal),
54 avg_sample_count_(avg_sample_count),
55 next_signal_pos_(0)
57 assert(signal_);
59 total_digits_ = signal_->total_digits();
60 sr_digits_ = signal_->sr_digits();
62 // Init (ring) buffer
63 avg_samples_.reserve(avg_sample_count_);
64 for (size_t i=0; i<avg_sample_count_; ++i)
65 avg_samples_[i] = 0;
67 connect(signal_.get(), &data::AnalogTimeSignal::sample_appended,
68 this, &MovingAvgChannel::on_sample_appended);
71 void MovingAvgChannel::on_sample_appended()
73 size_t signal_sample_count = signal_->sample_count();
74 while (next_signal_pos_ < signal_sample_count) {
75 auto sample = signal_->get_sample(next_signal_pos_, false);
76 avg_samples_[next_signal_pos_%avg_sample_count_] = sample.second;
77 double value = 0.;
78 for (size_t i=0; i<avg_sample_count_; ++i) {
79 value += avg_samples_[i];
81 value /= avg_sample_count_;
82 push_sample(value, sample.first);
83 ++next_signal_pos_;
87 } // namespace channels
88 } // namespace sv