function refSym = ehtReferenceSymbols(varargin)
%ehtReferenceSymbols Find the reference symbols of constellation diagram
%
%   REFSYM = ehtReferenceSymbols(MODSCHEME) returns the constellation used
%   for a specified modulation scheme as a column vector of complex
%   symbols.
%
%   MODSCHEME is the modulation scheme and must be one of 'BPSK', 'QBPSK',
%   'QPSK', '16QAM', '64QAM', '256QAM', '1024QAM', or '4096QAM'.
%
%   REFSYM = ehtReferenceSymbols(CFGEHT,USERNUMBER) returns the
%   constellation used in the data field of a transmission for an
%   individual user of interest in a multi-user configuration. When not
%   provided the USERNUMBER is assumed to be one.
%
%   CFGEHT is a EHT MU configuration object of type <a
%   href="matlab:help('ehtMUConfig')">ehtMUConfig</a>.
%
%   Example 1:
%   % Plot a noisy QPSK constellation with reference points
%
%       % Generate noisy QPSK symbols
%       sym = awgn(1/sqrt(2)*qammod(randi([0 3],100,1),4),20);
%       refSym = ehtReferenceSymbols('QPSK'); % Get reference constellation
%       figure;
%       plot(sym,'b.');
%       hold on;
%       plot(refSym,'ro');
%       legend('Symbols','Ref','Location','South');
%
%   Example 2:
%   % Plot a reference constellation of an EHT MU packet for 4096QAM
%
%       % Create EHT MU configuration object
%       cfgEHT = ehtMUConfig('CBW320');
%       cfgEHT.User{1}.MCS = 12;
%       % Plot reference constellation of second user
%       refSym = ehtReferenceSymbols(cfgEHT);
%       figure;
%       plot(refSym,'b*');
%
%   See also ehtMUConfig, comm.EVM.

%   Copyright 2020-2021 The MathWorks, Inc.

narginchk(1,3);

specifiedModScheme = true;
% Validate first input argument
validateattributes(varargin{1},{'char','string','ehtMUConfig'},{},mfilename,'first argument');
DCM = 0;
if ischar(varargin{1}) || isstring(varargin{1})
    % REFSYM = ehtReferenceSymbols(MODSCHEME,...)
    modScheme = varargin{1};

    % Validate modulation scheme
    modScheme = validatestring(modScheme,{'BPSK','QBPSK','pi/2-BPSK','QPSK','pi/2-QPSK','16QAM','pi/2-16QAM','64QAM','pi/2-64QAM','256QAM','1024QAM','4096QAM'},mfilename,'modulation scheme');
else
    cfgFormat = varargin{1};
    % REFSYM = ehtReferenceSymbols(CFGMU,USERNUMBER)
    if nargin>1
        userNum = varargin{2};
    else
        userNum = 1; % Assume single user
    end
    % Validate the user number for ehtMUConfig
    validateattributes(userNum,{'numeric'},{'integer','scalar','>=',1,'<=',length(cfgFormat.User)},mfilename,'user number');
    specifiedModScheme = false;
end

% Get the constellation used for the transmission
% Generate bit input for all symbols and constellation map
if specifiedModScheme==false
    nss = cfgFormat.User{userNum}.NumSpaceTimeStreams;
    mcsTable = wlan.internal.heRateDependentParameters(cfgFormat.RU{cfgFormat.User{userNum}.RUNumber}.Size,cfgFormat.User{userNum}.MCS,nss,0);

    NBPSCS = mcsTable.NBPSCS;
else
    switch modScheme
        case {'BPSK','QBPSK','pi/2-BPSK'}
            NBPSCS = 1;
        case {'QPSK','pi/2-QPSK'}
            NBPSCS = 2;
        case {'16QAM','pi/2-16QAM'}
            NBPSCS = 4;
        case {'64QAM','pi/2-64QAM'}
            NBPSCS = 6;
        case {'256QAM'}
            NBPSCS = 8;
        case {'1024QAM'}
            NBPSCS = 10; 
        otherwise
            NBPSCS = 12; % 4096QAM
    end
end
x = reshape(de2bi((0:((2^NBPSCS)-1)),'left-msb').',NBPSCS*2^NBPSCS,1);

% Return reference constellation
refSym = ehtConstellationMap(x,NBPSCS,DCM);

end