-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
163 lines (146 loc) · 5.73 KB
/
Copy pathtest.py
File metadata and controls
163 lines (146 loc) · 5.73 KB
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
import numpy as np
from PySide6.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QFileDialog, QMessageBox, QDialog, \
QLabel, QLineEdit, QHBoxLayout, QDialogButtonBox
from Filter import Filter
from PlotsWidget import FilterPlotsWidget
from ZPlaneWidget import ZPlaneWidget
from ElementsListWidget import ElementsListWidget
from FilterVisualizer import FilterVisualizer
from FilterCodeGenerator import FilterCodeGenerator
from AllPassFilter import AllPassFiltersListWidget
class FilterExportWidget(QWidget):
def __init__(self, filter):
super().__init__()
self.filter_realizer = FilterVisualizer(filter)
self.code_generator = FilterCodeGenerator(filter)
self.filter = filter
self.cascade_button = QPushButton("Show Cascade Form")
self.direct_form_ii_button = QPushButton("Show Direct Form II")
self.c_code_button = QPushButton("Generate C Code")
# self.add_all_pass_button = QPushButton("Add All-Pass Filter")
self.cascade_button.clicked.connect(self.show_cascade)
self.direct_form_ii_button.clicked.connect(self.show_direct_form_ii)
self.c_code_button.clicked.connect(self.generate_c_code)
# self.add_all_pass_button.clicked.connect(self.show_all_pass_dialog)
layout = QVBoxLayout()
layout.addWidget(self.cascade_button)
layout.addWidget(self.direct_form_ii_button)
layout.addWidget(self.c_code_button)
# layout.addWidget(self.add_all_pass_button)
self.setLayout(layout)
def show_cascade(self):
try:
self.filter_realizer.draw_cascade_form()
except ValueError as e:
response = QMessageBox.question(
self,
"Filter Not Realizable",
f"{str(e)}\n\nDo you want to auto-realize the filter?",
QMessageBox.Yes | QMessageBox.No
)
if response == QMessageBox.Yes:
self.filter.auto_realize_filter()
self.filter_realizer.draw_cascade_form()
else:
return
self.filter_realizer.show()
def show_direct_form_ii(self):
try:
self.filter_realizer.draw_direct_form_2()
except ValueError as e:
response = QMessageBox.question(
self,
"Filter Not Realizable",
f"{str(e)}\n\nDo you want to auto-realize the filter?",
QMessageBox.Yes | QMessageBox.No
)
if response == QMessageBox.Yes:
self.filter.auto_realize_filter()
self.filter_realizer.draw_direct_form_2()
else:
return
self.filter_realizer.show()
def generate_c_code(self):
file_path, _ = QFileDialog.getSaveFileName(
self,
"Save C Code",
"",
"C Source Files (*.c);;All Files (*.*)"
)
if file_path:
try:
header_path, source_path = self.code_generator.export_c_code(file_path)
except ValueError as e:
response = QMessageBox.question(
self,
"Filter Not Realizable",
f"{str(e)}\n\nDo you want to auto-realize the filter?",
QMessageBox.Yes | QMessageBox.No
)
if response == QMessageBox.Yes:
self.filter.auto_realize_filter()
header_path, source_path = self.code_generator.export_c_code(file_path)
else:
return
except Exception as e:
# Show error message if something goes wrong
QMessageBox.critical(
self,
"Error",
f"Failed to generate code: {str(e)}"
)
return
# Show success message
QMessageBox.information(
self,
"Success",
f"Filter code generated successfully!\n\n"
f"Header file: {header_path}\n"
f"Source file: {source_path}"
)
# def show_all_pass_dialog(self):
# dialog = AllPassFilterWidget(self)
# if dialog.exec():
# coefficient = dialog.get_coefficient()
# angle = dialog.get_angle()
# if coefficient is None or angle is None:
# QMessageBox.critical(self, "Error", "Invalid coefficient value or angle!")
# return
#
# # Calculate the zero and pole
# zero = 1 / coefficient
# pole = coefficient
#
# # Adjust the zero and pole positions using the angle
# angle_rad = np.deg2rad(angle)
# zero = zero * np.exp(1j * angle_rad)
# pole = pole * np.exp(1j * angle_rad)
#
# # Add to filter
# self.filter.add_zero(zero)
# self.filter.add_pole(pole)
#
# # Update visualizations
# self.filter.notify_subscribers(self)
# QMessageBox.information(self, "Success", "All-pass filter added successfully!")
# return
if __name__ == '__main__':
app = QApplication()
# Create filter and widgets
filter = Filter()
zplane = ZPlaneWidget()
plots = FilterPlotsWidget()
elements_list = ElementsListWidget()
realizer_widget = FilterExportWidget(filter)
all_pass_filters_widget = AllPassFiltersListWidget()
zplane.set_filter(filter)
plots.set_filter(filter)
elements_list.set_filter(filter)
all_pass_filters_widget.set_filter(filter)
# Show widgets
zplane.show()
plots.show()
elements_list.show()
realizer_widget.show()
all_pass_filters_widget.show()
app.exec()