]> sigrok.org Git - pulseview.git/blame - pv/data/analogsegment.cpp
Perform autoranging for analog traces also when new data comes in
[pulseview.git] / pv / data / analogsegment.cpp
CommitLineData
aba1dd16
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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 2 of the License, or
9 * (at your option) any later version.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
aba1dd16
JH
18 */
19
20#include <extdef.h>
21
22#include <assert.h>
23#include <string.h>
24#include <stdlib.h>
d9e71737 25#include <cmath>
aba1dd16 26
fda5b6e0
JH
27#include <algorithm>
28
7db61e77 29#include "analog.hpp"
f3d66e52 30#include "analogsegment.hpp"
aba1dd16 31
3b68d03d
JH
32using std::lock_guard;
33using std::recursive_mutex;
819f4c25
JH
34using std::max;
35using std::max_element;
36using std::min;
37using std::min_element;
aba1dd16
JH
38
39namespace pv {
1b1ec774 40namespace data {
aba1dd16 41
f3d66e52
JH
42const int AnalogSegment::EnvelopeScalePower = 4;
43const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
44const float AnalogSegment::LogEnvelopeScaleFactor =
fda5b6e0 45 logf(EnvelopeScaleFactor);
f3d66e52 46const uint64_t AnalogSegment::EnvelopeDataUnit = 64*1024; // bytes
fda5b6e0 47
7db61e77 48AnalogSegment::AnalogSegment(Analog& owner, uint64_t samplerate) :
73e377fe 49 Segment(samplerate, sizeof(float)),
7db61e77 50 owner_(owner),
73e377fe
SA
51 min_value_(0),
52 max_value_(0)
aba1dd16 53{
8dbbc7f0
JH
54 lock_guard<recursive_mutex> lock(mutex_);
55 memset(envelope_levels_, 0, sizeof(envelope_levels_));
aba1dd16
JH
56}
57
f3d66e52 58AnalogSegment::~AnalogSegment()
fda5b6e0 59{
8dbbc7f0
JH
60 lock_guard<recursive_mutex> lock(mutex_);
61 for (Envelope &e : envelope_levels_)
fda5b6e0
JH
62 free(e.samples);
63}
64
f3d66e52 65void AnalogSegment::append_interleaved_samples(const float *data,
bb2cdfff 66 size_t sample_count, size_t stride)
aba1dd16 67{
8dbbc7f0 68 assert(unit_size_ == sizeof(float));
bb2cdfff 69
8dbbc7f0 70 lock_guard<recursive_mutex> lock(mutex_);
bb2cdfff 71
7db61e77
SA
72 uint64_t prev_sample_count = sample_count_;
73
26a883ed
SA
74 for (uint32_t i=0; i < sample_count; i++) {
75 append_single_sample((void*)data);
bb2cdfff
JH
76 data += stride;
77 }
78
fda5b6e0
JH
79 // Generate the first mip-map from the data
80 append_payload_to_envelope_levels();
7db61e77
SA
81
82 if (sample_count > 1)
83 owner_.notify_samples_added(this, prev_sample_count + 1,
84 prev_sample_count + 1 + sample_count);
85 else
86 owner_.notify_samples_added(this, prev_sample_count + 1,
87 prev_sample_count + 1);
aba1dd16
JH
88}
89
f3d66e52 90const float* AnalogSegment::get_samples(
d3758367 91 int64_t start_sample, int64_t end_sample) const
a8acb46e 92{
d3758367 93 assert(start_sample >= 0);
8dbbc7f0 94 assert(start_sample < (int64_t)sample_count_);
d3758367 95 assert(end_sample >= 0);
8dbbc7f0 96 assert(end_sample < (int64_t)sample_count_);
d3758367
JH
97 assert(start_sample <= end_sample);
98
8dbbc7f0 99 lock_guard<recursive_mutex> lock(mutex_);
d3758367 100
26a883ed
SA
101 return (float*)get_raw_samples(start_sample, (end_sample - start_sample));
102}
103
73e377fe
SA
104const std::pair<float, float> AnalogSegment::get_min_max() const
105{
106 return std::make_pair(min_value_, max_value_);
107}
108
26a883ed
SA
109SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start) const
110{
111 return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start);
112}
113
114void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase) const
115{
116 Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
117}
118
119void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it) const
120{
121 Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
a8acb46e
JH
122}
123
f3d66e52 124void AnalogSegment::get_envelope_section(EnvelopeSection &s,
9320072d
JH
125 uint64_t start, uint64_t end, float min_length) const
126{
127 assert(end <= get_sample_count());
128 assert(start <= end);
129 assert(min_length > 0);
130
8dbbc7f0 131 lock_guard<recursive_mutex> lock(mutex_);
9320072d
JH
132
133 const unsigned int min_level = max((int)floorf(logf(min_length) /
134 LogEnvelopeScaleFactor) - 1, 0);
135 const unsigned int scale_power = (min_level + 1) *
136 EnvelopeScalePower;
137 start >>= scale_power;
138 end >>= scale_power;
139
140 s.start = start << scale_power;
141 s.scale = 1 << scale_power;
142 s.length = end - start;
143 s.samples = new EnvelopeSample[s.length];
8dbbc7f0 144 memcpy(s.samples, envelope_levels_[min_level].samples + start,
9320072d
JH
145 s.length * sizeof(EnvelopeSample));
146}
147
f3d66e52 148void AnalogSegment::reallocate_envelope(Envelope &e)
fda5b6e0
JH
149{
150 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
151 EnvelopeDataUnit) * EnvelopeDataUnit;
2ad82c2e 152 if (new_data_length > e.data_length) {
fda5b6e0
JH
153 e.data_length = new_data_length;
154 e.samples = (EnvelopeSample*)realloc(e.samples,
155 new_data_length * sizeof(EnvelopeSample));
156 }
157}
158
f3d66e52 159void AnalogSegment::append_payload_to_envelope_levels()
fda5b6e0 160{
8dbbc7f0 161 Envelope &e0 = envelope_levels_[0];
fda5b6e0
JH
162 uint64_t prev_length;
163 EnvelopeSample *dest_ptr;
26a883ed 164 SegmentRawDataIterator* it;
fda5b6e0
JH
165
166 // Expand the data buffer to fit the new samples
167 prev_length = e0.length;
8dbbc7f0 168 e0.length = sample_count_ / EnvelopeScaleFactor;
fda5b6e0
JH
169
170 // Break off if there are no new samples to compute
171 if (e0.length == prev_length)
172 return;
173
174 reallocate_envelope(e0);
175
176 dest_ptr = e0.samples + prev_length;
177
178 // Iterate through the samples to populate the first level mipmap
26a883ed
SA
179 uint64_t start_sample = prev_length * EnvelopeScaleFactor;
180 uint64_t end_sample = e0.length * EnvelopeScaleFactor;
181
182 it = begin_raw_sample_iteration(start_sample);
183 for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) {
184 const float* samples = (float*)it->value;
185
fda5b6e0 186 const EnvelopeSample sub_sample = {
26a883ed
SA
187 *min_element(samples, samples + EnvelopeScaleFactor),
188 *max_element(samples, samples + EnvelopeScaleFactor),
fda5b6e0
JH
189 };
190
73e377fe
SA
191 if (sub_sample.min < min_value_) min_value_ = sub_sample.min;
192 if (sub_sample.max > max_value_) max_value_ = sub_sample.max;
193
26a883ed 194 continue_raw_sample_iteration(it, EnvelopeScaleFactor);
fda5b6e0
JH
195 *dest_ptr++ = sub_sample;
196 }
26a883ed 197 end_raw_sample_iteration(it);
fda5b6e0
JH
198
199 // Compute higher level mipmaps
2ad82c2e 200 for (unsigned int level = 1; level < ScaleStepCount; level++) {
8dbbc7f0
JH
201 Envelope &e = envelope_levels_[level];
202 const Envelope &el = envelope_levels_[level-1];
fda5b6e0
JH
203
204 // Expand the data buffer to fit the new samples
205 prev_length = e.length;
206 e.length = el.length / EnvelopeScaleFactor;
207
26a883ed 208 // Break off if there are no more samples to be computed
fda5b6e0
JH
209 if (e.length == prev_length)
210 break;
211
212 reallocate_envelope(e);
213
26a883ed 214 // Subsample the lower level
fda5b6e0
JH
215 const EnvelopeSample *src_ptr =
216 el.samples + prev_length * EnvelopeScaleFactor;
217 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
26a883ed 218
fda5b6e0 219 for (dest_ptr = e.samples + prev_length;
2ad82c2e 220 dest_ptr < end_dest_ptr; dest_ptr++) {
fda5b6e0
JH
221 const EnvelopeSample *const end_src_ptr =
222 src_ptr + EnvelopeScaleFactor;
223
224 EnvelopeSample sub_sample = *src_ptr++;
2ad82c2e 225 while (src_ptr < end_src_ptr) {
73e377fe 226 sub_sample.min = min(sub_sample.min, src_ptr->min);;
fda5b6e0
JH
227 sub_sample.max = max(sub_sample.max, src_ptr->max);
228 src_ptr++;
229 }
230
231 *dest_ptr = sub_sample;
232 }
233 }
234}
235
1b1ec774 236} // namespace data
aba1dd16 237} // namespace pv