Working Kerr effect; PDM; speedups; removed unused files
[4yp.git] / adaptiveCMA.m
1 function [adaptFilterOut, convergeIdx] = adaptiveCMA(rSampled)
2   %% adaptive filter
3   %% CMA
4   taps = 19; % ODD taps
5   hxx = zeros(taps, 1);
6   %% hxx: real indices  -K, ..., 0, ..., K.   K = floor(taps/2)
7   %%    MATLAB indices   1      1+K     taps
8   %% initialize hxx, hxx[0] = 1, hxx[k] = hxx[-k] = 0
9   hxx(ceil(taps/2)) = 1;
10
11   mu = 1e-3;
12   numSymbs = length(rSampled);
13
14   %% Check average energy of symbols
15   rSampledUnitEnergy = normalizeEnergy(rSampled, numSymbs, 1);
16
17   adaptFilterOut = zeros(numSymbs, 1);
18   converged = 0;
19   convergeCount = 0;
20   convergeIdx = Inf;
21
22   for it = 1:numSymbs
23     if it <= (taps - 1) / 2;
24       xp = [zeros((taps - 1) / 2 - it + 1, 1); rSampledUnitEnergy(1:it + (taps - 1) / 2)];
25     elseif it + (taps - 1) / 2 > numSymbs
26       xp = [rSampledUnitEnergy(it - (taps - 1) / 2 : end); zeros(it + (taps - 1) / 2 - numSymbs, 1)];
27     else
28       xp = rSampledUnitEnergy(it - (taps - 1) / 2 : it + (taps - 1) / 2);
29     end
30
31     xout = sum(hxx .* xp);
32     ex = 1 - abs(xout) ^ 2;
33
34     if abs(ex) < 0.01
35       convergeCount = convergeCount + 1;
36     else
37       convergeCount = 0;
38     end
39     if ~converged && convergeCount >= 10
40       converged = 1;
41       convergeIdx = it;
42     end
43
44     adaptFilterOut(it) = xout;
45
46     hxx = hxx + mu * ex * xout * conj(xp);
47   end
48
49 %{
50   %% try MATLAB builtin equalizer
51   alg = cma(mu);
52   eqObj = lineareq(taps, alg);
53   eqObj.Weights((taps + 1) / 2) = 1;
54   rPadded = [rSampledUnitEnergy; zeros((taps - 1) / 2, 1)];
55   matlabEq = equalize(eqObj, rPadded);
56   matlabEq = matlabEq((taps + 1) / 2 : end);
57 %}
58 end