MQAM
Question: SEP of 4-QAM using analytical and Monte Carlo
% 4-QAM Symbol Error Probability (SEP) Calculation and
Plotting
clear;
M = 4; % 4-QAM
SNR_dB = 0:1:20; % SNR range in dB
num_realizations = 1e5; % Number of realizations for
MC simulation
% Preallocation
SEP_sim = zeros(1, length(SNR_dB));
SEP_analytical = zeros(1, length(SNR_dB));
% Symbol energy for normalized constellation
Es = 1;
% Loop over SNR values
for idx = 1:length(SNR_dB)
SNR_linear = 10^(SNR_dB(idx)/10); % SNR in linear
scale
noise_variance = Es / (2 * SNR_linear);
% Monte Carlo Simulation
error_count = 0;
for n = 1:num_realizations
% Generate equiprobable 4-QAM symbol
real_part = 2 * (randi([0,1]) - 0.5);
imag_part = 2 * (randi([0,1]) - 0.5);
symbol_tx = (real_part + 1j * imag_part) *
sqrt(Es/2);
% Add AWGN noise
noise = sqrt(noise_variance) * (randn + 1j *
randn);
symbol_rx = symbol_tx + noise;
% Maximum likelihood detection
real_decision = 2 * (real(symbol_rx) >= 0) - 1;
imag_decision = 2 * (imag(symbol_rx) >= 0) - 1;
symbol_dec = (real_decision + 1j *
imag_decision) * sqrt(Es/2);
% Error counting
if symbol_tx ~= symbol_dec
error_count = error_count + 1;
end
end
% Symbol Error Probability from Simulation
SEP_sim(idx) = error_count / num_realizations;
% Analytical SEP calculation for 4-QAM
b = 3 / (M - 1);
SEPsc = 2 * (1 - 1 / sqrt(M)) * qfunc(sqrt(b *
SNR_linear));
SEP_analytical(idx) = 1 - (1 - SEPsc)^2;
end
% Plotting
figure;
semilogy(SNR_dB, SEP_sim, 'bo-', 'LineWidth', 1.5,
'DisplayName', '4-QAM (Simulation)');
hold on;
semilogy(SNR_dB, SEP_analytical, 'r--', 'LineWidth',
1.5, 'DisplayName', '4-QAM (Analysis)');
grid on;
xlabel('SNR (dB)');
ylabel('Symbol Error Probability (SEP)');
title('Symbol Error Probability for 4-QAM in AWGN
channel');
legend('show');
hold off;
BPSK
Question:: SEP of BPSK using analytical and monte
carlo
% analytic_SEP.m
function [SEP] = analytic_SEP(SNR)
% Assuming BPSK modulation in AWGN channel
SEP = qfunc(sqrt(2 * SNR)); % Exact SEP formula
for BPSK
end
____________________________________________
% monteCarlo_SEP.m
function [SEP] = monteCarlo_SEP(SNR, N_symbols)
SEP = zeros(1, length(SNR));
for i = 1:length(SNR)
% Generate random BPSK symbols (+1 or -1)
transmitted_symbols = randi([0 1], 1,
N_symbols) * 2 - 1;
% Generate noise with zero mean and unit
variance
noise = (randn(1, N_symbols) + 1i * randn(1,
N_symbols)) / sqrt(2);
% Received symbols
received_symbols = sqrt(SNR(i)) *
transmitted_symbols + noise;
% Decision (BPSK)
detected_symbols = real(received_symbols) >
0;
transmitted_symbols_binary =
(transmitted_symbols + 1) / 2;
% Calculate symbol error rate
SEP(i) = sum(transmitted_symbols_binary ~=
detected_symbols) / N_symbols;
end
end
______________________________________________
% upper_bound_SEP.m
function [SEP] = upper_bound_SEP(SNR)
SEP = 0.5 * exp(-SNR); % Upper bound for BPSK
SEP
end
_____________________________________________
% Define the SNR range in dB
SNR_dB = 0:10;
SNR = 10.^(SNR_dB/10); % Convert SNR from dB to
linear scale
% Number of symbols for Monte Carlo simulation
N_symbols = 1e6;
% Call the subprograms
[SEP_analytic] = analytic_SEP(SNR); % Analytical
result
[SEP_simulation] = monteCarlo_SEP(SNR, N_symbols);
% Monte Carlo simulation
[SEP_upper_bound] = upper_bound_SEP(SNR); % Upper
bound result
% Plot the results
figure;
semilogy(SNR_dB, SEP_simulation, 'bo-',
'DisplayName', 'Simulation (Monte-Carlo)');
hold on;
semilogy(SNR_dB, SEP_analytic, 'r-', 'DisplayName',
'Analysis');
semilogy(SNR_dB, SEP_upper_bound, 'g-',
'DisplayName', 'Upper Bound');
xlabel('SNR in dB');
ylabel('SEP');
title('Symbol Error Probability vs. SNR');
legend show;
grid on;
Probability F.
Question:: Buffon Needle
N = 10^6; %number of throws
% Parameters
d = 9; % Distance between lines
L = 4.5; % Length of the needle
% Initialize the count for needle crossings
hits = 0;
for i = 1:N
Y = (d / 2) * rand(); % Uniformly distributed
between [0, d/2]
theta = (pi / 2) * rand(); % Uniformly distributed
between [0, pi/2]
% Check if the needle intersects a line
if Y <= (L / 2) * sin(theta)
hits = hits + 1;
end
end
% Estimate the probability p
p = hits / N;
pi_estimate = (2 * L) / (p * d);
fprintf('Estimated value of pi after %d throws:
%f\n', N, pi_estimate);
Question:: Central Limit Theorem
% Parameters for Gaussian distribution
mu = 1.8;
sigma2 = 10.8;
% Mean of the distribution
% Variance of the distribution
sigma = sqrt(sigma2); % Standard deviation
num_samples = 1e5; % Number of samples
% Generate the random sequence x[n]
x = mu + sigma * randn(1, num_samples); % Gaussian
random sequence
% Create the histogram of the generated data using
100 bins
figure;
% Generate the histogram (normalized to represent
PDF)
[hist_values, bin_edges] = histcounts(x, 100,
'Normalization','pdf');
bin_centers = (bin_edges(1:end-1) +
bin_edges(2:end)) / 2; % Midpoints of the bins
% Plot the normalized histogram as a bar plot
bar(bin_centers, hist_values, 'FaceColor', 'b',
'EdgeColor', 'b');
hold on;
% Theoretical PDF formula for Gaussian distribution
x_theoretical = linspace(min(x), max(x), 1000); %
X values for theoretical PDF
pdf_theoretical = (1 / (sigma * sqrt(2 * pi))) *
exp(-0.5 * ((x_theoretical - mu) / sigma) .^ 2); %
Plot the theoretical PDF
plot(x_theoretical, pdf_theoretical, 'r',
'LineWidth', 2); % Set line width to 2
% Add labels, title, and legend
xlabel('x', 'FontName', 'Arial', 'FontSize', 10); %
Set font to Arial and size 10
ylabel('Probability Density', 'FontName', 'Arial',
'FontSize', 10); % Set font to Arial and size 10
title('Simulated vs. Theoretical PDF of Gaussian
Random Sequence', 'FontName', 'Arial', 'FontSize',
10); % Title settings
legend('Simulated Histogram', 'Theoretical PDF',
'FontName', 'Arial', 'FontSize', 10); % Set legend
font and size
Delta Modulation
Question:: Max step size delta (delta/Ts = max. Of sgl)
% Parameters
Am = 1; % Amplitude of the message
signal
fm = 1; % Frequency of the message
signal (assume 1 Hz for single-tone modulation)
Ts = 0.045; % Sampling duration
% Task 1 (a): Single-tone modulation: m(t) = Am *
sin(2*pi*fm*t)
max_S_a = 2 * pi * fm * Am; % Maximum slope
Delta_a = Ts * max_S_a; % Step size for (a)
% Task 1 (b): m(t) = sin(t)
max_S_b = 1; % Maximum slope is 1 for
sin(t)
Delta_b = Ts * max_S_b; % Step size for (b)
% Display results
fprintf('Task 1 (a): For m(t) = Am *
sin(2*pi*fm*t), Delta = %.4f\n', Delta_a);
fprintf('Task 1 (b): For m(t) = sin(t), Delta =
%.4f\n', Delta_b);
Question: Delta mod.,demod.& difference sgl
Am = 1;
Ts = 0.045;
t = 0:Ts:9;
m_t = Am * sin(t);
Delta = Ts;Step size for modulation.
mq = 0; Instantaneous level of the demodulated
signal
N = length(t);
modulated_signal = zeros(1, N);
demodulated_signal = zeros(1, N);
for i = 1:N
if m_t(i) > mq
modulated_signal(i) = 1;
mq = mq + Delta;
else
modulated_signal(i) = 0;
mq = mq - Delta;
end
demodulated_signal(i) = mq;
end
difference_signal = abs(m_t - demodulated_signal);
figure;
plot(t, m_t, 'LineWidth', 2); hold on;
stairs(t, demodulated_signal, 'r', 'LineWidth', 2);
plot(t, demodulated_signal, 'g--', 'LineWidth', 2);
plot(t, difference_signal, 'k', 'LineWidth', 2);
legend('Original signal', 'Delta modulated
(stairs)', 'Demodulated signal', 'Difference
signal');
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Delta Modulation and Demodulation');
grid on;
squared_error = sum((m_t - demodulated_signal).^2);
fprintf('The sum of squared error is: %.4f\n',
squared_error);
PCM
Question: PCM encoding
% Parameters
fm = 3; % Message signal frequency (Hz)
fs = 30; % Sampling frequency (Hz)
Am = 2; % Amplitude of message signal (volts)
n = 3; % Number of bits
L = 2^n; % Number of quantization levels
t = 0:0.001/fm:1; % Time vector for continuous
signal
ts = 0:1/fs:1; % Time vector for sampled signal
% Message signal
message_signal = Am * sin(2 * pi * fm * t);
% Sampled signal
sampled_signal = Am * sin(2 * pi * fm * ts);
% Quantization parameters
Vmax = Am; % Maximum amplitude
Vmin = -Am; % Minimum amplitude
delta = (Vmax - Vmin) / L; % Step size
partition = Vmin + delta:delta:Vmax - delta; %
Partition values (decision boundaries)
codebook = Vmin + delta/2:delta:Vmax - delta/2; %
Quantization levels (codebook)
% Quantization process
[quantized_indexes, quantized_signal] =
quantiz(sampled_signal, partition, codebook);
% Encoding process
encoded_signal = de2bi(quantized_indexes, n,
'left-msb');
encoded_bits = reshape(encoded_signal.', 1, []); %
Convert to row vector
% Plotting
figure;
% 1. Original Message Signal and Sampled Signal
subplot(4,1,1);
plot(t, message_signal, 'b');
hold on;
stem(ts, sampled_signal, 'r', 'filled');
xlabel('Time (s)');
ylabel('Amplitude');
title('Original Message Signal and Sampled
Signal');
legend('Message Signal', 'Sampled Signal');
% 2. Sampled vs Quantized Values
subplot(4,1,2);
stem(ts, sampled_signal, 'r', 'filled');
hold on;
stairs(ts, quantized_signal, 'g', 'LineWidth', 2);
stem(ts, quantized_signal, 'g');
hold on;
stem(ts, sampled_signal, 'r');
xlabel('Time (s)');
ylabel('Amplitude');
title('Sampled vs Quantized Signal');
legend('Sampled Signal', 'Quantized Signal');
% 3. Sampled Signal vs Quantized Signal
subplot(4,1,3);
stem(ts, quantized_signal, 'g', 'filled');
hold on;
stairs(ts, quantized_signal, 'g', 'LineWidth', 2);
xlabel('Time (s)');
ylabel('Amplitude');
title('Quantized Signal');
legend('Quantized Signal');
% 4. Encoded Digital Signal
subplot(4,1,4);
stairs(encoded_bits, 'k', 'LineWidth', 2);
xlabel('Encoded Bits');
ylabel('Binary Value');
title('Encoded Digital Signal');
legend('Encoded Signal');
Question:: Quantizer
% Parameters
fm = 3; % Message signal frequency (Hz)
fs = 30; % Sampling frequency (Hz)
Am = 2; % Amplitude of message signal (volts)
n = 3; % Number of bits
L = 2^n; % Number of quantization levels
% Quantization parameters
Vmax = Am; % Maximum amplitude
Vmin = -Am; % Minimum amplitude
delta = (Vmax - Vmin) / L; % Step size
partition = Vmin + delta:delta:Vmax - delta; %
Partition (decision boundaries)
codebook = Vmin + delta/2:delta:Vmax - delta/2; %
Quantization levels (codebook)
% Steps for plotting (input values)
steps = Vmin:delta:Vmax; % Steps for x-axis
% Plot the quantizer characteristic curve
figure;
stairs(steps, [codebook codebook(end)],
'LineWidth', 2); % Match lengths by extending
codebook
xlabel('Input Values (Steps)');
ylabel('Quantized Levels');
title('Quantizer Characteristic Curve');
legend('Quantization Levels');
grid on;
PWM PPM
Question: PWM
fc = 4000;
fs = 80000;
fm = 900;
t1 = linspace(0, 4, 900); % Creates a time vector
from 0 to 4 seconds with 900 points
x = (1 + sin(2*pi*fm*t1))/2;
[y_pwm,t] = modulate(x, fc, fs, 'pwm', 'centered');
t_pwm = linspace(0, 4, length(y_pwm));
y_demod = demod(y_pwm, fc, fs, 'pwm', 'centered');
t_demod = linspace(0, 4, length(y_demod));
figure;
subplot(3,1,1);
plot(t1, x);
axis([0 2 -1.2 1.2]);
xlabel('Time (s)');
ylabel('Amplitude');
title('Normalized Message Signal');
legend('Message Signal');
subplot(3,1,2);
plot(t, y_pwm);
axis([0 0.004 -0.2 1.2]);
xlabel('Time (s)');
ylabel('Amplitude');
title('PWM Signal');
legend('PWM Signal');
subplot(3,1,3);
plot(t_demod, y_demod);
axis([0 2 -1.2 1.2]);
xlabel('Time (s)');
ylabel('Amplitude');
title('Demodulated Signal');
legend('Demodulated Signal');
Question: PWM with sawtooth
% Time vector for one second
t = 0:0.001:1;
% Message signal
m_t = -0.225*1i*(exp(1i*2*pi*t) - exp(-1i*2*pi*t));
% Message signal
% Sawtooth signal
s = sawtooth(20*pi*(t+0.05)); % Sawtooth signal
with a frequency of 10 Hz
% PWM signal generation
pwm_signal = zeros(size(t)); % Initialize PWM
signal
pwm_signal(real(m_t) > s) = 1; % Set high where
message amplitude is higher than sawtooth
% Plot message signal, sawtooth signal, and PWM
signal in one figure
figure;
% Plot message signal
%subplot(3,1,1);
plot(t, real(m_t)); % Taking the real part of the
complex message signal
xlabel('Time (s)');
ylabel('Amplitude');
title('Message Signal');
axis([0 1 -1.5 1.5]);
hold on;
% Plot sawtooth signal
%subplot(3,1,2);
plot(t, s);
xlabel('Time (s)');
ylabel('Amplitude');
title('Sawtooth Signal');
axis([0 1 -1.5 1.5]);
hold on;
% Plot PWM signal
%subplot(3,1,3);
plot(t, pwm_signal);
xlabel('Time (s)');
ylabel('Amplitude');
title('PWM Signal');
axis([0 1 -1.5 1.5]);
hold on;
% Add a legend
legend('Message Signal', 'Sawtooth Signal', 'PWM
Signal');
Question : PPM 1
% Define parameters
fc = 8000; % Carrier frequency in Hz
fs = 80000; % Sampling frequency in Hz
fm = 400; % Message frequency in Hz
t1 = linspace(0, 4, 800); % Time vector for 4
seconds
% Message signal
x = (1 + sin(2*pi*fm*t1))/2; % Message signal
% PPM modulation
[y_ppm,t] = modulate(x, fc, fs, 'ppm');
t_ppm = linspace(0, 4, length(y_ppm));
% Plot the message signal and PPM signal
figure;
% Plot message signal
subplot(2,1,1);
plot(t1, x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Original Message Signal');
axis([0 4 -0.2 1.2]);
% Plot PPM signal
subplot(2,1,2);
plot(t, y_ppm);
xlabel('Time (s)');
ylabel('Amplitude');
title('PPM Signal');
axis([0 0.004 -0.2 1.2]);
% Add a legend
legend('Message Signal', 'PPM Signal');
Question: PPM 2
% Define parameters
fc = 50; % Carrier frequency in Hz
fs = 1000; % Sampling frequency in Hz
fm = 200; % Message frequency in Hz
% Time vector
t1 = 0:1/fs:(2/fm - 1/fs); % Time vector for 2
periods of the message signal
% Message signal
x = 0.4 * cos(2*pi*fm*t1) + 0.5; % Message signal
% PPM modulation
[y_ppm,t] = modulate(x, fc, fs, 'ppm');
% PPM demodulation
y_demod = demod(y_ppm, fc, fs, 'ppm');
% Plot the message signal, PPM signal, and
demodulated signal
figure;
% Plot message signal
subplot(3,1,1);
plot(t1, x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Original Message Signal');
axis([0 0.009 -0.2 1.2]);
% Plot PPM signal
subplot(3,1,2);
plot(t, y_ppm);
xlabel('Time (s)');
ylabel('Amplitude');
title('PPM Signal');
axis([0 0.2 -0.2 1.2]);
% Plot demodulated signal
subplot(3,1,3);
plot(t1, x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Demodulated Signal');
axis([0 0.009 -0.2 1.2]);
% Add a legend
%legend('Message Signal', 'PPM Signal',
'Demodulated Signal');
% Calculate maximum deviation in pulse position
[max_diff, idx] =
max(abs(diff(find(diff(y_ppm)))));
disp(['Maximum deviation in pulse position: ',
num2str(max_diff), ' sample points']);
Sampling and PAM
Question Sampling and reconstruction
f1 = 250;
Ts = 1/2000;
t = 0:Ts/200:0.01;
n = 0:40;
x_t = cos(4*pi*500*t);
x_nTs = cos(0.5*pi*n);
x_nTss = cos(0.25*pi*n);
reconstructed_t = linspace(0, 0.01, 200);
xr_t = zeros(1, length(reconstructed_t));
for i = 1:length(n)
xr_t = xr_t + x_nTs(i)*sinc((reconstructed_t -
n(i)*Ts)/Ts);
end
figure;
subplot(3,1,1);
plot(reconstructed_t, xr_t, 'LineWidth', 2);
xlabel('Time (s)');
ylabel('Amplitude');
title('Original Signal x(t)');
grid on;
subplot(3,1,2);
stem(n, x_nTss, 'LineWidth', 2);
xlabel('Time (s)');
ylabel('Amplitude');
title('Sampled Signal x[nTs]');
grid on;
subplot(3,1,3);
plot(reconstructed_t, xr_t, 'LineWidth', 2);
xlabel('Time (s)');
ylabel('Amplitude');
title('Reconstructed Signal xr(t)');
grid on;