Skip to main content

Robust Signal Detection with Prefix and Postfix in MATLAB

 

MATLAB Code

clc;
clear;
close all;

% Parameters
fs = 1000; % Sampling frequency
msgLength = 100; % Length of the message
pnLength = 50; % Length of PN sequence
silenceLength = 20; % Length of silence before and after
lagAmount = 50; % Amount of lag (can be negative for lead)
threshold = 0.5; % Threshold for correlation peak detection

% Generate Unique PN Sequences
pnPrefix = 2 * (randi([0, 1], 1, pnLength) - 0.5);
pnPostfix = 2 * (randi([0, 1], 1, pnLength) - 0.5);

% Generate Message
originalMessage = (randi([0, 1], 1, msgLength));
message = 2*originalMessage - 1;

% Construct Dataframe
dataframe = [pnPrefix, message, pnPostfix];

% Introduce Lag or Lead
if lagAmount > 0
%laggedFrame = [zeros(1, lagAmount), dataframe(1:end - lagAmount)];
laggedFrame = [zeros(1, lagAmount), dataframe];
else
laggedFrame = [dataframe(-lagAmount + 1:end), zeros(1, -lagAmount)];
end

% Correlation with PN Sequences
corrPrefix = xcorr(laggedFrame, pnPrefix);
corrPostfix = xcorr(laggedFrame, pnPostfix);

% Normalize Correlation
corrPrefix = corrPrefix / max(corrPrefix);
corrPostfix = corrPostfix / max(corrPostfix);

% Adjust indices to align with laggedFrame
corrLen = length(laggedFrame);
prefixIndices = (1:length(corrPrefix)) - corrLen;
postfixIndices = (1:length(corrPostfix)) - corrLen;

% Detect Peaks in Correlation (Apply Threshold)
prefixPeaks = prefixIndices(corrPrefix > threshold);
postfixPeaks = postfixIndices(corrPostfix > threshold);

% Estimate Start and End of Message
if ~isempty(prefixPeaks)
startMessage = max(1, prefixPeaks(1) + length(pnPrefix));
else
startMessage = -1; % Detection failed
end

if ~isempty(postfixPeaks)
endMessage = min(length(laggedFrame), postfixPeaks(end) - length(pnPostfix) - 1);
else
endMessage = -1; % Detection failed
end

% Retrieve Original Message
if startMessage > 0 && endMessage > 0 && endMessage > startMessage
retrievedMessage = laggedFrame(startMessage+1:endMessage+pnLength+1)>0;
else
retrievedMessage = [];
end

% Display Original Message
disp('Original Message:'), disp(originalMessage);

% Display Results
disp('Retrieved Message:'), disp(retrievedMessage);

% Plot Results
figure;
subplot(3, 1, 1); plot(dataframe); title('Original Dataframe');
subplot(3, 1, 2); plot(laggedFrame); title('Lagged/Lead Dataframe');
subplot(3, 1, 3); plot(corrPrefix, 'b'); hold on; plot(corrPostfix, 'r');
title('Correlation with PN Sequences');
legend('Prefix Correlation', 'Postfix Correlation');

 

Output 

 Original Message:

     1     0     0     0     1     1     0     1     1     0     0     0     0     1     0     0     1     1     0     1     1  ......
 
Retrieved Message:

    1     0     0     0     1     1     0     1     1     0     0     0     0     1     0     0     1     1     0     1     1  ......
 
 










Copy the aforementioned MATLAB Code from here

 

 

Further Reading

 



Contact Us

Name

Email *

Message *

Popular Posts

Q-function in BER vs SNR Calculation (with Simulation)

Q-function in BER vs. SNR Calculation In digital communications and signal processing, the Q-function plays a significant role in predicting system reliability. It allows engineers to quantify the probability that Gaussian noise will exceed a specific threshold, causing a bit error. What is the Q-function? The Q-function is a mathematical function representing the tail probability of the standard normal (Gaussian) distribution. It is the complementary cumulative distribution function (CCDF) of a standard Gaussian distribution. Q(x) = (1 / √(2Ï€)) ∫â‚“∞ e^(-t² / 2) dt Q-Function Interactive Simulator Move the slider to see how the "Tail Probability" (the area in red) changes. This area represents the Probability of Error (BER) . Threshold Distance ( x ) — (Simulates Increasing SNR) x = 1.0 Q(x) = 0.1587 ...

BER vs SNR for M-ary QAM, M-ary PSK, QPSK, BPSK, ...(MATLAB Code + Simulator)

Bit Error Rate (BER) & SNR Guide Analyze communication system performance with our interactive simulators and MATLAB tools. 📘 Theory 🧮 Simulators 💻 MATLAB Code 📚 Resources BER Definition SNR Formula BER Calculator MATLAB Comparison 📂 Explore M-ary QAM, PSK, and QPSK Topics ▼ 🧮 Constellation Simulator: M-ary QAM 🧮 Constellation Simulator: M-ary PSK 🧮 BER calculation for ASK, FSK, and PSK 🧮 Approaches to BER vs SNR What is Bit Error Rate (BER)? The BER indicates how many corrupted bits are received compared to the total number of bits sent. It is the primary figure of merit f...

Frequency Shift Keying (FSK) Modulation & Demodulation (with Simulation)

Frequency Shift Keying (FSK) Theoretical Foundations: Frequency Shift Keying (FSK) is a discrete frequency modulation scheme wherein the digital information is encoded via instantaneous shifts in the carrier signal's frequency. The fundamental implementation is Binary FSK (BFSK), which maps binary data onto two distinct, discrete spectral states. A binary '1' (the "mark" state) is represented by a carrier frequency \( f_1 \), while a binary '0' (the "space" state) corresponds to frequency \( f_2 \). Each symbol is sustained for a bit interval denoted by \( T_b \). FSK Transmitter Characterization: The mathematical model for the modulated BFSK output \( s(t) \) is defined as: \[ s(t) = \begin{cases} A_c \cos(2\pi f_1 t), & \text{for } m = 1 \\ A_c \cos(2\pi f_2 t), & \text{for } m = 0 \end{cases} \] ...

RMS Delay Spread, Excess Delay Spread and Multi-path ...(with MATLAB + Simulator)

📘 Overview of Delay Spread and Multi-path 🧮 Excess Delay spread 🧮 Power delay Profile 🧮 RMS Delay Spread 📚 Further Reading 📂 Other Topics on RMS Delay Spread, Excess Delay ... 🧮 Multipath Components or MPCs 🧮 Online Simulator for Calculating RMS Delay Spread 🧮 Why is there significant multipath in the case of very high frequencies? 🧮 Why RMS Delay Spread is essential for wireless communication? 🧮 Why the Power Delay Profile is essential? 🧮 MATLAB Codes for Calculating Different Types of delay Spreads Delay Spread, Excess Delay Spread, and Multipath (MPCs) The fundamental distinction between wireless and wired connections is that in wireless connections signal reaches at receiver thru multipath signal propagation rather than directed transmission like co-axial cable. Wireless Communication has no set communication path between the transmitter and the receiver. The line...

FFT Butterfly Method Explained (with Example of 4-point DFT)

  FFT Using Butterfly Method Given: x[n] = {0, 1, 2, 3} Step 1: Split into Even & Odd Even indices: x e = {0, 2} Odd indices: x o = {1, 3} Step 2: 2-point DFT For any {a, b}: DFT = {a + b, a - b} Even Part: E = {0+2, 0-2} = {2, -2} Odd Part: O = {1+3, 1-3} = {4, -2} Step 3: Combine Using Butterfly X[k] = E[k] + W k O[k] X[k + N/2] = E[k] - W k O[k] For N = 4: W 0 = 1 W 1 = -j Final Calculations X[0] = 2 + 4 = 6 X[2] = 2 - 4 = -2 X[1] = -2 + (-j)(-2) = -2 + 2j X[3] = -2 - (-j)(-2) = -2 - 2j Final Answer: X[k] = {6, -2 + 2j, -2, -2 - 2j} Try Interactive Online Simulations Interactive FFT Online Simulator (For understanding Fundamentals)  Interactive FFT Online Simulator (Analyze .CSV, .MP3, .MP4, etc. Further Reading Fourier Transform OFDM Return to Fourier Transform Main Page →

AM Modulation Online Simulator

Amplitude Modulation Simulator s AM (t) = A c [1 + k a m(t)] cos(ω c t) where, ω = 2πf & k a = Amplitude Sensitivity Modulation index, μ = k a A m Message Frequency (fm): Carrier Frequency (fc): Carrier Amplitude (Ac): Modulation Index (m = Am / Ac):

Online Simulator for ASK, FSK, and PSK

Interactive Digital Signal Processing (DSP) Tutorial and Simulator for ASK, FSK, and BPSK modulation techniques. Try our new Digital Signal Processing Simulator!   •   Interactive ASK, FSK, and BPSK tools updated for 2025. Start Now Digital Modulation Visualizer: ASK, FSK, & BPSK Simulator Learn and visualize binary modulation techniques (ASK, FSK, BPSK) in real-time with adjustable carrier and sampling parameters. Perfect for DSP students and engineers. 📡 ASK Simulator 📶 FSK Simulator 🎚️ BPSK Simulator 📚 More Topics ASK Modulator FSK Modulator BPSK Modulator More Topics 1. ASK (Amplitude Shift Keying) Simulat...