MATLAB Code
clc;clear;
close all;
% Parameters
fs = 10000; % Sampling frequency (Hz)
symbolRate = 100; % Symbol rate (baud)
fc = 1000; % Carrier frequency (Hz)
message = [0 0 1 1 1 0 0 0]; % Original binary message
% Upsample the data by samples per symbol
samplesPerSymbol = fs / symbolRate;
upsampledData = upsample(2 * message - 1, samplesPerSymbol); % BPSK modulation (-1,1)
% Generate carrier signal
t = (0:length(upsampledData)-1) / fs; % Time vector
carrier = cos(2 * pi * fc * t); % Carrier signal
% Transmit signal (modulate the data with carrier)
txSignal = upsampledData .* carrier; % Modulated signal
txSignal = awgn(txSignal, 15); % Add noise to the signal
% Simulate lag (delay in transmission)
lagAmount = 200; % Introduce lag (can be positive or negative)
laggedSignal = [zeros(1, lagAmount), txSignal(1:end - lagAmount)];
% Receiver Side
% Demodulation: multiply received signal by the same carrier
rxSignal = laggedSignal .* carrier; % Demodulate by multiplying with carrier
% Low-pass filter to remove high frequencies (passband around symbolRate)
lowPassFilter = fir1(50, 2 * symbolRate / fs); % Low-pass filter
rxSignalFiltered = filter(lowPassFilter, 1, rxSignal); % Apply filter
% Cross-correlation to estimate the lag
[corr, lags] = xcorr(rxSignalFiltered, upsampledData);
[~, peakIdx] = max(abs(corr)); % Find the index of maximum correlation
% Estimate the lag (delay)
estimatedLag = lags(peakIdx);
% Shift the received signal to align it with the transmitted signal
rxSignalAligned = circshift(rxSignalFiltered, -estimatedLag);
% Downsample to recover original symbol rate
rxSignalDownsampled = rxSignalAligned(1:samplesPerSymbol:end);
% BPSK Demodulation (decision device)
recoveredBits = rxSignalDownsampled > 0; % Decision rule: > 0 -> 1, else 0
% Display Results
disp('Original Message:');
disp(message);
disp('Recovered Message:');
disp(recoveredBits);
% Plot Results
figure;
subplot(3, 1, 1); plot(txSignal); title('Transmitted Signal');
subplot(3, 1, 2); plot(laggedSignal); title('Lagged Signal');
subplot(3, 1, 3); plot(rxSignalFiltered); title('Filtered Received Signal');
web('https://www.salimwireless.com/search?q=correlation', '-browser');
Output
Original Message:
0 0 1 1 1 0 0 0
Recovered Message:
0 0 1 1 1 0 0 0