MATLAB Examples

+Overview --------------------------------------------------------------

In this code, we drive the Capacity of a MIMO system over Rayleigh fading channel with different number of transmit and receiver antennas. For each setting of a MIMO channel, after calculating the singular values of the channel matrix, transmit power is distributed based on water-filling algorithm. The obtained capacity is the best that MIMO system can deliver as the full channel state information is used both at TX and RX side. -Overview --------------------------------------------------------------

Contents

+Clear -----------------------------------------------------------------

    clear       % clear all variables in the workspace
    close all   % clsoe all open figures
    clc         % clear command window
%  -Clear -----------------------------------------------------------------

+Independent parameters ------------------------------------------------

system parameters

    numOfTxAntennas = [1 2 3 2 4]; % a vector to hold number of antennas at
                                   % the transmitter side
    numOfRxAntennas = [1 2 2 3 4]; % a vector to hold number of antennas at
                                   % the receiver side
    % related to AWGN channel
    noisePower            = 1e-4;       % AWGN noise power
    signalToNoiseRatio_dB = -10:3:20;   % a vector to hold SNR values

    % loop parameters
    numOfIterations = 1e4;         % number of iterations (should be above
                                   % 1e3 for accurate result)
    % related to plots
    curveType       = {'b.-';'rs-';'g+--';'k--^';'m--d'};
%  -Independent parameters ------------------------------------------------

+Check Independent parameters ------------------------------------------

    nOfAntennasVecTx = length(numOfTxAntennas);
    nOfAntennasVecRx = length(numOfTxAntennas);
    if nOfAntennasVecTx ~= nOfAntennasVecRx
        error('Vectors numOfTxAntennas and numOfRxAntennas must have the same size')
    else
        nOfAntennasVec = nOfAntennasVecRx;  % number of elements in antennas
    end
    if nOfAntennasVec < length(curveType)
        error('Number of elements in numOfTxAntennas must be smaller than %d.',...
            length(curveType))
    end
%  -Check Independent parameters ------------------------------------------

+Dependent parameters --------------------------------------------------

convert from dB to scalar

    signalToNoiseRatio      = 10.^(0.1*signalToNoiseRatio_dB);
    % number of elements in SNR vector
    nOfsignalToNoiseRatio   = length(signalToNoiseRatio);
    % allocate memory for capacity
    CapacityVec             = ...
        zeros(nOfAntennasVec,nOfsignalToNoiseRatio,numOfIterations);
    % allocate memory for singular value decompostion
    lambdaVec               = ...
        zeros(nOfAntennasVec,nOfsignalToNoiseRatio,numOfIterations,nOfAntennasVec);
%  -Dependent parameters --------------------------------------------------

+Main ------------------------------------------------------------------

    for n = 1 : nOfAntennasVec      % loop over number of antennas
        nTx = numOfTxAntennas(n);   % num of Tx antennas in this iteration
        nRx = numOfRxAntennas(n);   % num of Rx antennas in this iteration
        for j = 1 : nOfsignalToNoiseRatio
            % load SNR for this iteration
            snr = signalToNoiseRatio(j);    % SNR at this iteration
            % calaculate transmit power
            txPower = noisePower*snr;       % we know SNR = txPower/noisePower
                                            % with the assumption that
                                            % there is no pathloss. It
                                            % means here txPower = rxPower;
            for i = 1 : numOfIterations     % loop over number of iterations
                % generate channel coefficients
                h = 1/sqrt(2)*(randn(nRx,nTx)+1i*randn(nRx,nTx));
                % calaculate singular value decomposition
                S = svd(h);
                % store values of lambda in SVD
                lambdaVec(n,j,i,1:min(nRx,nTx)) = S;
                % find carrier to noise rations
                cnr = S.^2/noisePower;
                % find allocated power based on waterfilling algorithm
                allocatedPower = waterFilling(cnr,txPower);
                % calculate the capacity
                capacity = sum(log2(1+allocatedPower.*cnr));
                % store the value
                CapacityVec(n,j,i) = capacity;
            end
        end
    end
%  -Main ------------------------------------------------------------------

+Post processing -------------------------------------------------------

take the average of the capacity

    CapacityVecAvg = mean(CapacityVec,3);

%  -Post processing -------------------------------------------------------

+Figures ---------------------------------------------------------------

plot capacity for different number of Tx and Rx antennas vs SNR

    f1 = figure(1);
    clf
    legendStr = cell(nOfAntennasVec,1);
    for n = 1 : nOfAntennasVec      % loop over number of antennas
        plot(signalToNoiseRatio_dB,CapacityVecAvg(n,:),curveType{n})
        hold on
        legendStr{n} = sprintf('nTx = %d, nRx = %d',...
            numOfTxAntennas(n),numOfRxAntennas(n));
    end
    xlabel('SNR [dB]')
    ylabel('Capacity [b/s/Hz]')
    grid on
    title('Avg Capacity of a MIMO system for different num of tx-rx antennas')
    legend(legendStr,'location','best')

    % plot distribution of capacity at SNR = 5dB
    f2 = figure(2);
    clf
    targetSNRdB = 5;
    Ind = find(signalToNoiseRatio_dB == targetSNRdB);
    for n = 1 : nOfAntennasVec      % loop over number of antennas
        capacities = CapacityVec(n,Ind,:);
        [y,x] = hist(capacities(:),30);
        % update y
        y = y ./ sum(y) ;       % normalization
        plot(x,cumsum(y),curveType{n})
        hold on
    end
    xlabel('Capacity [b/s/Hz]')
    ylabel('Probability of Capacity < GivenCapacity')
    grid on
    title(sprintf('CDF of the Capacity at SNR = %ddB',targetSNRdB))
    legend(legendStr,'location','best')

    % plot pdf of lambda at target SNR for a given nTx and nRx
    n = 5;  % select a setting from nTx and nRx
    nTx = numOfTxAntennas(n);   % num of Tx antennas in this setting
    nRx = numOfRxAntennas(n);   % num of Rx antennas in this setting
    f3 = figure(3);
    clf
    legendStr = cell(min(nTx,nRx),1);
    for k = 1 : min(nTx,nRx)
        lambdas = lambdaVec(n,Ind,:,k);
        [y,x] = hist(lambdas(:),30);
        % update y
        y = y ./ sum(y) ;       % normalization
        plot(x,y,curveType{k})
        hold on
        if k == 1
            legendStr{k} = '1st singular value';
        elseif k == 2
            legendStr{k} = '2nd singular value';
        else
            legendStr{k} = sprintf('%d-th singular value',k);
        end
    end
    ylabel('Nolmalized probability')
    xlabel('singular value')
    legend(legendStr)
    grid on
    title(sprintf('normalized pdf of svd values for nTx=%d, nRx=%d',nTx,nRx))
%  -Figures ---------------------------------------------------------------

+Save Figures ----------------------------------------------------------

    %saveas(f1,'avgCapacity','jpg');
    %saveas(f2,'cdfCapacity','jpg');
    %saveas(f3,'pdfLambdas','jpg');
%  -Save Figures ----------------------------------------------------------