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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# -*- coding: utf-8 -*-
|
2 |
+
"""WealthFortress2.0
|
3 |
+
|
4 |
+
Automatically generated by Colab.
|
5 |
+
|
6 |
+
Original file is located at
|
7 |
+
https://colab.research.google.com/drive/1GH8ouvd4W8xGw_tGZ7VgSMxz0wVsfN7Z
|
8 |
+
"""
|
9 |
+
|
10 |
+
pip install torch cryptography numpy
|
11 |
+
|
12 |
+
import torch
|
13 |
+
import numpy as np
|
14 |
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
15 |
+
from cryptography.hazmat.backends import default_backend
|
16 |
+
from cryptography.hazmat.primitives import padding
|
17 |
+
|
18 |
+
# Step 1: Generate the dense wave (sinusoidal waveform modulated by message data)
|
19 |
+
def generate_dense_wave(message: str, frequency: float, sample_rate: int, duration: float):
|
20 |
+
t = torch.linspace(0, duration, int(sample_rate * duration))
|
21 |
+
# Convert message to numerical values (simple encoding)
|
22 |
+
message_bytes = [ord(c) for c in message]
|
23 |
+
message_tensor = torch.tensor(message_bytes, dtype=torch.float32)
|
24 |
+
# Create a carrier wave (sine wave)
|
25 |
+
carrier_wave = torch.sin(2 * np.pi * frequency * t)
|
26 |
+
# Modulate the carrier wave with the message tensor
|
27 |
+
modulated_wave = carrier_wave * torch.sin(2 * np.pi * message_tensor.mean() * t)
|
28 |
+
return modulated_wave
|
29 |
+
|
30 |
+
# Step 2: Encrypt the message
|
31 |
+
def encrypt_message(message: str, key: bytes):
|
32 |
+
backend = default_backend()
|
33 |
+
iv = b'\x00' * 16 # Initialization vector (in a real system, use a secure IV)
|
34 |
+
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
|
35 |
+
encryptor = cipher.encryptor()
|
36 |
+
|
37 |
+
# Pad the message to be AES block-size compliant
|
38 |
+
padder = padding.PKCS7(algorithms.AES.block_size).padder()
|
39 |
+
padded_data = padder.update(message.encode()) + padder.finalize()
|
40 |
+
|
41 |
+
encrypted_message = encryptor.update(padded_data) + encryptor.finalize()
|
42 |
+
return encrypted_message
|
43 |
+
|
44 |
+
# Step 3: Modulate encrypted message into the waveform
|
45 |
+
def modulate_wave_with_encryption(wave: torch.Tensor, encrypted_message: bytes):
|
46 |
+
# Convert encrypted message to tensor
|
47 |
+
encrypted_tensor = torch.tensor([byte for byte in encrypted_message], dtype=torch.float32)
|
48 |
+
# Normalize encrypted tensor and modulate it with the wave
|
49 |
+
modulated_wave = wave * encrypted_tensor.mean()
|
50 |
+
return modulated_wave
|
51 |
+
|
52 |
+
# Step 4: Demodulate and decrypt
|
53 |
+
def decrypt_message(encrypted_message: bytes, key: bytes):
|
54 |
+
backend = default_backend()
|
55 |
+
iv = b'\x00' * 16 # Same IV as in encryption
|
56 |
+
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
|
57 |
+
decryptor = cipher.decryptor()
|
58 |
+
|
59 |
+
decrypted_padded = decryptor.update(encrypted_message) + decryptor.finalize()
|
60 |
+
|
61 |
+
# Unpad the message
|
62 |
+
unpadder = padding.PKCS7(algorithms.AES.block_size).unpadder()
|
63 |
+
decrypted_message = unpadder.update(decrypted_padded) + unpadder.finalize()
|
64 |
+
|
65 |
+
return decrypted_message.decode()
|
66 |
+
|
67 |
+
# Step 5: Transform into wealth data (dummy transformation for demo)
|
68 |
+
def transform_to_wealth_data(decrypted_message: str):
|
69 |
+
# In a real-world application, this would involve parsing wealth-specific fields
|
70 |
+
wealth_data = {
|
71 |
+
"original_message": decrypted_message,
|
72 |
+
"net_worth": len(decrypted_message) * 1000, # Dummy wealth computation
|
73 |
+
"assets": len(decrypted_message) * 500,
|
74 |
+
}
|
75 |
+
return wealth_data
|
76 |
+
|
77 |
+
# Example usage
|
78 |
+
if __name__ == "__main__":
|
79 |
+
# Initial settings
|
80 |
+
message = "Transfer 1000 units"
|
81 |
+
key = b'\x01' * 32 # AES-256 key
|
82 |
+
frequency = 5.0 # Frequency in Hz
|
83 |
+
sample_rate = 100 # Samples per second
|
84 |
+
duration = 1.0 # Wave duration in seconds
|
85 |
+
|
86 |
+
# Step 1: Create dense wave
|
87 |
+
wave = generate_dense_wave(message, frequency, sample_rate, duration)
|
88 |
+
|
89 |
+
# Step 2: Encrypt the message
|
90 |
+
encrypted_message = encrypt_message(message, key)
|
91 |
+
|
92 |
+
# Step 3: Modulate the wave with encrypted message
|
93 |
+
modulated_wave = modulate_wave_with_encryption(wave, encrypted_message)
|
94 |
+
|
95 |
+
# Step 4: Decrypt the message (for demonstration)
|
96 |
+
decrypted_message = decrypt_message(encrypted_message, key)
|
97 |
+
|
98 |
+
# Step 5: Transform to wealth data
|
99 |
+
wealth_data = transform_to_wealth_data(decrypted_message)
|
100 |
+
|
101 |
+
print("Wealth Data:", wealth_data)
|
102 |
+
|
103 |
+
pip install matplotlib
|
104 |
+
|
105 |
+
import torch
|
106 |
+
import numpy as np
|
107 |
+
import matplotlib.pyplot as plt
|
108 |
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
109 |
+
from cryptography.hazmat.backends import default_backend
|
110 |
+
from cryptography.hazmat.primitives import padding
|
111 |
+
|
112 |
+
# Step 1: Generate the dense wave (sinusoidal waveform modulated by message data)
|
113 |
+
def generate_dense_wave(message: str, frequency: float, sample_rate: int, duration: float):
|
114 |
+
t = torch.linspace(0, duration, int(sample_rate * duration))
|
115 |
+
# Convert message to numerical values (simple encoding)
|
116 |
+
message_bytes = [ord(c) for c in message]
|
117 |
+
message_tensor = torch.tensor(message_bytes, dtype=torch.float32)
|
118 |
+
# Create a carrier wave (sine wave)
|
119 |
+
carrier_wave = torch.sin(2 * np.pi * frequency * t)
|
120 |
+
# Modulate the carrier wave with the message tensor
|
121 |
+
modulated_wave = carrier_wave * torch.sin(2 * np.pi * message_tensor.mean() * t)
|
122 |
+
return t, carrier_wave, modulated_wave
|
123 |
+
|
124 |
+
# Step 2: Encrypt the message
|
125 |
+
def encrypt_message(message: str, key: bytes):
|
126 |
+
backend = default_backend()
|
127 |
+
iv = b'\x00' * 16 # Initialization vector (in a real system, use a secure IV)
|
128 |
+
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
|
129 |
+
encryptor = cipher.encryptor()
|
130 |
+
|
131 |
+
# Pad the message to be AES block-size compliant
|
132 |
+
padder = padding.PKCS7(algorithms.AES.block_size).padder()
|
133 |
+
padded_data = padder.update(message.encode()) + padder.finalize()
|
134 |
+
|
135 |
+
encrypted_message = encryptor.update(padded_data) + encryptor.finalize()
|
136 |
+
return encrypted_message
|
137 |
+
|
138 |
+
# Step 3: Modulate encrypted message into the waveform
|
139 |
+
def modulate_wave_with_encryption(wave: torch.Tensor, encrypted_message: bytes):
|
140 |
+
# Convert encrypted message to tensor
|
141 |
+
encrypted_tensor = torch.tensor([byte for byte in encrypted_message], dtype=torch.float32)
|
142 |
+
# Normalize encrypted tensor and modulate it with the wave
|
143 |
+
modulated_wave = wave * encrypted_tensor.mean()
|
144 |
+
return modulated_wave
|
145 |
+
|
146 |
+
# Step 4: Visualization using Matplotlib
|
147 |
+
def visualize_modulation(t, carrier_wave, modulated_wave):
|
148 |
+
plt.figure(figsize=(12, 6))
|
149 |
+
|
150 |
+
# Plot the original carrier wave
|
151 |
+
plt.subplot(2, 1, 1)
|
152 |
+
plt.plot(t.numpy(), carrier_wave.numpy(), label="Carrier Wave", color="blue")
|
153 |
+
plt.title("Carrier Wave")
|
154 |
+
plt.xlabel("Time (s)")
|
155 |
+
plt.ylabel("Amplitude")
|
156 |
+
plt.grid(True)
|
157 |
+
|
158 |
+
# Plot the modulated wave
|
159 |
+
plt.subplot(2, 1, 2)
|
160 |
+
plt.plot(t.numpy(), modulated_wave.numpy(), label="Modulated Wave", color="orange")
|
161 |
+
plt.title("Modulated Wave (Encrypted Message)")
|
162 |
+
plt.xlabel("Time (s)")
|
163 |
+
plt.ylabel("Amplitude")
|
164 |
+
plt.grid(True)
|
165 |
+
|
166 |
+
# Show plots
|
167 |
+
plt.tight_layout()
|
168 |
+
plt.show()
|
169 |
+
|
170 |
+
# Example usage
|
171 |
+
if __name__ == "__main__":
|
172 |
+
# Initial settings
|
173 |
+
message = "Transfer 1000 units"
|
174 |
+
key = b'\x01' * 32 # AES-256 key
|
175 |
+
frequency = 5.0 # Frequency in Hz
|
176 |
+
sample_rate = 100 # Samples per second
|
177 |
+
duration = 1.0 # Wave duration in seconds
|
178 |
+
|
179 |
+
# Step 1: Create dense wave
|
180 |
+
t, carrier_wave, modulated_wave = generate_dense_wave(message, frequency, sample_rate, duration)
|
181 |
+
|
182 |
+
# Step 2: Encrypt the message
|
183 |
+
encrypted_message = encrypt_message(message, key)
|
184 |
+
|
185 |
+
# Step 3: Modulate the wave with encrypted message
|
186 |
+
modulated_wave_with_encryption = modulate_wave_with_encryption(modulated_wave, encrypted_message)
|
187 |
+
|
188 |
+
# Step 4: Visualize the modulation
|
189 |
+
visualize_modulation(t, carrier_wave, modulated_wave_with_encryption)
|
190 |
+
|
191 |
+
import torch
|
192 |
+
import numpy as np
|
193 |
+
import time
|
194 |
+
import base64
|
195 |
+
import matplotlib.pyplot as plt
|
196 |
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
197 |
+
from cryptography.hazmat.backends import default_backend
|
198 |
+
from cryptography.hazmat.primitives import padding
|
199 |
+
|
200 |
+
# Step 1: Generate the dense wave (sinusoidal waveform modulated by message data)
|
201 |
+
def generate_dense_wave(message: str, frequency: float, sample_rate: int, duration: float):
|
202 |
+
t = torch.linspace(0, duration, int(sample_rate * duration))
|
203 |
+
# Convert message to numerical values (simple encoding)
|
204 |
+
message_bytes = [ord(c) for c in message]
|
205 |
+
message_tensor = torch.tensor(message_bytes, dtype=torch.float32)
|
206 |
+
# Create a carrier wave (sine wave)
|
207 |
+
carrier_wave = torch.sin(2 * np.pi * frequency * t)
|
208 |
+
# Modulate the carrier wave with the message tensor
|
209 |
+
modulated_wave = carrier_wave * torch.sin(2 * np.pi * message_tensor.mean() * t)
|
210 |
+
return t, carrier_wave, modulated_wave
|
211 |
+
|
212 |
+
# Step 2: Encrypt the message (VPN layer encryption)
|
213 |
+
def encrypt_message(message: str, key: bytes):
|
214 |
+
backend = default_backend()
|
215 |
+
iv = b'\x00' * 16 # Initialization vector (in a real system, use a secure IV)
|
216 |
+
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
|
217 |
+
encryptor = cipher.encryptor()
|
218 |
+
|
219 |
+
# Pad the message to be AES block-size compliant
|
220 |
+
padder = padding.PKCS7(algorithms.AES.block_size).padder()
|
221 |
+
padded_data = padder.update(message.encode()) + padder.finalize()
|
222 |
+
|
223 |
+
encrypted_message = encryptor.update(padded_data) + encryptor.finalize()
|
224 |
+
return encrypted_message
|
225 |
+
|
226 |
+
# Step 3: Simulate VPN layer transmission with encryption
|
227 |
+
def vpn_layer_transmission(data: bytes):
|
228 |
+
# Simulate the "VPN" by encrypting the message
|
229 |
+
print("[VPN] Transmitting data securely...")
|
230 |
+
time.sleep(1) # Simulating network delay
|
231 |
+
encoded_data = base64.b64encode(data)
|
232 |
+
print(f"[VPN] Encrypted and transmitted data: {encoded_data.decode('utf-8')}")
|
233 |
+
return encoded_data
|
234 |
+
|
235 |
+
# Step 4: Simulate cloud storage transfer and deep space transmission
|
236 |
+
def cloud_transfer(encoded_data: bytes):
|
237 |
+
print("[Cloud] Storing data in cloud for deep space transmission...")
|
238 |
+
time.sleep(2) # Simulating storage delay
|
239 |
+
print(f"[Cloud] Data successfully stored: {encoded_data.decode('utf-8')}")
|
240 |
+
|
241 |
+
# Step 5: Visualization using Matplotlib
|
242 |
+
def visualize_modulation(t, carrier_wave, modulated_wave):
|
243 |
+
plt.figure(figsize=(12, 6))
|
244 |
+
|
245 |
+
# Plot the original carrier wave
|
246 |
+
plt.subplot(2, 1, 1)
|
247 |
+
plt.plot(t.numpy(), carrier_wave.numpy(), label="Carrier Wave", color="blue")
|
248 |
+
plt.title("Carrier Wave")
|
249 |
+
plt.xlabel("Time (s)")
|
250 |
+
plt.ylabel("Amplitude")
|
251 |
+
plt.grid(True)
|
252 |
+
|
253 |
+
# Plot the modulated wave
|
254 |
+
plt.subplot(2, 1, 2)
|
255 |
+
plt.plot(t.numpy(), modulated_wave.numpy(), label="Modulated Wave", color="orange")
|
256 |
+
plt.title("Modulated Wave (Encrypted Message)")
|
257 |
+
plt.xlabel("Time (s)")
|
258 |
+
plt.ylabel("Amplitude")
|
259 |
+
plt.grid(True)
|
260 |
+
|
261 |
+
# Show plots
|
262 |
+
plt.tight_layout()
|
263 |
+
plt.show()
|
264 |
+
|
265 |
+
# Example usage
|
266 |
+
if __name__ == "__main__":
|
267 |
+
# Initial settings
|
268 |
+
message = "Transfer 1000 units"
|
269 |
+
key = b'\x01' * 32 # AES-256 key
|
270 |
+
frequency = 5.0 # Frequency in Hz
|
271 |
+
sample_rate = 100 # Samples per second
|
272 |
+
duration = 1.0 # Wave duration in seconds
|
273 |
+
|
274 |
+
# Step 1: Create dense wave
|
275 |
+
t, carrier_wave, modulated_wave = generate_dense_wave(message, frequency, sample_rate, duration)
|
276 |
+
|
277 |
+
# Step 2: Encrypt the message
|
278 |
+
encrypted_message = encrypt_message(message, key)
|
279 |
+
|
280 |
+
# Step 3: VPN Layer Transmission (simulate VPN secure transmission)
|
281 |
+
vpn_encrypted_message = vpn_layer_transmission(encrypted_message)
|
282 |
+
|
283 |
+
# Step 4: Cloud transfer and simulated "deep space" transmission
|
284 |
+
cloud_transfer(vpn_encrypted_message)
|
285 |
+
|
286 |
+
# Step 5: Visualize the wave modulation
|
287 |
+
visualize_modulation(t, carrier_wave, modulated_wave)
|
288 |
+
|
289 |
+
import torch
|
290 |
+
import numpy as np
|
291 |
+
import time
|
292 |
+
import base64
|
293 |
+
import matplotlib.pyplot as plt
|
294 |
+
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
|
295 |
+
from cryptography.hazmat.backends import default_backend
|
296 |
+
from cryptography.hazmat.primitives import padding
|
297 |
+
|
298 |
+
# Step 1: Generate the dense wave (sinusoidal waveform modulated by message data)
|
299 |
+
def generate_dense_wave(message: str, frequency: float, sample_rate: int, duration: float):
|
300 |
+
t = torch.linspace(0, duration, int(sample_rate * duration))
|
301 |
+
# Convert message to numerical values (simple encoding)
|
302 |
+
message_bytes = [ord(c) for c in message]
|
303 |
+
message_tensor = torch.tensor(message_bytes, dtype=torch.float32)
|
304 |
+
# Create a carrier wave (sine wave)
|
305 |
+
carrier_wave = torch.sin(2 * np.pi * frequency * t)
|
306 |
+
# Modulate the carrier wave with the message tensor
|
307 |
+
modulated_wave = carrier_wave * torch.sin(2 * np.pi * message_tensor.mean() * t)
|
308 |
+
return t, carrier_wave, modulated_wave
|
309 |
+
|
310 |
+
# Step 2: Combine the waves (carrier wave + modulated wave)
|
311 |
+
def combine_waves(carrier_wave: torch.Tensor, modulated_wave: torch.Tensor):
|
312 |
+
combined_wave = carrier_wave + modulated_wave # Simple addition
|
313 |
+
return combined_wave
|
314 |
+
|
315 |
+
# Step 3: Encrypt the message (VPN layer encryption)
|
316 |
+
def encrypt_message(message: str, key: bytes):
|
317 |
+
backend = default_backend()
|
318 |
+
iv = b'\x00' * 16 # Initialization vector (in a real system, use a secure IV)
|
319 |
+
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=backend)
|
320 |
+
encryptor = cipher.encryptor()
|
321 |
+
|
322 |
+
# Pad the message to be AES block-size compliant
|
323 |
+
padder = padding.PKCS7(algorithms.AES.block_size).padder()
|
324 |
+
padded_data = padder.update(message.encode()) + padder.finalize()
|
325 |
+
|
326 |
+
encrypted_message = encryptor.update(padded_data) + encryptor.finalize()
|
327 |
+
return encrypted_message
|
328 |
+
|
329 |
+
# Step 4: Simulate VPN layer transmission with encryption
|
330 |
+
def vpn_layer_transmission(data: bytes):
|
331 |
+
# Simulate the "VPN" by encrypting the message
|
332 |
+
print("[VPN] Transmitting data securely...")
|
333 |
+
time.sleep(1) # Simulating network delay
|
334 |
+
encoded_data = base64.b64encode(data)
|
335 |
+
print(f"[VPN] Encrypted and transmitted data: {encoded_data.decode('utf-8')}")
|
336 |
+
return encoded_data
|
337 |
+
|
338 |
+
# Step 5: Simulate cloud storage transfer and deep space transmission
|
339 |
+
def cloud_transfer(encoded_data: bytes):
|
340 |
+
print("[Cloud] Storing data in cloud for deep space transmission...")
|
341 |
+
time.sleep(2) # Simulating storage delay
|
342 |
+
print(f"[Cloud] Data successfully stored: {encoded_data.decode('utf-8')}")
|
343 |
+
|
344 |
+
# Step 6: Visualization using Matplotlib
|
345 |
+
def visualize_modulation(t, carrier_wave, modulated_wave, combined_wave):
|
346 |
+
plt.figure(figsize=(12, 8))
|
347 |
+
|
348 |
+
# Plot the original carrier wave
|
349 |
+
plt.subplot(3, 1, 1)
|
350 |
+
plt.plot(t.numpy(), carrier_wave.numpy(), label="Carrier Wave", color="blue")
|
351 |
+
plt.title("Carrier Wave")
|
352 |
+
plt.xlabel("Time (s)")
|
353 |
+
plt.ylabel("Amplitude")
|
354 |
+
plt.grid(True)
|
355 |
+
|
356 |
+
# Plot the modulated wave
|
357 |
+
plt.subplot(3, 1, 2)
|
358 |
+
plt.plot(t.numpy(), modulated_wave.numpy(), label="Modulated Wave", color="orange")
|
359 |
+
plt.title("Modulated Wave (Encrypted Message)")
|
360 |
+
plt.xlabel("Time (s)")
|
361 |
+
plt.ylabel("Amplitude")
|
362 |
+
plt.grid(True)
|
363 |
+
|
364 |
+
# Plot the combined wave
|
365 |
+
plt.subplot(3, 1, 3)
|
366 |
+
plt.plot(t.numpy(), combined_wave.numpy(), label="Combined Wave", color="green")
|
367 |
+
plt.title("Combined Wave (Carrier + Modulated)")
|
368 |
+
plt.xlabel("Time (s)")
|
369 |
+
plt.ylabel("Amplitude")
|
370 |
+
plt.grid(True)
|
371 |
+
|
372 |
+
# Show plots
|
373 |
+
plt.tight_layout()
|
374 |
+
plt.show()
|
375 |
+
|
376 |
+
# Example usage
|
377 |
+
if __name__ == "__main__":
|
378 |
+
# Initial settings
|
379 |
+
message = "Transfer 1000 units"
|
380 |
+
key = b'\x01' * 32 # AES-256 key
|
381 |
+
frequency = 5.0 # Frequency in Hz
|
382 |
+
sample_rate = 100 # Samples per second
|
383 |
+
duration = 1.0 # Wave duration in seconds
|
384 |
+
|
385 |
+
# Step 1: Create dense wave
|
386 |
+
t, carrier_wave, modulated_wave = generate_dense_wave(message, frequency, sample_rate, duration)
|
387 |
+
|
388 |
+
# Step 2: Combine the carrier and modulated waves
|
389 |
+
combined_wave = combine_waves(carrier_wave, modulated_wave)
|
390 |
+
|
391 |
+
# Step 3: Encrypt the message
|
392 |
+
encrypted_message = encrypt_message(message, key)
|
393 |
+
|
394 |
+
# Step 4: VPN Layer Transmission (simulate VPN secure transmission)
|
395 |
+
vpn_encrypted_message = vpn_layer_transmission(encrypted_message)
|
396 |
+
|
397 |
+
# Step 5: Cloud transfer and simulated "deep space" transmission
|
398 |
+
cloud_transfer(vpn_encrypted_message)
|
399 |
+
|
400 |
+
# Step 6: Visualize the wave modulation and combined wave
|
401 |
+
visualize_modulation(t, carrier_wave, modulated_wave, combined_wave)
|
402 |
+
|
403 |
+
import numpy as np
|
404 |
+
|
405 |
+
# Hamming(7, 4) code for simple error detection and correction
|
406 |
+
def hamming_encode(message: str):
|
407 |
+
message_bits = [int(b) for b in ''.join(format(ord(c), '08b') for c in message)]
|
408 |
+
encoded_bits = []
|
409 |
+
|
410 |
+
# Apply Hamming(7,4) encoding
|
411 |
+
for i in range(0, len(message_bits), 4):
|
412 |
+
d = message_bits[i:i+4]
|
413 |
+
if len(d) < 4: # Pad if necessary
|
414 |
+
d += [0] * (4 - len(d))
|
415 |
+
|
416 |
+
p1 = d[0] ^ d[1] ^ d[3] # Parity bits
|
417 |
+
p2 = d[0] ^ d[2] ^ d[3]
|
418 |
+
p3 = d[1] ^ d[2] ^ d[3]
|
419 |
+
|
420 |
+
# Add data and parity bits
|
421 |
+
encoded_bits += [p1, p2, d[0], p3, d[1], d[2], d[3]]
|
422 |
+
|
423 |
+
return np.array(encoded_bits)
|
424 |
+
|
425 |
+
def hamming_decode(encoded_bits):
|
426 |
+
decoded_message = []
|
427 |
+
|
428 |
+
# Decode Hamming(7,4)
|
429 |
+
for i in range(0, len(encoded_bits), 7):
|
430 |
+
b = encoded_bits[i:i+7]
|
431 |
+
if len(b) < 7: # Skip if not enough bits
|
432 |
+
continue
|
433 |
+
|
434 |
+
# Calculate syndrome bits
|
435 |
+
p1 = b[0] ^ b[2] ^ b[4] ^ b[6]
|
436 |
+
p2 = b[1] ^ b[2] ^ b[5] ^ b[6]
|
437 |
+
p3 = b[3] ^ b[4] ^ b[5] ^ b[6]
|
438 |
+
|
439 |
+
# Error position (if any)
|
440 |
+
error_position = p1 + (p2 * 2) + (p3 * 4)
|
441 |
+
|
442 |
+
if error_position != 0:
|
443 |
+
b[error_position - 1] = 1 - b[error_position - 1] # Correct the bit
|
444 |
+
|
445 |
+
# Extract the original data bits
|
446 |
+
decoded_message += [b[2], b[4], b[5], b[6]]
|
447 |
+
|
448 |
+
return ''.join([chr(int(''.join(map(str, decoded_message[i:i+8])), 2)) for i in range(0, len(decoded_message), 8)])
|
449 |
+
|
450 |
+
# Example usage
|
451 |
+
message = "Test"
|
452 |
+
encoded_message = hamming_encode(message)
|
453 |
+
print(f"Encoded Message (Hamming): {encoded_message}")
|
454 |
+
|
455 |
+
# Simulate transmission and potential bit-flips (errors)
|
456 |
+
encoded_message[2] = 1 - encoded_message[2] # Introduce an error
|
457 |
+
|
458 |
+
# Decode and correct errors
|
459 |
+
decoded_message = hamming_decode(encoded_message)
|
460 |
+
print(f"Decoded Message: {decoded_message}")
|
461 |
+
|
462 |
+
import torch
|
463 |
+
import numpy as np
|
464 |
+
import matplotlib.pyplot as plt
|
465 |
+
|
466 |
+
# Step 1: Generate the dense wave (sinusoidal waveform modulated by message data)
|
467 |
+
def generate_dense_wave(message: str, frequency: float, sample_rate: int, duration: float):
|
468 |
+
t = torch.linspace(0, duration, int(sample_rate * duration))
|
469 |
+
message_bytes = [ord(c) for c in message]
|
470 |
+
message_tensor = torch.tensor(message_bytes, dtype=torch.float32)
|
471 |
+
carrier_wave = torch.sin(2 * np.pi * frequency * t)
|
472 |
+
modulated_wave = carrier_wave * torch.sin(2 * np.pi * message_tensor.mean() * t)
|
473 |
+
return t, carrier_wave, modulated_wave
|
474 |
+
|
475 |
+
# Step 2: Space-Time Coding (Alamouti Scheme with 2 antennas)
|
476 |
+
def space_time_code(wave1: torch.Tensor, wave2: torch.Tensor):
|
477 |
+
# Alamouti Space-Time Block Code for 2 antennas
|
478 |
+
s1 = wave1
|
479 |
+
s2 = wave2
|
480 |
+
transmit_antenna_1 = torch.stack([s1, -s2.conj()])
|
481 |
+
transmit_antenna_2 = torch.stack([s2, s1.conj()])
|
482 |
+
return transmit_antenna_1, transmit_antenna_2
|
483 |
+
|
484 |
+
# Step 3: Doppler Compensation
|
485 |
+
def doppler_compensation(wave: torch.Tensor, velocity: float, frequency: float, sample_rate: int):
|
486 |
+
c = 3e8 # Speed of light in meters per second
|
487 |
+
doppler_shift = frequency * (velocity / c) # Doppler shift formula
|
488 |
+
compensated_wave = wave * torch.exp(-1j * 2 * np.pi * doppler_shift * torch.arange(len(wave)) / sample_rate)
|
489 |
+
return compensated_wave.real # Take real part after compensation
|
490 |
+
|
491 |
+
# Step 4: Combine Waves (Carrier + Modulated)
|
492 |
+
def combine_waves(carrier_wave: torch.Tensor, modulated_wave: torch.Tensor):
|
493 |
+
combined_wave = carrier_wave + modulated_wave
|
494 |
+
return combined_wave
|
495 |
+
|
496 |
+
# Step 5: Visualization using Matplotlib
|
497 |
+
def visualize_modulation(t, wave1, wave2, combined_wave, title1, title2, combined_title):
|
498 |
+
plt.figure(figsize=(12, 8))
|
499 |
+
|
500 |
+
# Plot Wave 1
|
501 |
+
plt.subplot(3, 1, 1)
|
502 |
+
plt.plot(t.numpy(), wave1.numpy(), label=title1, color="blue")
|
503 |
+
plt.title(title1)
|
504 |
+
plt.xlabel("Time (s)")
|
505 |
+
plt.ylabel("Amplitude")
|
506 |
+
plt.grid(True)
|
507 |
+
|
508 |
+
# Plot Wave 2
|
509 |
+
plt.subplot(3, 1, 2)
|
510 |
+
plt.plot(t.numpy(), wave2.numpy(), label=title2, color="orange")
|
511 |
+
plt.title(title2)
|
512 |
+
plt.xlabel("Time (s)")
|
513 |
+
plt.ylabel("Amplitude")
|
514 |
+
plt.grid(True)
|
515 |
+
|
516 |
+
# Plot Combined Wave
|
517 |
+
plt.subplot(3, 1, 3)
|
518 |
+
plt.plot(t.numpy(), combined_wave.numpy(), label=combined_title, color="green")
|
519 |
+
plt.title(combined_title)
|
520 |
+
plt.xlabel("Time (s)")
|
521 |
+
plt.ylabel("Amplitude")
|
522 |
+
plt.grid(True)
|
523 |
+
|
524 |
+
plt.tight_layout()
|
525 |
+
plt.show()
|
526 |
+
|
527 |
+
# Example usage
|
528 |
+
if __name__ == "__main__":
|
529 |
+
# Initial settings
|
530 |
+
message = "Deep Space Message"
|
531 |
+
frequency = 5.0 # Frequency in Hz
|
532 |
+
sample_rate = 100 # Samples per second
|
533 |
+
duration = 1.0 # Wave duration in seconds
|
534 |
+
velocity = 10000 # Relative velocity (m/s) for Doppler compensation
|
535 |
+
|
536 |
+
# Step 1: Generate dense wave
|
537 |
+
t, carrier_wave, modulated_wave = generate_dense_wave(message, frequency, sample_rate, duration)
|
538 |
+
|
539 |
+
# Step 2: Space-Time Coding (using two antennas)
|
540 |
+
st_wave1, st_wave2 = space_time_code(carrier_wave, modulated_wave)
|
541 |
+
|
542 |
+
# Step 3: Doppler Compensation
|
543 |
+
doppler_wave1 = doppler_compensation(st_wave1[0], velocity, frequency, sample_rate)
|
544 |
+
doppler_wave2 = doppler_compensation(st_wave2[0], velocity, frequency, sample_rate)
|
545 |
+
|
546 |
+
# Step 4: Combine the waves (carrier + modulated)
|
547 |
+
combined_wave = combine_waves(doppler_wave1, doppler_wave2)
|
548 |
+
|
549 |
+
# Step 5: Visualization
|
550 |
+
visualize_modulation(t, doppler_wave1, doppler_wave2, combined_wave,
|
551 |
+
"Doppler-Compensated Wave 1", "Doppler-Compensated Wave 2",
|
552 |
+
"Combined Doppler-Compensated Wave")
|