]> sigrok.org Git - pulseview.git/blame - pv/data/analogsegment.cpp
Only show sampling points when zoomed in far enough.
[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
eb8269e3
UH
22#include <cassert>
23#include <cstring>
24#include <cstdlib>
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
c70e3464 109SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start)
26a883ed
SA
110{
111 return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start);
112}
113
c70e3464 114void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase)
26a883ed
SA
115{
116 Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
117}
118
c70e3464 119void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it)
26a883ed
SA
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 169
67b0df27
SA
170 // Calculate min/max values in case we have too few samples for an envelope
171 if (sample_count_ < EnvelopeScaleFactor) {
172 it = begin_raw_sample_iteration(0);
173 for (uint64_t i = 0; i < sample_count_; i++) {
174 const float sample = *((float*)it->value);
175 if (sample < min_value_) min_value_ = sample;
176 if (sample > max_value_) max_value_ = sample;
177 continue_raw_sample_iteration(it, 1);
178 }
179 end_raw_sample_iteration(it);
180 }
181
fda5b6e0
JH
182 // Break off if there are no new samples to compute
183 if (e0.length == prev_length)
184 return;
185
186 reallocate_envelope(e0);
187
188 dest_ptr = e0.samples + prev_length;
189
190 // Iterate through the samples to populate the first level mipmap
26a883ed
SA
191 uint64_t start_sample = prev_length * EnvelopeScaleFactor;
192 uint64_t end_sample = e0.length * EnvelopeScaleFactor;
193
194 it = begin_raw_sample_iteration(start_sample);
195 for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) {
196 const float* samples = (float*)it->value;
197
fda5b6e0 198 const EnvelopeSample sub_sample = {
26a883ed
SA
199 *min_element(samples, samples + EnvelopeScaleFactor),
200 *max_element(samples, samples + EnvelopeScaleFactor),
fda5b6e0
JH
201 };
202
73e377fe
SA
203 if (sub_sample.min < min_value_) min_value_ = sub_sample.min;
204 if (sub_sample.max > max_value_) max_value_ = sub_sample.max;
205
26a883ed 206 continue_raw_sample_iteration(it, EnvelopeScaleFactor);
fda5b6e0
JH
207 *dest_ptr++ = sub_sample;
208 }
26a883ed 209 end_raw_sample_iteration(it);
fda5b6e0
JH
210
211 // Compute higher level mipmaps
2ad82c2e 212 for (unsigned int level = 1; level < ScaleStepCount; level++) {
8dbbc7f0
JH
213 Envelope &e = envelope_levels_[level];
214 const Envelope &el = envelope_levels_[level-1];
fda5b6e0
JH
215
216 // Expand the data buffer to fit the new samples
217 prev_length = e.length;
218 e.length = el.length / EnvelopeScaleFactor;
219
26a883ed 220 // Break off if there are no more samples to be computed
fda5b6e0
JH
221 if (e.length == prev_length)
222 break;
223
224 reallocate_envelope(e);
225
26a883ed 226 // Subsample the lower level
fda5b6e0
JH
227 const EnvelopeSample *src_ptr =
228 el.samples + prev_length * EnvelopeScaleFactor;
229 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
26a883ed 230
fda5b6e0 231 for (dest_ptr = e.samples + prev_length;
2ad82c2e 232 dest_ptr < end_dest_ptr; dest_ptr++) {
fda5b6e0
JH
233 const EnvelopeSample *const end_src_ptr =
234 src_ptr + EnvelopeScaleFactor;
235
236 EnvelopeSample sub_sample = *src_ptr++;
2ad82c2e 237 while (src_ptr < end_src_ptr) {
73e377fe 238 sub_sample.min = min(sub_sample.min, src_ptr->min);;
fda5b6e0
JH
239 sub_sample.max = max(sub_sample.max, src_ptr->max);
240 src_ptr++;
241 }
242
243 *dest_ptr = sub_sample;
244 }
245 }
246}
247
1b1ec774 248} // namespace data
aba1dd16 249} // namespace pv