-
Notifications
You must be signed in to change notification settings - Fork 5
/
cdma.m
476 lines (379 loc) · 9.57 KB
/
cdma.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
% Simulation of CDMA communication system
% https://github.com/flandrade/cdma-simulation
%
% Copyright 2014. Fernanda Andrade
% Universidad de las Fuerzas Armadas - ESPE
%
% Last modified 16-Dec-2015
clear all
clc
%--------------------------------------------------------------------------
%----------------- COMMUNICATION OPTIONS ----------------------------------
%--------------------------------------------------------------------------
%----------------- Quantization -------------------------------------------
%Number of levels
nivel=32;
%TYPE OF QUANTIZATION
% Choose:
% 1 = Uniform
% 2 = Mu-law
% 3 = A-Law
opcion=1;
%----------------- Multiple Access ----------------------------------------
%TYPE OF CODE
% 1 = Orthogonal (Synchronous)
% 2 = Random (Asynchronous)
opt=1;
%GP Value
Gp=16;
%----------------- AWGN Chanel -------------------------------------------
% Eb/N0 The energy per bit to noise power spectral density ratio
% Option: 1 to 10 where 10 is the least noisy
ebno=5;
%%
%--------------------------------------------------------------------------
%---------------------- TRANSMITTER ---------------------------------------
%--------------------------------------------------------------------------
%--------------------- LOADING VOICES -------------------------------------
%Number of users
numusuarios = 4;
%Loading voices
[x1,fm1]=audioread('voz1.wav');
[x2,fm2]=audioread('voz2.wav');
[x3,fm3]=audioread('voz3.wav');
[x4,fm4]=audioread('voz4.wav');
%PLOT
%Plotting input signals (voices)
figure(1)
subplot(2,2,1)
plot(x1)
axis([ 0 4500 min(x1) max(x1) ])
title('Input signal 1');
subplot(2,2,2)
plot(x2)
axis([ 0 4500 min(x2) max(x2) ])
title('Input signal 2');
subplot(2,2,3)
plot(x3)
axis([ 0 4500 min(x3) max(x3) ])
title('Input signal 3');
subplot(2,2,4)
plot(x4)
axis([ 0 4500 min(x3) max(x3) ])
title('Input signal 4');
% Playing voices
disp('Playing input signals');
soundsc(x1,fm1);
pause(3)
soundsc(x2,fm2);
pause(3)
soundsc(x3,fm3);
pause(3)
soundsc(x4,fm4);
pause(3)
%------------------------- QUANTIZATION -----------------------------------
%Quantization
[y_1, x2_1, errorcuantizacion_1] = cuantizar(x1,opcion,nivel);
[y_2, x2_2, errorcuantizacion_2] = cuantizar(x2,opcion,nivel);
[y_3, x2_3, errorcuantizacion_3] = cuantizar(x3,opcion,nivel);
[y_4, x2_4, errorcuantizacion_4] = cuantizar(x4,opcion,nivel);
%Variables to plot
xg=x2_1; y=y_1; errorcuantizacion=errorcuantizacion_1;
x=x1; fm=fm1;
%PLOT
%Plotting input signal with levels of quantization
figure(2)
subplot(2,1,1)
plot(x);
axis([ 0 4500 min(x) max(x) ])
hold on
for vv=1:nivel
hold on
plot(y_1(vv,:));
end
grid on
xlabel('samples')
ylabel('x(t)')
title('Input signal with levels')
subplot(2,1,2)
plot(xg)
axis([ 0 4500 min(xg) max(xg)])
grid on
xlabel('samples')
title('Input signal quantized')
%---------------------- REMAP MATRIX TO TRANSMIT --------------------------
%------------ USER 1
%Transform
x3=x2_1-1;
bits=dec_bin(x3,log2(nivel));
[m n]=size(bits);
%Matrix to vector
tem=[];
for i=1:size(bits,1)
for j=1:size(bits,2)
tem=[tem bits(i,j)];
end
end
bitsc_1=tem;
%------------ USER 2
%Transform
x3=x2_2-1;
bits=dec_bin(x3,log2(nivel));
%Matrix to vector
tem=[];
for i=1:size(bits,1)
for j=1:size(bits,2)
tem=[tem bits(i,j)];
end
end
bitsc_2=tem;
%------------ USER 3
%Transforma
x3=x2_3-1;
bits=dec_bin(x3,log2(nivel));
%Matrix to vector
tem=[];
for i=1:size(bits,1)
for j=1:size(bits,2)
tem=[tem bits(i,j)];
end
end
bitsc_3=tem;
%------------ USER 4
%Transform
x3=x2_4-1;
bits=dec_bin(x3,log2(nivel));
%Matrix to vector
tem=[];
for i=1:size(bits,1)
for j=1:size(bits,2)
tem=[tem bits(i,j)];
end
end
bitsc_4=tem;
%%
%---------------------------- MODULATION ---------------------------------
%Matrix
bits=[bitsc_1; bitsc_2; bitsc_3; bitsc_4];
nbits=length(bits);
%BPSK matrix
bitsm=bits*2-1;
%%
%---------------------------- MULTIPLE ACCESS ----------------------------
%Access codes
if opt==1
codigos=(1/sqrt(Gp))*hadamard(Gp);
codigos=codigos(2:numusuarios+1,:);
else
codigos=(1/sqrt(Gp))*((round(rand(numusuarios,Gp)))*2-1);
%Correlation matrix
R=codigos*codigos';
end
%Repeat bits and remap according to GP, code and nbits
y_tx=kron(bitsm,ones(1,Gp)).*repmat(codigos,1,nbits); %BPSK
%%
%--------------------------------------------------------------------------
%---------------------- AWGN CHANNEL --------------------------------------
%--------------------------------------------------------------------------
ebn0=10^(ebno/10);
sigma=1/sqrt(2*ebn0);
y=sum(y_tx);
%Adding AWGN Noise
ruido=normrnd(0,sigma,1,length(y));
y_canal=y+ruido;
%%
%--------------------------------------------------------------------------
%---------------------- RECEIVER ------------------------------------------
%--------------------------------------------------------------------------
y3=reshape(y_canal,Gp,nbits);
y4=codigos*y3;
%------------ DEMODULATION
bitsmr=sign(y4);
bitsr=(bitsmr+1)/2;
%------------ MESSAGE
%Division
usuario1=bitsr(1,:);
p=1;
for i=1:m
for j=1:n
bitsr_usuario1(i,j)=usuario1(1,p);
p=p+1;
end
end
usuario2=bitsr(2,:);
p=1;
for i=1:m
for j=1:n
bitsr_usuario2(i,j)=usuario2(1,p);
p=p+1;
end
end
usuario3=bitsr(3,:);
p=1;
for i=1:m
for j=1:n
bitsr_usuario3(i,j)=usuario3(1,p);
p=p+1;
end
end
usuario4=bitsr(4,:);
p=1;
for i=1:m
for j=1:n
bitsr_usuario4(i,j)=usuario4(1,p);
p=p+1;
end
end
%---------- Play according to user
disp('1 - User 1')
disp('2 - User 2')
disp('3 - User 3')
disp('4 - User 4')
disp('5 - All (user voices)')
prompt = 'Which user do you want to play? ';
menusuario = input(prompt);
switch menusuario
case 1
%------------ USER 1
xr=int_state(bitsr_usuario1);
xrt=xr+1; xrt=xrt';
soundsc(xrt,fm1);
case 2
%------------ USER 2
xr=int_state(bitsr_usuario2);
xrt=xr+1; xrt=xrt';
soundsc(xrt,fm2);
case 3
%------------ USER 3
xr=int_state(bitsr_usuario3);
xrt=xr+1; xrt=xrt';
soundsc(xrt,fm3);
case 4
%------------ USER 4
xr=int_state(bitsr_usuario4);
xrt=xr+1; xrt=xrt';
soundsc(xrt,fm4);
case 5
%Sum
y5=sum(y4);
%Demodulator
bitsmr=sign(y5);
bitsr=(bitsmr+1)/2;
p=1;
for i=1:m
for j=1:n
bitsr_usuario(i,j)=bitsr(1,p);
p=p+1;
end
end
xr=int_state(bitsr_usuario);
xrt=xr+1; xrt=xrt';
soundsc(xrt,fm1);
otherwise
disp('Playing user 1')
xr=int_state(bitsr_usuario1);
xrt=xr+1; xrt=xrt';
soundsc(xrt,fm1);
end
%Output signal
figure(3)
plot(xrt)
axis([ 0 4500 min(xrt) max(xrt) ])
title('Output signal');
%%
%--------------------------------------------------------------------------
%-------------------------- BER ------------------------------------------
%--------------------------------------------------------------------------
%------------ Calculating PE Error in AWGN Channel
pet=[];
ebn0db=0:1:10;
for eb=ebn0db
ebn0=10^(eb/10);
sigma=1/sqrt(2*ebn0);
error=0;
pe=[];
while error<10
y=sum(y_tx);
%AWGN Noise
ruido=normrnd(0,sigma,1,length(y));
y_canal=y+ruido;
y3=reshape(y_canal,Gp,nbits);
y4=codigos*y3;
%------------ Demodulator
bitsmr=sign(y4);
bitsr=(bitsmr+1)/2;
%------------ Message
%Division
usuario1=bitsr(1,:);
p=1;
for i=1:m
for j=1:n
bitsr_usuario1(i,j)=usuario1(1,p);
p=p+1;
end
end
if numusuarios>1
usuario2=bitsr(2,:);
p=1;
for i=1:m
for j=1:n
bitsr_usuario2(i,j)=usuario2(1,p);
p=p+1;
end
end
if numusuarios>2
usuario3=bitsr(3,:);
p=1;
for i=1:m
for j=1:n
bitsr_usuario3(i,j)=usuario3(1,p);
p=p+1;
end
end
if numusuarios>3
usuario4=bitsr(4,:);
p=1;
for i=1:m
for j=1:n
bitsr_usuario4(i,j)=usuario4(1,p);
p=p+1;
end
end
end
end
end
% Bit Error Rate (BER) according to user
size(bitsc_1);
size(usuario1);
size(bitsr_usuario1);
switch menusuario
case 1
error=error+sum(xor(bitsc_1,usuario1));
pe=[pe sum(xor(bitsc_1,usuario1))/length(y_tx)];
case 2
error=error+sum(xor(bitsc_2,usuario2));
pe=[pe sum(xor(bitsc_2,usuario2))/length(y_tx)];
case 3
error=error+sum(xor(bitsc_3,usuario3));
pe=[pe sum(xor(bitsc_3,usuario3))/length(y_tx)];
case 4
error=error+sum(xor(bitsc_4,usuario4));
pe=[pe sum(xor(bitsc_4,usuario4))/length(y_tx)];
otherwise
break
end
end
%p=mean(pe);
pet=[pet mean(pe)];
end
%Pe error
errorpe=mean(pet);
%BER Plot
if (menusuario==1 || menusuario==2 || menusuario==3 || menusuario==4)
figure(4)
semilogy(ebn0db,pet,'o-');
xlabel('Eb/N0, dB')
ylabel('Bit Error Rate')
title('BER Curve')
grid on;
end