]> sigrok.org Git - pulseview.git/blame - pv/data/analogsegment.cpp
Avoid wrapping driver names etc in about box.
[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 22#include <cassert>
d9e71737 23#include <cmath>
aca9aa83
UH
24#include <cstdlib>
25#include <cstring>
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;
6f925ba9 34using std::make_pair;
819f4c25
JH
35using std::max;
36using std::max_element;
37using std::min;
38using std::min_element;
6f925ba9 39using std::pair;
aba1dd16
JH
40
41namespace pv {
1b1ec774 42namespace data {
aba1dd16 43
f3d66e52
JH
44const int AnalogSegment::EnvelopeScalePower = 4;
45const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
c063290a 46const float AnalogSegment::LogEnvelopeScaleFactor = logf(EnvelopeScaleFactor);
51d3950f 47const uint64_t AnalogSegment::EnvelopeDataUnit = 64 * 1024; // bytes
fda5b6e0 48
7db61e77 49AnalogSegment::AnalogSegment(Analog& owner, uint64_t samplerate) :
73e377fe 50 Segment(samplerate, sizeof(float)),
7db61e77 51 owner_(owner),
73e377fe
SA
52 min_value_(0),
53 max_value_(0)
aba1dd16 54{
8dbbc7f0
JH
55 lock_guard<recursive_mutex> lock(mutex_);
56 memset(envelope_levels_, 0, sizeof(envelope_levels_));
aba1dd16
JH
57}
58
f3d66e52 59AnalogSegment::~AnalogSegment()
fda5b6e0 60{
8dbbc7f0
JH
61 lock_guard<recursive_mutex> lock(mutex_);
62 for (Envelope &e : envelope_levels_)
fda5b6e0
JH
63 free(e.samples);
64}
65
f3d66e52 66void AnalogSegment::append_interleaved_samples(const float *data,
bb2cdfff 67 size_t sample_count, size_t stride)
aba1dd16 68{
8dbbc7f0 69 assert(unit_size_ == sizeof(float));
bb2cdfff 70
8dbbc7f0 71 lock_guard<recursive_mutex> lock(mutex_);
bb2cdfff 72
7db61e77
SA
73 uint64_t prev_sample_count = sample_count_;
74
c063290a 75 for (uint32_t i = 0; i < sample_count; i++) {
26a883ed 76 append_single_sample((void*)data);
bb2cdfff
JH
77 data += stride;
78 }
79
fda5b6e0
JH
80 // Generate the first mip-map from the data
81 append_payload_to_envelope_levels();
7db61e77
SA
82
83 if (sample_count > 1)
84 owner_.notify_samples_added(this, prev_sample_count + 1,
85 prev_sample_count + 1 + sample_count);
86 else
87 owner_.notify_samples_added(this, prev_sample_count + 1,
88 prev_sample_count + 1);
aba1dd16
JH
89}
90
f3d66e52 91const float* AnalogSegment::get_samples(
d3758367 92 int64_t start_sample, int64_t end_sample) const
a8acb46e 93{
d3758367 94 assert(start_sample >= 0);
8dbbc7f0 95 assert(start_sample < (int64_t)sample_count_);
d3758367 96 assert(end_sample >= 0);
8dbbc7f0 97 assert(end_sample < (int64_t)sample_count_);
d3758367
JH
98 assert(start_sample <= end_sample);
99
8dbbc7f0 100 lock_guard<recursive_mutex> lock(mutex_);
d3758367 101
26a883ed
SA
102 return (float*)get_raw_samples(start_sample, (end_sample - start_sample));
103}
104
6f925ba9 105const pair<float, float> AnalogSegment::get_min_max() const
73e377fe 106{
6f925ba9 107 return make_pair(min_value_, max_value_);
73e377fe
SA
108}
109
c70e3464 110SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start)
26a883ed
SA
111{
112 return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start);
113}
114
c70e3464 115void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase)
26a883ed
SA
116{
117 Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
118}
119
c70e3464 120void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it)
26a883ed
SA
121{
122 Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
a8acb46e
JH
123}
124
f3d66e52 125void AnalogSegment::get_envelope_section(EnvelopeSection &s,
9320072d
JH
126 uint64_t start, uint64_t end, float min_length) const
127{
128 assert(end <= get_sample_count());
129 assert(start <= end);
130 assert(min_length > 0);
131
8dbbc7f0 132 lock_guard<recursive_mutex> lock(mutex_);
9320072d
JH
133
134 const unsigned int min_level = max((int)floorf(logf(min_length) /
135 LogEnvelopeScaleFactor) - 1, 0);
136 const unsigned int scale_power = (min_level + 1) *
137 EnvelopeScalePower;
138 start >>= scale_power;
139 end >>= scale_power;
140
141 s.start = start << scale_power;
142 s.scale = 1 << scale_power;
143 s.length = end - start;
144 s.samples = new EnvelopeSample[s.length];
8dbbc7f0 145 memcpy(s.samples, envelope_levels_[min_level].samples + start,
9320072d
JH
146 s.length * sizeof(EnvelopeSample));
147}
148
f3d66e52 149void AnalogSegment::reallocate_envelope(Envelope &e)
fda5b6e0
JH
150{
151 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
152 EnvelopeDataUnit) * EnvelopeDataUnit;
2ad82c2e 153 if (new_data_length > e.data_length) {
fda5b6e0
JH
154 e.data_length = new_data_length;
155 e.samples = (EnvelopeSample*)realloc(e.samples,
156 new_data_length * sizeof(EnvelopeSample));
157 }
158}
159
f3d66e52 160void AnalogSegment::append_payload_to_envelope_levels()
fda5b6e0 161{
8dbbc7f0 162 Envelope &e0 = envelope_levels_[0];
fda5b6e0
JH
163 uint64_t prev_length;
164 EnvelopeSample *dest_ptr;
26a883ed 165 SegmentRawDataIterator* it;
fda5b6e0
JH
166
167 // Expand the data buffer to fit the new samples
168 prev_length = e0.length;
8dbbc7f0 169 e0.length = sample_count_ / EnvelopeScaleFactor;
fda5b6e0 170
67b0df27
SA
171 // Calculate min/max values in case we have too few samples for an envelope
172 if (sample_count_ < EnvelopeScaleFactor) {
173 it = begin_raw_sample_iteration(0);
174 for (uint64_t i = 0; i < sample_count_; i++) {
175 const float sample = *((float*)it->value);
c063290a
UH
176 if (sample < min_value_)
177 min_value_ = sample;
178 if (sample > max_value_)
179 max_value_ = sample;
67b0df27
SA
180 continue_raw_sample_iteration(it, 1);
181 }
182 end_raw_sample_iteration(it);
183 }
184
fda5b6e0
JH
185 // Break off if there are no new samples to compute
186 if (e0.length == prev_length)
187 return;
188
189 reallocate_envelope(e0);
190
191 dest_ptr = e0.samples + prev_length;
192
193 // Iterate through the samples to populate the first level mipmap
26a883ed 194 uint64_t start_sample = prev_length * EnvelopeScaleFactor;
c063290a 195 uint64_t end_sample = e0.length * EnvelopeScaleFactor;
26a883ed
SA
196
197 it = begin_raw_sample_iteration(start_sample);
198 for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) {
199 const float* samples = (float*)it->value;
200
fda5b6e0 201 const EnvelopeSample sub_sample = {
26a883ed
SA
202 *min_element(samples, samples + EnvelopeScaleFactor),
203 *max_element(samples, samples + EnvelopeScaleFactor),
fda5b6e0
JH
204 };
205
c063290a
UH
206 if (sub_sample.min < min_value_)
207 min_value_ = sub_sample.min;
208 if (sub_sample.max > max_value_)
209 max_value_ = sub_sample.max;
73e377fe 210
26a883ed 211 continue_raw_sample_iteration(it, EnvelopeScaleFactor);
fda5b6e0
JH
212 *dest_ptr++ = sub_sample;
213 }
26a883ed 214 end_raw_sample_iteration(it);
fda5b6e0
JH
215
216 // Compute higher level mipmaps
2ad82c2e 217 for (unsigned int level = 1; level < ScaleStepCount; level++) {
8dbbc7f0 218 Envelope &e = envelope_levels_[level];
c063290a 219 const Envelope &el = envelope_levels_[level - 1];
fda5b6e0
JH
220
221 // Expand the data buffer to fit the new samples
222 prev_length = e.length;
223 e.length = el.length / EnvelopeScaleFactor;
224
26a883ed 225 // Break off if there are no more samples to be computed
fda5b6e0
JH
226 if (e.length == prev_length)
227 break;
228
229 reallocate_envelope(e);
230
26a883ed 231 // Subsample the lower level
fda5b6e0
JH
232 const EnvelopeSample *src_ptr =
233 el.samples + prev_length * EnvelopeScaleFactor;
234 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
26a883ed 235
fda5b6e0 236 for (dest_ptr = e.samples + prev_length;
2ad82c2e 237 dest_ptr < end_dest_ptr; dest_ptr++) {
fda5b6e0
JH
238 const EnvelopeSample *const end_src_ptr =
239 src_ptr + EnvelopeScaleFactor;
240
241 EnvelopeSample sub_sample = *src_ptr++;
2ad82c2e 242 while (src_ptr < end_src_ptr) {
73e377fe 243 sub_sample.min = min(sub_sample.min, src_ptr->min);;
fda5b6e0
JH
244 sub_sample.max = max(sub_sample.max, src_ptr->max);
245 src_ptr++;
246 }
247
248 *dest_ptr = sub_sample;
249 }
250 }
251}
252
1b1ec774 253} // namespace data
aba1dd16 254} // namespace pv