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/>.
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"
40 MovingAvgChannel::MovingAvgChannel(
41 data::Quantity quantity
,
42 const set
<data::QuantityFlag
> &quantity_flags
,
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
),
54 avg_sample_count_(avg_sample_count
),
59 total_digits_
= signal_
->total_digits();
60 sr_digits_
= signal_
->sr_digits();
63 avg_samples_
.reserve(avg_sample_count_
);
64 for (size_t i
=0; i
<avg_sample_count_
; ++i
)
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
;
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
);
87 } // namespace channels