-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
306 lines (181 loc) · 8.19 KB
/
main.m
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
clc, clearvars, clear, format compact
% Import MATLAB packages
import communications.* % used for cyclegen
% Import local packages
import utils.*
import func.*
% Display Level Configuration:
% SIMULATION: Runs just the simulation
% ANALYSIS: Runs the analysis
% RESULT: Display values as per guidelines
% DEBUG: Additional display for variables or comparisons
% PLOTS: Toggle for generating plots
SIMULATION = 1; % Set to 1 to compute the simulation
ANALYSIS = 0; % Set to 1 to enable the analysis
RESULT = 1; % Set to 1 to display result values
DEBUG = 0; % Set to 1 to enable additional debugging display
PLOTS = 1; % Set to 1 to generate plots, 0 to disable plots
% = = = = = = = = = = = = = = = = = = = =
% Initial constant parameters
% source number 7
alphabet = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
probability_vector = [11, 7, 9, 1, 6, 6, 13, 14, 13, 5, 11, 4]/100;
tau = 60e-9; % symbol duration time, [s]
SNR = 8.1; % Signal-to-Noise-Ration, [dB]
% Source Code: Shannon-Fano
% Error correction code: Cyclic
generation_polynomial = [... % z^5 + z^2 + z^0
1 0 0 1 0 1];
codeword_length = 31; % m
f0 = 2.5e+9; % carrier frequency [Hz]
% Modulation: BPSK
phase_shift = pi; % Phase shift [rad]
U = 1; % amplitude BPSK signal [V]
% Additional data
transmitted_symbol_number = randi(1e5,1); % number of symbols
samples_per_symbol = 500; % samples per symbol
r = ceil(log2(codeword_length + 1));% r
k = codeword_length - r; % k
% Used for the scrambling/descrmabling algorithm
scrambler_key = randi(2, 1, codeword_length) - 1;
% = = = = = = = = = = = = = = = = = = = =
% Source generation
if SIMULATION
alphabet_matrix = [alphabet; probability_vector];
show(DEBUG, alphabet_matrix);
disp(" - Creating sequence");
initial_symbol_sequence = symbol_sequence_generator(alphabet_matrix, transmitted_symbol_number);
show(DEBUG, initial_symbol_sequence);
end
% = = = = = = = = Task 2 = = = = = = = =
% Analysis of data source with Shannon-Fano code
if ANALYSIS
run("analysis\source_data_analysis.m");
end
% = = = = = = = = = = = = = = = = = = = =
% Perform Shannon-Fano Source Encoding
if SIMULATION
disp(" - Performing Shannon-Fano encoding");
shannon_fano_encoded_sequence = shannon_fano_encoding(initial_symbol_sequence);
show(DEBUG, shannon_fano_encoded_sequence);
disp(" - Adding padding bits");
padded_sequence = add_padding_bits(shannon_fano_encoded_sequence, k, r);
show(DEBUG, padded_sequence);
end
% = = = = = = = = Task 3 = = = = = = = =
% Analysis of Shannon-Fano Source Encoding
if ANALYSIS
run("analysis\shannon_fano_encoding_analysis.m");
% = = = = = = = = Task 4 = = = = = = = =
% Analysis of Shannon theorem's condition
run("analysis\shannon_theorem_condition_analysis.m");
% = = = = = = = = Task 5 = = = = = = = =
% Analysis of cyclic coding
run("analysis\cyclic_coding_analysis.m");
% = = = = = = = = Task 6 = = = = = = = =
% 1 - For the analysis will be generated a new sequence
% 0 - The current sequence will be analized (better with high value for
% transmitted_symbol_number)
% NOTE: to work, the SIMULATION flag should be 1.
GENERATE_NEW_SEQUENCE = 1;
run("analysis\hamming_coding_analysis.m");
end
% = = = = = = = = = = = = = = = = = = = =
% Perform Cyclic-Hamming channel coding
if SIMULATION
disp(" - Performing Hamming-Encoding");
hamming_encoded_sequence = hamming_encoding(padded_sequence, codeword_length, k, generation_polynomial);
show(DEBUG, hamming_encoded_sequence);
% = = = = = = = = = = = = = = = = = = = =
% Perform interleaving
disp(" - Performing interleaving");
interleaved_sequence = interleaving(hamming_encoded_sequence, codeword_length);
show(DEBUG, interleaved_sequence)
% = = = = = = = = = = = = = = = = = = = =
% Perform scrambling
disp(" - Performing scrambling");
scrambled_sequence = scrambling(interleaved_sequence, scrambler_key);
show(DEBUG, scrambled_sequence);
% = = = = = = = = = = = = = = = = = = = =
% Perform modulation
disp(" - Performing BPSK modulation");
% Define the time-step
delta_t = tau / samples_per_symbol;
% Time intervals for one symbol
time_intervals = 0: delta_t: tau - delta_t;
% Create the carrier signal
carrier_signal = sin(2 * phase_shift * f0 * time_intervals);
% Calculate the energy per symbol
Eb = dot(carrier_signal, carrier_signal);
% Save length of encoded sequence
N = length(scrambled_sequence);
% Perform BPSK modulation
BPSK_signal = kron(-2 * scrambled_sequence + 1, carrier_signal);
end
% = = = = = = = = Task 7 = = = = = = = =
% Analysis of BPSK spectrum
if ANALYSIS
run("analysis\spectrum_analysis.m")
% = = = = = = = = Task 8 = = = = = = = =
% Analysis of Probability of > 2 errors occurring
run("analysis\over_two_errors_prob_analysis.m");
end
% = = = = = = = = = = = = = = = = = = = =
% Perform Gaussion White Noise addition
if SIMULATION
disp(" - Adding GWN");
% Reversed SNR formula
EbN0 = 10^(SNR / 10);
% Obtain noise spectral power density
N0 = Eb./EbN0;
% Calculate sigma for BPSK
sigma = sqrt(N0 / 2);
% Create noise signal
noise_signal = sigma * randn(1, N * samples_per_symbol);
% Create disturbed signal
disturbed_signal = BPSK_signal + noise_signal;
% = = = = = = = = = = = = = = = = = = = =
% Perform detection
disp(" - Performing BPSK demodulation");
% Slice recieved signal into segments in each column
sliced_disturbed_signal = reshape(disturbed_signal, samples_per_symbol, N);
% Detect the signal with the BPSK threshold
detected_signal = carrier_signal * sliced_disturbed_signal < 0;
show(DEBUG, sum(detected_signal ~= scrambled_sequence), "Demodulation" )
% = = = = = = = = = = = = = = = = = = = =
% Perform descrambling
disp(" - Performing descrambling");
descrambled_sequence = descrambling(detected_signal, scrambler_key);
show(DEBUG, sum(descrambled_sequence ~= interleaved_sequence), "Descrambling" )
% = = = = = = = = = = = = = = = = = = = =
% Perform deinterleaving
disp(" - Performing deinterleaving");
deinterleaved_sequence = deinterleaving(descrambled_sequence, codeword_length);
show(DEBUG, sum(deinterleaved_sequence ~= hamming_encoded_sequence), "Deinterleaving" )
errors_occurred = sum(deinterleaved_sequence ~= hamming_encoded_sequence);
fprintf(" Errors occurred: %d\n", errors_occurred);
% = = = = = = = = = = = = = = = = = = = =
% Perform Hamming-decoding and error correction
disp(" - Performing Hamming-decoding");
hamming_decoded_sequence = hamming_decoding(deinterleaved_sequence, codeword_length, k, generation_polynomial);
show(DEBUG, sum(hamming_decoded_sequence ~= padded_sequence), "Channel decoding" )
% = = = = = = = = = = = = = = = = = = = =
% Remove padding bits
disp(" - Removing padding bits");
unpadded_sequence = remove_padding_bits(hamming_decoded_sequence, r);
show(DEBUG, sum(unpadded_sequence ~= shannon_fano_encoded_sequence), "Unpadding" )
% = = = = = = = = = = = = = = = = = = = =
% Perform Shannon-Fano decoding
disp(" - Performing Shannon-Fano decoding");
shannon_fano_decoded_sequence = shannon_fano_decoding(unpadded_sequence);
show(DEBUG, sum(shannon_fano_decoded_sequence ~= initial_symbol_sequence), "Source decoding" )
% = = = = = = = = = = = = = = = = = = = =
% Correctness checkig
received_symbol_sequence = shannon_fano_decoded_sequence;
show(DEBUG, initial_symbol_sequence);
show(DEBUG, received_symbol_sequence);
% Shows the actual errors between the initial and the final sequence
errors_occurred = sum(shannon_fano_decoded_sequence ~= initial_symbol_sequence);
fprintf("\nReal number of errors: %d\n", errors_occurred);
end
fprintf("\n\nEnd of experiemnt.\n\n");