-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchapter4.m
More file actions
271 lines (229 loc) · 7.48 KB
/
Copy pathchapter4.m
File metadata and controls
271 lines (229 loc) · 7.48 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
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
clc
clear
sig_len = 1000;
sampl_per_bin = 100;
bin_data_len = sig_len/sampl_per_bin;
bin_data = round(rand(1,bin_data_len));
sig_carrier_base = sin(2*pi*(0:(1/sampl_per_bin):(1-(1/sampl_per_bin)))); % Baseline carrier
sig_carrier_freq = sin(2*2*pi*(0:(1/sampl_per_bin):(1-(1/sampl_per_bin)))); % Double frequency
sig_carrier_phase = sin(2*pi*(0:(1/sampl_per_bin):(1-(1/sampl_per_bin)))+(pi/2)); % Phase shifted by 45 degrees
plot(sig_carrier_base);hold;
plot(sig_carrier_freq);
plot(sig_carrier_phase);
sig_bin = [];
sig_ask = [];
sig_psk = [];
sig_fsk = [];
for ind = 1:bin_data_len
if(bin_data(ind)==1)
sig_bin = [sig_bin ones(1, sampl_per_bin)];
sig_ask = [sig_ask sig_carrier_base];
sig_psk = [sig_psk sig_carrier_base];
sig_fsk = [sig_fsk sig_carrier_base];
else
sig_bin = [sig_bin zeros(1, sampl_per_bin)];
sig_ask = [sig_ask 0.5*sig_carrier_base];
sig_psk = [sig_psk sig_carrier_phase];
sig_fsk = [sig_fsk sig_carrier_freq];
end
end
figure
subplot(4,1,1);grid;
plot(sig_bin);
subplot(4,1,2);
plot(sig_ask);
subplot(4,1,3);
plot(sig_psk);
subplot(4,1,4);
plot(sig_fsk);
%%
len = 10000;
bin1 = round(rand(1,len));
bin2 = round(0.5*rand(1,len) + 0.5*0.9);
enc_bin1 = [];
enc_bin2 = [];
enc1 = [];
enc2 = [];
% metoda 1
for ind=1:len
if bin1(ind)==1
if ind == 1
enc_bin1 = 1;
else
enc_bin1(end) = enc_bin1(end)+1;
end
else
enc_bin1 = [enc_bin1 0];
end
if bin2(ind)==1
if ind == 1
enc_bin2 = 1;
else
enc_bin2(end) = enc_bin2(end)+1;
end
else
enc_bin2 = [enc_bin2 0];
end
end
% metoda 2
i = 1;
j = 1;
while i < length(bin1)
c = 0;
while bin1(i) == 1 & i < length(bin1)
c = c+1;
i = i+1;
end
if c > 0
enc1(j) = c;
j = j+1;
end
i = i+1;
end
i = 1;
j = 1;
while i < length(bin2)
c = 0;
while bin2(i) == 1 & i < length(bin2)
c = c+1;
i = i+1;
end
if c > 0
enc2(j) = c;
j = j+1;
end
i = i+1;
end
ind1 = find(enc_bin1 ~= 0);
ind2 = find(enc_bin2 ~= 0);
[largest_ebin1,ind_largest_ebin1] = max(enc_bin1(ind1));
[largest_ebin2,ind_largest_ebin2] = max(enc_bin2(ind2));
numbits1 = length(dec2bin(largest_ebin1)-'0');
numbits2 = length(dec2bin(largest_ebin2)-'0');
total_size_ebin1 = length(ind1)*numbits1 + length(find(enc_bin1 == 0));
total_size_ebin2 = length(ind2)*numbits2 + length(find(enc_bin2 == 0));
%% repetition chanel coding
clc
clear
len = 100000; % Length of original binary data stream
N1 = 3; % First repetition factor; should be odd to avoid tie
N2 = 5; % Second repetition factor; should be odd to avoid tie
N3 = 7; % Third repetition factor; should be odd to avoid tie
% Generate binary data stream
bin_str = round(rand(1,len));
% Employ repetition code with repetition factors N1, N2, N3
chcode1_bin_str = zeros(1,N1*len);
chcode2_bin_str = zeros(1,N2*len);
chcode3_bin_str = zeros(1,N3*len);
for ind = 1:1:max([N1 N2 N3])
if (ind<=N1)
chcode1_bin_str(ind:N1:(N1*(len-1)+ind))=bin_str;
end
if (ind<=N2)
chcode2_bin_str(ind:N2:(N2*(len-1)+ind))=bin_str;
end
if (ind<=N3)
chcode3_bin_str(ind:N3:(N3*(len-1)+ind))=bin_str;
end
end
% Corrupt both binary strings with zero-mean unit variance Gaussian
% noise followed by rounding (creates "bit flipping" errors)
noisy_bin_str = bin_str + randn(1,len);
rx_bin_str0 = zeros(1,len);
ind0 = find(noisy_bin_str >= 0.5);
rx_bin_str0(ind0) = 1;
noisy_chcode1_bin_str = chcode1_bin_str + randn(1,N1*len);
rx_chcode1_bin_str = zeros(1,N1*len);
ind1 = find(noisy_chcode1_bin_str >= 0.5);
rx_chcode1_bin_str(ind1) = 1;
noisy_chcode2_bin_str = chcode2_bin_str + randn(1,N2*len);
rx_chcode2_bin_str = zeros(1,N2*len);
ind2 = find(noisy_chcode2_bin_str >= 0.5);
rx_chcode2_bin_str(ind2) = 1;
noisy_chcode3_bin_str = chcode3_bin_str + randn(1,N3*len);
rx_chcode3_bin_str = zeros(1,N3*len);
ind3 = find(noisy_chcode3_bin_str >= 0.5);
rx_chcode3_bin_str(ind3) = 1;
dec1_bin = (vec2mat(rx_chcode1_bin_str,N1)).';
dec2_bin = (vec2mat(rx_chcode2_bin_str,N2)).';
dec3_bin = (vec2mat(rx_chcode3_bin_str,N3)).';
ind11 = find(((sum(dec1_bin,1))/N1) >= 0.5);
ind12 = find(((sum(dec2_bin,1))/N2) >= 0.5);
ind13 = find(((sum(dec3_bin,1))/N3) >= 0.5);
rx_bin_str1 = zeros(1,len);
rx_bin_str1(ind11) = 1;
rx_bin_str2 = zeros(1,len);
rx_bin_str2(ind12) = 1;
rx_bin_str3 = zeros(1,len);
rx_bin_str3(ind13) = 1;
ber0 = sum(abs(bin_str - rx_bin_str0))/len;
ber1 = sum(abs(bin_str - rx_bin_str1))/len;
ber2 = sum(abs(bin_str - rx_bin_str2))/len;
ber3 = sum(abs(bin_str - rx_bin_str3))/len;
%% qam
% Decoding QAM waveform using I/Q receiver
% Define parameters
N_samp = 1000; % Number of samples per symbol
N_symb = 10; % Number of symbols in transmission
cfreq = 1/10; % Carrier frequency of cosine and sine carriers
% Generate inphase and quadrature channels with 2-PAM waveforms
chI = 2*round(rand(1,N_symb))-1;
chQ = 2*round(rand(1,N_symb))-1;
samp_I = [];
samp_Q = [];
for ind = 1:1:N_symb
samp_I = [samp_I chI(ind)*ones(1,N_samp)];
samp_Q = [samp_Q chQ(ind)*ones(1,N_samp)];
end
% Apply cosine and sine carriers to inphase and quadrature components,
% sum waveforms together into composite transmission
tx_signal = samp_I.*cos(2.*pi.*cfreq.*(1:1:length(samp_I)))+ samp_Q.*sin(2.*pi.*cfreq.*(1:1:length(samp_Q)));
% Separate out inphase and quadrature components from composite
%% modulation comparisons
clc
clear
len = 10000;
nvar = 0.15;
bin_str1 = round(rand(1,len));
bin_str2 = round(rand(1,len));
ind_wavefm = 2.*bin_str2 + 1.*bin_str1;
wavefm_4pam = zeros(1,len); % 4-PAM
wavefm_4qam = zeros(1,len); % 4-QAM
wavefm_qpsk = zeros(1,len); % QPSK
symb_4pam = [-3 -1 3 1];
symb_4qam = [-1+i 1+i -1-i 1-i];
symb_qpsk = [exp(i*(pi/5+pi/2)) exp(i*(pi/5+pi)) exp(i*(pi/5+0)) exp(i*(pi/5+3*pi/2)) ];
for ind = 1:4
wavefm_4pam(find(ind_wavefm == (ind-1))) = symb_4pam(ind);
wavefm_4qam(find(ind_wavefm == (ind-1))) = symb_4qam(ind);
wavefm_qpsk(find(ind_wavefm == (ind-1))) = symb_qpsk(ind);
end
noise_signal = (1/sqrt(2))*sqrt(nvar)*randn(1,len) + i*(1/sqrt(2))*sqrt(nvar)*randn(1,len);
rx_wavefm_4pam = wavefm_4pam + noise_signal;
rx_wavefm_4qam = wavefm_4qam + noise_signal;
rx_wavefm_qpsk = wavefm_qpsk + noise_signal;
% Go through every received waveform and determine Euclidean distance
% between received waveform and the available waveforms
eucl_dist_4pam = zeros(4,len);
eucl_dist_4qam = zeros(4,len);
eucl_dist_qpsk = zeros(4,len);
for ind = 1:1:4
eucl_dist_4pam(ind,1:1:len) = abs(symb_4pam(ind).*ones(1,len) - rx_wavefm_4pam);
eucl_dist_4qam(ind,1:1:len) = abs(symb_4qam(ind).*ones(1,len) - rx_wavefm_4qam);
eucl_dist_qpsk(ind,1:1:len) = abs(symb_qpsk(ind).*ones(1,len) - rx_wavefm_qpsk);
end
% Select shortest Euclidean distances
[mdist_4pam,min_ind_4pam] = min(eucl_dist_4pam);
[mdist_4qam,min_ind_4qam] = min(eucl_dist_4qam);
[mdist_qpsk,min_ind_qpsk] = min(eucl_dist_qpsk);
% Decode into estimated binary streams
bin_str_est_4pam = dec2bin(min_ind_4pam-ones(1,len)).';
bin_str_est_4qam = dec2bin(min_ind_4qam-ones(1,len)).';
bin_str_est_qpsk = dec2bin(min_ind_qpsk-ones(1,len)).';
% Calculate bit error rate
ber_4pam = sum([abs((bin_str_est_4pam(1,:)-'0') - bin_str2) ...
abs((bin_str_est_4pam(2,:)-'0') - bin_str1)])/(2*len);
ber_4qam = sum([abs((bin_str_est_4qam(1,:)-'0') - bin_str2) ...
abs((bin_str_est_4qam(2,:)-'0') - bin_str1)])/(2*len);
ber_qpsk = sum([abs((bin_str_est_qpsk(1,:)-'0') - bin_str2) ...
abs((bin_str_est_qpsk(2,:)-'0') - bin_str1)])/(2*len);