OFCDM MATLAB Code: Orthogonal Frequency Code Division Multiplexing

ofcdm
matlab
cdma
ofdm
ber

This section provides the MATLAB code for Orthogonal Frequency Code Division Multiplexing (OFCDM).

OFCDM combines CDMA and OFDM techniques. This example showcases an OFCDM transmitter with a FEC encoder (convolutional encoder), a BPSK modulator, a spreading module, and a 256-point IFFT module.

OFCDM MATLAB simulation code OFCDM MATLAB simulation code

The receiver consists of an FFT, despreading, a BPSK demodulator, and a Viterbi decoder. The following is the OFCDM MATLAB code and the corresponding BER curve, obtained by passing data through an AWGN channel.

OFCDM MATLAB Code

close all;
clear all;
clc;
n1=1;
n2=14;
FFT_SIZE=256;
%CP=16;
conv_in=[];

%% Data Generator
Data_gen = randint(1,11,255)
Data_IN=dec2bin(Data_gen);
s=0;

%% BER PARAMETERS
EbNo=0:1:15;
BER = zeros(1,length(EbNo));
numPackets=15;
frmLen = 1000;

for idx = 1: length(EbNo)
    for packetidx = 1 : numPackets

        %% Convolution Encoder
        conv_in=[];
        for index =1:11
            conv_in=[conv_in double(Data_IN(index,:))-48];
        end
        conv_in=[conv_in 0 0 0 0 0 0 0 0]; %%8 bits padding
        DIN=conv_in;
        trel = poly2trellis(7, [171 133]);  % Define trellis.
        code = convenc(conv_in,trel);
        inter_out=code;

        %% BPSK Data Mapping
        mapper_out=mapping(inter_out',1,1);
        clear inter_out;
        D=mapper_out;

        %% CDMA TRANSMITTER
        % encode bits and transmit
        % CDMA specific parameters
        C = [  -1  1 -1  1 ]; % code for the user which will be multiplied
        %with data stream of the user#1 i.e.mapper_out
        M = length(C); % length (number of bits) of code
        Y = size(mapper_out);
        N = Y(1); % number of unique senders / bit streams
        I = Y(2); % number of bits per stream
        T = []; % sum of all transmitted and encoded data on channel
        RECON = []; % vector of reconstructed bits at receiver
        G = zeros(I,M);

        for n = 1:N
            Z = zeros(I,M);
            for i = 1:I
                for m = 1:M
                    Z(i,m) = [D(n,i)*C(n,m)];
                end
            end
            G = G + Z; %G is the data to be transmitted after IFFT
        end

        ifft_in=zeros(256,4);
        for i=1:4
            ifft_in(:,i)=[0;G(1:96,i);zeros(32,1);zeros(31,1);G(97:192,i)];
        end
        for i=1:4
            tx_data(:,i)=ifft(ifft_in(:,i));
        end
        clear ifft_in;

        %% Passing the data through AWGN channel
        rx_data=zeros(256,4);
        rx_data1=zeros(192,4);
        for i=1:4
            rx_data(:,i)=awgn(tx_data(:,i)./sqrt(16),EbNo(idx),'measured');
        end
        for i=1:4
            rx_data(:,i)=awgn(rx_data(:,i)./sqrt(16),EbNo(idx),'measured');
        end
         for i=1:4
            rx_data(:,i)=awgn(rx_data(:,i)./sqrt(16),EbNo(idx),'measured');
        end

        %% OFCDM RECEIVER PART:
        %% Taking FFT of the noisy data after reception
        clear tx_data;
        for i=1:4
            rx_data(:,i)=fft(rx_data(:,i));
        end

        for i=1:4
            rx_data1(:,i)=[rx_data(2:97,i); rx_data(161:256,i)]; % taking out user#1 symbols for despreading
        end
        G=rx_data1;

        %% CDMA RECEIVER
        for n = 1:N
            TOT = zeros(1,I);
            R = zeros(I,M);
            for i = 1:I
                for m = 1:M
                    R(i,m) = G(i,m) * C (n,m);
                    TOT(i) = TOT(i) + R (i,m);
                end
            end
            RECON = [RECON ; TOT / M];
        end
        RECON
        rx_data1=RECON;
        Demap_out=demapper(rx_data1,1,1);

        %%viterbi decoder
        vit_out=vitdec(Demap_out,trel,7,'trunc','hard');
        DOUT=vit_out
        [number,ratio] = biterr(DIN,vit_out);
        error(packetidx) = biterr(DIN,vit_out);

    end % End of for loop for numPackets
    BER21(idx) = sum(error)/(log2(4)*numPackets*frmLen);

end

h=gcf;clf(h);
grid on;
hold on;
set(gca,'yscale','log','xlim',[EbNo(1), EbNo(end)],'ylim',[0 1]);
xlabel('Eb/No (dB)');
ylabel('BER');
set(h,'NumberTitle','off');
set(h,'Name','BER Results');
set(h, 'renderer', 'zbuffer');
title('OFCDM BER PLOTS');
semilogy(EbNo(1:end),BER21(1:end),'b-*');

OFCDM BER curve OFCDM BER curve

CDMA MATLAB Code for Simulation

CDMA MATLAB Code for Simulation

Explore CDMA (Code Division Multiple Access) with this MATLAB source code. Simulate CDMA transmission and reception, including BER analysis over AWGN.

cdma
matlab
simulation
OFDM MATLAB Code Implementation

OFDM MATLAB Code Implementation

Explore a basic OFDM transmitter and receiver chain implemented in MATLAB, including code for modulation, IFFT, cyclic prefix, and more.

ofdm
matlab
communication system