Working Kerr effect; PDM; speedups; removed unused files
[4yp.git] / CD_AWGN.m
index 1ea6ad8..241d22a 100644 (file)
--- a/CD_AWGN.m
+++ b/CD_AWGN.m
@@ -1,18 +1,18 @@
-numSymbs = 10000;
+numSymbs = 2^16;
 M = 4;
 
 Rsym = 2.5e10; % symbol rate (sym/sec)
 
 rolloff = 0.25;
 span = 6; % filter span
-sps = 4; % samples per symbol
+sps = 8; % samples per symbol
 
 fs = Rsym * sps; % sampling freq (Hz)
 Tsamp = 1 / fs;
 
-t = (0 : 1 / fs : numSymbs / Rsym + (1.5 * span * sps - 1) / fs)';
+t = (0 : 1 / fs : numSymbs / Rsym + (1.5 * span * sps - 1) / fs).';
 
-EbN0_db = 0:0.2:14;
+EbN0_db = 0:0.5:14;
 EbN0 = 10 .^ (EbN0_db ./ 10);
 
 Es = 1;
@@ -25,36 +25,102 @@ EsN0_db = 10 .* log10(EsN0);
 plotlen = length(EbN0);
 
 ber = zeros(1, plotlen);
+berNoComp = zeros(1, plotlen);
+berAdapt = zeros(1, plotlen);
+berMatlabAdapt = zeros(1, plotlen);
 
 data = randi([0 M - 1], numSymbs, 1);
-modData = pskmod(data, M, 0, 'gray');
+modData = pskmod(data, M, pi / M, 'gray');
 x = txFilter(modData, rolloff, span, sps);
 
 %% Simulate chromatic dispersion
-D = 20; % ps / (nm km)
+D = 17; % ps / (nm km)
 lambda = 1550; % nm
-z = 10; % km
+z = 3000; % km
 
-xCD = chromaticDispersion(x, D, lambda, z, Tsamp);
-xCD = normalizeEnergy(xCD, numSymbs, 1);
 
+[xCD, xCDkstart] = chromaticDispersion(x, D, lambda, z, Tsamp);
+
+TsampOrig = Tsamp;
 
 for i = 1:plotlen
+  sps = 8;
+
   snr = EbN0_db(i) + 10 * log10(log2(M)) - 10 * log10(sps);
-  noiseEnergy = 10 ^ (-snr / 10);
 
   y = awgn(xCD, snr, 'measured');
 
-  yCDComp = CDCompensation(y, D, lambda, z, Tsamp);
+  r = rxFilter(y, rolloff, span, sps);
+
+  sps = 2;
+  Tsamp = TsampOrig * 4;
+
+  [rCDComp, CDCompkstart] = CDCompensation(r, D, lambda, z, Tsamp);
+  rCDComp = normalizeEnergy(rCDComp, numSymbs*sps, 1);
+
+  rSampled = rCDComp(2:2:end);
+  rNoCompSampled = r(2:2:end);
+
+  %% rotate rNoCompSampled to match original data
+  theta = angle(-sum(rNoCompSampled .^ M)) / M;
+  %% if theta approx +pi/M, wrap to -pi/M
+  if abs(theta - pi / M) / (pi / M) < 0.1
+    theta = -pi / M;
+  end
+  rNoCompSampled = rNoCompSampled .* exp(-j * theta);
 
-  r = rxFilter(yCDComp, rolloff, span, sps);
-  %% normalize energy
-  %r = normalizeEnergy(r, numSymbs, 1 + noiseEnergy);
 
-  rSampled = r(sps*span/2+1:sps:(numSymbs + span/2) * sps);
-  demodData = pskdemod(rSampled, M, 0, 'gray');
+  %% Not entirely sure why, but after using FFT instead of time-domain
+  %% convolution for simulating CD, we now need to do the same rotation
+  %% for rSampled as well, but this time with a positive rotation.
+  theta = angle(-sum(rSampled .^ M)) / M;
+  if abs(theta + pi / M) / (pi / M) < 0.1
+    theta = +pi / M;
+  end
+  rSampled = rSampled .* exp(-1j * theta);
+
+
+
+  %% adaptive filter
+  adaptFilterOut = adaptiveCMA(rSampled);
+
+  demodData = pskdemod(rSampled, M, pi / M, 'gray');
+  demodNoComp = pskdemod(rNoCompSampled, M, pi / M, 'gray');
+  demodAdapt = pskdemod(adaptFilterOut, M, pi / M, 'gray');
+  %%demodMatlabAdapt = pskdemod(matlabEq, M, pi / M, 'gray');
 
   [bitErrors, ber(i)] = biterr(data, demodData);
+  [bitErrors, berNoComp(i)] = biterr(data, demodNoComp);
+  [~, berAdapt(i)] = biterr(data, demodAdapt);
+  %%[~, berMatlabAdapt(i)] = biterr(data, demodMatlabAdapt);
+
+%{
+  if EbN0_db(i) == 14
+    figure(1);
+    scatterplot(normalizeEnergy(rSampled, numSymbs, 1));
+    formatFigure;
+    title('Constellation after CD comp.', 'interpreter', 'latex');
+    xlabel('In-Phase', 'interpreter', 'latex');
+    ylabel('Quadrature', 'interpreter', 'latex');
+    set(gca, 'FontSize', 18);
+    %%scatterplot(modData);
+    %%title('Original constellation');
+    scatterplot(normalizeEnergy(rNoCompSampled, numSymbs, 1));
+    formatFigure;
+    title('Constellation without CD comp.', 'interpreter', 'latex');
+    xlabel('In-Phase', 'interpreter', 'latex');
+    ylabel('Quadrature', 'interpreter', 'latex');
+    set(gca, 'FontSize', 18);
+    %scatterplot(adaptFilterOut);
+    %title('Constellation with CD compensation and adaptive filter');
+    %scatterplot(matlabEq);
+    %title('Matlab equalizer');
+    ber(i)
+    %berNoComp(i)
+    %berAdapt(i)
+    %berMatlabAdapt(i)
+  end
+%}
 end
 
 figure(1);
@@ -63,11 +129,22 @@ clf;
 %% Plot simulated results
 semilogy(EbN0_db, ber, 'r', 'LineWidth', 2);
 hold on;
+semilogy(EbN0_db, berNoComp, 'm', 'LineWidth', 2);
+semilogy(EbN0_db, berAdapt, 'Color', [0, 0.6, 0], 'LineWidth', 2);
+%%%semilogy(EbN0_db, berMatlabAdapt, 'c', 'LineWidth', 1.4);
 
 theoreticalPSK(EbN0_db, M, 'b', 'LineWidth', 1);
-legend({'CD + AWGN + CD compensation', 'AWGN only'}, 'Location', 'southwest');
-
-title(strcat(num2str(M), '-PSK with chromatic dispersion and compensation'));
+%%legend({'CD + AWGN + CD comp.', 'CD + AWGN + CD comp.~+ CMA', ...
+%%        'Theoretical AWGN'}, 'Location', 'southwest');
+%%legend({'CD + AWGN + CD comp.', 'CD + AWGN', 'Theoretical AWGN'}, ...
+%%       'Location', 'southwest');
+legend({'CD + AWGN + CD comp.', 'CD + AWGN', ...
+        'CD + AWGN + CD comp.~+ CMA', 'Theoretical AWGN'}, 'Location', ...
+       'Southwest');
+
+%%title(strcat(num2str(M), '-PSK with chromatic dispersion and compensation'));
+title({'QPSK with chromatic dispersion and compensation', ...
+       strcat(['$D = 17$ ps/(nm km), $z = ', num2str(z), '$ km'])});
 grid on;
 xlabel('$E_b/N_0$ (dB)');
 ylabel('BER');