]> sigrok.org Git - pulseview.git/blame - pv/data/analogsegment.cpp
Device: Replaced Session::read_sample_rate with read_config
[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
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <extdef.h>
22
23#include <assert.h>
24#include <string.h>
25#include <stdlib.h>
d9e71737 26#include <cmath>
aba1dd16 27
fda5b6e0
JH
28#include <algorithm>
29
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
f3d66e52 48AnalogSegment::AnalogSegment(
ff008de6 49 uint64_t samplerate, const uint64_t expected_num_samples) :
f3d66e52 50 Segment(samplerate, sizeof(float))
aba1dd16 51{
27d7c96b
DK
52 set_capacity(expected_num_samples);
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
8dbbc7f0 72 data_.resize((sample_count_ + sample_count) * sizeof(float));
bb2cdfff 73
8dbbc7f0 74 float *dst = (float*)data_.data() + sample_count_;
bb2cdfff
JH
75 const float *dst_end = dst + sample_count;
76 while (dst != dst_end)
77 {
78 *dst++ = *data;
79 data += stride;
80 }
81
8dbbc7f0 82 sample_count_ += sample_count;
fda5b6e0
JH
83
84 // Generate the first mip-map from the data
85 append_payload_to_envelope_levels();
aba1dd16
JH
86}
87
f3d66e52 88const float* AnalogSegment::get_samples(
d3758367 89 int64_t start_sample, int64_t end_sample) const
a8acb46e 90{
d3758367 91 assert(start_sample >= 0);
8dbbc7f0 92 assert(start_sample < (int64_t)sample_count_);
d3758367 93 assert(end_sample >= 0);
8dbbc7f0 94 assert(end_sample < (int64_t)sample_count_);
d3758367
JH
95 assert(start_sample <= end_sample);
96
8dbbc7f0 97 lock_guard<recursive_mutex> lock(mutex_);
d3758367
JH
98
99 float *const data = new float[end_sample - start_sample];
8dbbc7f0 100 memcpy(data, (float*)data_.data() + start_sample, sizeof(float) *
d3758367
JH
101 (end_sample - start_sample));
102 return data;
a8acb46e
JH
103}
104
f3d66e52 105void AnalogSegment::get_envelope_section(EnvelopeSection &s,
9320072d
JH
106 uint64_t start, uint64_t end, float min_length) const
107{
108 assert(end <= get_sample_count());
109 assert(start <= end);
110 assert(min_length > 0);
111
8dbbc7f0 112 lock_guard<recursive_mutex> lock(mutex_);
9320072d
JH
113
114 const unsigned int min_level = max((int)floorf(logf(min_length) /
115 LogEnvelopeScaleFactor) - 1, 0);
116 const unsigned int scale_power = (min_level + 1) *
117 EnvelopeScalePower;
118 start >>= scale_power;
119 end >>= scale_power;
120
121 s.start = start << scale_power;
122 s.scale = 1 << scale_power;
123 s.length = end - start;
124 s.samples = new EnvelopeSample[s.length];
8dbbc7f0 125 memcpy(s.samples, envelope_levels_[min_level].samples + start,
9320072d
JH
126 s.length * sizeof(EnvelopeSample));
127}
128
f3d66e52 129void AnalogSegment::reallocate_envelope(Envelope &e)
fda5b6e0
JH
130{
131 const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) /
132 EnvelopeDataUnit) * EnvelopeDataUnit;
133 if (new_data_length > e.data_length)
134 {
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;
146
147 // Expand the data buffer to fit the new samples
148 prev_length = e0.length;
8dbbc7f0 149 e0.length = sample_count_ / EnvelopeScaleFactor;
fda5b6e0
JH
150
151 // Break off if there are no new samples to compute
152 if (e0.length == prev_length)
153 return;
154
155 reallocate_envelope(e0);
156
157 dest_ptr = e0.samples + prev_length;
158
159 // Iterate through the samples to populate the first level mipmap
8dbbc7f0 160 const float *const end_src_ptr = (float*)data_.data() +
fda5b6e0 161 e0.length * EnvelopeScaleFactor;
8dbbc7f0 162 for (const float *src_ptr = (float*)data_.data() +
fda5b6e0
JH
163 prev_length * EnvelopeScaleFactor;
164 src_ptr < end_src_ptr; src_ptr += EnvelopeScaleFactor)
165 {
166 const EnvelopeSample sub_sample = {
167 *min_element(src_ptr, src_ptr + EnvelopeScaleFactor),
168 *max_element(src_ptr, src_ptr + EnvelopeScaleFactor),
169 };
170
171 *dest_ptr++ = sub_sample;
172 }
173
174 // Compute higher level mipmaps
175 for (unsigned int level = 1; level < ScaleStepCount; level++)
176 {
8dbbc7f0
JH
177 Envelope &e = envelope_levels_[level];
178 const Envelope &el = envelope_levels_[level-1];
fda5b6e0
JH
179
180 // Expand the data buffer to fit the new samples
181 prev_length = e.length;
182 e.length = el.length / EnvelopeScaleFactor;
183
184 // Break off if there are no more samples to computed
185 if (e.length == prev_length)
186 break;
187
188 reallocate_envelope(e);
189
190 // Subsample the level lower level
191 const EnvelopeSample *src_ptr =
192 el.samples + prev_length * EnvelopeScaleFactor;
193 const EnvelopeSample *const end_dest_ptr = e.samples + e.length;
194 for (dest_ptr = e.samples + prev_length;
195 dest_ptr < end_dest_ptr; dest_ptr++)
196 {
197 const EnvelopeSample *const end_src_ptr =
198 src_ptr + EnvelopeScaleFactor;
199
200 EnvelopeSample sub_sample = *src_ptr++;
201 while (src_ptr < end_src_ptr)
202 {
203 sub_sample.min = min(sub_sample.min, src_ptr->min);
204 sub_sample.max = max(sub_sample.max, src_ptr->max);
205 src_ptr++;
206 }
207
208 *dest_ptr = sub_sample;
209 }
210 }
211}
212
1b1ec774 213} // namespace data
aba1dd16 214} // namespace pv