nu-lpw-ec 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,9 @@
1
+ Metadata-Version: 2.4
2
+ Name: nu_lpw_ec
3
+ Version: 0.0.1
4
+ Summary: Enjoy my codes!
5
+ Author-email: None None <userzeroonetwo091@gmail.com>
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: License :: OSI Approved :: MIT License
8
+ Classifier: Operating System :: OS Independent
9
+ Requires-Python: >=3.7
File without changes
@@ -0,0 +1,1177 @@
1
+ def doc():
2
+ print(r"""
3
+ | Function | Description |
4
+ |:--------:|-------------|
5
+ | p1() | Matched Filter Receiver |
6
+ | p2() | GSO |
7
+ | p3() | BPSK |
8
+ | p4() | QPSK |
9
+ | p5() | BFSK |
10
+ | p6() | DPSK |
11
+ | p7() | MPSK |
12
+ | p8() | Linear Block Code |
13
+ | p9() | Conv Code |
14
+ | p10() | Image and Audio |
15
+
16
+ """)
17
+
18
+ def p1():
19
+ print(r"""
20
+ -------------------------------------CELL 1-----------------------------------------------
21
+ clc;
22
+ clear;
23
+ close all;
24
+ % Common Parameters
25
+ Tb = 1; % Bit duration
26
+ Fs = 100; % Sampling frequency
27
+ Ns = Fs*Tb; % Samples/bit
28
+ dt = 1/Fs;
29
+ Eb = 1; % Energy per bit
30
+ A = sqrt(Eb/Tb); % Signal amplitude
31
+ t = 0 : dt : Tb-dt;
32
+ % Generation of Binary Antipodal Signal
33
+ bit = 0; % Change to 0 if desired
34
+ if bit==1
35
+ s = A*ones(1,Ns);
36
+ else
37
+ s = -A*ones(1,Ns);
38
+ end
39
+ figure;
40
+ plot(t,s,'LineWidth',2);
41
+ xlabel('Time (s)');
42
+ ylabel('Amplitude');
43
+ title('Binary Antipodal Signal');
44
+ grid on;
45
+ % Transmission through AWGN Channel
46
+ EbN0_dB = 6;
47
+ EbN0 = 10^(EbN0_dB/10);
48
+ N0 = Eb/EbN0;
49
+ sigma = sqrt(N0/2);
50
+ noise = sigma*randn(1,Ns);
51
+ r = s + noise;
52
+ figure;
53
+ plot(t,r,'LineWidth',1.5);
54
+ xlabel('Time (s)');
55
+ ylabel('Amplitude');
56
+ title('Received Signal');
57
+ grid on;
58
+ % Correlation Receiver
59
+ Correlation_Output = sum(r.*s)*dt;
60
+ fprintf('\nCorrelation Receiver Output = %f\n',Correlation_Output);
61
+ % Matched Filter Receiver
62
+ h = fliplr(s);
63
+ MF_Output = conv(r,h)*dt;
64
+ time_mf = 0:dt:(length(MF_Output)-1)*dt;
65
+ figure;
66
+ plot(time_mf,MF_Output,'LineWidth',2);
67
+ xlabel('Time (s)');
68
+ ylabel('Amplitude');
69
+ title('Matched Filter Output');
70
+ grid on;
71
+ Sample_Output = MF_Output(Ns);
72
+ fprintf('Matched Filter Output = %f\n',Sample_Output);
73
+ % Verification of Equivalence
74
+ Difference = abs(Correlation_Output-Sample_Output);
75
+ fprintf('\nDifference = %e\n',Difference);
76
+ if Difference<1e-10
77
+ disp('Matched Filter = Correlation Receiver');
78
+ else
79
+ disp('Outputs are equal within numerical accuracy.');
80
+ end
81
+ % Optimum Decision Rule
82
+ disp('Decision Device');
83
+ if Sample_Output>0
84
+ DetectedBit=1;
85
+ else
86
+ DetectedBit=0;
87
+ end
88
+ fprintf('\nTransmitted Bit = %d\n',bit);
89
+ fprintf('Detected Bit = %d\n',DetectedBit);
90
+ % BER Performance
91
+ disp('BER Simulation');
92
+ Nbits = 100000;
93
+ EbN0_dB = 0:2:10;
94
+ BER = zeros(size(EbN0_dB));
95
+ for k=1:length(EbN0_dB)
96
+ EbN0 = 10^(EbN0_dB(k)/10);
97
+ N0 = Eb/EbN0;
98
+ sigma = sqrt(N0/2);
99
+ bits = randi([0 1],1,Nbits);
100
+ symbols = 2*bits-1;
101
+ noise = sigma*randn(1,Nbits);
102
+ received = symbols + noise;
103
+ detected = received>0;
104
+ BER(k)=sum(bits~=detected)/Nbits;
105
+ end
106
+ Theory = 0.5*erfc(sqrt(10.^(EbN0_dB/10)));
107
+ figure;
108
+ semilogy(EbN0_dB,BER,'o-','LineWidth',2);
109
+ hold on;
110
+ semilogy(EbN0_dB,Theory,'*-','LineWidth',2);
111
+ xlabel('E_b/N_0 (dB)');
112
+ ylabel('Bit Error Rate');
113
+ title('BER Performance');
114
+ legend('Simulation','Theory','Location','southwest');
115
+ grid on;
116
+
117
+ ------------------------------------------CELL 2------------------------------------
118
+ Modify the MATLAB program to transmit a random sequence of 1000 binary bits instead of a single bit.
119
+ clc;
120
+ clear;
121
+ close all;
122
+ %% Common Parameters
123
+ Tb = 1; % Bit duration
124
+ Fs = 100; % Sampling frequency
125
+ Ns = Fs*Tb; % Samples per bit
126
+ dt = 1/Fs;
127
+ Eb = 1;
128
+ A = sqrt(Eb/Tb);
129
+ t = 0:dt:Tb-dt;
130
+ %% Generate Random Binary Sequence
131
+ Nbits = 1000;
132
+ bits = randi([0 1],1,Nbits);
133
+ DetectedBit_MF = zeros(1,Nbits);
134
+ DetectedBit_Corr = zeros(1,Nbits);
135
+ EbN0_dB = 6;
136
+ EbN0 = 10^(EbN0_dB/10);
137
+ N0 = Eb/EbN0;
138
+ sigma = sqrt(N0/2);
139
+ %% Process Each Bit
140
+ for k = 1:Nbits
141
+ % Binary Antipodal Signal
142
+ if bits(k)==1
143
+ s = A*ones(1,Ns);
144
+ else
145
+ s = -A*ones(1,Ns);
146
+ end
147
+ % AWGN Channel
148
+ noise = sigma*randn(1,Ns);
149
+ r = s + noise;
150
+ % Correlation Receiver
151
+ Correlation_Output = sum(r.*s)*dt;
152
+ if Correlation_Output>0
153
+ DetectedBit_Corr(k)=1;
154
+ else
155
+ DetectedBit_Corr(k)=0;
156
+ end
157
+ % Matched Filter
158
+ h = fliplr(s);
159
+ MF_Output = conv(r,h)*dt;
160
+ Sample_Output = MF_Output(Ns);
161
+ if Sample_Output>0
162
+ DetectedBit_MF(k)=1;
163
+ else
164
+ DetectedBit_MF(k)=0;
165
+ end
166
+
167
+ % Verify Equivalence
168
+ Difference = abs(Correlation_Output-Sample_Output);
169
+ if Difference>1e-10
170
+ fprintf('Difference at Bit %d = %e\n',k,Difference);
171
+ end
172
+ end
173
+ %% BER Calculation
174
+ BER_MF = sum(bits~=DetectedBit_MF)/Nbits;
175
+ BER_Corr = sum(bits~=DetectedBit_Corr)/Nbits;
176
+ %% Display Results
177
+ disp('-----------------------------------------');
178
+ disp('Matched Filter and Correlation Receiver');
179
+ disp('-----------------------------------------');
180
+ fprintf('Number of transmitted bits : %d\n',Nbits);
181
+ fprintf('Eb/N0 : %d dB\n',EbN0_dB);
182
+ fprintf('BER using Matched Filter : %e\n',BER_MF);
183
+ fprintf('BER using Correlator : %e\n',BER_Corr);
184
+ disp('-----------------------------------------');
185
+ %% Plot First Transmitted Signal
186
+ if bits(1)==1
187
+ s = A*ones(1,Ns);
188
+ else
189
+ s = -A*ones(1,Ns);
190
+ end
191
+ figure;
192
+ plot(t,s,'LineWidth',2);
193
+ xlabel('Time (s)');
194
+ ylabel('Amplitude');
195
+ title('First Transmitted Binary Antipodal Signal');
196
+ grid on;
197
+ %% Plot First Received Signal
198
+ noise = sigma*randn(1,Ns);
199
+ r = s + noise;
200
+ figure;
201
+ plot(t,r,'LineWidth',2);
202
+ xlabel('Time (s)');
203
+ ylabel('Amplitude');
204
+ title('First Received Signal');
205
+ grid on;
206
+ %% Plot First Matched Filter Output
207
+ h = fliplr(s);
208
+ MF_Output = conv(r,h)*dt;
209
+ tmf = 0:dt:(length(MF_Output)-1)*dt;
210
+ figure;
211
+ plot(tmf,MF_Output,'LineWidth',2);
212
+ xlabel('Time (s)');
213
+ ylabel('Amplitude');
214
+ title('Matched Filter Output');
215
+ grid on;
216
+
217
+ """)
218
+ def p2():
219
+ print(r"""
220
+ p2
221
+ ---------------------------------------CELL 1------------------------------
222
+ clear;
223
+ clc;
224
+ close all;
225
+
226
+ T = 3;
227
+ fs = 5000;
228
+ dt = 1/fs;
229
+ t = 0:dt:T-dt;
230
+ tol = 1e-10;
231
+
232
+ s1 = double((t >= 0) & (t < T/3));
233
+ s2 = double((t >= 0) & (t < 2*T/3));
234
+ s3 = double((t >= T/3) & (t < T));
235
+ s4 = double((t >= 0) & (t <= T));
236
+
237
+ g1 = s1;
238
+ E1 = sum(g1.^2)*dt;
239
+ phi1 = g1/sqrt(E1);
240
+
241
+ S21 = sum(s2.*phi1)*dt;
242
+ g2 = s2 - S21*phi1;
243
+ E2 = sum(g2.^2)*dt;
244
+ phi2 = g2/sqrt(E2);
245
+
246
+ S31 = sum(s3.*phi1)*dt;
247
+ S32 = sum(s3.*phi2)*dt;
248
+ g3 = s3 - S31*phi1 - S32*phi2;
249
+ E3 = sum(g3.^2)*dt;
250
+
251
+ if E3 < tol
252
+ phi3 = zeros(size(t));
253
+ else
254
+ phi3 = g3/sqrt(E3);
255
+ end
256
+
257
+ S41 = sum(s4.*phi1)*dt;
258
+ S42 = sum(s4.*phi2)*dt;
259
+ S43 = sum(s4.*phi3)*dt;
260
+ g4 = s4 - S41*phi1 - S42*phi2 - S43*phi3;
261
+ E4 = sum(g4.^2)*dt;
262
+
263
+ if E4 < tol
264
+ phi4 = zeros(size(t));
265
+ else
266
+ phi4 = g4/sqrt(E4);
267
+ end
268
+
269
+ figure('Position',[100 100 900 700]);
270
+ ---------------------------------------CELL 2------------------------------
271
+ subplot(4,2,1);
272
+ plot(t,s1,'LineWidth',2);
273
+ title('s_1(t)');
274
+ grid on;
275
+
276
+ subplot(4,2,2);
277
+ plot(t,phi1,'LineWidth',2);
278
+ title('phi_1(t)');
279
+ grid on;
280
+
281
+ subplot(4,2,3);
282
+ plot(t,s2,'LineWidth',2);
283
+ title('s_2(t)');
284
+ grid on;
285
+
286
+ subplot(4,2,4);
287
+ plot(t,phi2,'LineWidth',2);
288
+ title('phi_2(t)');
289
+ grid on;
290
+
291
+ subplot(4,2,5);
292
+ plot(t,s3,'LineWidth',2);
293
+ title('s_3(t)');
294
+ grid on;
295
+
296
+ subplot(4,2,6);
297
+ plot(t,phi3,'LineWidth',2);
298
+ title('phi_3(t)');
299
+ grid on;
300
+
301
+ subplot(4,2,7);
302
+ plot(t,s4,'LineWidth',2);
303
+ title('s_4(t)');
304
+ grid on;
305
+
306
+ subplot(4,2,8);
307
+ plot(t,phi4,'LineWidth',2);
308
+ title('phi_4(t)');
309
+ grid on;
310
+ """)
311
+ def p3():
312
+ print(r"""
313
+ p3
314
+ ---------------------------------------BPSK------------------------------
315
+ clc;
316
+ clear all;
317
+ close all;
318
+ N = 5;
319
+ b = randi([0, 1], 1, N); % Binary Message Signal
320
+ A = 5; % Amplitude = 5
321
+ fc = 10;
322
+ fs = 150;
323
+ t = 0:1/fs:1;
324
+ C = A*sin(2*pi*fc*t);
325
+ bpsk_signal = [];
326
+ full_carrier = [];
327
+ full_time = [];
328
+
329
+ %mod = zeros(1:150);
330
+ for i = 1:N
331
+ if b(i) == 1
332
+ mod = C; % Transmit carrier signal for bit 1
333
+ else
334
+ mod = -C; % Transmit zero for bit 0
335
+ end
336
+ bpsk_signal = [bpsk_signal, mod];
337
+ full_carrier = [full_carrier, C];
338
+ full_time = [full_time, t + (i-1)];
339
+ end
340
+
341
+ figure;
342
+
343
+ subplot(2,1,1);
344
+ plot(full_time, full_carrier, 'LineWidth', 1.5);
345
+ grid on;
346
+ xlabel('Time (seconds)');
347
+ ylabel('Amplitude');
348
+ title('Reference Carrier Signal (No Phase Shifts)');
349
+
350
+ subplot(2,1,2);
351
+ plot(full_time, bpsk_signal, 'LineWidth', 1.5);
352
+ grid on;
353
+ xlabel('Time (seconds)');
354
+ ylabel('Amplitude');
355
+ title(['Final BPSK Modulated Signal for Bits: ', num2str(b)]);
356
+
357
+ -------------------------------------Const Dia--------------------------------
358
+ % Scatter Plot (Constellation) of BPSK
359
+
360
+ mod1 = zeros(1, N);
361
+
362
+ for i = 1:N
363
+ if b(i) == 1
364
+ mod1(i) = 1;
365
+ else
366
+ mod1(i) = -1;
367
+ end
368
+ end
369
+
370
+ figure;
371
+ scatterplot(mod1);
372
+ grid on;
373
+ xlabel('(mod1)');
374
+ ylabel('(Q)');
375
+ title(['BPSK Scatter Plot for Bits: ', num2str(b)]);
376
+
377
+ -------------------------------------BER--------------------------------
378
+ Tb = 1;
379
+ Fs = 100; Ns = Fs*Tb;
380
+ dt = 1/Fs; Eb = 1;
381
+ A = sqrt(Eb/Tb);
382
+ t=0:dt:Tb-dt;
383
+ Nbits = 100000;
384
+ EbN0_dB = 0:2:10; BER = zeros(size(EbN0_dB));
385
+ for k=1:length(EbN0_dB)
386
+ EbN0 = 10^(EbN0_dB(k)/10);
387
+ N0 = Eb/EbN0;
388
+ sigma = sqrt(N0/2);
389
+ bits = randi([0 1],1,Nbits);
390
+ symbols = 2*bits-1;
391
+ noise = sigma*randn(1,Nbits);
392
+ received = symbols + noise;
393
+ detected = received>0;
394
+ BER(k)=sum(bits~=detected)/Nbits;
395
+ end
396
+
397
+ -------------------------------------Compare BER--------------------------------
398
+ Theory = 0.5*erfc(sqrt(10.^(EbN0_dB/10)));
399
+ figure;
400
+ semilogy(EbN0_dB, BER,'o-','LineWidth',2);
401
+ hold on;
402
+ semilogy(EbN0_dB,Theory,'*-','LineWidth',2);
403
+ xlabel('E_b/N_0 (dB)');
404
+ ylabel('Bit Error Rate');
405
+ title('BER Performance');
406
+ legend('Simulation','Theory','Location','southwest');
407
+ grid on;
408
+ """)
409
+ def p4():
410
+ print(r"""
411
+ p4
412
+ ---------------------------------------QPSK------------------------------
413
+ clc;
414
+ clear;
415
+ close all;
416
+
417
+ A = 5;
418
+ fc = 5;
419
+ fs = 100;
420
+
421
+ t = 0:1/fs:1;
422
+
423
+ % Carrier signal
424
+ c = A*cos(2*pi*fc*t);
425
+
426
+ % Random binary sequence
427
+ b = [0 0 0 1 1 0 1 1];
428
+
429
+ temp = [];
430
+ full_carrier = [];
431
+ full_time = [];
432
+
433
+ for i = 2:length(b)
434
+
435
+ if(b(i-1) == 0 & b(i) == 0)
436
+ S = A*cos(2*pi*fc*t + 5*pi/4);
437
+
438
+ elseif(b(i-1) == 0 & b(i) == 1)
439
+ S = A*cos(2*pi*fc*t + 3*pi/4);
440
+
441
+ elseif(b(i-1) == 1 & b(i) == 0)
442
+ S = A*cos(2*pi*fc*t + 3*pi/4);
443
+
444
+ elseif(b(i-1) == 1 & b(i) == 1)
445
+ S = A*cos(2*pi*fc*t + pi/4);
446
+ end
447
+
448
+ temp = [temp S];
449
+ full_carrier = [full_carrier c];
450
+ full_time = [full_time t + (i-2)*max(t)];
451
+ end
452
+
453
+ % Plot carrier wave
454
+ figure;
455
+ plot(full_time, full_carrier);
456
+ xlabel('Time');
457
+ ylabel('Amplitude');
458
+ title('Carrier Wave');
459
+ grid on;
460
+
461
+ % Plot QPSK signal
462
+ figure;
463
+ plot(full_time, temp);
464
+ xlabel('Time');
465
+ ylabel('Amplitude');
466
+ title('QPSK Signal');
467
+ grid on;
468
+
469
+ ------------------------------------Const Dia---------------------------------
470
+
471
+ mod1 = zeros(1, length(b)-1);
472
+
473
+ for i = 2:length(b)
474
+
475
+ if b(i-1) == 0 & b(i) == 0
476
+ mod1(i-1) = exp(1j*5*pi/4);
477
+
478
+ elseif b(i-1) == 0 & b(i) == 1
479
+ mod1(i-1) = exp(1j*3*pi/4);
480
+
481
+ elseif b(i-1) == 1 & b(i) == 0
482
+ mod1(i-1) = exp(1j*pi/4);
483
+
484
+ elseif b(i-1) == 1 & b(i) == 1
485
+ mod1(i-1) = exp(1j*7*pi/4);
486
+
487
+ end
488
+
489
+ end
490
+
491
+ figure;
492
+ scatterplot(mod1);
493
+
494
+ grid on;
495
+ xlabel('In-phase (I)');
496
+ ylabel('Quadrature (Q)');
497
+ title('QPSK Constellation Diagram');
498
+
499
+ ------------------------------------BER vs SNR---------------------------------
500
+
501
+ SNR_dB = 0:1:15;
502
+
503
+ % Number of bits
504
+ N = 30000;
505
+
506
+ % Make N even because 2 bits = 1 QPSK symbol
507
+ N = N - mod(N,2);
508
+
509
+ % Random binary data
510
+ b = randi([0 1], 1, N);
511
+
512
+ % Group bits into pairs
513
+ bits = reshape(b, 2, []).';
514
+
515
+ % Number of QPSK symbols
516
+ num_symbols = size(bits,1);
517
+
518
+ % QPSK symbols
519
+ tx = zeros(1, num_symbols);
520
+
521
+ % QPSK mapping
522
+ for i = 1:num_symbols
523
+
524
+ if bits(i,1) == 0 && bits(i,2) == 0
525
+ tx(i) = exp(1j*5*pi/4);
526
+
527
+ elseif bits(i,1) == 0 && bits(i,2) == 1
528
+ tx(i) = exp(1j*3*pi/4);
529
+
530
+ elseif bits(i,1) == 1 && bits(i,2) == 0
531
+ tx(i) = exp(1j*pi/4);
532
+
533
+ elseif bits(i,1) == 1 && bits(i,2) == 1
534
+ tx(i) = exp(1j*7*pi/4);
535
+
536
+ end
537
+
538
+ end
539
+
540
+ BER = zeros(1,length(SNR_dB));
541
+
542
+ % SNR loop
543
+ for j = 1:length(SNR_dB)
544
+
545
+ SNR = 10^(SNR_dB(j)/10);
546
+
547
+ % Add AWGN
548
+ noise = sqrt(1/(2*SNR)) * ...
549
+ (randn(1,num_symbols) + 1j*randn(1,num_symbols));
550
+
551
+ rx = tx + noise;
552
+
553
+ % Demodulation
554
+ rx_bits = zeros(num_symbols,2);
555
+
556
+ for i = 1:num_symbols
557
+
558
+ % Decision based on I and Q
559
+ I = real(rx(i));
560
+ Q = imag(rx(i));
561
+
562
+ if I < 0 && Q < 0
563
+ % 00 -> 5pi/4
564
+ rx_bits(i,:) = [0 0];
565
+
566
+ elseif I < 0 && Q > 0
567
+ % 01 -> 3pi/4
568
+ rx_bits(i,:) = [0 1];
569
+
570
+ elseif I > 0 && Q > 0
571
+ % 10 -> pi/4
572
+ rx_bits(i,:) = [1 0];
573
+
574
+ elseif I > 0 && Q < 0
575
+ % 11 -> 7pi/4
576
+ rx_bits(i,:) = [1 1];
577
+
578
+ end
579
+
580
+ end
581
+
582
+ % Convert received bits into a single sequence
583
+ received = reshape(rx_bits.',1,[]);
584
+
585
+ % Calculate BER
586
+ BER(j) = sum(b ~= received)/N;
587
+
588
+ end
589
+
590
+ % Plot BER vs SNR
591
+ figure;
592
+
593
+ semilogy(SNR_dB, BER, 'o-', 'LineWidth', 1.5);
594
+
595
+ grid on;
596
+ xlabel('SNR (dB)');
597
+ ylabel('Bit Error Rate (BER)');
598
+ title('BER vs SNR for QPSK');
599
+ """)
600
+ def p5():
601
+ print(r"""
602
+ p5
603
+ ---------------------------------------BFSK------------------------------
604
+ clc;
605
+ clear all;
606
+ close all;
607
+ A = 5;
608
+ f1 = 5;
609
+ f2 = 10;
610
+ fs = 150;
611
+ t = 0:1/fs:1;
612
+ b = randi([0 1], 1, 8);
613
+ temp = [];
614
+ full_carrier1 = [];
615
+ full_carrier2 = [];
616
+ full_time = [];
617
+ bit_count = 0;
618
+ for i = 1:length(b)
619
+ bit_count = bit_count + 1;
620
+ c1 = A*cos(2*pi*f1*t);
621
+ c2 = A*cos(2*pi*f2*t);
622
+ if b(i) == 0
623
+ S = c1;
624
+ else
625
+ S = c2;
626
+ end
627
+ temp = [temp, S];
628
+ full_carrier1 = [full_carrier1, c1];
629
+ full_carrier2 = [full_carrier2, c2];
630
+ full_time = [full_time, t + (bit_count - 1)];
631
+ end
632
+ figure;
633
+ subplot(3,1,1);
634
+ plot(full_time, full_carrier1)
635
+ grid on;
636
+ xlabel('Time(seconds)');
637
+ ylabel('Amplitude');
638
+ title(['Carrier 1 (f1 = ', num2str(f1), ' Hz)']);
639
+ subplot(3,1,2);
640
+ plot(full_time, full_carrier2)
641
+ grid on;
642
+ xlabel('Time(seconds)');
643
+ ylabel('Amplitude');
644
+ title(['Carrier 2(f2 = ', num2str(f2), ' Hz)']);
645
+ subplot(3,1,3);
646
+ plot(full_time, temp)
647
+ grid on;
648
+ xlabel('Time (seconds)');
649
+ ylabel('Amplitude');
650
+ title(['Final Coherent BFSK Modulated Signal(8 bits): ', num2str(b)]);
651
+
652
+ ---------------------------------------Const Dia------------------------------
653
+ A = 5;
654
+ b = [0 0 0 1 1 0 1 1];
655
+ mod2 = zeros(1, length(b));
656
+ for i=1:length(b)
657
+ if b(i) == 0
658
+ mod2(i) = A + 1i*0;
659
+ else if b(i) == 1
660
+ mod2(i) = 0+1i*A;
661
+ end
662
+ end
663
+ end
664
+ scatterplot(mod2);
665
+ title("BFSK Constellation Diagram");
666
+ grid on;
667
+
668
+ ---------------------------------------BER vs SNR------------------------------
669
+ N = 10000;
670
+ b = randi([0 1], 1, N);
671
+
672
+ SNRdB = 0:10;
673
+ SNR = 10.^(SNRdB/10);
674
+
675
+ BER = zeros(1, length(SNR));
676
+ theory_BER = zeros(1, length(SNR));
677
+
678
+ for i = 1:length(SNR)
679
+
680
+ A = sqrt(SNR(i)); % Generate amplitude
681
+
682
+ s1 = [];
683
+ s2 = [];
684
+
685
+ for k = 1:N
686
+
687
+ if b(k) == 0
688
+ s1 = [s1 A];
689
+ s2 = [s2 0];
690
+ else
691
+ s1 = [s1 0];
692
+ s2 = [s2 A];
693
+ end
694
+
695
+ end
696
+
697
+ d = zeros(1, N);
698
+
699
+ for j = 1:N
700
+
701
+ n1 = randn();
702
+ n2 = randn();
703
+
704
+ r1 = s1(j) + n1;
705
+ r2 = s2(j) + n2;
706
+
707
+ if r2 >= r1
708
+ d(j) = 1;
709
+ else
710
+ d(j) = 0;
711
+ end
712
+
713
+ end
714
+
715
+ error = sum(xor(b,d)); % Calculate Erroneous bits
716
+ BER(i) = error/N; % BER
717
+
718
+ theory_BER(i) = 0.5 * erfc(sqrt(SNR(i)/2)); % theoretical BER
719
+
720
+ end
721
+
722
+ figure;
723
+
724
+ semilogy(SNRdB, BER, 'Y-', 'LineWidth', 1);
725
+ hold on;
726
+
727
+ semilogy(SNRdB, theory_BER, 'B--', 'LineWidth', 1);
728
+
729
+ xlabel('SNR (dB)');
730
+ ylabel('BER');
731
+ title('BFSK BER vs SNR');
732
+
733
+ legend('Simulated BER', 'Theoretical BER');
734
+ grid on;
735
+ """)
736
+ def p6():
737
+ print(r"""
738
+ p6
739
+ ----------------------------Cell 1-------------------------------
740
+ % Parameter
741
+ Nb = 10;
742
+ Tb = 1;
743
+ N = 100;
744
+ fs = N/Tb;
745
+ fc = 4/Tb;
746
+ sigma_demo = 0.3;
747
+
748
+ rng(1);
749
+ b = randi([0 1], 1, Nb);
750
+
751
+ % Differential encoding:
752
+ d = zeros(1, Nb+1);
753
+ d(1) = 1;
754
+ for k = 1:Nb
755
+ d(k+1) = xor(d(k), b(k));
756
+ end
757
+
758
+ % DPSK modulation
759
+ sym = 2*d - 1;
760
+ t = 0:1/fs:(Nb+1)*Tb - 1/fs;
761
+ tx = repelem(sym, N) .* cos(2*pi*fc*t);
762
+
763
+ % AWGN channel
764
+ rx = tx + sigma_demo*randn(size(tx));
765
+
766
+ % Demodulation:
767
+ rx_delay = [zeros(1, N), rx(1:end-N)];
768
+ prod_sig = rx .* rx_delay;
769
+ y = sum(reshape(prod_sig, N, []), 1);
770
+ y = y(2:end);
771
+ b_hat = double(y < 0);
772
+
773
+ fprintf('Transmitted bits : %s\n', num2str(b));
774
+ fprintf('Detected bits : %s\n', num2str(b_hat));
775
+ fprintf('Bit errors : %d\n\n', sum(b ~= b_hat));
776
+
777
+ figure('Name','DPSK Modulation and Demodulation','Position',[100 100 900 750]);
778
+ subplot(5,1,1);
779
+ stairs(0:Nb-1, b, 'LineWidth', 1.5); ylim([-0.5 1.5]);
780
+ title('Input Binary Data'); ylabel('Amplitude'); grid on;
781
+
782
+ subplot(5,1,2);
783
+ stairs(0:Nb, d, 'LineWidth', 1.5); ylim([-0.5 1.5]);
784
+ title('Differentially Encoded Data'); ylabel('Amplitude'); grid on;
785
+
786
+ subplot(5,1,3);
787
+ plot(t, tx, 'LineWidth', 1);
788
+ title('Modulated DPSK Signal'); ylabel('Amplitude'); grid on;
789
+
790
+ subplot(5,1,4);
791
+ plot(t, rx);
792
+ title('Received DPSK Signal'); ylabel('Amplitude'); grid on;
793
+
794
+ subplot(5,1,5);
795
+ stairs(1:Nb, b_hat, 'LineWidth', 1.5); ylim([-0.5 1.5]);
796
+ title('Demodulated Data'); xlabel('Bit index'); ylabel('Amplitude'); grid on;
797
+
798
+
799
+ % Constellation diagram
800
+ Nsym = 2000;
801
+ bits = randi([0 1], 1, Nsym);
802
+ dd = zeros(1, Nsym+1); dd(1) = 1;
803
+ for k = 1:Nsym
804
+ dd(k+1) = xor(dd(k), bits(k));
805
+ end
806
+ s = 2*dd - 1;
807
+
808
+ EbN0_const = 10;
809
+ N0c = 1/10^(EbN0_const/10);
810
+ nc = sqrt(N0c/2)*(randn(size(s)) + 1j*randn(size(s)));
811
+ r_c = s + nc;
812
+
813
+ figure('Name','DPSK Constellation','Position',[150 150 900 400]);
814
+ plot(real(s), imag(s), 'bo', 'MarkerSize', 10, 'LineWidth', 2); hold on;
815
+ plot([-2 2], [0 0], 'k:'); plot([0 0], [-2 2], 'k:');
816
+ axis([-2 2 -2 2]); axis square; grid on;
817
+ xlabel('In-phase'); ylabel('Quadrature');
818
+ title('Constellation Diagram');
819
+
820
+
821
+
822
+ % BER over AWGN
823
+ EbN0_dB = 0:1:12;
824
+ EbN0 = 10.^(EbN0_dB/10);
825
+ Nbits = 1e6;
826
+ ber_sim = zeros(size(EbN0_dB));
827
+
828
+ for i = 1:length(EbN0_dB)
829
+ bi = randi([0 1], 1, Nbits);
830
+ di = [1, mod(1 + cumsum(bi), 2)];
831
+ si = 2*di - 1;
832
+ N0 = 1/EbN0(i);
833
+ n = sqrt(N0/2)*(randn(size(si)) + 1j*randn(size(si)));
834
+ ri = si + n;
835
+ z = real(ri(2:end) .* conj(ri(1:end-1)));
836
+ bi_hat = double(z < 0);
837
+
838
+ ber_sim(i) = sum(bi ~= bi_hat)/Nbits;
839
+ end
840
+
841
+ % Theoretical BER and comparison
842
+ ber_dpsk_theory = 0.5*exp(-EbN0);
843
+ ber_bpsk_theory = 0.5*erfc(sqrt(EbN0));
844
+
845
+ figure('Name','BER of DPSK over AWGN');
846
+ semilogy(EbN0_dB, ber_sim, 'ro-', 'LineWidth', 1.5, 'MarkerSize', 7); hold on;
847
+ semilogy(EbN0_dB, ber_dpsk_theory, 'b-', 'LineWidth', 1.8);
848
+ semilogy(EbN0_dB, ber_bpsk_theory, 'k--', 'LineWidth', 1.3);
849
+ grid on; ylim([1e-6 1]);
850
+ xlabel('E_b/N_0 (dB)'); ylabel('Bit Error Rate (BER)');
851
+ title('BER Performance of DPSK over AWGN Channel');
852
+ legend('DPSK Simulated', 'DPSK Theoretical: 0.5e^{-E_b/N_0}', ...
853
+ 'Coherent BPSK Theoretical', 'Location', 'southwest');
854
+
855
+ """)
856
+ def p7():
857
+ print(r"""
858
+ p7
859
+ ---------------------------Cell 1--------------------------------
860
+ clear all;
861
+ close all;
862
+ clc;
863
+
864
+ % Parameter
865
+ N = 10000;
866
+ SNR = 0:2:14;
867
+ M = [2 4 8 16];
868
+
869
+ BER = zeros(length(M),length(SNR));
870
+
871
+ for m = 1:length(M)
872
+
873
+ % Generate random data
874
+ data = randi([0 M(m)-1],N,1);
875
+
876
+ % M-PSK Modulation
877
+ tx = pskmod(data,M(m),0);
878
+
879
+ % Constellation diagram
880
+ figure;
881
+ scatterplot(tx);
882
+ title(['M-PSK Constellation, M = ' num2str(M(m))]);
883
+ grid on;
884
+
885
+ % AWGN channel and BER
886
+ for k = 1:length(SNR)
887
+
888
+ rx = awgn(tx,SNR(k),'measured');
889
+
890
+ % M-PSK Demodulation
891
+ data_rx = pskdemod(rx,M(m),0);
892
+
893
+ % Calculate BER
894
+ [~,BER(m,k)] = biterr(data,data_rx);
895
+
896
+ end
897
+ end
898
+
899
+ % Plot BER
900
+ figure;
901
+
902
+ for m = 1:length(M)
903
+ semilogy(SNR,BER(m,:),'-o');
904
+ hold on;
905
+ end
906
+
907
+ grid on;
908
+ xlabel('SNR (dB)');
909
+ ylabel('BER');
910
+ title('BER Performance of M-PSK over AWGN Channel');
911
+
912
+ legend('BPSK (M=2)','QPSK (M=4)', ...
913
+ '8-PSK (M=8)','16-PSK (M=16)');
914
+
915
+
916
+
917
+ """)
918
+ def p8():
919
+ print(r"""
920
+ p8
921
+ --------------------Cell 1--------------------------
922
+ clear all ; close all ; clc ;
923
+
924
+ % Parameter
925
+ n = 7;
926
+ k = 4;
927
+ P = [1 1 0;
928
+ 1 0 1;
929
+ 0 1 1;
930
+ 1 1 1];
931
+
932
+ % Systematic Generator Matrix G = [I_k | P]
933
+ G = [eye(k), P];
934
+
935
+ % Parity-Check Matrix H = [P' | I_(n-k)]
936
+ H = [P', eye(n-k)];
937
+
938
+ disp('Generator Matrix G:'); disp(G);
939
+ disp('Parity-Check Matrix H:'); disp(H);
940
+
941
+ % Single-Bit Error Correction Demonstration
942
+ msg_demo = [1 0 1 1];
943
+ codeword_demo = mod(msg_demo * G, 2);
944
+
945
+ % Introduce a single-bit error at a chosen test position
946
+ error_pos = 2;
947
+ error_vector = zeros(1, n);
948
+ error_vector(error_pos) = 1;
949
+
950
+ % Received vector with the error
951
+ received_demo = mod(codeword_demo + error_vector, 2);
952
+
953
+ % Syndrome calculation: S = R * H' (mod 2)
954
+ syndrome_demo = mod(received_demo * H', 2);
955
+
956
+ % Error Detection and Correction using Syndrome Decoding
957
+ detected_pos = 0;
958
+ if all(syndrome_demo == 0)
959
+ corrected_demo = received_demo;
960
+ fprintf('Syndrome is zero: No Error Detected.\n');
961
+ else
962
+ for i = 1:n
963
+ if H(:, i)' == syndrome_demo
964
+ detected_pos = i;
965
+ break;
966
+ end
967
+ end
968
+
969
+ % Correct the error if identified
970
+ corrected_demo = received_demo;
971
+ if detected_pos > 0
972
+ corrected_demo(detected_pos) = mod(corrected_demo(detected_pos) + 1, 2);
973
+ end
974
+ end
975
+
976
+ fprintf('\nSingle-Bit Error\n');
977
+ fprintf('Original Message: [%s]\n', num2str(msg_demo));
978
+ fprintf('Generated Codeword: [%s]\n', num2str(codeword_demo));
979
+ fprintf('Received Codeword: [%s] (Error at bit %d)\n', num2str(received_demo), error_pos);
980
+ fprintf('Calculated Syndrome: [%s]\n', num2str(syndrome_demo));
981
+ fprintf('Detected Error at: Bit %d\n', detected_pos);
982
+ fprintf('Corrected Codeword: [%s]\n\n', num2str(corrected_demo));
983
+
984
+
985
+ %Performance Evaluation over a Binary Symmetric Channel (BSC)
986
+ num_blocks = 10^5;
987
+ p_vector = logspace(-4, -1, 10);
988
+ ber_uncoded = zeros(1, length(p_vector));
989
+ ber_coded = zeros(1, length(p_vector));
990
+
991
+ syndrome_table = zeros(2^(n-k), 1);
992
+ for i = 1:n
993
+ syn_dec = bi2de(H(:, i)', 'left-msb');
994
+ syndrome_table(syn_dec + 1) = i;
995
+ end
996
+
997
+ for idx = 1:length(p_vector)
998
+ p = p_vector(idx);
999
+
1000
+ % Generate random source message bits
1001
+ tx_msg = randi([0 1], num_blocks, k);
1002
+
1003
+ % Encode messages
1004
+ tx_code = mod(tx_msg * G, 2);
1005
+
1006
+ % Simulate BSC noise (add bit-flips with probability p)
1007
+ noise = rand(num_blocks, n) < p;
1008
+ rx_code = mod(tx_code + noise, 2);
1009
+
1010
+ % Keep track of raw raw errors for uncoded benchmark line
1011
+ ber_uncoded(idx) = mean(noise(:));
1012
+
1013
+ % Decode received words block-by-block
1014
+ rx_msg = zeros(num_blocks, k);
1015
+
1016
+ for b = 1:num_blocks
1017
+ this_rx = rx_code(b, :);
1018
+ syn = mod(this_rx * H', 2);
1019
+
1020
+ if any(syn)
1021
+ % Check our syndrome table lookup
1022
+ syn_dec = bi2de(syn, 'left-msb');
1023
+ err_bit = syndrome_table(syn_dec + 1);
1024
+ if err_bit > 0
1025
+ this_rx(err_bit) = 1 - this_rx(err_bit); % Flip it back
1026
+ end
1027
+ end
1028
+ % Systematic bits are the first k bits
1029
+ rx_msg(b, :) = this_rx(1:k);
1030
+ end
1031
+
1032
+ % Evaluate post-correction performance
1033
+ bit_errors = tx_msg ~= rx_msg;
1034
+ ber_coded(idx) = mean(bit_errors(:));
1035
+ end
1036
+
1037
+ % Plot Results
1038
+ figure;
1039
+ loglog(p_vector, ber_uncoded, 'r--o', 'LineWidth', 1.5, 'MarkerSize', 6); hold on;
1040
+ loglog(p_vector, ber_coded, 'b-s', 'LineWidth', 1.5, 'MarkerSize', 6);
1041
+ grid on;
1042
+ xlabel('BSC Crossover Probability (p)');
1043
+ ylabel('Bit Error Rate (BER)');
1044
+ title('Linear Block Code Evaluation Over BSC');
1045
+ legend('Uncoded Performance (Baseline)', 'Coded Performance (with Syndrome Correction)', 'Location', 'SouthEast');
1046
+
1047
+ """)
1048
+ def p9():
1049
+ print(r"""
1050
+ p9
1051
+ --------------------------------Cell 1----------------------------
1052
+
1053
+
1054
+ %Parameters
1055
+ N = 100000; % number of message bits
1056
+ trellis = poly2trellis(3,[7 5]); % rate 1/2, constraint length 3
1057
+ tbdepth = 15; % traceback depth for Viterbi
1058
+ rate = 1/2; % code rate
1059
+ EbN0_dB = 0:1:8; % Eb/N0 range in dB
1060
+
1061
+ %Encoder
1062
+ demo_msg = [1 0 1 1 0 0 1];
1063
+ demo_coded = convenc(demo_msg, trellis);
1064
+ disp('Message bits :'); disp(demo_msg);
1065
+ disp('Encoded bits :'); disp(demo_coded);
1066
+
1067
+ %Generate random message and encode
1068
+ msg = randi([0 1], 1, N);
1069
+ coded = convenc(msg, trellis);
1070
+
1071
+ BER_uncoded = zeros(size(EbN0_dB));
1072
+ BER_coded = zeros(size(EbN0_dB));
1073
+
1074
+ for i = 1:length(EbN0_dB)
1075
+ EbN0 = 10^(EbN0_dB(i)/10);
1076
+
1077
+ %Uncoded BPSK over AWGN
1078
+ tx_u = 2*msg - 1;
1079
+ sigma_u = sqrt(1/(2*EbN0));
1080
+ rx_u = tx_u + sigma_u*randn(size(tx_u));
1081
+ dec_u = rx_u > 0;
1082
+ BER_uncoded(i) = sum(dec_u ~= msg)/N;
1083
+
1084
+ %Convolutional coded BPSK over AWGN
1085
+ tx_c = 2*coded - 1;
1086
+ sigma_c = sqrt(1/(2*rate*EbN0));
1087
+ rx_c = tx_c + sigma_c*randn(size(tx_c));
1088
+ hard = double(rx_c > 0);
1089
+ dec_c = vitdec(hard, trellis, tbdepth, 'trunc', 'hard');
1090
+ BER_coded(i) = sum(dec_c ~= msg)/N;
1091
+ end
1092
+
1093
+ %Plot BER curves
1094
+ semilogy(EbN0_dB, BER_uncoded, 'r-o', 'LineWidth', 1.5); hold on;
1095
+ semilogy(EbN0_dB, BER_coded, 'b-s', 'LineWidth', 1.5);
1096
+ grid on;
1097
+ xlabel('E_b/N_0 (dB)'); ylabel('Bit Error Rate (BER)');
1098
+ title('BER: Coded vs Uncoded over AWGN Channel');
1099
+ legend('Uncoded BPSK', 'Convolutional Coded (Viterbi)');
1100
+
1101
+ """)
1102
+ def p10():
1103
+ print(r"""
1104
+ p10
1105
+ ------------------------------Cell 1------------------------------------------
1106
+ clc; clear; close all;
1107
+
1108
+ %Image Transmission
1109
+ SNR_dB = [0 5 10];
1110
+ img = imread('peppers.png');
1111
+ gray = uint8(mean(img,3)); % convert to grayscale
1112
+ [r,c] = size(gray);
1113
+
1114
+ bits = bytes2bits(gray(:)); % image to bit stream
1115
+
1116
+ figure('Name','Image Transmission');
1117
+ subplot(2,2,1); imshow(gray); title('Original Image');
1118
+
1119
+ for k = 1:length(SNR_dB)
1120
+ rx_bits = transmit(bits, SNR_dB(k)); % BPSK + AWGN + detection
1121
+ rx_img = reshape(bits2bytes(rx_bits), r, c); % bits to image
1122
+
1123
+ BER = sum(bits ~= rx_bits)/length(bits);
1124
+ mse = mean((double(gray(:)) - double(rx_img(:))).^2);
1125
+ PSNR = 10*log10(255^2/mse);
1126
+
1127
+ subplot(2,2,k+1); imshow(uint8(rx_img));
1128
+ title(sprintf('SNR=%d dB, BER=%.4f, PSNR=%.1f dB', SNR_dB(k), BER, PSNR));
1129
+ fprintf('Image: SNR = %2d dB BER = %.5f PSNR = %.2f dB\n', SNR_dB(k), BER, PSNR);
1130
+ end
1131
+
1132
+ %Audio Transmission
1133
+ load handel; % gives y (audio) and Fs (sampling rate)
1134
+ y = y(1:20000); % use a short segment
1135
+ q = round((y + 1)/2 * 255); % quantize to 8 bits (0 to 255)
1136
+
1137
+ abits = bytes2bits(q); % audio to bit stream
1138
+
1139
+ figure('Name','Audio Transmission');
1140
+ subplot(2,2,1); plot(y); title('Original Audio'); ylim([-1 1]);
1141
+
1142
+ for k = 1:length(SNR_dB)
1143
+ rx_bits = transmit(abits, SNR_dB(k));
1144
+ q_rx = bits2bytes(rx_bits);
1145
+ y_rx = q_rx/255*2 - 1; % bits to audio
1146
+
1147
+ BER = sum(abits ~= rx_bits)/length(abits);
1148
+ snr_out = 10*log10(sum(y.^2)/sum((y - y_rx).^2));
1149
+
1150
+ subplot(2,2,k+1); plot(y_rx); ylim([-1 1]);
1151
+ title(sprintf('SNR=%d dB, BER=%.4f', SNR_dB(k), BER));
1152
+ fprintf('Audio: SNR = %2d dB BER = %.5f Output SNR = %.2f dB\n', SNR_dB(k), BER, snr_out);
1153
+
1154
+ % sound(y_rx, Fs); pause(3);
1155
+ end
1156
+ % sound(y, Fs);
1157
+
1158
+ function rx_bits = transmit(bits, snr_dB)
1159
+ tx = 2*bits - 1; % BPSK: 0 -> -1, 1 -> +1
1160
+ EbN0 = 10^(snr_dB/10);
1161
+ sigma = sqrt(1/(2*EbN0)); % noise standard deviation
1162
+ rx = tx + sigma*randn(size(tx)); % AWGN channel
1163
+ rx_bits = double(rx > 0); % hard decision
1164
+ end
1165
+
1166
+ function bits = bytes2bits(x)
1167
+ b = dec2bin(double(x(:)), 8)' - '0'; % 8 bits per sample/pixel
1168
+ bits = double(b(:)');
1169
+ end
1170
+
1171
+ function x = bytes2bits_dummy(~)
1172
+ end
1173
+
1174
+ function x = bits2bytes(bits)
1175
+ x = reshape(bits, 8, [])' * [128;64;32;16;8;4;2;1];
1176
+ end
1177
+ """)
@@ -0,0 +1,9 @@
1
+ Metadata-Version: 2.4
2
+ Name: nu_lpw_ec
3
+ Version: 0.0.1
4
+ Summary: Enjoy my codes!
5
+ Author-email: None None <userzeroonetwo091@gmail.com>
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: License :: OSI Approved :: MIT License
8
+ Classifier: Operating System :: OS Independent
9
+ Requires-Python: >=3.7
@@ -0,0 +1,7 @@
1
+ pyproject.toml
2
+ nu_lpw_ec/__init__.py
3
+ nu_lpw_ec/dc.py
4
+ nu_lpw_ec.egg-info/PKG-INFO
5
+ nu_lpw_ec.egg-info/SOURCES.txt
6
+ nu_lpw_ec.egg-info/dependency_links.txt
7
+ nu_lpw_ec.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ nu_lpw_ec
@@ -0,0 +1,22 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "nu_lpw_ec"
7
+ version = "0.0.1"
8
+ authors = [
9
+ { name="None None", email="userzeroonetwo091@gmail.com" },
10
+ ]
11
+ description = "Enjoy my codes!"
12
+ requires-python = ">=3.7"
13
+ dependencies = [
14
+ ]
15
+ classifiers = [
16
+ "Programming Language :: Python :: 3",
17
+ "License :: OSI Approved :: MIT License",
18
+ "Operating System :: OS Independent",
19
+ ]
20
+
21
+ [tool.setuptools]
22
+ packages = ["nu_lpw_ec"]
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+