-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcode.m
540 lines (504 loc) · 13.4 KB
/
mcode.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
%%
%parta
clc;clear;close all;
Fs = 1e06; Fc=1e04;F0=1e04;Bw=1e03;
Pulse = 1e-03;
length_of_pulse = Pulse*Fs;
One=ones(1,length_of_pulse);
Zero=-ones(1,length_of_pulse);
x=(sign(rand(1,100)-.5)+1)/2;
figure()
stem(x, 'r.');
xlabel('t');
ylabel('input pulse');
title('input pulse time domine ');
grid on;grid minor;
[in1,in2]=Divide(x);
input1=PulseShaping(in1,One,Zero);
input2=PulseShaping(in2,One,Zero);
t=1/Fs:1/Fs:length(input1)*1/Fs;
figure;
subplot(2,1,1)
plot(t,input1);
xlabel('t');
ylabel('input pulse first part');
title('input pulse first part time domine ');
grid on;grid minor;
subplot(2,1,2)
plot(t,input2);
xlabel('t');
ylabel('input pulse second part');
title('input pulse second part time domine ');
grid on;grid minor;
X=AnalogMod(input1,input2,Fs,Fc);
figure;
subplot(2,1,1);
plot(t,X);
xlabel('t');
ylabel('dumolated signal in channle');
title('dumolated signal in channle time domine ');
grid on;grid minor;
xlim([0,0.005]);
output=Channel(X,Fs,F0,Bw);
subplot(2,1,2);
plot(t,output);
ylabel('dumolated signal in channle after channel');
title('dumolated signal in channle after channel time domine ');
grid on;grid minor;
xlim([0,0.005]);
[out1,out2]=AnalogDemod(output,Fs,Bw,Fc);
figure;
subplot(2,1,1);
plot(t,out1);
xlabel('t');
ylabel('demolated signal part one ');
title('demolated signal part one time domine ');
grid on;grid minor;
ylim([-1.5,1.5]);
subplot(2,1,2);
plot(t,out2);
xlabel('t');
ylabel('demolated signal part two ');
title('demolated signal part two time domine ');
grid on;grid minor;
ylim([-1.5,1.5]);
[one_cor,zero_cor,out1]=MatchedFilt(real(out1),One,Zero);
figure;
subplot(2,1,1);
stem(one_cor,'r*')
ylabel('crrolation ');
title('crrolation of first part of signal with "one" ');
grid on;grid minor;
subplot(2,1,2);
stem(zero_cor,'c.');
ylabel('crrolation ');
title('crrolation of first part of signal with "zero" ');
grid on;grid minor;
[one_cor,zero_cor,out2]=MatchedFilt(real(out2),One,Zero);
figure;
subplot(2,1,1);
stem(one_cor,'r*')
ylabel('crrolation ');
title('crrolation of second part of signal with "one" ');
grid on;grid minor;
subplot(2,1,2);
stem(zero_cor,'c.');
ylabel('crrolation ');
title('crrolation of second part of signal with "zero" ');
grid on;grid minor;
figure;
subplot(2,1,1)
stem(out1,'r*');
xlabel('t');
ylabel('input pulse first part AFTER CHANNLE');
title('input pulse first part AFTER CHANNLE time domine ');
grid on;grid minor;
subplot(2,1,2)
stem(out2,'g*');
xlabel('t');
ylabel('input pulse second part AFTER CHANNLE');
title('input pulse second part AFTER CHANNLE time domine ');
grid on;grid minor;
demulated=Combine(out1,out2);
figure();
subplot(2,1,1);
stem(demulated,'r*');
ylabel('input pulse after channle');
title('input pulse after channle time domine ');
grid on;grid minor;
subplot(2,1,2);
stem(x,'g*');
ylabel('input pulse');
title('input pulse time domine ');
grid on;grid minor;
%%
%partb
clc; clear;
Fs = 1e06;Fc=1e04;F0=1e04;Bw=1e03;
Pulse=1e-03;
length_of_pulse=Pulse*Fs;
One=ones(1,length_of_pulse);
zero=-ones(1,length_of_pulse);
snr=linspace(0,-800,800);
for i=1:1:length(snr)
i
x=(sign(rand(1,50)-.5)+1)/2;
[in1,in2]=Divide(x);
input1=PulseShaping(in1,One,zero);
input2=PulseShaping(in2,One,zero);
X = AnalogMod(input1,input2,Fs,Fc);
X = awgn(X,snr(i));
output = Channel(X,Fs,F0,Bw);
[out1,out2] = AnalogDemod(output,Fs,Bw,Fc);
[cor1,cor0,out1] = MatchedFilt(real(out1),One,zero);
[cor1,cor0,out2] = MatchedFilt(real(out2),One,zero);
demulated = Combine(out1,out2);
er(i)=sum(abs(x-demulated))/length(x)*100;
end
figure;
plot(snr,er,'r');
xlabel('snr');
ylabel('erobability of error');
title('probability of error in snr domin');
grid on;
grid minor;
%%
%histogram
clc; clear;
Fs = 1e06;Fc=1e04;F0=1e04;Bw=1e03;
dt = 1/Fs;
Pulse=1e-03;
length_of_pulse=Pulse*Fs;
One=ones(1,length_of_pulse);
zero=-ones(1,length_of_pulse);
snr=linspace(0,-800,16);
figure;
for i=1:1:length(snr)
i
x=(sign(rand(1,50)-.5)+1)/2;
[in1,in2]=Divide(x);
input1=PulseShaping(in1,One,zero);
input2=PulseShaping(in2,One,zero);
X = AnalogMod(input1,input2,Fs,Fc);
X = awgn(X,snr(i));
output = Channel(X,Fs,F0,Bw);
[out1,out2] = AnalogDemod(output,Fs,Bw,Fc);
[cor1_1,cor0_1,out1] = MatchedFilt(real(out1),One,zero);
[cor1_2,cor0_2,out2] = MatchedFilt(real(out2),One,zero);
subplot(4,4,i);
hist3([cor1_1',cor0_1'],'CDataMode','auto','FaceColor','interp');
xlabel('cor1')
ylabel('cor0')
title(['snr of noise is' ,num2str(snr(i)), 'db'])
grid on; grid minor;
demulated = Combine(out1,out2);
er(i)=sum(abs(x-demulated))/length(x)*100;
end
%%
%partd______500hz
clc;clear;close all;
Fs = 1e06; Fc=1e04;F0=1e04;Bw=1e03;
Pulse = 1e-03;
length_of_pulse = Pulse*Fs;
for i =1:length_of_pulse
One(i)=sin(2*pi*500*i/Fs);
Zero(i)=-sin(2*pi*500*i/Fs);
end
x=(sign(rand(1,100)-.5)+1)/2;
figure()
stem(x, 'r.');
xlabel('t');
ylabel('input pulse');
title('input pulse time domine ');
grid on;grid minor;
[in1,in2]=Divide(x);
input1=PulseShaping(in1,One,Zero);
input2=PulseShaping(in2,One,Zero);
t=1/Fs:1/Fs:length(input1)*1/Fs;
figure;
subplot(2,1,1)
plot(t,input1);
xlabel('t');
ylabel('input pulse first part');
title('input pulse first part time domine ');
grid on;grid minor;
subplot(2,1,2)
plot(t,input2);
xlabel('t');
ylabel('input pulse second part');
title('input pulse second part time domine ');
grid on;grid minor;
X=AnalogMod(input1,input2,Fs,Fc);
figure;
subplot(2,1,1);
plot(t,X);
xlabel('t');
ylabel('dumolated signal in channle');
title('dumolated signal in channle time domine ');
grid on;grid minor;
xlim([0,0.005]);
output=Channel(X,Fs,F0,Bw);
subplot(2,1,2);
plot(t,output);
ylabel('dumolated signal in channle after channel');
title('dumolated signal in channle after channel time domine ');
grid on;grid minor;
xlim([0,0.005]);
[out1,out2]=AnalogDemod(output,Fs,Bw,Fc);
figure;
subplot(2,1,1);
plot(t,out1);
xlabel('t');
ylabel('demolated signal part one ');
title('demolated signal part one time domine ');
grid on;grid minor;
ylim([-1.5,1.5]);
subplot(2,1,2);
plot(t,out2);
xlabel('t');
ylabel('demolated signal part two ');
title('demolated signal part two time domine ');
grid on;grid minor;
ylim([-1.5,1.5]);
[one_cor,zero_cor,out1]=MatchedFilt(real(out1),One,Zero);
figure;
subplot(2,1,1);
stem(one_cor,'r*')
ylabel('crrolation ');
title('crrolation of first part of signal with "one" ');
grid on;grid minor;
subplot(2,1,2);
stem(zero_cor,'c.');
ylabel('crrolation ');
title('crrolation of first part of signal with "zero" ');
grid on;grid minor;
[one_cor,zero_cor,out2]=MatchedFilt(real(out2),One,Zero);
figure;
subplot(2,1,1);
stem(one_cor,'r*')
ylabel('crrolation ');
title('crrolation of second part of signal with "one" ');
grid on;grid minor;
subplot(2,1,2);
stem(zero_cor,'c.');
ylabel('crrolation ');
title('crrolation of second part of signal with "zero" ');
grid on;grid minor;
figure;
subplot(2,1,1)
stem(out1,'r*');
xlabel('t');
ylabel('input pulse first part AFTER CHANNLE');
title('input pulse first part AFTER CHANNLE time domine ');
grid on;grid minor;
subplot(2,1,2)
stem(out2,'g*');
xlabel('t');
ylabel('input pulse second part AFTER CHANNLE');
title('input pulse second part AFTER CHANNLE time domine ');
grid on;grid minor;
demulated=Combine(out1,out2);
figure();
subplot(2,1,1);
stem(demulated,'r*');
ylabel('input pulse after channle');
title('input pulse after channle time domine ');
grid on;grid minor;
subplot(2,1,2);
stem(x,'g*');
ylabel('input pulse');
title('input pulse time domine ');
grid on;grid minor;
%%
%%noise 500hz
clc; clear;
Fs = 1e06;Fc=1e04;F0=1e04;Bw=1e03;
dt = 1/Fs;
Pulse=1e-03;
length_of_pulse=Pulse*Fs;
for i =1:length_of_pulse
One(i)=sin(2*pi*500*i/Fs);
zero(i)=-sin(2*pi*500*i/Fs);
end
snr=linspace(0,-800,25);
figure;
for i=1:1:length(snr)
i
x=(sign(rand(1,50)-.5)+1)/2;
[in1,in2]=Divide(x);
input1=PulseShaping(in1,One,zero);
input2=PulseShaping(in2,One,zero);
X = AnalogMod(input1,input2,Fs,Fc);
X = awgn(X,snr(i));
output = Channel(X,Fs,F0,Bw);
[out1,out2] = AnalogDemod(output,Fs,Bw,Fc);
[cor1,cor0,out1] = MatchedFilt(real(out1),One,zero);
[cor1,cor0,out2] = MatchedFilt(real(out2),One,zero);
demulated = Combine(out1,out2);
er(i)=sum(abs(x-demulated))/length(x)*100;
end
figure;
plot(snr,er,'r');
xlabel('snr');
ylabel('erobability of error');
title('probability of error in snr domin');
grid on;
grid minor;
%%
%histogram 500 hz
clc; clear;
Fs = 1e06;Fc=1e04;F0=1e04;Bw=1e03;
dt = 1/Fs;
Pulse=1e-03;
length_of_pulse=Pulse*Fs;
for i =1:length_of_pulse
One(i)=sin(2*pi*500*i/Fs);
zero(i)=-sin(2*pi*500*i/Fs);
end
snr=linspace(0,-800,16);
figure;
for i=1:1:length(snr)
i
x=(sign(rand(1,50)-.5)+1)/2;
[in1,in2]=Divide(x);
input1=PulseShaping(in1,One,zero);
input2=PulseShaping(in2,One,zero);
X = AnalogMod(input1,input2,Fs,Fc);
X = awgn(X,snr(i));
output = Channel(X,Fs,F0,Bw);
[out1,out2] = AnalogDemod(output,Fs,Bw,Fc);
[cor1_1,cor0_1,out1] = MatchedFilt(real(out1),One,zero);
[cor1_2,cor0_2,out2] = MatchedFilt(real(out2),One,zero);
subplot(4,4,i);
hist3([cor1_1',cor0_1'],'CDataMode','auto','FaceColor','interp');
xlabel('cor1')
ylabel('cor0')
title(['snr of noise is' ,num2str(snr(i)), 'db'])
grid on; grid minor;
demulated = Combine(out1,out2);
er(i)=sum(abs(x-demulated))/length(x)*100;
end
%%
%bakhsh se
clc; clear;
Fs = 1e06;Fc=1e04;F0=1e04;Bw=1e03;
Pulse=1e-03;
length_of_pulse=Pulse*Fs;
One=ones(1,length_of_pulse);
zero=-ones(1,length_of_pulse);
snr=linspace(-700,-800,5);
for i=1:1:length(snr)
i
snr(i);
x = floor((rand(1,100)*256));
a = SourceGenerator(x);
[in1,in2]=Divide(a);
input1=PulseShaping(in1,One,zero);
input2=PulseShaping(in2,One,zero);
X = AnalogMod(input1,input2,Fs,Fc);
X = awgn(X,snr(i));
output = Channel(X,Fs,F0,Bw);
[out1,out2] = AnalogDemod(output,Fs,Bw,Fc);
[cor1,cor0,out1] = MatchedFilt(real(out1),One,zero);
[cor1,cor0,out2] = MatchedFilt(real(out2),One,zero);
demulated = Combine(out1,out2);
a = OutputDecoder(demulated);
er(i)=sum(abs((x-a)).^2)
end
figure;
plot(snr,er,'r');
xlabel('snr');
ylabel('square of error');
title('square of error in snr domin');
grid on;
grid minor;
%%
%functions
function out = SourceGenerator(x)
out = [];
for i = 1:length(x)
out = [out,de2bi(x(i),8)];
end
end
function out = OutputDecoder(x)
out = [];
for i = 1:8:length(x)
out = [out,bi2de([x(i),x(i+1),x(i+2),x(i+3),x(i+4),x(i+5),x(i+6),x(i+7)])];
end
end
function [ a , b ] = Divide(x)
for i = 1:length(x)
if mod(i,2) == 0
b (i/2) = x(i);
end
if mod(i,2) == 1
a ((i+1)/2) = x(i);
end
end
end
function out = Combine(input1,input2)
out=[];
for i=1:1:length(input1)
out = [out,input1(i),input2(i)];
end
end
function out = PulseShaping( input , one , zero)
out = [];
for i = 1:length(input)
if input(i) == 1 ;
out = [out ,one];
end
if input(i) == 0 ;
out = [out ,zero];
end
end
end
function out = AnalogMod( input1 , input2 , Fs ,Fc)
for i = 1: length(input1)
out(i) = input1(i) * cos( 2*pi*Fc * i / Fs)...
+input2(i) * sin( 2* pi* Fc * i / Fs);
end
end
function out = Channel( input , Fs ,Fc ,Bw)
num = length( input );
fft_signal = fftshift (fft(input));
F = -Fs/2 : Fs/num : Fs/2 - Fs/num;
out_fft = zeros(1,length(fft_signal));
min2 = floor((Fc-Bw/2+Fs/2)/(Fs/num));
max2 = floor((Fc+Bw/2+Fs/2)/(Fs/num));
min1 = floor((-Fc-Bw/2+Fs/2)/(Fs/num));
max1 = floor(-Fc+Bw/2+Fs/2)/(Fs/num);
for i = min1-100:1:max1+100
if abs(F(i)-Fc)<=Bw/2
out_fft(i) = fft_signal(i);
elseif abs(F(i)+Fc)<=Bw/2
out_fft(i) = fft_signal(i);
else
out_fft(i) = 0;
end
end
for i = min2-100:1:max2+100
if abs(F(i)-Fc)<=Bw/2
out_fft(i) = fft_signal(i);
elseif abs(F(i)+Fc)<=Bw/2
out_fft(i) = fft_signal(i);
else
out_fft(i) = 0;
end
end
out = ifft(ifftshift(out_fft));
end
function [x1,x2]=AnalogDemod(input,Fs,Bw,Fc)
t=1/Fs:1/Fs:(length(input)*1/Fs);
Cos_vec = cos(2*pi*Fc*t);
Sin_vec = sin(2*pi*Fc*t);
x1=2*input.*Cos_vec;
x2=2*input.*Sin_vec ;
X1=fftshift(fft(x1));
X2=fftshift(fft(x2));
F=linspace(0,Fs,length(X1))-Fs/2;
min = floor(((Fs/2)-(Bw/2))/(Fs/length(X1)));
max = floor(((Fs/2)+(Bw/2))/(Fs/length(X1)));
out1 = zeros(1,length(X1));
out2 = zeros(1,length(X1));
for i=min-100:1:max+100
if(abs(F(i))<Bw)
out1(i)=X1(i);
out2(i)=X2(i);
end
end
x1=ifft(ifftshift(out1));
x2=ifft(ifftshift(out2));
end
function [cor_one,cor_zero,out]=MatchedFilt(input,one,zero)
psd_one = one.*one;
Energy=sum(psd_one);
cor_one=conv(input,one);
cor_zero=conv(input,zero);
out=zeros(1,length(input)/length(zero));
for i=1:length(out)
if cor_one(i*length(zero))>=Energy/2
out(i)=1;
end
end
end