-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAMI_BER_FINAL.m
275 lines (199 loc) · 6.93 KB
/
AMI_BER_FINAL.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
%%
% Intergrated Communication System
close all;
clear all;
clc;
%% Parameters
Nbits = 1000; % Number of bits generated
Rbits = 1000; % bps
Ts = 1/Rbits;
% SNR
SNR = [5 6 7 8 9 10 11 12 ];
lenSNR = length(SNR);
Nthres = 100;
% RRC filter
rr = 0.35; % RRC roll-off factor
Nup = 4; % Upsampling rate
rgd = 4; % RRC group delay
Rsam = Nup*Rbits;
Nb2d = 1; % sample to start down-sampling
% valid values: 1:Nup
Npre = 2*rgd*Nup; % Length of preamble bits
pre_pat = zeros(1,Npre); % create the preamble pattern
pre_pat(1:2:Npre) = 1;
pre_pat(Npre) = 1;
% DAC
Bdac = 8;
DRdac = 2; % DAC dynamic range, in Volts
% Arrays initialization
errors = zeros(1, lenSNR);
Ber = zeros(1,lenSNR);
Sym = zeros(Nbits,1);
Sam_dac = zeros(Nbits*Nup,1);
% Calculate RRC coefficients
disp('SRRC filter - Channel output');
rcosSpec = fdesign.pulseshaping(Nup, 'Square Root Raised Cosine', 'Nsym,beta', 2*rgd, rr);
rcosFlt = design(rcosSpec);
rcosFltcoefs = rcosFlt.Numerator/max(rcosFlt.Numerator);
%% Modulator Model
disp('AMI Modulator ....');
disp(' ');
for m=1:lenSNR
cnt = 0;
fprintf('\nSNR = %3.2f dB', SNR(m));
while errors(m) < Nthres
cnt=cnt+1;
Sym = zeros(Nbits,1);
Sam_dac = zeros(Nbits*Nup,1);
for ns = 1:(Nbits+Npre)
if ns <= Npre
Bits(ns) = pre_pat(ns);
else
Bits(ns) = randi([0, 1]); % New bit
end
% AMI modulation
flag=0;
for i=1:length(Bits)
if Bits(i)==0
Sym(i)=0;
else
if flag==0
Sym(i)=1;
flag=1;
else
Sym(i)=-1;
flag=0;
end
end
end
%RRC
SNup(2*Nup*rgd+(ns-1)*Nup+1:2*Nup*rgd+ns*Nup) = [Sym(ns) zeros(1, Nup-1)]; % Insert (Nup-1) zeros
for mn=1:Nup % Pulse shape (Direct FIR)
len = 2*Nup*rgd+ns*Nup;
tmp = SNup(len+mn-Nup-2*Nup*rgd:len+mn-Nup);
Sam_dac((ns-1)*Nup+mn) = sum(tmp.*rcosFltcoefs);
end
end
Sam_dac = Sam_dac';
%% Channel
Ps = sum(Sam_dac.^2)/length(Sam_dac);
Pn = Ps/(10^(SNR(m)/10));
ch_noise = sqrt(Pn)*(sqrt(2*log(1./(1-rand(1,length(Sam_dac)))))).*cos(2*pi*rand(1,length(Sam_dac)));
% fprintf('\nAWGN noise added. SNR = %3.1f dB', 10*log10(Ps/Pn));
Sam_adc = Sam_dac + ch_noise;
%% Rx
Sam_rrc = filter(rcosFltcoefs, 1, Sam_adc);
%% Inverse AMI
sym_rx = Sam_rrc(Nb2d:Nup:end);
sym_rx2=sym_rx;
for ns=1:length( sym_rx2);
abs1=abs( sym_rx2(ns)+1);
abs2=abs( sym_rx2(ns));
abs3=abs( sym_rx2(ns)-1);
y=min([abs1 abs2 abs3]);
if y==abs1
sym_rx2(ns)=-1;
elseif y==abs2
sym_rx2(ns)=0;
else
sym_rx2(ns)=1;
end
end
for j=1:length( sym_rx2)
if sym_rx2(j)==0
bits_rx(j)=0;
else
bits_rx(j)=1;
end
end
%%
% Correlation
t1 = (0:length(Bits)-1)/Rbits;
t2 = (0:length(bits_rx)-1)/Rbits;
[acor,lag] = xcorr(Bits,bits_rx);
[~,I] = max(abs(acor));
lagDiff = lag(I);
timeDiff = lagDiff/Rbits;
BitsExcor = bits_rx(abs(lagDiff)+1:end);
%%
sum2=0;
for ns=1:length(BitsExcor)
sum2=sum2+bitxor(BitsExcor(ns),Bits(ns));
end
errors(m) = errors(m) + sum2;
end
%Ber
Ber(m) = errors(m)/(cnt*Nbits);
fprintf(' BER = %3.2e', Ber(m));
fprintf(' No of Bits = %d Errors = %d\n', cnt*Nbits, errors(m));
end
%% Display
% % Scope
%
% % fprintf('\nSNR = %3.1d dB',SNR);
%
%
% scrsz = get(0,'ScreenSize');
% figure('Position', [50 50 scrsz(3)-100 scrsz(4)-200]);
% set(gcf, 'color', 'white');
% FontSize = 11;
% set(gcf,'DefaultLineLineWidth',1);
% set(gcf,'DefaultTextFontSize', FontSize, 'DefaultAxesFontSize', FontSize, 'DefaultLineMarkerSize', FontSize);
%
%
%
% ax(1) = subplot(6,1,1);
% stem((0:length(Sym)-1)*Ts, Bits);
% ylabel('Bits');
% title(['SNR = ',num2str(SNR)]);
%
% grid on
%
% ax(2) = subplot(6,1,2);
% stem((0:length(Sym)-1)*Ts, Sym);
% ylabel('Sym');
% grid on
%
% ax(3) = subplot(6,1,3);
% plot((0:length(Sam_dac)-1)*Ts/Nup, Sam_dac);
% hold on;
% plot((0:length(Sam_adc)-1)*Ts/Nup, Sam_adc, '-r');
%
% legend('Sam dac','Sam adc');
%
% grid on
%
% ax(4) = subplot(6,1,4);
% plot((0:length(Sam_rrc)-1)*Ts/Nup, Sam_rrc, '-o', 'MarkerSize', 4);
% ylabel('Sam rrc');
%
% grid on
%
% ax(5) = subplot(6,1,5);
% stem((0:length(sym_rx)-1)*Ts, sym_rx2);
% ylabel('Sym rx');
%
% grid on
%
% ax(6) = subplot(6,1,6);
% stem((0:length(BitsExcor)-1)*Ts, BitsExcor);
% ylabel('BitsExcor');
% grid on
%
%
%
% linkaxes(ax,'x');
%%
% Scope
scrsz = get(0,'ScreenSize');
figure('Position',[scrsz(4)/20 scrsz(4)/8 scrsz(3)/2 6*scrsz(4)/8]);
set(gcf, 'color', 'white');
FontSize = 13;
set(gcf,'DefaultLineLineWidth',2);
set(gcf,'DefaultTextFontSize', FontSize, 'DefaultAxesFontSize', FontSize, 'DefaultLineMarkerSize', FontSize);
semilogy(SNR, Ber, '.-');
grid on;
axis([min(SNR)-1 max(SNR)+1 1e-6 1]);
xlabel('SNR [dB]');
ylabel('BER');
text(4.2, 0.014, 'AMI', 'Color', 'b')