ngscopeclient 0.1-dev+51fbda87c
SParameters.h
Go to the documentation of this file.
1/***********************************************************************************************************************
2* *
3* libscopehal v0.1 *
4* *
5* Copyright (c) 2012-2022 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 SParameters_h
36#define SParameters_h
37
38#include <complex>
39
44{
45public:
47 {}
48
49 SParameterPoint(float f, float a, float p)
50 : m_frequency(f)
51 , m_amplitude(a)
52 , m_phase(p)
53 {
54 }
55
56 SParameterPoint(float f, std::complex<float> c)
57 : m_frequency(f)
58 , m_amplitude(abs(c))
59 , m_phase(arg(c))
60 {
61 }
62
63 float m_frequency; //Hz
64 float m_amplitude; //magnitude
65 float m_phase; //radians from -pi to +pi
66
67 std::complex<float> ToComplex()
68 { return std::polar(m_amplitude, m_phase); }
69};
70
75{
76public:
78 {}
79
83 SParameterVector(const WaveformBase* wmag, const WaveformBase* wang)
84 {
85 auto umag = dynamic_cast<const UniformAnalogWaveform*>(wmag);
86 auto smag = dynamic_cast<const SparseAnalogWaveform*>(wmag);
87
88 auto uang = dynamic_cast<const UniformAnalogWaveform*>(wang);
89 auto sang = dynamic_cast<const SparseAnalogWaveform*>(wang);
90
91 if(umag && uang)
92 ConvertFromWaveforms(umag, uang);
93 else
94 ConvertFromWaveforms(smag, sang);
95 }
96
101 {
102 ConvertFromWaveforms(wmag, wang);
103 }
104
109 {
110 ConvertFromWaveforms(wmag, wang);
111 }
112
118 template<class T>
119 __attribute__((noinline))
120 void ConvertFromWaveforms(const T* wmag, const T* wang)
121 {
122 if( (wmag == nullptr) || (wang == nullptr) )
123 {
124 LogError("Null input supplied to SParameterVector::ConvertFromWaveforms\n");
125 return;
126 }
127
128 size_t len = std::min(wmag->size(), wang->size());
129 m_points.resize(len);
130 m_points.PrepareForCpuAccess();
131
132 float ascale = M_PI / 180;
133 for(size_t i=0; i<len; i++)
134 {
135 m_points[i] = SParameterPoint(
136 GetOffsetScaled(wmag, i),
137 pow(10, wmag->m_samples[i] / 20),
138 wang->m_samples[i] * ascale);
139 }
140
141 m_points.MarkModifiedFromCpu();
142 }
143
149 template<class T>
150 __attribute__((noinline))
151 void ZeroFromWaveforms(const T* wmag, const T* wang)
152 {
153 if( (wmag == nullptr) || (wang == nullptr) )
154 {
155 LogError("Null input supplied to SParameterVector::ZeroFromWaveforms\n");
156 return;
157 }
158
159 size_t len = std::min(wmag->size(), wang->size());
160 m_points.resize(len);
161 m_points.PrepareForCpuAccess();
162
163 for(size_t i=0; i<len; i++)
164 m_points[i] = SParameterPoint(GetOffsetScaled(wmag, i), 0, 0);
165
166 m_points.MarkModifiedFromCpu();
167 }
168
170
171 SParameterPoint InterpolatePoint(float frequency) const;
172 float InterpolateMagnitude(float frequency) const;
173 float InterpolateAngle(float frequency) const;
174
176
177 void resize(size_t nsize)
178 { m_points.resize(nsize); }
179
180 float GetGroupDelay(size_t bin) const;
181
182 size_t size() const
183 { return m_points.size(); }
184
185 SParameterPoint& operator[](size_t i)
186 { return m_points[i]; }
187
188 void clear()
189 { m_points.clear(); }
190
191protected:
192 float InterpolatePhase(float phase_lo, float phase_hi, float frac) const;
193};
194
195typedef std::pair<int, int> SPair;
196
201{
202public:
203 SParameters();
204 virtual ~SParameters();
205
206 void Clear();
207 void Allocate(int nports = 2);
208
209 bool empty() const
210 { return m_params.empty(); }
211
215 SParameterPoint SamplePoint(int to, int from, float frequency)
216 { return m_params[ SPair(to, from) ]->InterpolatePoint(frequency); }
217
218 SParameterVector& operator[] (SPair pair)
219 { return *m_params[pair]; }
220
221 const SParameterVector& operator[] (SPair pair) const
222 { return *(m_params.find(pair)->second); }
223
224 friend class TouchstoneParser;
225
226 enum FreqUnit
227 {
228 FREQ_HZ,
229 FREQ_KHZ,
230 FREQ_MHZ,
231 FREQ_GHZ
232 };
233
234 enum ParameterFormat
235 {
236 FORMAT_MAG_ANGLE,
237 FORMAT_DBMAG_ANGLE,
238 FORMAT_REAL_IMAGINARY
239 };
240
241 void SaveToFile(const std::string& path, ParameterFormat format = FORMAT_MAG_ANGLE, FreqUnit freqUnit = FREQ_GHZ);
242
243 size_t GetNumPorts() const
244 { return m_nports; }
245
246protected:
247 std::map< SPair , SParameterVector*> m_params;
248
249 size_t m_nports;
250};
251
252#endif
void MarkModifiedFromCpu()
Marks the CPU-side copy of the buffer as modified.
Definition: AcceleratorBuffer.h:852
void PrepareForCpuAccess()
Prepares the buffer to be accessed from the CPU.
Definition: AcceleratorBuffer.h:877
void resize(size_t size)
Change the usable size of the container.
Definition: AcceleratorBuffer.h:457
void clear()
Resize the container to be empty (but don't free memory)
Definition: AcceleratorBuffer.h:478
A single point in an S-parameter dataset.
Definition: SParameters.h:44
A single S-parameter array.
Definition: SParameters.h:75
float InterpolatePhase(float phase_lo, float phase_hi, float frac) const
Interpolates a phase angle, wrapping appropriately.
Definition: SParameters.cpp:172
__attribute__((noinline)) void ConvertFromWaveforms(const T *wmag
Loads the vector from a pair of waveforms in mag/angle format.
SParameterVector(const WaveformBase *wmag, const WaveformBase *wang)
Creates an S-parameter vector from analog waveforms in dB / degree format.
Definition: SParameters.h:83
__attribute__((noinline)) void ZeroFromWaveforms(const T *wmag
Similar to ConvertFromWaveforms() but sets mag/angle values to zero indicating "no data".
void ConvertToWaveforms(SparseAnalogWaveform *wmag, SparseAnalogWaveform *wang)
Copy our state to analog mag/angle waveforms.
Definition: SParameters.cpp:46
SParameterVector(const SparseAnalogWaveform *wmag, const SparseAnalogWaveform *wang)
Creates an S-parameter vector from analog waveforms in dB / degree format.
Definition: SParameters.h:100
float GetGroupDelay(size_t bin) const
Gets the group delay at a given bin.
Definition: SParameters.cpp:206
SParameterVector(const UniformAnalogWaveform *wmag, const UniformAnalogWaveform *wang)
Creates an S-parameter vector from analog waveforms in dB / degree format.
Definition: SParameters.h:108
A set of S-parameters.
Definition: SParameters.h:201
SParameterPoint SamplePoint(int to, int from, float frequency)
Sample a single point from a single S-parameter.
Definition: SParameters.h:215
void Clear()
Clears out current S-parameters before reloading them.
Definition: SParameters.cpp:236
void SaveToFile(const std::string &path, ParameterFormat format=FORMAT_MAG_ANGLE, FreqUnit freqUnit=FREQ_GHZ)
Serializes a S-parameter model to a Touchstone file.
Definition: SParameters.cpp:264
A waveform sampled at irregular intervals.
Definition: Waveform.h:460
Touchstone (SxP) file parser.
Definition: TouchstoneParser.h:45
A waveform sampled at uniform intervals.
Definition: Waveform.h:365
Base class for all Waveform specializations.
Definition: Waveform.h:59
int64_t GetOffsetScaled(T *wfm, size_t i)
Returns the offset of a sample from the start of the waveform, in X axis units.
Definition: Waveform.h:702