Department of Electronics and Electrical Communications Engineering
Faculty of Engineering - Cairo University
ELC 3070 – Spring 2025
Communications 2
Project #2: Matched Filter
Submitted to
Dr. Mohamed Nafea
Dr. Mohamed Khairy
Eng. Mohamed Khaled
Submitted by
Team 7 :
Name Code (Sec + BN)
احمد ابراهيم حسن ابراهيم 103
محمد احمد محمد السيد 319
محمد جمال منصور فضل 321
محمد حسن سالمه عبدالحميد 323
محمد خالد فتحى محمد ربيع 324
Table of Contents
1 Role of each member ..................................................................................................................... 3
2 Preparing Data ............................................................................................................................... 3
3 Requirement 1 ................................................................................................................................ 4
3.1 Part-A: ................................................................................................................................................. 4
3.2 Part-B: ................................................................................................................................................. 4
4 Requirement 2 ................................................................................................................................ 5
5 Requirement 3: ............................................................................................................................... 6
5.1 At Point A: .......................................................................................................................................... 6
5.2 At Point B:........................................................................................................................................... 6
6 Full MATLAB code ....................................................................................................................... 7
6.1 Requirement 1 Code: ........................................................................................................................... 7
6.2 Requirement 2 Code: ......................................................................................................................... 10
6.3 Requirement 3 Code: ......................................................................................................................... 12
P a g e 2 | 12
1 Role of each member
Name Role
احمد ابراهيم حسن ابراهيم Requirement 3 + Report preparation
محمد احمد محمد السيد Requirement 1 + Report preparation
محمد جمال منصور فضل Requirement 2 + Report preparation
محمد حسن سالمه عبدالحميد Requirement 2 + Report preparation
محمد خالد فتحى محمد ربيع Requirement 3 + Report preparation
2 Preparing Data
We first start by making the Up-sampled signal and then we convolved symbol with 10-pulses Up-sampled signal and plotting
them on each other to show where is the best place to sample.
Applying the matched filter and plotting the output with the sampling signal also to show what is the output at the sampled time
(The decision is to be made here).
Same for the Square filter and Correlator.
P a g e 3 | 12
3 Requirement 1
3.1 Part-A:
Draw the output of both filters (in (e) above) on two subplots in the same figure using two different colours, assuming a noise
free system. Compare between the outputs of the filters at the sampling instants.
Comment:
From the graph we can see that the matched filter has a peak of 1 at the sampling time (takes all signal energy and maximizes
the SNR), while the square filter has a peak of approximately 0.9 (lowed SNR, and some loss of energy of the signal).
However both are correct to detect the signal, but we will see if the noise will affect one more than the other in the next
requirement.
3.2 Part-B:
Draw the output of the matched filter and the output of a correlator to p[n] on the same plot with two different colours.
Comment:
With the correlator we find it has the same amplitude at the sample time of 1 (saving the signal energy and maximized the SNR
like matched filter), but it has more curves unlike the sharp matched filter output.
But both detected the transmitted signal correctly, with the correlator being easier for implementation (sample multiplication
and integration) unlike the matched filter convolution.
P a g e 4 | 12
4 Requirement 2
Plot the BER vs Eb/N0 in both cases of using a matched filter at the receiver and using the filter with the following response
(after sampling).
Plot both BER curves on the same graph along with the theoretical BER=0.5 *erfc(sqrt(Eb/No)).
Comment:
The blue curve that represents the BER of the matched filter is very close to the theoretical BER because the matched filter is
designed to maximize the signal to noise ratio (SNR).
The red curve that represents the BER of the rectangular filter is worse than the matched filter and that is expected because it
doesn’t match the transmitted signal, and that lead to not maximizing the signal to noise ratio (SNR) such as the matched filter.
P a g e 5 | 12
5 Requirement 3:
For the 4 cases mentioned above, plot the eye pattern for the data length of 100 bits at points A and B. In MATLAB, eye
pattern can be drawn using the command eye diagram. Comment on the relation between the sampling instant and the eye
opening.
5.1 At Point A:
Point_A_R_0_delay_2 Point_A_R_0_delay_8
Point_A_R_1_delay_2 Point_A_R_1_delay_8
5.2 At Point B:
Point_B_R_0_delay_2 Point_B_R_0_delay_8
P a g e 6 | 12
Point_B_R_1_delay_2 Point_B_R_1_delay_8
Comments
• Rolloff factor effect (R)
Lower R → sharper transitions, narrower bandwidth, more intermember interference (ISI) sensitivity
Higher R → wider bandwidth, softer transitions, more tolerance to timing errors, less ISI
As the R is higher, the eye is more open.
• Length of the Filter (Delay)
Higher delay → longer filter, more accurate pulse shaping, better ISI performance
Lower delay → shorter filter, faster computation, more ISI
• Sampling instant and eye opening
The sampling instant is ideally chosen at the center of the eye opening, where the signal is most stable, and the probability of
error is minimized. The wider the eye opening, the more tolerant the system is to timing shifts or sampling errors. In other
words, when the eye is wide, small deviations in the sampling instant will still fall within the high-confidence region of the
signal, leading to correct detection of the transmitted bits. A narrower eye-opening means that even a small shift in the
sampling instant could cause the sample to be taken during a transition between symbols, increasing the chance of error.
• In summary
Wider eye opening → more flexibility in sampling time, better timing tolerance.
Narrow eye opening → requires very precise timing, more sensitive to small time shifting.
6 Full MATLAB code
6.1 Requirement 1 Code:
clear; clc;
%================== Control flags ==================%
Ts = 1; % Symbol duration
A = 5; % Symbol's peak
Length = 10; % Length of the signal
UpsampleFactor = 5; % Upsampling factor
%===================================================%
%================== Generating Transmitted Signal ==================%
% Generate random data
RandomBits = randi([0, 1], 1, Length);
PolarLineCode = (2*RandomBits)-1;
p = [5 4 3 2 1]/sqrt(55);
% Upsample the signal
P a g e 7 | 12
UpsampledSignal = upsample(PolarLineCode, UpsampleFactor);
UpsampledSignal_shifted = UpsampledSignal;
% Convolution symbol with upsampled signal
ConvSignal = conv(UpsampledSignal, p, 'full');
% Adjust length to match upsampled signal
ConvSignal = ConvSignal(1:length(UpsampledSignal));
%===================================================================%
%================== Applying Matched Filter ==================%
MatchedFilter = fliplr(p); % Flip the pulse for matched filter
FilteredSignal = conv(ConvSignal, MatchedFilter, 'full');
FilteredSignal = FilteredSignal(1:length(ConvSignal)); %% Adjust length to match upsampled signal
% Shift the upsampled signal to correct the delay and plot on each other
delay = 2 + floor(length(MatchedFilter)/2);
UpsampledSignal_shifted = circshift(UpsampledSignal_shifted, delay);
%======================================================%
%================== Applying Square Filter ==================%
SquareFilter = [1 1 1 1 1]/sqrt(5);
FilteredSignal2 = conv(ConvSignal, SquareFilter, 'full');
FilteredSignal2 = FilteredSignal2(1:length(ConvSignal));
%======================================================%
%================== Applying Correlator ==================%
Correlator = zeros(1, length(ConvSignal));
% Loop through the signal, stepping in multiples of 5 (UpsampleFactor)
for i = UpsampleFactor:UpsampleFactor:length(ConvSignal)
for j = 0:UpsampleFactor-1
% Define the window of ConvSignal being multiplied
current_window = ConvSignal((i-UpsampleFactor+1):(i-j));
% Define the portion of p being used
current_p = p(1:(end-j));
% Perform element-wise multiplication and summation
Correlator(i-j) = sum(current_window .* current_p);
end
end
%======================================================%
%================== Plots graphs ==================%
Ts_axis = ((0:length(ConvSignal)-1) / UpsampleFactor + 0.2);
% Plot upsampled signal on convoluted signal in the same figure
figure;
hold on;
stem(Ts_axis(1:length(UpsampledSignal)), UpsampledSignal, 'filled', 'MarkerSize', 3);
plot(Ts_axis, ConvSignal, 'b', 'LineWidth', 2);
hold off;
title('Upsampled Signal & Convolved Output');
xlabel('Time (sec)');
ylabel('Amplitude');
legend('Upsampled Signal', 'Convolved Signal');
grid on;
% Plot the upsampled signal on the output of the matched filter
figure;
hold on;
stem(Ts_axis(1:length(UpsampledSignal_shifted)), UpsampledSignal_shifted,'filled', 'MarkerSize', 3);
plot(Ts_axis, FilteredSignal, 'r', 'LineWidth', 2);
hold off;
title('Upsampled Signal & Matched Filter Output');
xlabel('Time (sec)');
ylabel('Amplitude');
P a g e 8 | 12
legend('Upsampled Signal', 'Matched Filter Output');
grid on;
% Plot the upsampled signal on the output of the square filter
figure;
hold on;
stem(Ts_axis(1:length(UpsampledSignal_shifted)), UpsampledSignal_shifted, 'filled', 'MarkerSize', 3);
plot(Ts_axis, FilteredSignal2, 'r', 'LineWidth', 2);
hold off;
title('Upsampled Signal & Square Filtered Output');
xlabel('Time (sec)');
ylabel('Amplitude');
legend('Upsampled Signal', 'Square Filter Output');
grid on;
% Plot the upsampled signal on the output of the correlator
figure;
hold on;
stem(Ts_axis(1:length(UpsampledSignal_shifted)), UpsampledSignal_shifted, 'filled', 'MarkerSize', 3);
plot(Ts_axis, Correlator, 'r', 'LineWidth', 2);
hold off;
title('Upsampled Signal & Correlator Output');
xlabel('Time (sec)');
ylabel('Amplitude');
legend('Upsampled Signal', 'Correlator Output');
grid on;
% Plot Matched filter and Square filter output and upsampled signal
figure;
hold on;
plot(Ts_axis, FilteredSignal, 'b', 'LineWidth', 3);
plot(Ts_axis, FilteredSignal2, 'r', 'LineWidth', 2);
stem(Ts_axis(1:length(UpsampledSignal_shifted)), UpsampledSignal_shifted, 'filled', 'MarkerSize', 3);
hold off;
title('Matched Filer and Square Filter Outputs');
xlabel('Time (sec)');
ylabel('Amplitude');
legend('Matched Filter', 'Square Filter', 'UpsampledSignalShifted');
grid on;
% Plot Mathced filter and Correlator output and upsampled signal
figure;
hold on;
plot(Ts_axis, FilteredSignal, 'b', 'LineWidth', 3);
plot(Ts_axis, Correlator, 'r', 'LineWidth', 2);
stem(Ts_axis(1:length(UpsampledSignal_shifted)), UpsampledSignal_shifted, 'filled', 'MarkerSize', 3);
hold off;
title('Matched Filer and Correlator Output');
xlabel('Time (sec)');
ylabel('Amplitude');
legend('Matched Filter', 'Correlator Output', 'UpsampledSignalShifted');
grid on;
%=================================================%
P a g e 9 | 12
6.2 Requirement 2 Code:
%% Part2: Noise Analysis and BER Calculation
clear; clc;
% ================== System Parameters ================== %
Ts = 1; % Symbol duration
Length = 10000; % Number of bits to transmit (changed from 10 to 10000)
UpsampleFactor = 5; % Upsampling factor (5 samples per symbol)
p = [5 4 3 2 1]/sqrt(55); % Pulse shape (normalized)
Eb = 1; % Energy per bit (since pulse energy is 1 and symbols are ±1)
% Define Eb/N0 range in dB
EbN0_dB = -2:1:5; % Define Eb/N0 from -2dB to 5dB in 1dB steps
EbN0_linear = 10.^(EbN0_dB/10); % Get the linear value of Eb/N0
% Initialize BER arrays
BER_matched = zeros(size(EbN0_dB)); % Array of BER for matched filter
BER_rect = zeros(size(EbN0_dB)); % Array of BER for rect filter
BER_theoretical = zeros(size(EbN0_dB)); % Theoretical BER
% Define filters
matched_filter = fliplr(p); % Matched filter
rect_filter = [5 5 5 5 5]/sqrt(125); % Rect filter (5 for Ts, normalized)
% ================== Simulation Loop ================== %
for i = 1:length(EbN0_dB)
% Calculate N0 from Eb/N0
N0 = Eb/EbN0_linear(i);
% a. Generate random data
bits = randi([0, 1], 1, Length);
% b. Convert to polar line coding
symbols = (2*bits)-1; % Map 0 → -1, 1 → +1
% c. Upsample and pulse shape
upsampled = upsample(symbols, UpsampleFactor); % Insert zeros between symbols
tx_signal = conv(upsampled, p, 'full');
tx_signal = tx_signal(1:length(upsampled)); % Trim to original length
% Generate AWGN noise
noise = randn(1, length(tx_signal)); % Unity variance
noise = sqrt(N0/2) * noise; % Scale to variance N0/2
% Add noise to transmitted signal
rx_signal = tx_signal + noise;
% Apply matched filter
matchedf_output = conv(rx_signal, matched_filter, 'full');
matchedf_output = matchedf_output(1:length(rx_signal)); % Trim to original length
% Apply rect filter
rectf_output = conv(rx_signal, rect_filter, 'full');
rectf_output = rectf_output(1:length(rx_signal)); % Trim to original length
% Sample at symbol rate (every 5 samples)
sampled_matchedf = matchedf_output(UpsampleFactor:UpsampleFactor:end);
sampled_rectf = rectf_output(UpsampleFactor:UpsampleFactor:end);
% Make sure we have enough samples
sampled_matchedf = sampled_matchedf(1:Length);
P a g e 10 | 12
sampled_rectf = sampled_rectf(1:Length);
% Decision (threshold at 0)
decoded_matchedf = sampled_matchedf > 0;
decoded_rectf = sampled_rectf > 0;
% Calculate BER
BER_matched(i) = sum(decoded_matchedf ~= bits)/Length; % Count mismatches and divide by total number of bits
BER_rect(i) = sum(decoded_rectf ~= bits)/Length;
% Theoretical BER
BER_theoretical(i) = 0.5*erfc(sqrt(EbN0_linear(i)));
end
% ================== Plot Results ================== %
figure;
semilogy(EbN0_dB, BER_matched, 'bo-', 'LineWidth', 2, 'MarkerFaceColor', 'b');
hold on;
semilogy(EbN0_dB, BER_rect, 'rs-', 'LineWidth', 2, 'MarkerFaceColor', 'r');
semilogy(EbN0_dB, BER_theoretical, 'k--', 'LineWidth', 2);
grid on;
xlabel('Eb/N0 (dB)');
ylabel('Bit Error Rate (BER)');
title('BER Performance Comparison');
legend('Matched Filter', 'Rect Filter', 'Theoretical BER');
axis([-2 5 1e-5 1]);
set(gca, 'YScale', 'log');
P a g e 11 | 12
6.3 Requirement 3 Code:
clear; clc; close all
%================== Control flags ==================%
R = 0; % Roll-off factors
delay = 2; % Delay
Length = 100; % Length of the signal
UpsampleFactor = 5; % Upsampling factor
group_delay = delay * UpsampleFactor; % Group delay in samples caused by the filter
%===================================================%
% Generate random data
RandomBits = randi([0, 1], 1, Length);
PolarLineCode = (2*RandomBits)-1;
% Upsample the signal
UpsampledSignal = upsample(PolarLineCode, UpsampleFactor);
% Generate the square root raised cosine
filter_coff = rcosdesign(R, delay, UpsampleFactor);
% Tx Filter
A_Tx = conv(filter_coff, UpsampledSignal); % Apply transmit filter
A_Tx = A_Tx(1:length(UpsampledSignal)); % Trim to match input length
A_Tx = A_Tx(group_delay+1:end); % Remove initial transient samples
% Rx Filter
B_Rx = conv(filter_coff, A_Tx); % Apply receive filter
B_Rx = B_Rx(1:length(A_Tx)); % Trim to match input length
B_Rx = B_Rx(group_delay+1:end); % Remove initial transient samples
figure;
hold on;
plot(filter_coff, 'b', 'LineWidth', 2);
hold off;
title('Square Root raised cosine');
xlabel('n (Samples)');
ylabel('Amplitude');
grid on;
eyediagram(A_Tx,2*UpsampleFactor)
title('Eyediagram at Point A');
eyediagram(B_Rx,2*UpsampleFactor)
title('Eyediagram at Point B');
Thanks
P a g e 12 | 12