ngscopeclient 0.1-dev+51fbda87c
CouplerDeEmbedFilter.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopeprotocols *
4* *
5* Copyright (c) 2012-2024 Andrew D. Zonenberg and contributors *
6* All rights reserved. *
7* *
8* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
9* following conditions are met: *
10* *
11* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
12* following disclaimer. *
13* *
14* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
15* following disclaimer in the documentation and/or other materials provided with the distribution. *
16* *
17* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
18* derived from this software without specific prior written permission. *
19* *
20* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
21* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
22* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
23* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
24* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
26* POSSIBILITY OF SUCH DAMAGE. *
27* *
28***********************************************************************************************************************/
29
35#ifndef CouplerDeEmbedFilter_h
36#define CouplerDeEmbedFilter_h
37
38#include "DeEmbedFilter.h"
39
41{
42public:
44 : m_cachedBinSize(0)
45 , m_groupDelayFs(0)
46 , m_groupDelaySamples(0)
47 {}
48
52 bool NeedUpdate(WaveformBase* wmag, WaveformBase* wang, double bin_hz)
53 {
54 //Check if bin size changed
55 if(fabs(m_cachedBinSize - bin_hz) > FLT_EPSILON)
56 return true;
57
58 if( (m_magKey != wmag) || (m_angleKey != wang) )
59 return true;
60 return false;
61 }
62
66 void Refresh(
67 WaveformBase* wmag,
68 WaveformBase* wang,
69 double bin_hz,
70 bool invert,
71 size_t nouts,
72 float maxGain,
73 int64_t timescale,
74 size_t npoints)
75 {
76 //Update cache keys to reflect the current waveforms we're processing
77 m_magKey = wmag;
78 m_angleKey = wang;
79
80 m_resampledSparamCosines.clear();
81 m_resampledSparamSines.clear();
82 InterpolateSparameters(wmag, wang, bin_hz, invert, nouts, maxGain);
83
84 m_groupDelayFs = GetGroupDelay();
85 m_groupDelaySamples = ceil( m_groupDelayFs / timescale );
86
87 //Sanity check: if we have noisy or poor quality S-parameter data, group delay might not make sense.
88 //Skip this correction pass in that case.
89 if(static_cast<size_t>(llabs(m_groupDelaySamples)) >= npoints)
90 {
91 m_groupDelayFs = 0;
92 m_groupDelaySamples = 0;
93 }
94 }
95
97 WaveformBase* wmag,
98 WaveformBase* wang,
99 float bin_hz,
100 bool invert,
101 size_t nouts,
102 float maxGain);
103
104 virtual int64_t GetGroupDelay();
105
106 AcceleratorBuffer<float> m_resampledSparamSines;
107 AcceleratorBuffer<float> m_resampledSparamCosines;
108
109 WaveformCacheKey m_magKey;
110 WaveformCacheKey m_angleKey;
111
112 SParameterVector m_cachedSparams;
113
114 double m_cachedBinSize;
115
116 int64_t m_groupDelayFs;
117 int64_t m_groupDelaySamples;
118};
119
121{
122public:
123 CouplerDeEmbedFilter(const std::string& color);
124 virtual ~CouplerDeEmbedFilter();
125
126 virtual void Refresh(vk::raii::CommandBuffer& cmdBuf, std::shared_ptr<QueueHandle> queue) override;
127 virtual DataLocation GetInputLocation() override;
128
129 static std::string GetProtocolName();
130
131 virtual bool ValidateChannel(size_t i, StreamDescriptor stream) override;
132
133 PROTOCOL_DECODER_INITPROC(CouplerDeEmbedFilter)
134
135protected:
136
138 vk::raii::CommandBuffer& cmdBuf,
139 std::unique_ptr<VulkanFFTPlan>& plan,
140 AcceleratorBuffer<float>& samplesIn,
141 AcceleratorBuffer<float>& samplesOut,
142 size_t npointsPadded,
143 size_t npointsUnpadded
144 );
145
146 void ApplySParameters(
147 vk::raii::CommandBuffer& cmdBuf,
148 AcceleratorBuffer<float>& samplesIn,
149 AcceleratorBuffer<float>& samplesOut,
150 CouplerSParameters& params,
151 size_t npoints,
152 size_t nouts);
153
155 vk::raii::CommandBuffer& cmdBuf,
156 AcceleratorBuffer<float>& samplesInout,
157 CouplerSParameters& params,
158 size_t npoints,
159 size_t nouts);
160
161 void Subtract(
162 vk::raii::CommandBuffer& cmdBuf,
163 AcceleratorBuffer<float>& samplesP,
164 AcceleratorBuffer<float>& samplesN,
165 AcceleratorBuffer<float>& samplesOut,
166 size_t npoints);
167
168 void SubtractInPlace(
169 vk::raii::CommandBuffer& cmdBuf,
170 AcceleratorBuffer<float>& samplesInout,
171 AcceleratorBuffer<float>& samplesSub,
172 size_t npoints);
173
175 vk::raii::CommandBuffer& cmdBuf,
176 std::unique_ptr<VulkanFFTPlan>& plan,
177 size_t istart,
178 size_t iend,
179 WaveformBase* refin,
180 size_t stream,
181 size_t npoints,
182 int64_t phaseshift,
183 AcceleratorBuffer<float>& samplesIn);
184
186 CouplerSParameters& params,
187 size_t& istart,
188 size_t& iend,
189 int64_t& phaseshift,
190 bool invert);
191
192 std::string m_maxGainName;
193
194 enum TruncationMode
195 {
196 TRUNC_AUTO,
197 TRUNC_MANUAL
198 };
199
200 float m_cachedMaxGain;
201
202 size_t m_cachedNumPoints;
203
204 CouplerSParameters m_forwardCoupledParams;
205 CouplerSParameters m_reverseCoupledParams;
206 CouplerSParameters m_forwardLeakageParams;
207 CouplerSParameters m_reverseLeakageParams;
208
209 AcceleratorBuffer<float> m_scalarTempBuf1;
210 AcceleratorBuffer<float> m_vectorTempBuf1;
211 AcceleratorBuffer<float> m_vectorTempBuf2;
212 AcceleratorBuffer<float> m_vectorTempBuf3;
213 AcceleratorBuffer<float> m_vectorTempBuf4;
214
215 ComputePipeline m_rectangularComputePipeline;
216 ComputePipeline m_deEmbedComputePipeline;
217 ComputePipeline m_deEmbedInPlaceComputePipeline;
218 ComputePipeline m_normalizeComputePipeline;
219 ComputePipeline m_subtractInPlaceComputePipeline;
220 ComputePipeline m_subtractComputePipeline;
221
222 std::unique_ptr<VulkanFFTPlan> m_vkForwardPlan;
223 std::unique_ptr<VulkanFFTPlan> m_vkForwardPlan2;
224
225 std::unique_ptr<VulkanFFTPlan> m_vkReversePlan;
226};
227
228#endif
Declaration of DeEmbedFilter.
void clear()
Resize the container to be empty (but don't free memory)
Definition: AcceleratorBuffer.h:478
Encapsulates a Vulkan compute pipeline and all necessary resources to use it.
Definition: ComputePipeline.h:55
Definition: CouplerDeEmbedFilter.h:121
void ApplySParameters(vk::raii::CommandBuffer &cmdBuf, AcceleratorBuffer< float > &samplesIn, AcceleratorBuffer< float > &samplesOut, CouplerSParameters &params, size_t npoints, size_t nouts)
Apply a set of processed S-parameters (either forward or inverse channel response)
Definition: CouplerDeEmbedFilter.cpp:430
void ApplySParametersInPlace(vk::raii::CommandBuffer &cmdBuf, AcceleratorBuffer< float > &samplesInout, CouplerSParameters &params, size_t npoints, size_t nouts)
Apply a set of processed S-parameters (either forward or inverse channel response)
Definition: CouplerDeEmbedFilter.cpp:451
void GroupDelayCorrection(CouplerSParameters &params, size_t &istart, size_t &iend, int64_t &phaseshift, bool invert)
Calculate bounds for the meaningful output data. Since we're phase shifting, there's gonna be some ga...
Definition: CouplerDeEmbedFilter.cpp:365
virtual DataLocation GetInputLocation() override
Gets the desired location of the nodes's input data.
Definition: CouplerDeEmbedFilter.cpp:135
void GenerateScalarOutput(vk::raii::CommandBuffer &cmdBuf, std::unique_ptr< VulkanFFTPlan > &plan, size_t istart, size_t iend, WaveformBase *refin, size_t stream, size_t npoints, int64_t phaseshift, AcceleratorBuffer< float > &samplesIn)
Generates a scalar output from a complex input.
Definition: CouplerDeEmbedFilter.cpp:389
void SubtractInPlace(vk::raii::CommandBuffer &cmdBuf, AcceleratorBuffer< float > &samplesInout, AcceleratorBuffer< float > &samplesSub, size_t npoints)
Subtract one signal from another and overwrite the first.
Definition: CouplerDeEmbedFilter.cpp:328
void Subtract(vk::raii::CommandBuffer &cmdBuf, AcceleratorBuffer< float > &samplesP, AcceleratorBuffer< float > &samplesN, AcceleratorBuffer< float > &samplesOut, size_t npoints)
Subtract one signal from another and overwrite the first.
Definition: CouplerDeEmbedFilter.cpp:345
void ProcessScalarInput(vk::raii::CommandBuffer &cmdBuf, std::unique_ptr< VulkanFFTPlan > &plan, AcceleratorBuffer< float > &samplesIn, AcceleratorBuffer< float > &samplesOut, size_t npointsPadded, size_t npointsUnpadded)
Zero-pad a scalar input to the proper length and FFT it.
Definition: CouplerDeEmbedFilter.cpp:472
Definition: CouplerDeEmbedFilter.h:41
void Refresh(WaveformBase *wmag, WaveformBase *wang, double bin_hz, bool invert, size_t nouts, float maxGain, int64_t timescale, size_t npoints)
Refresh the cached data.
Definition: CouplerDeEmbedFilter.h:66
virtual int64_t GetGroupDelay()
Returns the max mid-band group delay of the channel.
Definition: CouplerDeEmbedFilter.cpp:506
bool NeedUpdate(WaveformBase *wmag, WaveformBase *wang, double bin_hz)
Check to see if we need to refresh our cache.
Definition: CouplerDeEmbedFilter.h:52
void InterpolateSparameters(WaveformBase *wmag, WaveformBase *wang, float bin_hz, bool invert, size_t nouts, float maxGain)
Recalculate the cached S-parameters (and clamp gain if requested)
Definition: CouplerDeEmbedFilter.cpp:524
Abstract base class for all filter graph blocks which are not physical instrument channels.
Definition: Filter.h:95
virtual void Refresh() override
Evaluates a filter graph node.
Definition: Filter.cpp:816
A single S-parameter array.
Definition: SParameters.h:75
Descriptor for a single stream coming off a channel.
Definition: StreamDescriptor.h:46
Base class for all Waveform specializations.
Definition: Waveform.h:59
Describes a particular revision of a waveform.
Definition: Filter.h:52