Added technical milestone report and changes to 1st presentation
[4yp.git] / baseband.m
CommitLineData
8449f934
AIL
1function baseband(rolloff, M, numSymbs)
2 %% Set defaults for inputs
3 if nargin < 3
4 numSymbs = 1000;
5 end
6 if nargin < 2
7 M = 2;
8 end
9 if nargin < 1
10 rolloff = 0.5;
11 end
12
8449f934
AIL
13 %% https://www.mathworks.com/help/comm/examples/passband-modulation-with-adjacent-channel-interference.html
14 Rsym = 1e6; % symbol rate (sym/sec)
15
16 span = 6; % filter span
f9a73e9e 17 sps = 2; % samples per symbol
8449f934 18
8449f934
AIL
19 fs = Rsym * sps; % sampling freq (Hz)
20
21 t = (0 : 1 / fs : numSymbs / Rsym + (1.5 * span * sps - 1) / fs)';
22
8449f934
AIL
23 EbN0_db = 0:0.2:10;
24 EbN0 = 10 .^ (EbN0_db ./ 10);
25 Es = 1;
26 Eb = Es / log2(M);
27 N0 = Eb ./ EbN0;
28
29 EsN0 = EbN0 .* log2(M);
30 EsN0_db = 10 .* log10(EsN0);
31
32 plotlen = length(EbN0);
33 ber = zeros(1, plotlen);
34
35 data = randi([0 M - 1], numSymbs, 1);
36 modData = pskmod(data, M, 0, 'gray');
37
1eeb62fb 38 xBaseband = txFilter(modData, rolloff, span, sps);
8449f934
AIL
39
40 for i = 1:plotlen
1eeb62fb 41 snr = EbN0_db(i) + 10 * log10(log2(M)) - 10 * log10(sps);
8449f934
AIL
42 noiseEnergy = 10 ^ (-snr / 10);
43
44 yBaseband = awgn(xBaseband, snr, 'measured');
45
1eeb62fb 46 rBaseband = rxFilter(yBaseband, rolloff, span, sps);
8449f934
AIL
47
48 rSampled = rBaseband(sps*span/2+1:sps:(numSymbs+span/2)*sps);
8449f934 49 demodData = pskdemod(rSampled, M, 0, 'gray');
8449f934
AIL
50 [bitErrors, ber(i)] = biterr(data, demodData);
51 end
52
1eeb62fb 53 figure(1);
8449f934
AIL
54 clf;
55
56 %% Plot simulated results
57 semilogy(EbN0_db, ber, 'r', 'LineWidth', 2);
58 hold on;
59
1eeb62fb
AIL
60 theoreticalPSK(EbN0_db, M, 'b', 'LineWidth', 1);
61 legend('Simulated', 'Discrete');
8449f934
AIL
62
63 title(strcat(num2str(M), '-PSK with Gray code'));
64 grid on;
65 xlabel('$E_b/N_0$ (dB)');
66 ylabel('BER');
67
68 formatFigure;
8449f934 69end