]> sigrok.org Git - pulseview.git/blame - pv/data/analogsegment.cpp
Free unused segment memory after acquisition
[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
f3d66e52 29#include "analogsegment.hpp"
aba1dd16 30
3b68d03d
JH
31using std::lock_guard;
32using std::recursive_mutex;
819f4c25
JH
33using std::max;
34using std::max_element;
35using std::min;
36using std::min_element;
aba1dd16
JH
37
38namespace pv {
1b1ec774 39namespace data {
aba1dd16 40
f3d66e52
JH
41const int AnalogSegment::EnvelopeScalePower = 4;
42const int AnalogSegment::EnvelopeScaleFactor = 1 << EnvelopeScalePower;
43const float AnalogSegment::LogEnvelopeScaleFactor =
fda5b6e0 44 logf(EnvelopeScaleFactor);
f3d66e52 45const uint64_t AnalogSegment::EnvelopeDataUnit = 64*1024; // bytes
fda5b6e0 46
26a883ed 47AnalogSegment::AnalogSegment(uint64_t samplerate) :
f3d66e52 48 Segment(samplerate, sizeof(float))
aba1dd16 49{
8dbbc7f0
JH
50 lock_guard<recursive_mutex> lock(mutex_);
51 memset(envelope_levels_, 0, sizeof(envelope_levels_));
aba1dd16
JH
52}
53
f3d66e52 54AnalogSegment::~AnalogSegment()
fda5b6e0 55{
8dbbc7f0
JH
56 lock_guard<recursive_mutex> lock(mutex_);
57 for (Envelope &e : envelope_levels_)
fda5b6e0
JH
58 free(e.samples);
59}
60
f3d66e52 61void AnalogSegment::append_interleaved_samples(const float *data,
bb2cdfff 62 size_t sample_count, size_t stride)
aba1dd16 63{
8dbbc7f0 64 assert(unit_size_ == sizeof(float));
bb2cdfff 65
8dbbc7f0 66 lock_guard<recursive_mutex> lock(mutex_);
bb2cdfff 67
26a883ed
SA
68 for (uint32_t i=0; i < sample_count; i++) {
69 append_single_sample((void*)data);
bb2cdfff
JH
70 data += stride;
71 }
72
fda5b6e0
JH
73 // Generate the first mip-map from the data
74 append_payload_to_envelope_levels();
aba1dd16
JH
75}
76
f3d66e52 77const float* AnalogSegment::get_samples(
d3758367 78 int64_t start_sample, int64_t end_sample) const
a8acb46e 79{
d3758367 80 assert(start_sample >= 0);
8dbbc7f0 81 assert(start_sample < (int64_t)sample_count_);
d3758367 82 assert(end_sample >= 0);
8dbbc7f0 83 assert(end_sample < (int64_t)sample_count_);
d3758367
JH
84 assert(start_sample <= end_sample);
85
8dbbc7f0 86 lock_guard<recursive_mutex> lock(mutex_);
d3758367 87
26a883ed
SA
88 return (float*)get_raw_samples(start_sample, (end_sample - start_sample));
89}
90
91SegmentAnalogDataIterator* AnalogSegment::begin_sample_iteration(uint64_t start) const
92{
93 return (SegmentAnalogDataIterator*)begin_raw_sample_iteration(start);
94}
95
96void AnalogSegment::continue_sample_iteration(SegmentAnalogDataIterator* it, uint64_t increase) const
97{
98 Segment::continue_raw_sample_iteration((SegmentRawDataIterator*)it, increase);
99}
100
101void AnalogSegment::end_sample_iteration(SegmentAnalogDataIterator* it) const
102{
103 Segment::end_raw_sample_iteration((SegmentRawDataIterator*)it);
a8acb46e
JH
104}
105
f3d66e52 106void AnalogSegment::get_envelope_section(EnvelopeSection &s,
9320072d
JH
107 uint64_t start, uint64_t end, float min_length) const
108{
109 assert(end <= get_sample_count());
110 assert(start <= end);
111 assert(min_length > 0);
112
8dbbc7f0 113 lock_guard<recursive_mutex> lock(mutex_);
9320072d
JH
114
115 const unsigned int min_level = max((int)floorf(logf(min_length) /
116 LogEnvelopeScaleFactor) - 1, 0);
117 const unsigned int scale_power = (min_level + 1) *
118 EnvelopeScalePower;
119 start >>= scale_power;
120 end >>= scale_power;
121
122 s.start = start << scale_power;
123 s.scale = 1 << scale_power;
124 s.length = end - start;
125 s.samples = new EnvelopeSample[s.length];
8dbbc7f0 126 memcpy(s.samples, envelope_levels_[min_level].samples + start,
9320072d
JH
127 s.length * sizeof(EnvelopeSample));
128}
129
f3d66e52 130void AnalogSegment::reallocate_envelope(Envelope &e)
fda5b6e0
JH
131{
132 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
133 EnvelopeDataUnit) * EnvelopeDataUnit;
2ad82c2e 134 if (new_data_length > e.data_length) {
fda5b6e0
JH
135 e.data_length = new_data_length;
136 e.samples = (EnvelopeSample*)realloc(e.samples,
137 new_data_length * sizeof(EnvelopeSample));
138 }
139}
140
f3d66e52 141void AnalogSegment::append_payload_to_envelope_levels()
fda5b6e0 142{
8dbbc7f0 143 Envelope &e0 = envelope_levels_[0];
fda5b6e0
JH
144 uint64_t prev_length;
145 EnvelopeSample *dest_ptr;
26a883ed 146 SegmentRawDataIterator* it;
fda5b6e0
JH
147
148 // Expand the data buffer to fit the new samples
149 prev_length = e0.length;
8dbbc7f0 150 e0.length = sample_count_ / EnvelopeScaleFactor;
fda5b6e0
JH
151
152 // Break off if there are no new samples to compute
153 if (e0.length == prev_length)
154 return;
155
156 reallocate_envelope(e0);
157
158 dest_ptr = e0.samples + prev_length;
159
160 // Iterate through the samples to populate the first level mipmap
26a883ed
SA
161 uint64_t start_sample = prev_length * EnvelopeScaleFactor;
162 uint64_t end_sample = e0.length * EnvelopeScaleFactor;
163
164 it = begin_raw_sample_iteration(start_sample);
165 for (uint64_t i = start_sample; i < end_sample; i += EnvelopeScaleFactor) {
166 const float* samples = (float*)it->value;
167
fda5b6e0 168 const EnvelopeSample sub_sample = {
26a883ed
SA
169 *min_element(samples, samples + EnvelopeScaleFactor),
170 *max_element(samples, samples + EnvelopeScaleFactor),
fda5b6e0
JH
171 };
172
26a883ed 173 continue_raw_sample_iteration(it, EnvelopeScaleFactor);
fda5b6e0
JH
174 *dest_ptr++ = sub_sample;
175 }
26a883ed 176 end_raw_sample_iteration(it);
fda5b6e0
JH
177
178 // Compute higher level mipmaps
2ad82c2e 179 for (unsigned int level = 1; level < ScaleStepCount; level++) {
8dbbc7f0
JH
180 Envelope &e = envelope_levels_[level];
181 const Envelope &el = envelope_levels_[level-1];
fda5b6e0
JH
182
183 // Expand the data buffer to fit the new samples
184 prev_length = e.length;
185 e.length = el.length / EnvelopeScaleFactor;
186
26a883ed 187 // Break off if there are no more samples to be computed
fda5b6e0
JH
188 if (e.length == prev_length)
189 break;
190
191 reallocate_envelope(e);
192
26a883ed 193 // Subsample the lower level
fda5b6e0
JH
194 const EnvelopeSample *src_ptr =
195 el.samples + prev_length * EnvelopeScaleFactor;
196 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
26a883ed 197
fda5b6e0 198 for (dest_ptr = e.samples + prev_length;
2ad82c2e 199 dest_ptr < end_dest_ptr; dest_ptr++) {
fda5b6e0
JH
200 const EnvelopeSample *const end_src_ptr =
201 src_ptr + EnvelopeScaleFactor;
202
203 EnvelopeSample sub_sample = *src_ptr++;
2ad82c2e 204 while (src_ptr < end_src_ptr) {
fda5b6e0
JH
205 sub_sample.min = min(sub_sample.min, src_ptr->min);
206 sub_sample.max = max(sub_sample.max, src_ptr->max);
207 src_ptr++;
208 }
209
210 *dest_ptr = sub_sample;
211 }
212 }
213}
214
1b1ec774 215} // namespace data
aba1dd16 216} // namespace pv