]> sigrok.org Git - pulseview.git/blame_incremental - pv/data/analogsegment.cpp
TraceView: Restore vertical offset
[pulseview.git] / pv / data / analogsegment.cpp
... / ...
CommitLineData
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
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <extdef.h>
21
22#include <assert.h>
23#include <string.h>
24#include <stdlib.h>
25#include <cmath>
26
27#include <algorithm>
28
29#include "analogsegment.hpp"
30
31using std::lock_guard;
32using std::recursive_mutex;
33using std::max;
34using std::max_element;
35using std::min;
36using std::min_element;
37
38namespace pv {
39namespace data {
40
41const int AnalogSegment::EnvelopeScalePower = 4;
42const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
43const float AnalogSegment::LogEnvelopeScaleFactor =
44 logf(EnvelopeScaleFactor);
45const uint64_t AnalogSegment::EnvelopeDataUnit = 64*1024; // bytes
46
47AnalogSegment::AnalogSegment(
48 uint64_t samplerate, const uint64_t expected_num_samples) :
49 Segment(samplerate, sizeof(float))
50{
51 set_capacity(expected_num_samples);
52
53 lock_guard<recursive_mutex> lock(mutex_);
54 memset(envelope_levels_, 0, sizeof(envelope_levels_));
55}
56
57AnalogSegment::~AnalogSegment()
58{
59 lock_guard<recursive_mutex> lock(mutex_);
60 for (Envelope &e : envelope_levels_)
61 free(e.samples);
62}
63
64void AnalogSegment::append_interleaved_samples(const float *data,
65 size_t sample_count, size_t stride)
66{
67 assert(unit_size_ == sizeof(float));
68
69 lock_guard<recursive_mutex> lock(mutex_);
70
71 // If we're out of memory, this will throw std::bad_alloc
72 data_.resize((sample_count_ + sample_count) * sizeof(float));
73
74 float *dst = (float*)data_.data() + sample_count_;
75 const float *dst_end = dst + sample_count;
76 while (dst != dst_end) {
77 *dst++ = *data;
78 data += stride;
79 }
80
81 sample_count_ += sample_count;
82
83 // Generate the first mip-map from the data
84 append_payload_to_envelope_levels();
85}
86
87const float* AnalogSegment::get_samples(
88 int64_t start_sample, int64_t end_sample) const
89{
90 assert(start_sample >= 0);
91 assert(start_sample < (int64_t)sample_count_);
92 assert(end_sample >= 0);
93 assert(end_sample < (int64_t)sample_count_);
94 assert(start_sample <= end_sample);
95
96 lock_guard<recursive_mutex> lock(mutex_);
97
98 float *const data = new float[end_sample - start_sample];
99 memcpy(data, (float*)data_.data() + start_sample, sizeof(float) *
100 (end_sample - start_sample));
101 return data;
102}
103
104void AnalogSegment::get_envelope_section(EnvelopeSection &s,
105 uint64_t start, uint64_t end, float min_length) const
106{
107 assert(end <= get_sample_count());
108 assert(start <= end);
109 assert(min_length > 0);
110
111 lock_guard<recursive_mutex> lock(mutex_);
112
113 const unsigned int min_level = max((int)floorf(logf(min_length) /
114 LogEnvelopeScaleFactor) - 1, 0);
115 const unsigned int scale_power = (min_level + 1) *
116 EnvelopeScalePower;
117 start >>= scale_power;
118 end >>= scale_power;
119
120 s.start = start << scale_power;
121 s.scale = 1 << scale_power;
122 s.length = end - start;
123 s.samples = new EnvelopeSample[s.length];
124 memcpy(s.samples, envelope_levels_[min_level].samples + start,
125 s.length * sizeof(EnvelopeSample));
126}
127
128void AnalogSegment::reallocate_envelope(Envelope &e)
129{
130 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
131 EnvelopeDataUnit) * EnvelopeDataUnit;
132 if (new_data_length > e.data_length) {
133 e.data_length = new_data_length;
134 e.samples = (EnvelopeSample*)realloc(e.samples,
135 new_data_length * sizeof(EnvelopeSample));
136 }
137}
138
139void AnalogSegment::append_payload_to_envelope_levels()
140{
141 Envelope &e0 = envelope_levels_[0];
142 uint64_t prev_length;
143 EnvelopeSample *dest_ptr;
144
145 // Expand the data buffer to fit the new samples
146 prev_length = e0.length;
147 e0.length = sample_count_ / EnvelopeScaleFactor;
148
149 // Break off if there are no new samples to compute
150 if (e0.length == prev_length)
151 return;
152
153 reallocate_envelope(e0);
154
155 dest_ptr = e0.samples + prev_length;
156
157 // Iterate through the samples to populate the first level mipmap
158 const float *const end_src_ptr = (float*)data_.data() +
159 e0.length * EnvelopeScaleFactor;
160 for (const float *src_ptr = (float*)data_.data() +
161 prev_length * EnvelopeScaleFactor;
162 src_ptr < end_src_ptr; src_ptr += EnvelopeScaleFactor) {
163 const EnvelopeSample sub_sample = {
164 *min_element(src_ptr, src_ptr + EnvelopeScaleFactor),
165 *max_element(src_ptr, src_ptr + EnvelopeScaleFactor),
166 };
167
168 *dest_ptr++ = sub_sample;
169 }
170
171 // Compute higher level mipmaps
172 for (unsigned int level = 1; level < ScaleStepCount; level++) {
173 Envelope &e = envelope_levels_[level];
174 const Envelope &el = envelope_levels_[level-1];
175
176 // Expand the data buffer to fit the new samples
177 prev_length = e.length;
178 e.length = el.length / EnvelopeScaleFactor;
179
180 // Break off if there are no more samples to computed
181 if (e.length == prev_length)
182 break;
183
184 reallocate_envelope(e);
185
186 // Subsample the level lower level
187 const EnvelopeSample *src_ptr =
188 el.samples + prev_length * EnvelopeScaleFactor;
189 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
190 for (dest_ptr = e.samples + prev_length;
191 dest_ptr < end_dest_ptr; dest_ptr++) {
192 const EnvelopeSample *const end_src_ptr =
193 src_ptr + EnvelopeScaleFactor;
194
195 EnvelopeSample sub_sample = *src_ptr++;
196 while (src_ptr < end_src_ptr) {
197 sub_sample.min = min(sub_sample.min, src_ptr->min);
198 sub_sample.max = max(sub_sample.max, src_ptr->max);
199 src_ptr++;
200 }
201
202 *dest_ptr = sub_sample;
203 }
204 }
205}
206
207} // namespace data
208} // namespace pv