-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhack_epy_block_0.py
More file actions
110 lines (69 loc) · 2.71 KB
/
hack_epy_block_0.py
File metadata and controls
110 lines (69 loc) · 2.71 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
"""
Embedded Python Blocks:
Each time this file is saved, GRC will instantiate the first class it finds
to get ports and parameters of your block. The arguments to __init__ will
be the parameters. All of them are required to have default values!
"""
import numpy as np
import random
from gnuradio import gr
class blk(gr.sync_block):
def __init__(self, message="hello", seed=42, key_len=16):
gr.sync_block.__init__(
self,
name="dsss_bpsk_source",
in_sig=None,
out_sig=[np.float32]
)
self.message = message
self.seed = seed
self.key_len = key_len
# Barker
barker = [1,1,1,1,1,0,0,1,1,0,1,0,1]
key = self.generate_key(seed, key_len)
# ---------- HEADER ----------
msg_bytes = message.encode("utf-8")
length = len(msg_bytes)
length_bits = format(length, "08b")
payload_bits = "".join(format(b, "08b") for b in msg_bytes)
bits = length_bits + payload_bits
# ---------- DSSS ----------
spread_bits = self.dsss_spread(bits, key)
# ---------- BPSK ----------
data_symbols = self.bpsk_mod(spread_bits)
# ---------- PREAMBLE ----------
preamble = self.build_preamble(barker, key)
# ---------- FINAL PACKET ----------
self.packet = np.array(self.final_signal(preamble, data_symbols), dtype=np.float32)
self.index = 0
# ------------------------------------------------
def generate_key(self, seed, key_len):
random.seed(seed)
return [random.randint(0,1) for _ in range(key_len)]
# ------------------------------------------------
def dsss_spread(self, bits, key):
bits_list = [int(b) for b in bits]
repeated_bits = np.repeat(bits_list, len(key))
repeated_key = np.tile(key, len(bits))
spreaded = repeated_key ^ repeated_bits
return spreaded
# ------------------------------------------------
def bpsk_mod(self, bits):
return [1 if b==1 else -1 for b in bits]
# ------------------------------------------------
def build_preamble(self, preamble, key):
spread = self.dsss_spread(preamble, key)
return self.bpsk_mod(spread)
# ------------------------------------------------
def final_signal(self, preamble, data):
return preamble + data
# ------------------------------------------------
def work(self, input_items, output_items):
out = output_items[0]
n = len(out)
for i in range(n):
out[i] = self.packet[self.index]
self.index += 1
if self.index >= len(self.packet):
self.index = 0
return n