-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallbacks.py
156 lines (136 loc) · 6.58 KB
/
callbacks.py
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
import numpy as np
from dash import dash_table
from dash.dependencies import Input, Output
import plotly.graph_objs as go
import utils.utilities
from components.graphs import create_graph
from modulation import modulate_signal, demodulate_signal
from utils import ascii_to_binary_stream, binary_stream_to_ascii, calculate_ber, encode_hamming_7_4, decode_hamming_7_4, \
noise
def register_callbacks(app):
@app.callback(
Output('modulated-signal-graph', 'figure'),
Output('transmitted-signal-graph', 'figure'),
Output('demodulated-signal-graph', 'figure'),
Output('comparison-container', 'children'),
Output('table-container', 'children'),
Output('table-container-params', 'children'),
Input('w-input', 'value'),
Input('fs-input', 'value'),
Input('tb-input', 'value'),
Input('modulation', 'value'),
Input('text', 'value'),
Input('tabs', 'value'),
Input('alpha-slider', 'value'),
Input('beta-slider', 'value'),
Input('noise-type', 'value'),
Input('order-type', 'value')
)
def update_signal(_w, _fs, _tb, modulation_type, text, tab, alpha, beta, noise_type, order):
try:
w = float(_w)
fs = int(_fs)
tb = float(_tb)
except ValueError:
empty_fig = create_graph('', [], [], '')
return empty_fig, empty_fig, empty_fig, [], [], []
bits = ascii_to_binary_stream(text)
encoded_bits = encode_hamming_7_4(bits)
num_bits = len(encoded_bits)
tc = tb * num_bits
fn = w * (1 / tb)
N = int(fs * tc)
samples_per_bit = N // num_bits
N = samples_per_bit * num_bits
t = np.linspace(0, tc, N, endpoint=False)
fn1 = (w + 1) / tb
fn2 = (w + 2) / tb
ts = 1 / fs
params_table_data = [{
'W': w,
'Fs': fs,
"Modulation Type": modulation_type,
'Alpha': alpha,
'Beta': beta,
'Tb': tb,
'fn': fn,
'fn1': fn1,
'fn2': fn2,
'ts': ts,
'N': N,
'Bit count': num_bits
}]
table_params = dash_table.DataTable(
columns=[{'name': col, 'id': col} for col in params_table_data[0].keys()],
data=params_table_data,
style_table={'margin': '20px auto', 'width': '50%'},
style_cell={'textAlign': 'left', 'fontFamily': 'Arial, sans-serif', 'padding': '10px'}
)
if tab == 'modulation':
signal = modulate_signal(encoded_bits, modulation_type, fn, t, fn1, fn2)
if order == '1':
if noise_type == 'white':
noisy_signal = signal + alpha * noise.white_noise(t, 10)
elif noise_type == 'gaussian':
noisy_signal = signal + alpha * noise.gaussian_noise(t, 0, alpha)
noisy_signal *= np.exp(-beta * t)
else:
noisy_signal = signal * np.exp(-beta * t)
noisy_signal += alpha * noise.white_noise(t, 10)
modulated_fig = create_graph('Modulated Signal' + modulation_type, t, signal, 'Amplitude')
transmitted_fig = create_graph('Transmitted Signal' + modulation_type, t, noisy_signal, 'Amplitude')
demodulated_bits = demodulate_signal(noisy_signal, modulation_type, fn, samples_per_bit, ts, N, fn1, fn2)
demodulated_bits = utils.utilities.convert_to_binary_stream(demodulated_bits, samples_per_bit)
decoded_hamming, syndrome = decode_hamming_7_4(demodulated_bits)
ber = calculate_ber(bits, decoded_hamming)
demodulated_fig = create_graph('Demodulated Signal', np.arange(len(bits)), bits, name="Original bit stream",
y_name="", x_name="Bits")
demodulated_fig.add_trace(
go.Scatter(x=np.arange(len(bits)), y=decoded_hamming, mode='lines', name='Transmission result'))
table_data = [{
'Number of transmitted bits': len(bits),
'Decoded phrase': binary_stream_to_ascii(decoded_hamming),
'Syndrome': syndrome,
'BER': ber
}]
table = dash_table.DataTable(
columns=[{'name': col, 'id': col} for col in table_data[0].keys()],
data=table_data,
style_table={'margin': '20px auto', 'width': '50%'},
style_cell={'textAlign': 'left', 'fontFamily': 'Arial, sans-serif', 'padding': '10px'}
)
return modulated_fig, transmitted_fig, demodulated_fig, None, table, table_params
else:
comparison_content = []
comparison_data = []
for modulation in ['ASK', 'PSK', 'FSK']:
signal = modulate_signal(encoded_bits, modulation, fn, t, fn1, fn2)
if order == '1':
if noise_type == 'white':
noisy_signal = signal + alpha * noise.white_noise(t, 10)
elif noise_type == 'gaussian':
noisy_signal = signal + alpha * noise.gaussian_noise(t, 0, alpha)
noisy_signal *= np.exp(-beta * t)
else:
noisy_signal = signal * np.exp(-beta * t)
noisy_signal += alpha * noise.white_noise(t, 10)
demodulated_bits = demodulate_signal(noisy_signal, modulation, fn, samples_per_bit, ts, N, fn1, fn2)
demodulated_bits = utils.utilities.convert_to_binary_stream(demodulated_bits, samples_per_bit)
decoded_hamming, syndrome = decode_hamming_7_4(demodulated_bits)
ber = calculate_ber(bits, decoded_hamming)
comparison_data.append({
'Modulation': modulation,
'BER': ber,
'Decoded phrase': binary_stream_to_ascii(decoded_hamming)
})
comparison_table = dash_table.DataTable(
columns=[{'name': 'Modulation', 'id': 'Modulation'},
{'name': 'BER', 'id': 'BER'},
{'name': 'Decoded phrase', 'id': 'Decoded phrase'}],
data=comparison_data,
style_table={'margin': '20px auto', 'width': '80%'},
style_cell={'textAlign': 'left', 'fontFamily': 'Arial, sans-serif', 'padding': '10px'}
)
comparison_content.append(comparison_table)
empty_fig = create_graph('', [], [], '')
return empty_fig, empty_fig, empty_fig, comparison_content, None, table_params