0% found this document useful (0 votes)
17 views22 pages

Lab SH 4

Uploaded by

saisiva3002
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)
17 views22 pages

Lab SH 4

Uploaded by

saisiva3002
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/ 22

COMMUNICATION SYSTEM LAB SHEET 4

S Bhargava Siva Sai


SC21B121
Labsheet_4.pdf

Question 1

Generation of bits: The source is assumed to produce a stream of bits. The bits are usually
modelled as an independent and identically distributed random process with the probability of a
bit being 1 being 0.5. Generate a random sequence of bits of length N (input) satisfying this
property.

rng(0)
random_sequence = generate_random_bits(10);
disp(random_sequence);

Ques on 2

The above bit sequence is then converted to a baseband signal. The baseband signal is obtained by
conver ng 0 and 1 into appropriate pulses of dura on T = 1s. For a bit sequence with N = 10, obtain a
baseband signal with 0 represented using a level of −1 and 1 using a level of 1. Note that all
con nuous me signals have to be represented by their sampled counterparts. Clearly state the
sampling rate that you have used. Plot the sampled version of the con nuous me signal that you
have obtained.

N = 10;
random_sequence = generate_random_bits(N);

% Parameters
T = 1; % Duration of each pulse (s)
fs = 100; % Sampling rate (samples per second)

% Generate baseband signal


baseband_signal = generate_baseband_signal(random_sequence, T, fs);
% Plot sampled version of the continuous time signal
time_axis = (0:length(baseband_signal)-1) / fs;
stem(time_axis, baseband_signal, 'b', 'Marker', 'none');
xlabel('Time (s)');
ylabel('Amplitude');
title('Sampled Baseband Signal');
grid on;

Ques on 3

Plot the power spectrum of the baseband signal for N = 1000. Note that the power spectrum should
be plo ed with respect to the con nuous me frequency (Hint: carefully think about energy vs.
power)

% Generate random bit sequence


N = 1000;
random_sequence = generate_random_bits(N);

% Parameters
T = 1; % Duration of each pulse (s)
fs = 100; % Sampling rate (samples per second)

% Generate baseband signal


baseband_signal = generate_baseband_signal(random_sequence, T, fs);

% Calculate power spectrum


N_fft = length(baseband_signal);
frequencies = (-N_fft/2:N_fft/2-1) * fs / N_fft; % Frequency axis for DFT
baseband_fft = fftshift(fft(baseband_signal, N_fft)); % Shifted DFT
power_spectrum = abs(baseband_fft).^2 / N_fft; % Power spectrum
% Plot power spectrum
figure(2)
plot(frequencies, power_spectrum);
xlabel('Frequency (Hz)');
ylabel('Power');
title('Power Spectrum of Baseband Signal');
grid on;

Ques on 4

For modelling a passband channel use any FIR filter design technique to obtain a filter response that
corresponds to a channel with passband centered at 25Hz, one-sided bandwidth of 10 Hz, and a gain
of g in the passband. Plot the magnitude spectrum of the channel that you are using - clearly
labelling the axes for g = 1.

% Parameters
center_freq = 25; % Center frequency of passband (Hz)
bandwidth = 10; % One-sided bandwidth of passband (Hz)
gain = 1; % Gain in the passband

% Calculate normalized frequencies for filter design


nyquist_freq = 0.5; % Nyquist frequency (normalized)
passband_edges = [center_freq - bandwidth/2, center_freq + bandwidth/2] /
(fs/2);

% Design FIR filter using Parks-McClellan algorithm


filter_order = 128; % Filter order
fir_coeff = fir1(filter_order, passband_edges, 'BANDPASS');

% Plot magnitude spectrum of the filter


freq_response = freqz(fir_coeff,1024, fs);
frequencies = linspace(0, fs/2, numel(freq_response)); % Frequency axis

plot(frequencies, abs(freq_response));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Magnitude Spectrum of Passband Channel');
grid on;

Ques on 5

Note that the baseband signal that you have generated cannot be sent through the passband
channel directly. So modulate the baseband signal using a carrier of frequency 25 Hz.

% Generate time vector for baseband signal


t_baseband = (0:length(baseband_signal)-1)/ fs;

% Generate carrier signal at 25 Hz


carrier_frequency = 25; % Hz
carrier_signal = cos(2*pi*carrier_frequency*t_baseband);

% Modulate baseband signal using carrier


modulated_signal = baseband_signal .* carrier_signal;

% Plot modulated signal


figure;
plot(t_baseband(1:1000), modulated_signal(1:1000));
xlabel('Time (s)');
ylabel('Amplitude');
title('Modulated Signal');
grid on;

Ques on 6

Plot the modulated signal in me domain - clearly labelling the axis.

% Plot modulated signal


figure;
plot(t_baseband, modulated_signal);
xlabel('Time (s)');
ylabel('Amplitude');
title('Modulated Signal in Time Domain');
grid on;

Ques on 7

Plot the power spectral density of the modulated signal. Compare the PSD of the modulated signal
with that of the baseband signal. State your observa ons - what are the similar es and differences
between the two PSDs?

[baseband_Pxx, baseband_freq] = periodogram(baseband_signal, [], [], fs,


'power');

% Calculate power spectral density (PSD) of modulated signal


[modulated_Pxx, modulated_freq] = periodogram(modulated_signal, [], [], fs,
'power');

% Plot PSDs
figure;
subplot(2,1,1)
plot(baseband_freq, baseband_Pxx, 'b', 'LineWidth', 1.5);
xlabel('Frequency (Hz)');
ylabel('Power Spectral Density (dB/Hz)');
legend('power spectral density of base band signal')
subplot(2,1,2)
plot(modulated_freq, modulated_Pxx, 'g', 'LineWidth', 1.5);
hold off;
xlabel('Frequency (Hz)');
ylabel('Power Spectral Density (dB/Hz)');
title('Power Spectral Density');
legend(' power spectral density Modulated Signal');
grid on;

Ques on 8

Simulate sending the passband signal through the above channel. Plot the output signal from the
channel as well as its PSD.
% Apply the FIR filter to the modulated signal
output_signal = conv(modulated_signal, fir_coeff, 'same');

% Calculate the power spectral density (PSD) of the output signal


[output_Pxx, output_freq] = periodogram(output_signal, [], [], fs, 'power');

% Plot the output signal


figure;
subplot(2,1,1)
plot(t_baseband, output_signal);
xlabel('Time (s)');
ylabel('Amplitude');
title('Output Signal from Channel (Time Domain)');
grid on;

% Plot the power spectral density (PSD) of the output signal

subplot(2,1,2)
plot(output_freq, output_Pxx);
xlabel('Frequency (Hz)');
ylabel('Power Spectral Density (dB/Hz)');
title('Power Spectral Density of Output Signal from Channel');
grid on;

Ques on 9

Now implement the addi ve noise model for the channel so that the channel introduces addi ve
white Gaussian noise with variance σ 2 . For this set of tasks use σ^2 = 0.1.

% Parameters

% Parameters
sigma_sq = 0.1; % Variance of additive white Gaussian noise

% Generate Gaussian noise with zero mean and variance sigma_sq


noise = sqrt(sigma_sq) * randn(size(output_signal));
% Add noise to the output signal
output_with_noise = output_signal + noise;

% Calculate the power spectral density (PSD) of the output signal with noise
[output_with_noise_Pxx, output_with_noise_freq] =
periodogram(output_with_noise, [], [], fs, 'power');

% Plot the output signal with noise


figure;
subplot(2,1,1)
plot(t_baseband(1:10), output_with_noise(1:10));
xlabel('Time (s)');
ylabel('Amplitude');
title('Output Signal from Channel with Noise (Time Domain)');
grid on;

% Plot the power spectral density (PSD) of the output signal with noise
subplot(2,1,2)
plot(output_with_noise_freq, output_with_noise_Pxx);
xlabel('Frequency (Hz)');
ylabel('Power Spectral Density (dB/Hz)');
title('Power Spectral Density of Output Signal from Channel with Noise');
grid on;

Ques on 10

Plot the magnitude response of the filter that you have implemented. What are the specifications
of the passband and stopband for this filter? Why did you choose those specifications?
% Specifications
passband_center_freq = 25; % Passband center frequency (Hz)
passband_bandwidth = 5; % Passband bandwidth (Hz)
stopband_margin = 10; % Stopband margin (Hz)

% Calculate passband and stopband edges


passband_edges = [passband_center_freq - passband_bandwidth/2,
passband_center_freq + passband_bandwidth/2];
stopband_edges = [0, passband_center_freq - passband_bandwidth/2 -
stopband_margin, ...
passband_center_freq + passband_bandwidth/2 +
stopband_margin, fs/2];

% Design bandpass filter


filter_order = 256; % Filter order
bandpass_filter = fir1(filter_order, passband_edges / (fs/2), 'bandpass');
% Plot magnitude response of the filter
freqz(bandpass_filter, 1, 1024, fs);
title('Magnitude Response of Bandpass Filter');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

Ques on 11

Implement a coherent demodulator for the above system. Please show to the TAs how you have
made sure that the demodulator’s (more precisely the local oscillator signal’s) phase matches with
that of the received carrier.

% Phase estimation
received_carrier_phase = atan2(imag(output_signal), real(output_signal)); %
Estimate phase of received carrier

% Generate local oscillator signal with same phase as received carrier


lo_signal = cos(2*pi*carrier_frequency*t_baseband + received_carrier_phase);

% Demodulate the received signal using the local oscillator signal


demodulated_signal = output_signal .* lo_signal;

% Plot the demodulated signal


figure;
stem(t_baseband(1:1000), demodulated_signal(1:1000));
xlabel('Time (s)');
ylabel('Amplitude');
title('Demodulated Signal');
grid on;

Ques on 12

Implement a sampler for sampling the output of the demodulator. Then implement a decision
making device - a thresholder that will convert the samples to 0 or 1, and obtain the bit estimates
at the output of the decision device.
% Sampling rate and threshold level
sampling_rate = fs; % Same as the sampling rate used for modulation
threshold_level = 0; % Threshold level for decision making

% Sample the demodulated signal


samples = demodulated_signal(1:sampling_rate:end); % Take samples at the
specified rate

% Thresholding: Convert samples to 0 or 1 based on threshold level


bit_estimates = samples > threshold_level;

% Plot the bit estimates


figure;
stem(bit_estimates, 'Marker', 'none');
xlabel('Sample Index');
ylabel('Bit Estimate');
title('Bit Estimates at the Output of the Decision Device');
ylim([-0.1, 1.1]); % Set y-axis limits to clearly show binary values
grid on;
Ques on 13

Exercise 4.2

For a sequence of 200 bits obtain the waveform corresponding to QPSK. The symbol time can
be chosen to be 1 second. Record the bit rate. Plot the PSD of the QPSK waveform and record
the null to null bandwidth. Plot the signal constellation for this transmission scheme.
% Parameters
num_bits = 200;
symbol_time = 1; % seconds
carrier_frequency = 25; % Hz

% Generate random bit sequence


bit_sequence = randi([0, 1], 1, num_bits);

% Map bits to QPSK symbols


symbols = zeros(1, num_bits/2);
for i = 1:2:num_bits
symbol_idx = bi2de(bit_sequence(i:i+1), 'left-msb') + 1;
symbols((i+1)/2) = qpsk_symbol_map(symbol_idx);
end

% Modulation
time = 0:1/carrier_frequency:num_bits*symbol_time-1/carrier_frequency;
carrier_wave = exp(1j * 2 * pi * carrier_frequency * time);
modulated_signal = zeros(1, length(time));
for i = 1:length(symbols)
modulated_signal((i-1)*carrier_frequency+1:i*carrier_frequency) =
symbols(i) * carrier_wave((i-1)*carrier_frequency+1:i*carrier_frequency);
end

% Power Spectral Density (PSD)


[psd, freqs] = pwelch(modulated_signal, [], [], [], carrier_frequency);
figure;
plot(freqs, 10*log10(psd));
title('PSD of QPSK Modulated Signal');
xlabel('Frequency (Hz)');
ylabel('Power/Frequency (dB/Hz)');
grid on;

% Null-to-Null Bandwidth Calculation


main_lobe_index = find(psd == max(psd), 1);
half_power = psd(main_lobe_index) / 2;
left_null_index = find(psd(1:main_lobe_index) < half_power, 1, 'last');
right_null_index = main_lobe_index + find(psd(main_lobe_index:end) <
half_power, 1) - 1;
null_to_null_bandwidth = freqs(right_null_index) - freqs(left_null_index);
disp(['Null-to-Null Bandwidth: ', num2str(null_to_null_bandwidth), ' Hz']);

% Signal Constellation
figure;
scatter(real(symbols), imag(symbols), 'r');
title('QPSK Signal Constellation');
xlabel('I (In-phase)');
ylabel('Q (Quadrature)');
grid on;

% Function to map bits to QPSK symbols


Ques on 2

For a sequence of 400 bits obtain the waveform corresponding to 16-QAM. Again record the bit
rate and the null to null bandwidth. Plot the signal constellation for this transmission scheme.

% Parameters
num_bits = 400;
symbol_time = 1; % seconds
carrier_frequency = 25; % Hz

% Generate random bit sequence


bit_sequence = randi([0, 1], 1, num_bits);

% Map bits to 16-QAM symbols


symbols = zeros(1, num_bits/4);
for i = 1:4:num_bits
symbol_idx = bi2de(bit_sequence(i:i+3), 'left-msb') + 1;
symbols((i+3)/4) = qam16_symbol_map(symbol_idx);
end

% Modulation
time = 0:1/carrier_frequency:num_bits*symbol_time-1/carrier_frequency;
carrier_wave = exp(1j * 2 * pi * carrier_frequency * time);
modulated_signal = zeros(1, length(time));
for i = 1:length(symbols)
modulated_signal((i-1)*carrier_frequency+1:i*carrier_frequency) =
symbols(i) * carrier_wave((i-1)*carrier_frequency+1:i*carrier_frequency);
end

% Power Spectral Density (PSD)


[psd, freqs] = pwelch(modulated_signal, [], [], [], carrier_frequency);
figure;
plot(freqs, 10*log10(psd));
title('PSD of 16-QAM Modulated Signal');
xlabel('Frequency (Hz)');
ylabel('Power/Frequency (dB/Hz)');
grid on;

% Null-to-Null Bandwidth Calculation


main_lobe_index = find(psd == max(psd), 1);
half_power = psd(main_lobe_index) / 2;
left_null_index = find(psd(1:main_lobe_index) < half_power, 1, 'last');
right_null_index = main_lobe_index + find(psd(main_lobe_index:end) <
half_power, 1) - 1;
null_to_null_bandwidth = freqs(right_null_index) - freqs(left_null_index);
disp(['Null-to-Null Bandwidth: ', num2str(null_to_null_bandwidth), ' Hz']);

% Signal Constellation
figure;
scatter(real(symbols), imag(symbols), 'r');
title('16-QAM Signal Constellation');
xlabel('I (In-phase)');
ylabel('Q (Quadrature)');
grid on;

% Function to map bits to 16-QAM symbols


Question 3
For a sequence of 300 bits obtain the waveform corresponding to 8-PSK. Again record the bit
rate and the null to null bandwidth. Plot the signal constellation for this transmission scheme

% Parameters
num_bits = 300;
symbol_time = 1; % seconds
carrier_frequency = 25; % Hz

% Generate random bit sequence


bit_sequence = randi([0, 1], 1, num_bits);

% Map bits to 8-PSK phases


phases = zeros(1, num_bits/3);
for i = 1:3:num_bits
phase_idx = bi2de(bit_sequence(i:i+2), 'left-msb');
phases((i+2)/3) = psk8_phase_map(phase_idx);
end

% Modulation
time = 0:1/carrier_frequency:num_bits*symbol_time-1/carrier_frequency;
carrier_wave = exp(1j * 2 * pi * carrier_frequency * time);
modulated_signal = zeros(1, length(time));
for i = 1:length(phases)
modulated_signal((i-1)*carrier_frequency+1:i*carrier_frequency) = exp(1j *
phases(i)) * carrier_wave((i-1)*carrier_frequency+1:i*carrier_frequency);
end

% Power Spectral Density (PSD)


[psd, freqs] = pwelch(modulated_signal, [], [], [], carrier_frequency);
figure;
plot(freqs, 10*log10(psd));
title('PSD of 8-PSK Modulated Signal');
xlabel('Frequency (Hz)');
ylabel('Power/Frequency (dB/Hz)');
grid on;

% Null-to-Null Bandwidth Calculation


main_lobe_index = find(psd == max(psd), 1);
half_power = psd(main_lobe_index) / 2;
left_null_index = find(psd(1:main_lobe_index) < half_power, 1, 'last');
right_null_index = main_lobe_index + find(psd(main_lobe_index:end) <
half_power, 1) - 1;
null_to_null_bandwidth = freqs(right_null_index) - freqs(left_null_index);
disp(['Null-to-Null Bandwidth: ', num2str(null_to_null_bandwidth), ' Hz']);

% Signal Constellation
figure;
polarplot(phases, ones(size(phases)), 'ro');
title('8-PSK Signal Constellation');
grid on;
% Function to map bits to 8-PSK phases
Question 4
For a sequence of 200 bits obtain the waveform corresponding to 4-FSK. Again record the bit
rate and the null to null bandwidth.
% Parameters
num_bits = 200;
symbol_time = 1; % seconds
carrier_frequency = 25; % Hz
frequency_spacing = 5; % Hz, spacing between frequencies

% Generate random bit sequence


bit_sequence = randi([0, 1], 1, num_bits);

% Map bits to 4-FSK frequencies


frequencies = zeros(1, num_bits/2);
for i = 1:2:num_bits
freq_idx = bi2de(bit_sequence(i:i+1), 'left-msb');
frequencies((i+1)/2) = carrier_frequency + freq_idx * frequency_spacing;
end

% Modulation
time = 0:1/carrier_frequency:num_bits*symbol_time-1/carrier_frequency;
modulated_signal = zeros(1, length(time));
for i = 1:length(frequencies)
modulated_signal((i-1)*carrier_frequency+1:i*carrier_frequency) = cos(2 *
pi * frequencies(i) * time((i-1)*carrier_frequency+1:i*carrier_frequency));
end

% Power Spectral Density (PSD)


[psd, freqs] = pwelch(modulated_signal, [], [], [], carrier_frequency);
figure;
plot(freqs, 10*log10(psd));
title('PSD of 4-FSK Modulated Signal');
xlabel('Frequency (Hz)');
ylabel('Power/Frequency (dB/Hz)');
grid on;

% Null-to-Null Bandwidth Calculation


main_lobe_index = find(psd == max(psd), 1);
half_power = psd(main_lobe_index) / 2;
left_null_index = find(psd(1:main_lobe_index) < half_power, 1, 'last');
right_null_index = main_lobe_index + find(psd(main_lobe_index:end) <
half_power, 1) - 1;
null_to_null_bandwidth = freqs(right_null_index) - freqs(left_null_index);
disp(['Null-to-Null Bandwidth: ', num2str(null_to_null_bandwidth), ' Hz']);
% Bit Rate Calculation
bit_rate = num_bits / symbol_time;
disp(['Bit Rate: ', num2str(bit_rate), ' bps']);

Null-to-Null Bandwidth: Hz
Bit Rate: 200 bps

Correlation Receiver
Under the assumption of perfect frame and timing synchronization, implement correlation
receivers for each of the four schemes discussed above. Please use the starter code to get an
idea about how to do this. 1.

Fix a sequence of 40 bits


fixed_bit_sequence = [1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 0
0 1 1 0 1 1 0 0 1 0];

qpsk_reference_symbols = [-1-1i, -1+1i, 1-1i, 1+1i];


received_signal = zeros(size(qpsk_reference_symbols));
for i = 1:length(qpsk_reference_symbols)
received_signal(i) = correlate_with_reference(fixed_bit_sequence,
qpsk_reference_symbols(i));
end
[max_corr, qpsk_symbol_idx] = max(received_signal);
disp(['QPSK: Detected Symbol Index - ', num2str(qpsk_symbol_idx), ', Max
Correlation - ', num2str(max_corr)]);

QPSK: Detected Symbol Index - 2, Max Correlation - -19+19i


Ques on 3

For this fixed sequence of bits generate QPSK, 16-QAM, 8-PSK, and 4-FSK passband
waveforms
fixed_bit_sequence = [1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 0 0 1 1 0 1 1 0 0 1 0 1 0
0 1 1 0 1 1 0 0 1 0];

% QPSK modulation
qpsk_reference_symbols = [-1-1i, -1+1i, 1-1i, 1+1i];
qpsk_symbols = zeros(1, length(fixed_bit_sequence)/2);
for i = 1:2:length(fixed_bit_sequence)
bit_pair = fixed_bit_sequence(i:i+1);
symbol_idx = bi2de(bit_pair, 'left-msb');
qpsk_symbols((i+1)/2) = qpsk_reference_symbols(symbol_idx + 1);
end
[qpsk_time, qpsk_signal] = modulate_passband_waveform(qpsk_symbols,
carrier_frequency, symbol_time);

% 16-QAM modulation
% Define reference symbols for 16-QAM
% qam16_symbols = ...; % To be filled
% [qam16_time, qam16_signal] = modulate_passband_waveform(qam16_symbols,
carrier_frequency, symbol_time);

% 8-PSK modulation
% Define reference symbols for 8-PSK
% psk8_symbols = ...; % To be filled
% [psk8_time, psk8_signal] = modulate_passband_waveform(psk8_symbols,
carrier_frequency, symbol_time);

% 4-FSK modulation
fsk4_frequencies = zeros(1, length(fixed_bit_sequence)/2);
for i = 1:2:length(fixed_bit_sequence)
bit_pair = fixed_bit_sequence(i:i+1);
freq_idx = bi2de(bit_pair, 'left-msb');
fsk4_frequencies((i+1)/2) = carrier_frequency + freq_idx *
frequency_spacing;
end
[fsk4_time, fsk4_signal] = modulate_passband_waveform_fsk(fsk4_frequencies,
carrier_frequency, symbol_time);

% Plot passband waveforms


figure;
subplot(2, 2, 1);
plot(qpsk_time, real(qpsk_signal), 'b');
title('QPSK Passband Waveform');
xlabel('Time (s)');
ylabel('Amplitude');
% Plot other passband waveforms similarly

% Function to modulate passband waveform

function [time, signal] = modulate_passband_waveform_fsk(frequencies,


carrier_frequency, symbol_time)
time = 0:1/carrier_frequency:(length(frequencies)*symbol_time-
1/carrier_frequency);
signal = zeros(size(time));
for i = 1:length(frequencies)
signal((i-1)*carrier_frequency+1:i*carrier_frequency) = cos(2 * pi *
frequencies(i) * time((i-1)*carrier_frequency+1:i*carrier_frequency));
end
end
function [time, signal] = modulate_passband_waveform(symbols,
carrier_frequency, symbol_time)
time = 0:1/carrier_frequency:(length(symbols)*symbol_time-
1/carrier_frequency);
carrier_wave = exp(1j * 2 * pi * carrier_frequency * time);
signal = zeros(size(time));
for i = 1:length(symbols)
signal((i-1)*carrier_frequency+1:i*carrier_frequency) = symbols(i) *
carrier_wave((i-1)*carrier_frequency+1:i*carrier_frequency);
end
end

You might also like