]>
Commit | Line | Data |
---|---|---|
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> | |
26 | #include <math.h> | |
27 | ||
fda5b6e0 JH |
28 | #include <algorithm> |
29 | ||
aba1dd16 JH |
30 | #include <boost/foreach.hpp> |
31 | ||
1b1ec774 | 32 | #include "analogsnapshot.h" |
aba1dd16 JH |
33 | |
34 | using namespace boost; | |
35 | using namespace std; | |
36 | ||
37 | namespace pv { | |
1b1ec774 | 38 | namespace data { |
aba1dd16 | 39 | |
fda5b6e0 JH |
40 | const int AnalogSnapshot::EnvelopeScalePower = 4; |
41 | const int AnalogSnapshot::EnvelopeScaleFactor = 1 << EnvelopeScalePower; | |
42 | const float AnalogSnapshot::LogEnvelopeScaleFactor = | |
43 | logf(EnvelopeScaleFactor); | |
44 | const uint64_t AnalogSnapshot::EnvelopeDataUnit = 64*1024; // bytes | |
45 | ||
1b1ec774 JH |
46 | AnalogSnapshot::AnalogSnapshot(const sr_datafeed_analog &analog) : |
47 | Snapshot(sizeof(float)) | |
aba1dd16 JH |
48 | { |
49 | lock_guard<recursive_mutex> lock(_mutex); | |
fda5b6e0 | 50 | memset(_envelope_levels, 0, sizeof(_envelope_levels)); |
aba1dd16 JH |
51 | append_payload(analog); |
52 | } | |
53 | ||
fda5b6e0 JH |
54 | AnalogSnapshot::~AnalogSnapshot() |
55 | { | |
56 | lock_guard<recursive_mutex> lock(_mutex); | |
57 | BOOST_FOREACH(Envelope &e, _envelope_levels) | |
58 | free(e.samples); | |
59 | } | |
60 | ||
1b1ec774 | 61 | void AnalogSnapshot::append_payload( |
aba1dd16 JH |
62 | const sr_datafeed_analog &analog) |
63 | { | |
64 | lock_guard<recursive_mutex> lock(_mutex); | |
aba1dd16 | 65 | append_data(analog.data, analog.num_samples); |
fda5b6e0 JH |
66 | |
67 | // Generate the first mip-map from the data | |
68 | append_payload_to_envelope_levels(); | |
aba1dd16 JH |
69 | } |
70 | ||
d3758367 JH |
71 | const float* AnalogSnapshot::get_samples( |
72 | int64_t start_sample, int64_t end_sample) const | |
a8acb46e | 73 | { |
d3758367 JH |
74 | assert(start_sample >= 0); |
75 | assert(start_sample < (int64_t)_sample_count); | |
76 | assert(end_sample >= 0); | |
77 | assert(end_sample < (int64_t)_sample_count); | |
78 | assert(start_sample <= end_sample); | |
79 | ||
80 | lock_guard<recursive_mutex> lock(_mutex); | |
81 | ||
82 | float *const data = new float[end_sample - start_sample]; | |
83 | memcpy(data, (float*)_data + start_sample, sizeof(float) * | |
84 | (end_sample - start_sample)); | |
85 | return data; | |
a8acb46e JH |
86 | } |
87 | ||
9320072d JH |
88 | void AnalogSnapshot::get_envelope_section(EnvelopeSection &s, |
89 | uint64_t start, uint64_t end, float min_length) const | |
90 | { | |
91 | assert(end <= get_sample_count()); | |
92 | assert(start <= end); | |
93 | assert(min_length > 0); | |
94 | ||
95 | lock_guard<recursive_mutex> lock(_mutex); | |
96 | ||
97 | const unsigned int min_level = max((int)floorf(logf(min_length) / | |
98 | LogEnvelopeScaleFactor) - 1, 0); | |
99 | const unsigned int scale_power = (min_level + 1) * | |
100 | EnvelopeScalePower; | |
101 | start >>= scale_power; | |
102 | end >>= scale_power; | |
103 | ||
104 | s.start = start << scale_power; | |
105 | s.scale = 1 << scale_power; | |
106 | s.length = end - start; | |
107 | s.samples = new EnvelopeSample[s.length]; | |
108 | memcpy(s.samples, _envelope_levels[min_level].samples + start, | |
109 | s.length * sizeof(EnvelopeSample)); | |
110 | } | |
111 | ||
fda5b6e0 JH |
112 | void AnalogSnapshot::reallocate_envelope(Envelope &e) |
113 | { | |
114 | const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) / | |
115 | EnvelopeDataUnit) * EnvelopeDataUnit; | |
116 | if (new_data_length > e.data_length) | |
117 | { | |
118 | e.data_length = new_data_length; | |
119 | e.samples = (EnvelopeSample*)realloc(e.samples, | |
120 | new_data_length * sizeof(EnvelopeSample)); | |
121 | } | |
122 | } | |
123 | ||
124 | void AnalogSnapshot::append_payload_to_envelope_levels() | |
125 | { | |
126 | Envelope &e0 = _envelope_levels[0]; | |
127 | uint64_t prev_length; | |
128 | EnvelopeSample *dest_ptr; | |
129 | ||
130 | // Expand the data buffer to fit the new samples | |
131 | prev_length = e0.length; | |
132 | e0.length = _sample_count / EnvelopeScaleFactor; | |
133 | ||
134 | // Break off if there are no new samples to compute | |
135 | if (e0.length == prev_length) | |
136 | return; | |
137 | ||
138 | reallocate_envelope(e0); | |
139 | ||
140 | dest_ptr = e0.samples + prev_length; | |
141 | ||
142 | // Iterate through the samples to populate the first level mipmap | |
143 | const float *const end_src_ptr = (float*)_data + | |
144 | e0.length * EnvelopeScaleFactor; | |
145 | for (const float *src_ptr = (float*)_data + | |
146 | prev_length * EnvelopeScaleFactor; | |
147 | src_ptr < end_src_ptr; src_ptr += EnvelopeScaleFactor) | |
148 | { | |
149 | const EnvelopeSample sub_sample = { | |
150 | *min_element(src_ptr, src_ptr + EnvelopeScaleFactor), | |
151 | *max_element(src_ptr, src_ptr + EnvelopeScaleFactor), | |
152 | }; | |
153 | ||
154 | *dest_ptr++ = sub_sample; | |
155 | } | |
156 | ||
157 | // Compute higher level mipmaps | |
158 | for (unsigned int level = 1; level < ScaleStepCount; level++) | |
159 | { | |
160 | Envelope &e = _envelope_levels[level]; | |
161 | const Envelope &el = _envelope_levels[level-1]; | |
162 | ||
163 | // Expand the data buffer to fit the new samples | |
164 | prev_length = e.length; | |
165 | e.length = el.length / EnvelopeScaleFactor; | |
166 | ||
167 | // Break off if there are no more samples to computed | |
168 | if (e.length == prev_length) | |
169 | break; | |
170 | ||
171 | reallocate_envelope(e); | |
172 | ||
173 | // Subsample the level lower level | |
174 | const EnvelopeSample *src_ptr = | |
175 | el.samples + prev_length * EnvelopeScaleFactor; | |
176 | const EnvelopeSample *const end_dest_ptr = e.samples + e.length; | |
177 | for (dest_ptr = e.samples + prev_length; | |
178 | dest_ptr < end_dest_ptr; dest_ptr++) | |
179 | { | |
180 | const EnvelopeSample *const end_src_ptr = | |
181 | src_ptr + EnvelopeScaleFactor; | |
182 | ||
183 | EnvelopeSample sub_sample = *src_ptr++; | |
184 | while (src_ptr < end_src_ptr) | |
185 | { | |
186 | sub_sample.min = min(sub_sample.min, src_ptr->min); | |
187 | sub_sample.max = max(sub_sample.max, src_ptr->max); | |
188 | src_ptr++; | |
189 | } | |
190 | ||
191 | *dest_ptr = sub_sample; | |
192 | } | |
193 | } | |
194 | } | |
195 | ||
1b1ec774 | 196 | } // namespace data |
aba1dd16 | 197 | } // namespace pv |