-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnitCircle.py
292 lines (248 loc) · 11 KB
/
UnitCircle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import numpy as np
from PyQt6 import QtWidgets, QtCore
from PyQt6.QtCore import QPointF
from PyQt6.QtGui import QColor, QIcon
from functools import partial
import pyqtgraph as pg
import functools
class UnitCircle:
def __init__(self, main_window):
# Create lists to store poles and zeros
self.main_window = main_window
self.Poles = []
self.Zeros = []
self.zPlane = self.main_window.ui.zPlane
self.zPlane.setXRange(-1.1, 1.1)
self.zPlane.setYRange(-1.1, 1.1)
self.zPlane.showGrid(x=True, y=True)
self.zPlane.setBackground("transparent")
self.zPlane.setMenuEnabled(False)
self.zeros_button = self.main_window.ui.zerosButton
self.poles_button = self.main_window.ui.polesButton
self.zeros_button.setIcon(QIcon("Icons\circle.png"))
self.poles_button.setIcon(QIcon("Icons\cross.png"))
self.main_window.clear_button.setIcon(QIcon("Icons\clear.png"))
self.zeros_button_pressed = True
self.poles_button_pressed = False
self.poles_conjugates = []
self.zeros_conjugates = []
self.conjugate_checked_for_dragging = False
self.conjugate_of_drag = None
self.clear_mode = self.main_window.ui.Clear_selection.currentText()
# self.main_window.ui.Clear_selection.set()
self.change_color()
self.zeros_button.clicked.connect(self.handle_mode_of_insertion)
self.poles_button.clicked.connect(self.handle_mode_of_insertion)
self.main_window.Clear_selection.currentIndexChanged.connect(
self.handle_clearing_mode)
self.main_window.clear_button.clicked.connect(self.clear)
# Set up the unit circle
self.x, self.y = self.calculate_circle_points()
self.update_plot(self.x, self.y)
self.zPlane.scene().sigMouseClicked.connect(self.handleMouseClick)
def update_z_plane_view(self):
max_value = 1
for item in self.Zeros + self.Poles:
max_value = max(max_value, item.pos().x())
max_value = max(max_value, item.pos().y())
max_value += 0.2
self.zPlane.setXRange(-max_value, max_value)
self.zPlane.setYRange(-max_value, max_value)
def add_pole(self, pos):
curr_pole = self.draw_item(pos, "x", "r")
self.Poles.append(curr_pole)
def add_zero(self, pos):
curr_zero = self.draw_item(pos, "o", "b")
self.Zeros.append(curr_zero)
def remove_item(self, pos, item):
if item == 'pole':
self.Poles.remove(pos)
self.zPlane.removeItem(pos)
elif item == 'zero':
print(pos)
self.Zeros.remove(pos)
self.zPlane.removeItem(pos)
def draw_item(self, pos, symbol, color):
item = pg.TargetItem(
pos=pos,
size=10,
movable=True,
symbol=symbol,
pen=pg.mkPen(color),
)
self.zPlane.addItem(item)
# Connect the sigPositionChanged signal to the update_positions function
item.sigPositionChanged.connect(
lambda ev, item=item: self.update_positions(ev, item))
item.mouseClickEvent = functools.partial(
lambda ev, item=item: self.contextMenuEvent(ev, item))
return item
def change_color(self):
# Update color for zeros_button
zeros_color = QColor(
64, 92, 245) if self.zeros_button_pressed else QColor(0, 0, 0)
self.zeros_button.setStyleSheet(
f'background-color: {zeros_color.name()}; border: 1px solid gray; padding: 6px; border-radius: 6px;font-size: 14px;')
# Update color for poles_button
poles_color = QColor(
64, 92, 245) if self.poles_button_pressed else QColor(0, 0, 0)
self.poles_button.setStyleSheet(
f'background-color: {poles_color.name()}; border: 1px solid gray; padding: 6px; border-radius: 6px;font-size: 14px;')
def handle_mode_of_insertion(self):
source = self.main_window.ui.sender()
if source is self.zeros_button:
self.zeros_button_pressed = True
self.poles_button_pressed = False
self.change_color()
elif source is self.poles_button:
self.poles_button_pressed = True
self.zeros_button_pressed = False
self.change_color()
def handle_clearing_mode(self):
self.clear_mode = self.main_window.ui.sender().currentText()
def clear(self):
if self.clear_mode == "Zeros":
self.clear_items(self.Zeros)
self.Zeros = list()
elif self.clear_mode == "Poles":
self.clear_items(self.Poles)
self.Poles = list()
else:
self.clear_items(self.Poles)
self.clear_items(self.Zeros)
self.Poles = list()
self.Zeros = list()
self.main_window.update_zeros_poles()
self.plotting()
def clear_items(self, list_of_items):
for item in list_of_items:
self.zPlane.removeItem(item)
def handleMouseClick(self, event):
if self.poles_button_pressed or self.zeros_button_pressed:
pos = self.zPlane.plotItem.vb.mapSceneToView(event.scenePos())
if event.button() == QtCore.Qt.MouseButton.LeftButton:
if self.poles_button_pressed:
if self.main_window.Conj_pair.isChecked():
self.add_pole(pos)
conjugate_pos = QPointF(pos.x(), -pos.y())
self.add_pole(conjugate_pos)
self.poles_conjugates.append(
(self.Poles[-1], self.Poles[-2]))
else:
self.add_pole(pos)
if self.zeros_button_pressed:
if self.main_window.Conj_pair.isChecked():
self.add_zero(pos)
conjugate_pos = QPointF(pos.x(), -pos.y())
self.add_zero(conjugate_pos)
self.zeros_conjugates.append(
(self.Zeros[-1], self.Zeros[-2]))
else:
self.add_zero(pos)
self.main_window.update_zeros_poles()
self.plotting()
def update_positions(self, event, item):
if self.main_window.Conj_pair.isChecked():
self.conjugate_of_drag = self.get_conjugate(item)
if self.conjugate_of_drag:
self.conjugate_of_drag.setPos(item.pos().x(), - item.pos().y())
self.main_window.update_zeros_poles()
self.plotting()
def get_conjugate(self, item):
if item in self.Poles:
for conjugates in self.poles_conjugates:
if item in conjugates:
conjugate = conjugates[0] if conjugates[0] != item else conjugates[1]
return conjugate
elif item in self.Zeros:
for conjugates in self.zeros_conjugates:
if item in conjugates:
conjugate = conjugates[0] if conjugates[0] != item else conjugates[1]
return conjugate
return None
def contextMenuEvent(self, event, curr_item):
if event.button() == QtCore.Qt.MouseButton.RightButton:
menu = QtWidgets.QMenu()
for pole_pos in self.Poles:
if pole_pos == curr_item:
action_1 = menu.addAction('Remove Pole')
action_2 = menu.addAction('Swap to Zero')
action_1.triggered.connect(
partial(self.remove_action, pole_pos, 'pole'))
action_2.triggered.connect(
partial(self.swap_action, pole_pos, 'pole'))
for zero_pos in self.Zeros:
if zero_pos == curr_item:
action_1 = menu.addAction('Remove Zero')
action_2 = menu.addAction('Swap to Pole')
action_1.triggered.connect(
partial(self.remove_action, zero_pos, 'zero'))
action_2.triggered.connect(
partial(self.swap_action, zero_pos, 'zero'))
global_pos = self.zPlane.mapToGlobal(
self.zPlane.mapFromScene(event.scenePos()))
action = menu.exec(global_pos)
# If an action was triggered, reset clicked point
if action:
self.main_window.update_zeros_poles()
self.plotting()
def plotting(self):
self.main_window.plot_magnitude_and_phase()
self.main_window.outputSignal.clear()
self.main_window.inputSignal.clear()
if len(self.main_window.signal.data):
self.main_window.plot_input_and_output_signal()
def calculate_circle_points(self, num_points=300):
theta = 2 * np.pi * np.linspace(0, 1, num_points)
x = np.cos(theta)
y = np.sin(theta)
return x, y
def update_plot(self, x, y):
self.zPlane.clear()
pen_color = QColor(255, 255, 255)
line_width = 2
self.zPlane.plot(x, y, pen=pg.mkPen(
color=pen_color, width=line_width))
vLine = pg.InfiniteLine(
pos=0, angle=90, movable=False, pen=(255, 255, 255))
hLine = pg.InfiniteLine(
pos=0, angle=0, movable=False, pen=(255, 255, 255))
self.zPlane.addItem(vLine)
self.zPlane.addItem(hLine)
def swap_action(self, item, identity):
if self.main_window.Conj_pair.isChecked():
conjugate_item = self.get_conjugate(item)
self.swap_item_identity(item, identity)
if conjugate_item:
self.swap_item_identity(conjugate_item, identity)
self.add_swapped_conjugates(identity) # last 2
self.handle_conjugates_lists(item, identity)
else:
self.swap_item_identity(item, identity)
def add_swapped_conjugates(self, identity):
if identity == "pole":
self.zeros_conjugates.append((self.Zeros[-1], self.Zeros[-2]))
else:
self.poles_conjugates.append((self.Poles[-1], self.Poles[-2]))
def remove_action(self, item, identity):
if self.main_window.Conj_pair.isChecked():
conjugate_item = self.get_conjugate(item)
if conjugate_item:
self.remove_item(conjugate_item, identity)
self.remove_item(item, identity)
def swap_item_identity(self, item, identity):
if identity == "zero":
self.remove_item(item, identity)
self.add_pole(item.pos())
else:
self.remove_item(item, identity)
self.add_zero(item.pos())
def handle_conjugates_lists(self, item, identity):
if identity == "pole":
for conjugates in self.poles_conjugates:
if item in conjugates:
self.poles_conjugates.remove(conjugates)
else:
for conjugates in self.zeros_conjugates:
if item in conjugates:
self.zeros_conjugates.remove(conjugates)