% Quantum Key Distribution (QKD) protocol
% Version: January 14, 2016
classdef decoder < QKD_Peer
    % QKD decoder   
    methods
        % decoding function
        function [ D, selectedbases ] = decode(obj,  R)
            % R = received photon angles
            % save basis selected for each photon
            selectedbases=[];
            % Measure the polarity of photons
            obj.data = [];
            for i=1:length(R)
                % randomly select a basis and corresponding angles
                b=randi([1 2]);
                selectedbases=[ selectedbases b ];
                B=obj.basis(b,:);
                if R(i)==B(1)
                    obj.data = [obj.data  0];
                elseif R(i)==B(2)
                    obj.data = [obj.data  1];
                else
                    obj.data = [obj.data randi([0 1])];
                end
            end
            D=obj.data;
        end
    end  
end

