0% found this document useful (0 votes)
16 views11 pages

Mini Codes

The document contains MATLAB code for simulating OFDM systems using QAM modulation (16-QAM and 64-QAM) under both AWGN and Rician fading channels. It initializes system parameters, generates random data, applies custom QAM modulation, and calculates the Bit Error Rate (BER) across different SNR values. The results are plotted to visualize the performance of the modulation schemes under varying conditions.

Uploaded by

thanmayee8475
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views11 pages

Mini Codes

The document contains MATLAB code for simulating OFDM systems using QAM modulation (16-QAM and 64-QAM) under both AWGN and Rician fading channels. It initializes system parameters, generates random data, applies custom QAM modulation, and calculates the Bit Error Rate (BER) across different SNR values. The results are plotted to visualize the performance of the modulation schemes under varying conditions.

Uploaded by

thanmayee8475
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

QAM 16BIT

% System parameters
numSubcarriers = 64;
modOrder = 16;
snrRange = 0:5:30;
numFrames = 100;
% Initialize BER array
berOFDM = zeros(1, length(snrRange));
for snrIdx = 1:length(snrRange)
snr = snrRange(snrIdx);
errors = 0;
bits = 0;
for frame = 1:numFrames
% Generate random data
data = randi([0 modOrder-1], numSubcarriers, 1);
% Custom QAM Modulation
tx = customQamMod(data, modOrder);
ifftSignal = ifft(tx, numSubcarriers);
% Apply AWGN
rxSignal = awgn(ifftSignal, snr, 'measured');
% OFDM Demodulation
fftSignal = fft(rxSignal, numSubcarriers);
rxData = customQamDemod(fftSignal, modOrder);
% Count errors
errors = errors + sum(data ~= rxData);
bits = bits + numSubcarriers * log2(modOrder);
end
% Calculate BER
berOFDM(snrIdx) = errors / bits;
end

% Plot results
figure;
semilogy(snrRange, berOFDM, 'b-o');
xlabel('SNR (dB)');
ylabel('BER');
title('BER vs. SNR for OFDM under AWGN');
grid on;
% Custom QAM Modulation Function
function modulatedSignal = customQamMod(data, M)
k = log2(M); % Number of bits per symbol
symbols = qammod(data, M, 'gray');
modulatedSignal = symbols / sqrt(mean(abs(symbols).^2)); % Normalize power
end
% Custom QAM Demodulation Function
function demodulatedData = customQamDemod(modulatedSignal, M)
symbols = modulatedSignal * sqrt(mean(abs(qammod(0:M-1, M, 'gray')).^2));
demodulatedData = qamdemod(symbols, M, 'gray');
End
QAM64BIT
% System parameters
numSubcarriers = 64;
modOrder = 64; % Change to 64-QAM
snrRange = 0:5:30;
numFrames = 100;

% Initialize BER array


berOFDM = zeros(1, length(snrRange));

for snrIdx = 1:length(snrRange)


snr = snrRange(snrIdx);
errors = 0;
bits = 0;
for frame = 1:numFrames
% Generate random data
data = randi([0 modOrder-1], numSubcarriers, 1);
% Custom QAM Modulation
tx = customQamMod(data, modOrder);
ifftSignal = ifft(tx, numSubcarriers);
% Apply AWGN
rxSignal = awgn(ifftSignal, snr, 'measured');
% OFDM Demodulation
fftSignal = fft(rxSignal, numSubcarriers);
rxData = customQamDemod(fftSignal, modOrder);
% Count errors
errors = errors + sum(data ~= rxData);
bits = bits + numSubcarriers * log2(modOrder);
end
% Calculate BER
berOFDM(snrIdx) = errors / bits;
end

% Plot results
figure;
semilogy(snrRange, berOFDM, 'b-o');
xlabel('SNR (dB)');
ylabel('BER');
title('BER vs. SNR for OFDM under AWGN (64-QAM)');
grid on;

% Custom QAM Modulation Function


function modulatedSignal = customQamMod(data, M)
k = log2(M); % Number of bits per symbol
symbols = qammod(data, M, 'gray');
modulatedSignal = symbols / sqrt(mean(abs(symbols).^2)); % Normalize power
end

% Custom QAM Demodulation Function


function demodulatedData = customQamDemod(modulatedSignal, M)
symbols = modulatedSignal * sqrt(mean(abs(qammod(0:M-1, M, 'gray')).^2));
demodulatedData = qamdemod(symbols, M, 'gray');
end
QAM16&64BIT
% System parameters
numSubcarriers = 64;
snrRange = 0:5:15;
numFrames = 100;

% Modulation orders for 16-QAM and 64-QAM


modOrders = [16, 64];

% Initialize BER arrays


berOFDM_16QAM = zeros(1, length(snrRange));
berOFDM_64QAM = zeros(1, length(snrRange));

% Loop over both modulation schemes (16-QAM and 64-QAM)


for modIdx = 1:length(modOrders)
modOrder = modOrders(modIdx);
% Loop over each SNR value
for snrIdx = 1:length(snrRange)
snr = snrRange(snrIdx);
errors = 0;
bits = 0;
% Process multiple frames
for frame = 1:numFrames
% Generate random data
data = randi([0 modOrder-1], numSubcarriers, 1);
% Custom QAM Modulation
tx = customQamMod(data, modOrder);
ifftSignal = ifft(tx, numSubcarriers);
% Apply AWGN
rxSignal = awgn(ifftSignal, snr, 'measured');
% OFDM Demodulation
fftSignal = fft(rxSignal, numSubcarriers);
rxData = customQamDemod(fftSignal, modOrder);
% Count errors
errors = errors + sum(data ~= rxData);
bits = bits + numSubcarriers * log2(modOrder);
end
% Calculate BER
if modOrder == 16
berOFDM_16QAM(snrIdx) = errors / bits;
elseif modOrder == 64
berOFDM_64QAM(snrIdx) = errors / bits;
end
end
end

% Plot results
figure;
semilogy(snrRange, berOFDM_16QAM, 'b-o', 'DisplayName', '16-QAM');
hold on;
semilogy(snrRange, berOFDM_64QAM, 'r-s', 'DisplayName', '64-QAM');
xlabel('SNR (dB)');
ylabel('BER');
title('BER vs. SNR for 16-QAM and 64-QAM under AWGN');
legend('Location', 'southwest');
grid on;
% Custom QAM Modulation Function
function modulatedSignal = customQamMod(data, M)
symbols = qammod(data, M, 'gray');
modulatedSignal = symbols / sqrt(mean(abs(symbols).^2)); % Normalize power
end
% Custom QAM Demodulation Function
function demodulatedData = customQamDemod(modulatedSignal, M)
symbols = modulatedSignal * sqrt(mean(abs(qammod(0:M-1, M, 'gray')).^2));
demodulatedData = qamdemod(symbols, M, 'gray');
end
RICIAN16BIT
% System parameters
numSubcarriers = 64;
modOrder = 16;
snrRange = 0:5:30;
numFrames = 100;

% Rician channel parameters


K = 6; % Rician K-factor (ratio of LOS to NLOS components)
fd = 5; % Doppler frequency in Hz
sampleRate = 1e3; % Example sample rate, adjust as needed
pathDelays = 0; % No delay
averagePathGains = 0; % Single path, no loss

% Initialize BER array


berOFDM = zeros(1, length(snrRange));

% Create Rician channel object


ricianChan = comm.RicianChannel( ...
'SampleRate', sampleRate, ...
'KFactor', K, ...
'PathDelays', pathDelays, ...
'AveragePathGains', averagePathGains, ...
'DopplerSpectrum', doppler('Jakes'), ... % Only specify 'Jakes'
'MaximumDopplerShift', fd); % Specify maximum Doppler shift here

for snrIdx = 1:length(snrRange)


snr = snrRange(snrIdx);
errors = 0;
bits = 0;
for frame = 1:numFrames
% Generate random data
data = randi([0 modOrder-1], numSubcarriers, 1);
% Custom QAM Modulation
tx = customQamMod(data, modOrder);
ifftSignal = ifft(tx, numSubcarriers);
% Pass the signal through the Rician fading channel
fadedSignal = ricianChan(ifftSignal);
% Apply AWGN
rxSignal = awgn(fadedSignal, snr, 'measured');
% OFDM Demodulation
fftSignal = fft(rxSignal, numSubcarriers);
rxData = customQamDemod(fftSignal, modOrder);
% Count errors
errors = errors + sum(data ~= rxData);
bits = bits + numSubcarriers * log2(modOrder);
end
% Calculate BER
berOFDM(snrIdx) = errors / bits;
end

% Plot results
figure;
semilogy(snrRange, berOFDM, 'b-o');
xlabel('SNR (dB)');
ylabel('BER');
title('BER vs. SNR for OFDM under Rician Fading Channel');
grid on;
% Custom QAM Modulation Function
function modulatedSignal = customQamMod(data, M)
k = log2(M); % Number of bits per symbol
symbols = qammod(data, M, 'gray');
modulatedSignal = symbols / sqrt(mean(abs(symbols).^2)); % Normalize power
end

% Custom QAM Demodulation Function


function demodulatedData = customQamDemod(modulatedSignal, M)
symbols = modulatedSignal * sqrt(mean(abs(qammod(0:M-1, M, 'gray')).^2));
demodulatedData = qamdemod(symbols, M, 'gray');
end
RICIAN64BIT
% System parameters
numSubcarriers = 64;
modOrder = 16;
snrRange = 0:5:30;
numFrames = 100;

% Rician channel parameters


K = 6; % Rician K-factor (ratio of LOS to NLOS components)
fd = 5; % Doppler frequency in Hz
sampleRate = 1e3; % Example sample rate, adjust as needed
pathDelays = 0; % No delay
averagePathGains = 0; % Single path, no loss

% Initialize BER array


berOFDM = zeros(1, length(snrRange));

% Create Rician channel object


ricianChan = comm.RicianChannel( ...
'SampleRate', sampleRate, ...
'KFactor', K, ...
'PathDelays', pathDelays, ...
'AveragePathGains', averagePathGains, ...
'DopplerSpectrum', doppler('Jakes'), ... % Only specify 'Jakes'
'MaximumDopplerShift', fd); % Specify maximum Doppler shift here

for snrIdx = 1:length(snrRange)


snr = snrRange(snrIdx);
errors = 0;
bits = 0;
for frame = 1:numFrames
% Generate random data
data = randi([0 modOrder-1], numSubcarriers, 1);
% Custom QAM Modulation
tx = customQamMod(data, modOrder);
ifftSignal = ifft(tx, numSubcarriers);
% Pass the signal through the Rician fading channel
fadedSignal = ricianChan(ifftSignal);
% Apply AWGN
rxSignal = awgn(fadedSignal, snr, 'measured');
% OFDM Demodulation
fftSignal = fft(rxSignal, numSubcarriers);
rxData = customQamDemod(fftSignal, modOrder);
% Count errors
errors = errors + sum(data ~= rxData);
bits = bits + numSubcarriers * log2(modOrder);
end
% Calculate BER
berOFDM(snrIdx) = errors / bits;
end

% Plot results
figure;
semilogy(snrRange, berOFDM, 'b-o');
xlabel('SNR (dB)');
ylabel('BER');
title('BER vs. SNR for OFDM under Rician Fading Channel');
grid on;
% Custom QAM Modulation Function
function modulatedSignal = customQamMod(data, M)
k = log2(M); % Number of bits per symbol
symbols = qammod(data, M, 'gray');
modulatedSignal = symbols / sqrt(mean(abs(symbols).^2)); % Normalize power
end

% Custom QAM Demodulation Function


function demodulatedData = customQamDemod(modulatedSignal, M)
symbols = modulatedSignal * sqrt(mean(abs(qammod(0:M-1, M, 'gray')).^2));
demodulatedData = qamdemod(symbols, M, 'gray');
end
RICIAN 16&64BIT
% System parameters
numSubcarriers = 64;
snrRange = 0:5:30;
numFrames = 100;

% Rician channel parameters


K = 6; % Rician K-factor (ratio of LOS to NLOS components)
fd = 5; % Doppler frequency in Hz
sampleRate = 1e3; % Example sample rate, adjust as needed
pathDelays = 0; % No delay
averagePathGains = 0; % Single path, no loss

% Initialize BER arrays


berOFDM_16QAM = zeros(1, length(snrRange));
berOFDM_64QAM = zeros(1, length(snrRange));

% Create Rician channel object


ricianChan = comm.RicianChannel( ...
'SampleRate', sampleRate, ...
'KFactor', K, ...
'PathDelays', pathDelays, ...
'AveragePathGains', averagePathGains, ...
'DopplerSpectrum', doppler('Jakes'), ...
'MaximumDopplerShift', fd);

% Run the simulation for 16-QAM


modOrder = 16; % 16-QAM
berOFDM_16QAM = simulateOFDM(numSubcarriers, modOrder, snrRange, numFrames,
ricianChan);

% Run the simulation for 64-QAM


modOrder = 64; % 64-QAM
berOFDM_64QAM = simulateOFDM(numSubcarriers, modOrder, snrRange, numFrames,
ricianChan);

% Plot results for both 16-QAM and 64-QAM


figure;
semilogy(snrRange, berOFDM_16QAM, 'b-o', 'DisplayName', '16-QAM');
hold on;
semilogy(snrRange, berOFDM_64QAM, 'r-s', 'DisplayName', '64-QAM');
xlabel('SNR (dB)');
ylabel('BER');
title('BER vs. SNR for 16-QAM and 64-QAM under Rician Fading Channel');
grid on;
legend show;
hold off;

% Function to simulate OFDM over Rician channel


function berOFDM = simulateOFDM(numSubcarriers, modOrder, snrRange,
numFrames, ricianChan)
berOFDM = zeros(1, length(snrRange));
for snrIdx = 1:length(snrRange)
snr = snrRange(snrIdx);
errors = 0;
bits = 0;
for frame = 1:numFrames
% Generate random data
data = randi([0 modOrder-1], numSubcarriers, 1);
% Custom QAM Modulation
tx = customQamMod(data, modOrder);
ifftSignal = ifft(tx, numSubcarriers);
% Pass the signal through the Rician fading channel
fadedSignal = ricianChan(ifftSignal);
% Apply AWGN
rxSignal = awgn(fadedSignal, snr, 'measured');
% OFDM Demodulation
fftSignal = fft(rxSignal, numSubcarriers);
rxData = customQamDemod(fftSignal, modOrder);
% Count errors
errors = errors + sum(data ~= rxData);
bits = bits + numSubcarriers * log2(modOrder);
end
% Calculate BER
berOFDM(snrIdx) = errors / bits;
end
end

% Custom QAM Modulation Function


function modulatedSignal = customQamMod(data, M)
symbols = qammod(data, M, 'gray');
modulatedSignal = symbols / sqrt(mean(abs(symbols).^2)); % Normalize power
end

% Custom QAM Demodulation Function


function demodulatedData = customQamDemod(modulatedSignal, M)
symbols = modulatedSignal * sqrt(mean(abs(qammod(0:M-1, M, 'gray')).^2));
demodulatedData = qamdemod(symbols, M, 'gray');
End

You might also like