Working Kerr effect; PDM; speedups; removed unused files
[4yp.git] / chromaticDispersion1Signal.m
1 M = 4;
2 numSymbs = 5e5;
3
4 Rsym = 2.5e10; % symbol rate (sym/sec)
5
6 span = 6; % Tx/Rx filter span
7 rolloff = 0.25; % Tx/Rx RRC rolloff
8 sps = 8; % samples per symbol
9
10 fs = Rsym * sps; % sampling freq (Hz)
11 Tsamp = 1 / fs;
12
13 t = (0 : 1 / fs : numSymbs / Rsym + (1.5 * span * sps - 1) / fs).';
14
15 data = randi([0 M - 1], numSymbs, 1);
16 modData = pskmod(data, M, pi / M, 'gray');
17 x = txFilter(modData, rolloff, span, sps);
18
19 x = normalizeEnergy(x, numSymbs*sps, 1);
20
21 %% Simulate chromatic dispersion
22 D = 17; % ps / (nm km)
23 lambda = 1550; % nm
24 z = 500 % km
25
26 [xCD, xCDkstart] = chromaticDispersion(x, D, lambda, z, Tsamp);
27
28 EbN0_db = 8;
29 snr = EbN0_db + 10 * log10(log2(M)) - 10 * log10(sps);
30
31 %%y = awgn(xCD, snr, 'measured');
32 y = xCD;
33
34 r = rxFilter(y, rolloff, span, sps);
35
36 sps = 2;
37 Tsamp = Tsamp * 4;
38
39
40 [rCDComp, CDCompkstart] = CDCompensation(r, D, lambda, z, Tsamp);
41 rCDComp = normalizeEnergy(rCDComp, numSymbs*sps, 1);
42
43 rSampled = rCDComp(2:2:end);
44 rNoCompSa = r(2:2:end);
45
46 %% if no CD comp, then rotate constellation. Use:
47 theta = angle(-sum(rNoCompSa .^ M)) / M;
48 %% if theta approx +pi/M, wrap to -pi/M
49 if abs(theta - pi / M) / (pi / M) < 0.1
50   theta = -pi / M;
51 end
52 rNoCompSa = rNoCompSa .* exp(-j * theta);
53
54
55 %% Not entirely sure why, but after using FFT instead of time-domain
56 %% convolution for simulating CD, we now need to do the same rotation
57 %% for rSampled as well, but this time with a positive rotation.
58 theta = angle(-sum(rSampled .^ M)) / M;
59 if abs(theta + pi / M) / (pi / M) < 0.1
60   theta = +pi / M;
61 end
62 rSampled = rSampled .* exp(-1j * theta);
63
64
65 %%rAdaptEq = adaptiveCMA(rSampled);
66 %{
67 %% Compare original signal and compensated signal
68 figure(101);
69 clf;
70 tsym = t(sps*span/2+1:sps:(numSymbs+span/2)*sps);
71 subplot(211);
72 plot(t(1:length(x)), real(normalizeEnergy(x, numSymbs*sps, 1)), 'b');
73 hold on
74 plot(t(1:length(x)), real(normalizeEnergy(yCDComp(1:length(x)), numSymbs*sps, 1)), 'r');
75 plot(tsym, real(rAdaptEq), 'x', 'Color', [0, 0.6, 0], 'LineWidth', 2);
76 hold off;
77 title('Real part');
78 legend('original', 'dispersion compensated', 'CMA equalized samples');
79 axis([t(6000*sps+1) t(6000*sps+150) -Inf +Inf]);
80 subplot(212);
81 plot(t(1:length(x)), imag(normalizeEnergy(x, numSymbs*sps, 1)), 'b');
82 hold on;
83 plot(t(1:length(x)), imag(normalizeEnergy(yCDComp(1:length(x)), numSymbs*sps, 1)), 'r');
84 plot(tsym, imag(rAdaptEq), 'x', 'Color', [0, 0.6, 0], 'LineWidth', 2);
85 hold off;
86 title('Imag part');
87 axis([t(6000*sps+1) t(6000*sps+150) -Inf +Inf]);
88
89 scatterplot(modData);
90 formatFigure;
91 %title('Constellation of original modulation', 'interpreter', 'latex');
92 xlabel('In-Phase', 'interpreter', 'latex');
93 %scatterplot(rSampled);
94 %title('Constellation of matched filter output');
95 scatterplot(rNoCompSa);
96 title('Constellation of dispersed signal', 'interpreter', 'latex');
97 scatterplot(rAdaptEq);
98 title('Constellation of adaptive filter output');
99 %}
100 demodData = pskdemod(rSampled, M, pi / M, 'gray');
101 %%demodAdapt = pskdemod(rAdaptEq, M, pi / M, 'gray');
102
103 [~, ber] = biterr(data, demodData)
104 %[~, berNoComp] = biterr(data, pskdemod(rNoCompSa, M, pi/M, 'gray'))
105 %[~, ber] = biterr(data, demodAdapt)