function Do = descrambler(Ds)
    Do = zeros(length(Ds), 1);
    x = zeros(17, 1);

    detector_count = 0;
    invert = 0;

    for i = 1:length(Ds)
        Do(i) = bitxor(Ds(i), bitxor(x(14), x(17)));

        % Note 2: invertor        
        if invert == 1
            invert = 0;
            Do(i) = ~Do(i);
        end

        % Note 2: detector
        if Ds(i) == 1
            detector_count = detector_count + 1;
        else
            detector_count = 0;
        end
        if detector_count >= 64
            invert = 1;
        end
        
        x = rshift(x);
        x(1) = Ds(i);
    end
