Lab SH 4
Lab SH 4
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)
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)
% Parameters
T = 1; % Duration of each pulse (s)
fs = 100; % Sampling rate (samples per second)
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
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.
Ques on 6
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?
% 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');
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
% 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 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)
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
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
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
% 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
% Signal Constellation
figure;
scatter(real(symbols), imag(symbols), 'r');
title('QPSK Signal Constellation');
xlabel('I (In-phase)');
ylabel('Q (Quadrature)');
grid on;
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
% 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
% Signal Constellation
figure;
scatter(real(symbols), imag(symbols), 'r');
title('16-QAM Signal Constellation');
xlabel('I (In-phase)');
ylabel('Q (Quadrature)');
grid on;
% Parameters
num_bits = 300;
symbol_time = 1; % seconds
carrier_frequency = 25; % Hz
% 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
% 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
% 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
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.
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);