function constellation_vec = generateConstellation(type)
% All constellations normalized to max distance = 1!

switch(type)
    case 'bpsk'
        order = 2;
        points = 0:2*pi/order:2*pi*(1-1/order);
        constellation_vec = exp(i*points); % Create all points and normalize.
    case {'4qam', '4psk'}
        order = 4;
        points = 0:2*pi/order:2*pi*(1-1/order);
        constellation_vec = exp(i*points); % Create all points and normalize.
        
        % Sort into gray code
        [gray_seq, ~]    = bin2gray((0:order-1)', 'psk', order);
        constellation_vec_tmp = constellation_vec;
        for jj = 1:order
            constellation_vec_tmp(jj) = constellation_vec( gray_seq(jj)+1);
        end
        constellation_vec = constellation_vec_tmp;
        
        scatterplot(constellation_vec)
        for k = 1:order
            text(real(constellation_vec(k))-0.1,imag(constellation_vec(k))+0.1,...
                dec2base((k-1),2,log2(order)));
        end
        
    case '8psk'       
        order = 8;
        points = 0:2*pi/order:2*pi*(1-1/order);
        constellation_vec = exp(i*points); % Create all points and normalize.
        
        % Sort into gray code
        [gray_seq, ~]    = bin2gray((0:order-1)', 'psk', order);
        constellation_vec_tmp = constellation_vec;
        for jj = 1:order
            constellation_vec_tmp(jj) = constellation_vec( gray_seq(jj)+1);
        end
        constellation_vec = constellation_vec_tmp;
        
        scatterplot(constellation_vec)
        for k = 1:order
            text(real(constellation_vec(k))-0.1,imag(constellation_vec(k))+0.1,...
                dec2base((k-1),2,log2(order)));
        end
        
    case '32psk'       
        order = 32;
        points = 0:2*pi/order:2*pi*(1-1/order);
        constellation_vec = exp(i*points); % Create all points and normalize.
        
        % Sort into gray code
        [gray_seq, ~]    = bin2gray((0:order-1)', 'psk', order);
        constellation_vec_tmp = constellation_vec;
        for jj = 1:order
            constellation_vec_tmp(jj) = constellation_vec( gray_seq(jj)+1);
        end
        constellation_vec = constellation_vec_tmp;
        
        scatterplot(constellation_vec)
        for k = 1:order
            text(real(constellation_vec(k))-0.1,imag(constellation_vec(k))+0.1,...
                dec2base((k-1),2,log2(order)));
        end
%         figure
%         plot(constellation_vec, 'or');
%         axis square
        
        
    case '16qam'      
        % Generate matrix of 16-qam points.
        order = 16;  % orderber of qam constellation points.
        
        constx = 0:(sqrt(order)-1);
        constx = repmat(constx, sqrt(order),1);
        consty = 0:(sqrt(order)-1);
        consty = repmat(consty', 1, sqrt(order));
        const = constx+1i*consty; % Make each point into complex orderber.
        tot_d = (sqrt(order)-1);
        const = const - (tot_d/2 + tot_d/2*1i); % Translate matrix to center on origin.
        % Next step: organise constellation matrix elements into vector sorted in
        % gray sequence.

        constellation_vec = reshape(const, [1 16])./(tot_d/2); % Normalize, giving max magnitude = 1.
        
        % Sort into gray code
        [gray_seq, map]    = bin2gray((0:order-1)', 'qam', order);
        constellation_vec_tmp = constellation_vec;
        for jj = 1:order
            constellation_vec_tmp(jj) = constellation_vec( gray_seq(jj)+1);
        end
        constellation_vec = constellation_vec_tmp;
        
        scatterplot(constellation_vec)
        for k = 1:order
            text(real(constellation_vec(k))-0.1,imag(constellation_vec(k))+0.1,...
        dec2base((k-1),2,4));
        end
        
 
        
    case '64qam'
        % This is tricky because, although the points lie on three
        % concentric circles, they are not evenly spaced -> assign using
        % some kind of grid rather than rotation.
        
        % Generate matrix of 16-qam points.
        order = 64;  % orderber of qam constellation points.
        
        constx = 0:(sqrt(order)-1);
        constx = repmat(constx, sqrt(order),1);
        consty = 0:(sqrt(order)-1);
        consty = repmat(consty', 1, sqrt(order));
        const = constx+1i*consty; % Make each point into complex orderber.
        
        % Total distance on each axis
        tot_d = (sqrt(order)-1);
        const = const - (tot_d/2 + tot_d/2*1i); % Translate matrix to center on origin.
        % Next step: organise constellation matrix elements into vector sorted in
        % gray sequence.

        constellation_vec = reshape(const, [1 order])./(tot_d/2); % Normalize, giving max magnitude = 1.
        
        % Sort into gray code
        [gray_seq, ~]    = bin2gray((0:order-1)', 'qam', order);
        constellation_vec_tmp = constellation_vec;
        for jj = 1:order
            constellation_vec_tmp(jj) = constellation_vec( gray_seq(jj)+1);
        end
        constellation_vec = constellation_vec_tmp;
        
        scatterplot(constellation_vec)
        for k = 1:order
            text(real(constellation_vec(k))-0.1,imag(constellation_vec(k))+0.1,...
                dec2base((k-1),2,log2(order)));
        end

    case '256qam'
        % This is tricky because, although the points lie on three
        % concentric circles, they are not evenly spaced -> assign using
        % some kind of grid rather than rotation.
        
        % Generate matrix of 16-qam points.
        order = 256;  % orderber of qam constellation points.
        
        constx = 0:(sqrt(order)-1);
        constx = repmat(constx, sqrt(order),1);
        consty = 0:(sqrt(order)-1);
        consty = repmat(consty', 1, sqrt(order));
        const = constx+1i*consty; % Make each point into complex orderber.
        
        % Total distance on each axis
        tot_d = (sqrt(order)-1);
        const = const - (tot_d/2 + tot_d/2*1i); % Translate matrix to center on origin.
        % Next step: organise constellation matrix elements into vector sorted in
        % gray sequence.

        constellation_vec = reshape(const, [1 order])./(tot_d/2); % Normalize, giving max magnitude = 1.
        
       % Sort into gray code
        [gray_seq, ~]    = bin2gray((0:order-1)', 'qam', order);
        constellation_vec_tmp = constellation_vec;
        for jj = 1:order
            constellation_vec_tmp(jj) = constellation_vec( gray_seq(jj)+1);
        end
        constellation_vec = constellation_vec_tmp;
        
    case '1024qam'
        % This is tricky because, although the points lie on three
        % concentric circles, they are not evenly spaced -> assign using
        % some kind of grid rather than rotation.
        
        % Generate matrix of 16-qam points.
        order = 1024;  % orderber of qam constellation points.
        
        constx = 0:(sqrt(order)-1);
        constx = repmat(constx, sqrt(order),1);
        consty = 0:(sqrt(order)-1);
        consty = repmat(consty', 1, sqrt(order));
        const = constx+1i*consty; % Make each point into complex orderber.
        
        % Total distance on each axis
        tot_d = (sqrt(order)-1);
        const = const - (tot_d/2 + tot_d/2*1i); % Translate matrix to center on origin.
        % Next step: organise constellation matrix elements into vector sorted in
        % gray sequence.

        constellation_vec = reshape(const, [1 order])./(tot_d/2); % Normalize, giving max magnitude = 1.
        
        % Sort into gray code
        [gray_seq, ~]    = bin2gray((0:order-1)', 'qam', order);
        constellation_vec_tmp = constellation_vec;
        for jj = 1:order
            constellation_vec_tmp(jj) = constellation_vec( gray_seq(jj)+1);
        end
        constellation_vec = constellation_vec_tmp;
        
        plot(constellation_vec, 'ob')
        grid on
        
        case '4096qam'
        % This is tricky because, although the points lie on three
        % concentric circles, they are not evenly spaced -> assign using
        % some kind of grid rather than rotation.
        
        % Generate matrix of 16-qam points.
        order = 4096;  % orderber of qam constellation points.
        
        constx = 0:(sqrt(order)-1);
        constx = repmat(constx, sqrt(order),1);
        consty = 0:(sqrt(order)-1);
        consty = repmat(consty', 1, sqrt(order));
        const = constx+1i*consty; % Make each point into complex orderber.
        
        % Total distance on each axis
        tot_d = (sqrt(order)-1);
        const = const - (tot_d/2 + tot_d/2*1i); % Translate matrix to center on origin.
        % Next step: organise constellation matrix elements into vector sorted in
        % gray sequence.

        constellation_vec = reshape(const, [1 order])./(tot_d/2); % Normalize, giving max magnitude = 1.
        
        % Sort into gray code
        [gray_seq, ~]    = bin2gray((0:order-1)', 'qam', order);
        constellation_vec_tmp = constellation_vec;
        for jj = 1:order
            constellation_vec_tmp(jj) = constellation_vec( gray_seq(jj)+1);
        end
        constellation_vec = constellation_vec_tmp;
        
        plot(constellation_vec, 'ob')
        grid on
    otherwise
        disp('Unrecognized constellation type. Provide a valid string')
end