-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTimeDomainGraphs.py
More file actions
75 lines (59 loc) · 3.4 KB
/
Copy pathTimeDomainGraphs.py
File metadata and controls
75 lines (59 loc) · 3.4 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
from PySide6 import QtCore
import pyqtgraph as pg
import numpy as np
class TimeDomainGraphs:
def __init__(self):
# Set up three PyQtGraph plot widgets
self.signal_plot = pg.PlotWidget()
self.reconstruction_plot = pg.PlotWidget()
self.difference_plot = pg.PlotWidget()
self.original_pen = pg.mkPen(color='b', width=2)
self.reconstruction_pen = pg.mkPen(color='g', width=2)
self.difference_pen = pg.mkPen(color='r', width=2, style=QtCore.Qt.DashLine)
# Set titles and labels
self.signal_plot.setTitle("Signal Plot")
self.signal_plot.setLabel("bottom", "Time", units="s")
self.signal_plot.setLabel("left", "Amplitude")
self.reconstruction_plot.setTitle("Reconstruction Plot")
self.reconstruction_plot.setLabel("bottom", "Time", units="s")
self.reconstruction_plot.setLabel("left", "Amplitude")
self.difference_plot.setTitle("Difference Plot")
self.difference_plot.setLabel("bottom", "Time", units="s")
self.difference_plot.setLabel("left", "Amplitude")
self.difference_plot_legend = pg.LegendItem(offset=(-10, 2))
self.difference_plot_legend.setParentItem(self.difference_plot.plotItem)
# Link the plots for synchronized panning and zooming
self.reconstruction_plot.setXLink(self.signal_plot)
self.reconstruction_plot.setYLink(self.signal_plot)
self.difference_plot.setXLink(self.signal_plot)
def draw_signal(self, linspace, data_points):
"""Draws a continuous signal in the signal plot."""
self.signal_plot.clear()
self.signal_plot.plot(linspace, data_points, pen=self.original_pen, name='Signal')
def draw_samples(self, linspace, sampled_data):
"""Draws samples as 'x' markers in the signal plot."""
self.signal_plot.plot(linspace, sampled_data, pen=None, symbol='x', symbolSize=10, symbolBrush='r', name='Samples')
def draw_reconstruction(self, linspace, reconstruction_data):
"""Draws the reconstructed signal in the reconstruction plot."""
self.reconstruction_plot.clear()
self.reconstruction_plot.plot(linspace, reconstruction_data, pen=self.reconstruction_pen, name='Reconstructed Signal')
def draw_difference(self, linspace, signal_data1, signal_data2):
"""Draws the difference between two signals in the difference plot."""
if len(signal_data1) != len(signal_data2):
print("Error: Signal lengths do not match.")
return
difference = signal_data1 - signal_data2
self.difference_plot.clear()
self.difference_plot_legend.clear()
original_signal = pg.PlotDataItem(linspace, signal_data1, pen=self.original_pen)
reconstructed_signal = pg.PlotDataItem(linspace, signal_data2, pen=self.reconstruction_pen)
root_mean_squared_error = np.sqrt(np.mean(difference ** 2))
rmse_text = f"RMSE: {root_mean_squared_error:.4f}"
rmse_text_item = pg.TextItem(rmse_text, color=(200, 50, 50), anchor=(0, 1))
rmse_text_item.setPos(linspace[0], np.mean(difference))
self.difference_plot.addItem(rmse_text_item)
difference = pg.PlotDataItem(linspace, difference, pen=self.difference_pen)
self.difference_plot.addItem(difference)
self.difference_plot_legend.addItem(difference, "Difference")
# Set y-axis range from -1 to 1
self.difference_plot.setYRange(-2, 2)