Lab 6 Matlab Channel Modelling Rayleigh

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Eg1

%----Rayleigh_PDF-----------------------------------------
%----------Input Section----------------
N=1000000; %Number of samples to generate
variance = 0.5; % Variance of underlying Gaussian random variables
%---------------------------------------
%Independent Gaussian random variables with zero mean and unit variance
x = randn(1, N);
y = randn(1, N);
%Rayleigh fading envelope with the desired variance
r = sqrt(variance*(x.^2 + y.^2));
%Define bin steps and range for histogram plotting
step = 0.1;
range = 0:step:3;
%Get histogram values and approximate it to get the pdf curve
h = hist(r, range);
approxPDF = h/(step*sum(h)); %Simulated PDF from the x and y samples
%Theoritical PDF from the Rayleigh Fading equation
theoretical = (range/variance).*exp(-range.^2/(2*variance));
plot(range, approxPDF,'b', range, theoretical,'r*');
title('Simulated and Theoretical Rayleigh PDF for variance = 0.5')
legend('Simulated PDF','Theoretical PDF')
xlabel('r --->');
ylabel('P(r)---> ');
grid;
%PDF of phase of the Rayleigh envelope
theta = atan(y./x);
figure(2)
hist(theta); %Plot histogram of the phase part
%Approximate the histogram of the phase part to a nice PDF curve
[counts,range] = hist(theta,100);
step=range(2)-range(1);
%Normalizing the PDF to match theoretical curve
approxPDF = counts/(step*sum(counts)); %Simulated PDF from the x and y samples
bar(range, approxPDF,'b');
hold on
plotHandle=plot(range, approxPDF,'r');
set(plotHandle,'LineWidth',3.5);
axis([-2 2 0 max(approxPDF)+0.1])
hold off
title('Simulated PDF of Phase of Rayleigh Distribution ');
xlabel('\theta --->');
ylabel('P(\theta) --->');
grid;
Eg2
clc; close all; clear all;
N = 100000;
sigma = 1;
s=[0 1 2 4];
plotStyle={'b-','r-','k-','g-'};
%Simulating the PDF from two Gaussian Random Variables
for i = 1: length(s)
X = s(i) + sigma.*randn(1,N); %Gaussian RV with mean=s and given sigma
Y = 0 + sigma.*randn(1,N); %Gaussian RV with mean=0 and same sigma as Y
Z=X+1i*Y;
[val,bin]=hist(abs(Z),1000); % pdf of generated Raleigh Fading samples
plot(bin,val/trapz(bin,val),plotStyle{i}); %Normalizing the PDF to match
theoretical result
%Trapz function gives the total area under the PDF curve. It is used as the
normalization factor
hold on;
end
%Theoretical PDF computation
for i=1:length(s)
x=s(i);
m1=sqrt(x);
m2=sqrt(x*(x-1));
r=0:0.01:9;
ss=sqrt(m1^2+m2^2);
x=r.*ss/(sigma^2);
f=r./(sigma^2).*exp(-((r.^2+ss^2)./(2*sigma^2))).*besseli(0,x);
plot(r,f,plotStyle{i},'LineWidth',2.5);
legendInfo{i} = ['s = ' num2str(s(i))];
hold on;
end
legend(legendInfo);

EX1

SISO= One transmitter one receiver


% MATLAB code for SISO scheme with AWGN and plots

% Define parameters
SNR_dB = 10; % Set Signal-to-Noise Ratio (SNR) in dB

% Create and configure the system objects


numBits = 100;
src = randi([0 1], numBits, 1);
modulator = comm.BPSKModulator();
demodulator = comm.BPSKDemodulator();
awgn_channel = comm.AWGNChannel('EbNo', SNR_dB, 'SignalPower', 1);

error_rate = comm.ErrorRate();
% Initialize variables for plots
tx_signal = zeros(numBits, 1);
rx_signal = zeros(numBits, 1);

% Simulate the system


for i = 1:length(src)
% Transmit
data = src(i);
modulated_data = modulator(data);
received_data = awgn_channel(modulated_data);

% Receive and calculate error rate


demodulated_data = demodulator(received_data);
error_stats = error_rate(data, demodulated_data);

% Store signals for plotting


tx_signal(i) = modulated_data;
rx_signal(i) = received_data;
end

% Display results
disp(['Bit Error Rate: ', num2str(error_stats(1))]);

% Plot transmitted and received signals


figure;

subplot(2, 1, 1);
stem(tx_signal, 'r', 'LineWidth', 1.5);
title('Transmitted Signal (BPSK Modulation)');
xlabel('Bit Index');
ylabel('Amplitude');
grid on;

subplot(2, 1, 2);
stem(rx_signal, 'b', 'LineWidth', 1.5);
title('Received Signal (After AWGN)');
xlabel('Bit Index');
ylabel('Amplitude');
grid on;
Ex2
% MIMO Communication System with AWGN and Rayleigh Fading
% Parameters
SNR_dB = 20; % Signal-to-Noise Ratio in dB
numSymbols = 100; % Number of QPSK symbols (reduced for better readability)
numTx = 2; % Number of transmit antennas
numRx = 2; % Number of receive antennas
% Generate random QPSK symbols for each transmit antenna
txSymbols1 = randi([0, 3], 1, numSymbols);
txSymbols2 = randi([0, 3], 1, numSymbols);
txSymbols = [txSymbols1; txSymbols2];
% QPSK Modulation
modulatedSymbols = qammod(txSymbols, 4);
% Rayleigh Fading Channel
H = (randn(numRx, numTx) + 1i * randn(numRx, numTx)) / sqrt(2);
% AWGN
noise = sqrt(0.5 / (10^(SNR_dB/10))) * (randn(numRx, numSymbols) + 1i *
randn(numRx, numSymbols));
% Received Signal
receivedSignal = H * modulatedSymbols + noise;
% Time vector
time = (0:numSymbols-1);
% Plot real and imaginary parts of transmitted and received signals for each
antenna
figure;
% Transmitted signals
subplot(4, 2, 1);
plot(time, real(modulatedSymbols(1, :)), 'b', 'LineWidth', 1.5);
title('Real Part of Transmitted Signal (Antenna 1)');
xlabel('Time');
ylabel('Amplitude');
subplot(4, 2, 2);
plot(time, imag(modulatedSymbols(1, :)), 'b', 'LineWidth', 1.5);
title('Imaginary Part of Transmitted Signal (Antenna 1)');
xlabel('Time');
ylabel('Amplitude');
subplot(4, 2, 3);
plot(time, real(modulatedSymbols(2, :)), 'g', 'LineWidth', 1.5);
title('Real Part of Transmitted Signal (Antenna 2)');
xlabel('Time');
ylabel('Amplitude');
subplot(4, 2, 4);
plot(time, imag(modulatedSymbols(2, :)), 'g', 'LineWidth', 1.5);
title('Imaginary Part of Transmitted Signal (Antenna 2)');
xlabel('Time');
ylabel('Amplitude');
% Received signals
subplot(4, 2, 5);
plot(time, real(receivedSignal(1, :)), 'r', 'LineWidth', 1.5);
title('Real Part of Received Signal (Antenna 1)');
xlabel('Time');
ylabel('Amplitude');
subplot(4, 2, 6);
plot(time, imag(receivedSignal(1, :)), 'r', 'LineWidth', 1.5);
title('Imaginary Part of Received Signal (Antenna 1)');
xlabel('Time');
ylabel('Amplitude');
subplot(4, 2, 7);
plot(time, real(receivedSignal(2, :)), 'm', 'LineWidth', 1.5);
title('Real Part of Received Signal (Antenna 2)');
xlabel('Time');
ylabel('Amplitude');
subplot(4, 2, 8);
plot(time, imag(receivedSignal(2, :)), 'm', 'LineWidth', 1.5);
title('Imaginary Part of Received Signal (Antenna 2)');
xlabel('Time');
ylabel('Amplitude');

You might also like