]>
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 | ||
fda5b6e0 JH |
88 | void AnalogSnapshot::reallocate_envelope(Envelope &e) |
89 | { | |
90 | const uint64_t new_data_length = ((e.length + EnvelopeDataUnit - 1) / | |
91 | EnvelopeDataUnit) * EnvelopeDataUnit; | |
92 | if (new_data_length > e.data_length) | |
93 | { | |
94 | e.data_length = new_data_length; | |
95 | e.samples = (EnvelopeSample*)realloc(e.samples, | |
96 | new_data_length * sizeof(EnvelopeSample)); | |
97 | } | |
98 | } | |
99 | ||
100 | void AnalogSnapshot::append_payload_to_envelope_levels() | |
101 | { | |
102 | Envelope &e0 = _envelope_levels[0]; | |
103 | uint64_t prev_length; | |
104 | EnvelopeSample *dest_ptr; | |
105 | ||
106 | // Expand the data buffer to fit the new samples | |
107 | prev_length = e0.length; | |
108 | e0.length = _sample_count / EnvelopeScaleFactor; | |
109 | ||
110 | // Break off if there are no new samples to compute | |
111 | if (e0.length == prev_length) | |
112 | return; | |
113 | ||
114 | reallocate_envelope(e0); | |
115 | ||
116 | dest_ptr = e0.samples + prev_length; | |
117 | ||
118 | // Iterate through the samples to populate the first level mipmap | |
119 | const float *const end_src_ptr = (float*)_data + | |
120 | e0.length * EnvelopeScaleFactor; | |
121 | for (const float *src_ptr = (float*)_data + | |
122 | prev_length * EnvelopeScaleFactor; | |
123 | src_ptr < end_src_ptr; src_ptr += EnvelopeScaleFactor) | |
124 | { | |
125 | const EnvelopeSample sub_sample = { | |
126 | *min_element(src_ptr, src_ptr + EnvelopeScaleFactor), | |
127 | *max_element(src_ptr, src_ptr + EnvelopeScaleFactor), | |
128 | }; | |
129 | ||
130 | *dest_ptr++ = sub_sample; | |
131 | } | |
132 | ||
133 | // Compute higher level mipmaps | |
134 | for (unsigned int level = 1; level < ScaleStepCount; level++) | |
135 | { | |
136 | Envelope &e = _envelope_levels[level]; | |
137 | const Envelope &el = _envelope_levels[level-1]; | |
138 | ||
139 | // Expand the data buffer to fit the new samples | |
140 | prev_length = e.length; | |
141 | e.length = el.length / EnvelopeScaleFactor; | |
142 | ||
143 | // Break off if there are no more samples to computed | |
144 | if (e.length == prev_length) | |
145 | break; | |
146 | ||
147 | reallocate_envelope(e); | |
148 | ||
149 | // Subsample the level lower level | |
150 | const EnvelopeSample *src_ptr = | |
151 | el.samples + prev_length * EnvelopeScaleFactor; | |
152 | const EnvelopeSample *const end_dest_ptr = e.samples + e.length; | |
153 | for (dest_ptr = e.samples + prev_length; | |
154 | dest_ptr < end_dest_ptr; dest_ptr++) | |
155 | { | |
156 | const EnvelopeSample *const end_src_ptr = | |
157 | src_ptr + EnvelopeScaleFactor; | |
158 | ||
159 | EnvelopeSample sub_sample = *src_ptr++; | |
160 | while (src_ptr < end_src_ptr) | |
161 | { | |
162 | sub_sample.min = min(sub_sample.min, src_ptr->min); | |
163 | sub_sample.max = max(sub_sample.max, src_ptr->max); | |
164 | src_ptr++; | |
165 | } | |
166 | ||
167 | *dest_ptr = sub_sample; | |
168 | } | |
169 | } | |
170 | } | |
171 | ||
1b1ec774 | 172 | } // namespace data |
aba1dd16 | 173 | } // namespace pv |