Skip to content

Commit abb8795

Browse files
committed
Working polarization demo
1 parent fc39209 commit abb8795

3 files changed

Lines changed: 189 additions & 41 deletions

File tree

‎demos/polarization/polarization-ui.qml‎

Lines changed: 149 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -98,26 +98,32 @@ Common.ESPARGOSApplication {
9898
Layout.fillHeight: true
9999
spacing: 20
100100

101-
// Combined constellation diagram for L and R antenna feeds
102-
Item {
103-
id: chartFeedsContainer
104-
Layout.fillWidth: true
101+
// Left third: constellation diagrams stacked vertically
102+
ColumnLayout {
105103
Layout.fillHeight: true
106-
Layout.minimumWidth: 300
107-
Layout.minimumHeight: 300
108-
109-
ChartView {
110-
id: chartFeeds
111-
title: "Antenna Feeds (L/R)"
112-
titleColor: textColor
113-
width: Math.min(parent.width, parent.height)
114-
height: Math.min(parent.width, parent.height)
115-
anchors.centerIn: parent
116-
antialiasing: true
117-
backgroundColor: "#1a252e"
118-
legend.visible: true
119-
legend.labelColor: textColor
120-
legend.alignment: Qt.AlignBottom
104+
Layout.preferredWidth: parent.width / 3
105+
Layout.maximumWidth: parent.width / 3
106+
spacing: 10
107+
108+
// Combined constellation diagram for L and R antenna feeds
109+
Item {
110+
id: chartFeedsContainer
111+
Layout.fillWidth: true
112+
Layout.fillHeight: true
113+
Layout.minimumHeight: 200
114+
115+
ChartView {
116+
id: chartFeeds
117+
title: "Antenna Feeds (L/R)"
118+
titleColor: textColor
119+
width: Math.min(parent.width, parent.height)
120+
height: Math.min(parent.width, parent.height)
121+
anchors.centerIn: parent
122+
antialiasing: true
123+
backgroundColor: "#1a252e"
124+
legend.visible: true
125+
legend.labelColor: textColor
126+
legend.alignment: Qt.AlignBottom
121127

122128
ValueAxis {
123129
id: axisXFeeds
@@ -167,28 +173,27 @@ Common.ESPARGOSApplication {
167173
borderWidth: 1
168174
}
169175
}
170-
}
176+
}
171177

172-
// Combined constellation diagram for H and V (linear polarization)
173-
Item {
174-
id: chartLinearContainer
175-
Layout.fillWidth: true
176-
Layout.fillHeight: true
177-
Layout.minimumWidth: 300
178-
Layout.minimumHeight: 300
179-
180-
ChartView {
181-
id: chartLinear
182-
title: "Linear Polarization (H/V)"
183-
titleColor: textColor
184-
width: Math.min(parent.width, parent.height)
185-
height: Math.min(parent.width, parent.height)
186-
anchors.centerIn: parent
187-
antialiasing: true
188-
backgroundColor: "#1a252e"
189-
legend.visible: true
190-
legend.labelColor: textColor
191-
legend.alignment: Qt.AlignBottom
178+
// Combined constellation diagram for H and V (linear polarization)
179+
Item {
180+
id: chartLinearContainer
181+
Layout.fillWidth: true
182+
Layout.fillHeight: true
183+
Layout.minimumHeight: 200
184+
185+
ChartView {
186+
id: chartLinear
187+
title: "Linear Polarization (H/V)"
188+
titleColor: textColor
189+
width: Math.min(parent.width, parent.height)
190+
height: Math.min(parent.width, parent.height)
191+
anchors.centerIn: parent
192+
antialiasing: true
193+
backgroundColor: "#1a252e"
194+
legend.visible: true
195+
legend.labelColor: textColor
196+
legend.alignment: Qt.AlignBottom
192197

193198
ValueAxis {
194199
id: axisXLinear
@@ -237,6 +242,104 @@ Common.ESPARGOSApplication {
237242
borderColor: "#ff9896"
238243
borderWidth: 1
239244
}
245+
}
246+
}
247+
}
248+
249+
// Right two-thirds: polarization ellipse visualization
250+
Item {
251+
id: circleContainer
252+
Layout.fillHeight: true
253+
Layout.fillWidth: true
254+
Layout.preferredWidth: parent.width * 2 / 3
255+
256+
// Store ellipse points and rotation direction from backend
257+
property var ellipsePoints: []
258+
property int rotationDirection: 0 // 1=CCW, -1=CW, 0=linear
259+
260+
Canvas {
261+
id: polarizationCanvas
262+
width: Math.min(parent.width, parent.height) * 0.95
263+
height: width
264+
anchors.centerIn: parent
265+
266+
onPaint: {
267+
var ctx = getContext("2d")
268+
ctx.reset()
269+
270+
var centerX = width / 2
271+
var centerY = height / 2
272+
var scale = width / 2 * 0.9 // Leave some margin
273+
274+
// Draw reference circle (unit circle)
275+
ctx.strokeStyle = "#444444"
276+
ctx.lineWidth = 1
277+
ctx.beginPath()
278+
ctx.arc(centerX, centerY, scale, 0, 2 * Math.PI)
279+
ctx.stroke()
280+
281+
// Draw axes
282+
ctx.strokeStyle = "#666666"
283+
ctx.lineWidth = 1
284+
ctx.beginPath()
285+
ctx.moveTo(centerX - scale, centerY)
286+
ctx.lineTo(centerX + scale, centerY)
287+
ctx.moveTo(centerX, centerY - scale)
288+
ctx.lineTo(centerX, centerY + scale)
289+
ctx.stroke()
290+
291+
// Draw polarization ellipse
292+
var points = circleContainer.ellipsePoints
293+
var rotDir = circleContainer.rotationDirection
294+
if (points.length > 0) {
295+
ctx.strokeStyle = "#ffffff"
296+
ctx.lineWidth = 3
297+
ctx.beginPath()
298+
ctx.moveTo(centerX + points[0][0] * scale, centerY - points[0][1] * scale)
299+
for (var i = 1; i < points.length; i++) {
300+
ctx.lineTo(centerX + points[i][0] * scale, centerY - points[i][1] * scale)
301+
}
302+
ctx.closePath()
303+
ctx.stroke()
304+
305+
// Draw arrow to indicate rotation direction (if not linear)
306+
if (rotDir !== 0 && points.length > 10) {
307+
// Pick a point along the ellipse (around 25% of the way)
308+
var arrowIdx = Math.floor(points.length / 4)
309+
var arrowX = centerX + points[arrowIdx][0] * scale
310+
var arrowY = centerY - points[arrowIdx][1] * scale
311+
312+
// Compute tangent direction from adjacent points
313+
var prevIdx = arrowIdx - 1
314+
var nextIdx = arrowIdx + 1
315+
var dx = (points[nextIdx][0] - points[prevIdx][0]) * scale
316+
var dy = -(points[nextIdx][1] - points[prevIdx][1]) * scale // Flip Y
317+
318+
// Normalize tangent
319+
var tangentLen = Math.sqrt(dx * dx + dy * dy)
320+
if (tangentLen > 0) {
321+
dx = dx / tangentLen
322+
dy = dy / tangentLen
323+
}
324+
325+
// Arrow size
326+
var arrowSize = 15
327+
328+
// Perpendicular to tangent
329+
var perpX = -dy
330+
var perpY = dx
331+
332+
// Draw arrowhead
333+
ctx.fillStyle = "#ffffff"
334+
ctx.beginPath()
335+
ctx.moveTo(arrowX + dx * arrowSize, arrowY + dy * arrowSize)
336+
ctx.lineTo(arrowX - dx * arrowSize * 0.5 + perpX * arrowSize * 0.5, arrowY - dy * arrowSize * 0.5 + perpY * arrowSize * 0.5)
337+
ctx.lineTo(arrowX - dx * arrowSize * 0.5 - perpX * arrowSize * 0.5, arrowY - dy * arrowSize * 0.5 - perpY * arrowSize * 0.5)
338+
ctx.closePath()
339+
ctx.fill()
340+
}
341+
}
342+
}
240343
}
241344
}
242345
}
@@ -288,6 +391,12 @@ Common.ESPARGOSApplication {
288391
scatterV.append(linearVPoints[l][0], linearVPoints[l][1])
289392
}
290393
}
394+
395+
function onUpdatePolarizationEllipse(ellipsePoints, rotationDirection) {
396+
circleContainer.ellipsePoints = ellipsePoints
397+
circleContainer.rotationDirection = rotationDirection
398+
polarizationCanvas.requestPaint()
399+
}
291400
}
292401
}
293402
}

‎demos/polarization/polarization.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ class EspargosDemoPolarization(BacklogMixin, SingleCSIFormatMixin, ESPARGOSAppli
2121
# Each points list contains [[i1, q1], [i2, q2], ...]
2222
updateConstellation = PyQt6.QtCore.pyqtSignal(list, list, list, list, float)
2323

24+
# Signal to update polarization ellipse: list of [x, y] points tracing the ellipse, and rotation direction (1=CCW/LHCP, -1=CW/RHCP, 0=linear)
25+
updatePolarizationEllipse = PyQt6.QtCore.pyqtSignal(list, int)
26+
2427
DEFAULT_CONFIG = {
2528
"crosspol_compensation": True,
2629
"show_mean": True,
@@ -162,6 +165,42 @@ def update(self):
162165
# Emit constellation data to QML
163166
self.updateConstellation.emit(feed_L_list, feed_R_list, linear_H_list, linear_V_list, float(self._smoothed_axis_scale))
164167

168+
# Compute mean H and V components of polarization for displaying polarization as ellipse
169+
mean_H = np.mean(linear_H_points)
170+
mean_V = np.mean(linear_V_points)
171+
172+
# Compute polarization ellipse by sampling the parametric curve:
173+
# x(t) = Re(mean_H * e^(i*t)), y(t) = Re(mean_V * e^(i*t))
174+
# This traces out the polarization ellipse as t goes from 0 to 2*pi
175+
# Negate t to fix the rotation direction (match physical antenna rotation)
176+
t = np.linspace(0, 2 * np.pi, 100)
177+
ellipse_x = np.real(mean_H * np.exp(1j * t))
178+
ellipse_y = np.real(mean_V * np.exp(1j * t))
179+
180+
# Normalize by the maximum radius of the ellipse (semi-major axis)
181+
# This ensures the largest dimension always matches the reference circle
182+
ellipse_radius = np.sqrt(ellipse_x**2 + ellipse_y**2)
183+
max_radius = np.max(ellipse_radius)
184+
if max_radius > 0:
185+
ellipse_x = ellipse_x / max_radius
186+
ellipse_y = ellipse_y / max_radius
187+
188+
# Determine rotation direction (handedness) from the phase relationship
189+
# Im(mean_H * conj(mean_V)) > 0 means V leads H by 90° (LHCP/CCW when looking at source)
190+
# Im(mean_H * conj(mean_V)) < 0 means H leads V by 90° (RHCP/CW when looking at source)
191+
cross_product = np.imag(mean_H * np.conj(mean_V))
192+
threshold = 0.1 * np.abs(mean_H) * np.abs(mean_V) # Threshold for considering it linear
193+
if cross_product > threshold:
194+
rotation_direction = 1 # CCW / LHCP
195+
elif cross_product < -threshold:
196+
rotation_direction = -1 # CW / RHCP
197+
else:
198+
rotation_direction = 0 # Linear (no rotation)
199+
200+
# Convert to list of [x, y] points for QML
201+
ellipse_points = [[float(x), float(y)] for x, y in zip(ellipse_x, ellipse_y)]
202+
self.updatePolarizationEllipse.emit(ellipse_points, int(rotation_direction))
203+
165204

166205
app = EspargosDemoPolarization(sys.argv)
167206
sys.exit(app.exec())

‎espargos/constants.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
WIFI_SUBCARRIER_SPACING = 312.5e3
2727
"Subcarrier spacing of WiFi (in Hz)"
2828

29-
ANTENNA_JONES_MATRIX_SIMPLE = np.sqrt(2) / 2 * np.asarray([[1, -1], [1, 1]])
29+
ANTENNA_JONES_MATRIX_SIMPLE = np.sqrt(2) / 2 * np.asarray([[-1, 1], [1, 1]])
3030
"Simple Jones matrix to convert from linear (H/V) to feed (R/L) polarization basis"
3131

3232
ANTENNA_JONES_CROSSPOL_MATRIX = np.asarray([[0.33, 0.05 + 0.05j], [0.05 + 0.05j, 0.33]])

0 commit comments

Comments
 (0)