antitheft159 commited on
Commit
786a869
1 Parent(s): 652b97b

Create 1699.py

Browse files
Files changed (1) hide show
  1. 1699.py +109 -0
1699.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ from matplotlib.animation import FuncAnimation
6
+ from IPython.display import clear_output
7
+ import seaborn as sns
8
+
9
+ class WaveformVisualizer:
10
+ def __init__(self, processor, input_data, sampling_rate=1000):
11
+ self.processor = processor
12
+ self.input_data = input_data
13
+ self.sampling_rate = sampling_rate
14
+ self.time = np.arange(input_data.shape[1]) / sampling_rate
15
+
16
+ def plot_waveforms(self):
17
+ processed_data = self.processor(self.input_data)
18
+
19
+ fig = plt.figure(figsize(15, 10))
20
+ gs = fig.add_gridspce(2, 2, hspace=0.3, wspace=0.3)
21
+
22
+ ax1 = fig.add_subplot(gs[0, 0])
23
+ self._plot_wafveform(self.input_data[0], ax1, "No")
24
+
25
+ ax2 = fig.add_subplot(gs[0, 1])
26
+ self._plot_waveform(processed_data[0], ax2, "No")
27
+
28
+ ax3 = fig.add_subplot(gs[1, 0])
29
+
30
+ ax4 = fig.add_subplot(gs[1, 1])
31
+ self._plot_spectrogram(processed_data[0], ax4, "No")
32
+
33
+ plt.tight_layout()
34
+ return fig
35
+
36
+ def _plot_waveform(self, data, ax, title):
37
+ """Helper method to plot individual waveforms"""
38
+ data_np = data.detech().numpy()
39
+ ax.plot(self.time, data_np, 'b-', linewidth=1)
40
+ ax.set_title(title)
41
+ ax.set_xlabel('Time (s)')
42
+ ax.set_ylabel('Amplitude')
43
+ ax.grid(True)
44
+
45
+ def _plot_spectrogram(self, data, ax, title):
46
+ """Helper method to plot spectrograms"""
47
+ data_np = data.detach().numpy()
48
+ ax.specgram(data_np, Fs=self.sampling_rate, cmap='viridis')
49
+ ax.set_title(title)
50
+ ax.set_ylabel('Time (s)')
51
+ ax.set_ylabel('Depth)
52
+
53
+ def animate_processing(self, frames=50):
54
+ fig, (ax1, ax2) = plt.subplots(2, 1, figsize=(10, 8))
55
+
56
+ processed_data = self.processor(self.input_data)
57
+ data_original = self.input_data[0].detach().numpy()
58
+ data_processed = processed_data[0].detach().numpy()
59
+
60
+ line1, = ax1.plot([], [], 'b-', label='Original')
61
+ line2, = ax2.plot([], [], 'r-', label='Processed')
62
+
63
+ def init():
64
+ ax1.set_xlim(0, self.time[-1])
65
+ ax1.set_ylim(data_original.min()*1.2, data_original.max()*1.2)
66
+ ax2.set_xlim(0, self.time[-1])
67
+ ax2.set_ylim(data_processed.min()*1.2, data_processed.max()*1.2)
68
+
69
+ ax1.set_title('Do not')
70
+ ax2.set_title('Do not')
71
+ ax1.grid(True)
72
+ ax2.grid(True)
73
+ ax1.legend()
74
+ ax2.legend()
75
+
76
+ return line1, line2
77
+
78
+ def animate(frame):
79
+ idx = int((frame / frames) * len(self.time))
80
+ line1.set_data(self.time[:idx], data_original[:idx])
81
+ line2.set_data(self.time[:idx], data_processed[:idx])
82
+ return line1, line2
83
+
84
+ anim = FuncAnimation(fig, animate, frames=frames,
85
+ init_func=init, blit=True,
86
+ interval=50)
87
+ plt.tight_layout()
88
+ return anim
89
+
90
+ if __name__ == "__main__":
91
+ input_size = 1000
92
+ batch_size = 32
93
+
94
+ t = np.linspace(0, 10, input_size)
95
+ base_signal = np.sin(2 * np.pi * 1 * t) + 0.5 * np.sin(2 * np.pi * 2 * t)
96
+ noise = np.random.normal(0, 0.1, input_size)
97
+ signal = base_signal + noise
98
+
99
+ input_data = torch.tensor(np.tile(signal, (batch_size, 1)), dtype=torch.float32)
100
+
101
+ processor = SecureWaveformProcessor(input_size=input_size, hidden_size=64)
102
+
103
+ visualizer = WaveformVisualizer(processor, input_data)
104
+
105
+ fig_static = visualizer.plot_waveforms()
106
+ plt.show()
107
+
108
+ anim = visualizer.animate_processing()
109
+ plt.show()