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

 

People are good at skipping over material they already know!

View Related Topics to







Contact Us

Name

Email *

Message *

Popular Posts

Theoretical BER vs SNR for binary ASK, FSK, and PSK with MATLAB Code + Simulator

📘 Overview & Theory 🧮 MATLAB Codes 📚 Further Reading Bit Error Rate (BER) Equations BER formulas for ASK, FSK, and PSK modulation schemes. ASK BER = 0.5 × erfc(0.5 × √SNR) FSK BER = 0.5 × erfc(√(SNR / 2)) PSK BER = 0.5 × erfc(√SNR) Theoretical BER vs SNR for Amplitude Shift Keying (ASK) The theoretical Bit Error Rate (BER) for binary ASK depends on how binary bits are mapped to signal amplitudes. For typical cases: If bits are mapped to 1 and -1, the BER is: BER = Q(√(2 × SNR)) If bits are mapped to 0 and 1, the BER becomes: BER = Q(√(SNR / 2)) Where: Q(x) is the Q-function: Q(x) = 0.5 × erfc(x / √2) SNR : Signal-to-Noise Ratio N₀ : Noise Power Spectral Density Understanding the Q-F...

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 for a...

Simulation of ASK, FSK, and PSK using MATLAB Simulink (with Online Simulator)

📘 Overview 🧮 How to use MATLAB Simulink 🧮 Simulation of ASK using MATLAB Simulink 🧮 Simulation of FSK using MATLAB Simulink 🧮 Simulation of PSK using MATLAB Simulink 🧮 Simulator for ASK, FSK, and PSK 🧮 Digital Signal Processing Simulator 📚 Further Reading ASK, FSK & PSK HomePage MATLAB Simulation Simulation of Amplitude Shift Keying (ASK) using MATLAB Simulink In Simulink, we pick different components/elements from MATLAB Simulink Library. Then we connect the components and perform a particular operation. Result A sine wave source, a pulse generator, a product block, a mux, and a scope are shown in the diagram above. The pulse generator generates the '1' and '0' bit sequences. Sine wave sources produce a specific amplitude and frequency. The scope displays the modulated signal as well as the original bit sequence created by the pulse generator. Mux i...

MATLAB Code for Constellation Diagram of QAM configurations such as 4, 8, 16, 32, 64, 128, and 256-QAM

📘 Overview of QAM 🧮 4-QAM MATLAB 🧮 16-QAM MATLAB 🚀 Online Simulator 📂 Other Topics on Constellation Diagrams... ▼ 🧮 MATLAB Code for 4-QAM 🧮 MATLAB Code for 16-QAM 🧮 MATLAB Code for m-ary QAM 🧮 Simulator for m-ary PSK 🧮 Simulator for m-ary QAM 🧮 Overview of Energy per Bit (Eb / N0) 🧮 Simulator for ASK, FSK, and PSK Overview of QAM One of the best-performing modulation techniques is QAM [↗] . Here, we modulate the symbols by varying the carrier signal's amplitude and phase in response to the variation in the message signal (or voltage variation). So, we may say that QAM is a combination of phase and amplitude modulation. Additionally, it performs better than ASK or PSK [↗] . In fact, any constellation for any type of modulatio...

OFDM Waveform with MATLAB Code

  In OFDM (Orthogonal Frequency Division Multiplexing) , we transmit multiple orthogonal subcarriers simultaneously. Since the subcarriers are orthogonal , they do not interfere with each other, which is one of the main advantages of OFDM. Practically, OFDM converts a wideband signal into multiple narrowband orthogonal subcarriers. For typical wireless communication, if the signal bandwidth (or symbol duration) exceeds the coherence bandwidth of the channel, the signal experiences frequency-selective fading . Fading distorts the signal, making it difficult to recover the original information. By using OFDM, we transmit the same wideband signal across multiple orthogonal narrowband subcarriers, reducing the effect of fading. For example, if we want to transmit a signal of bandwidth 1024 kHz , we can divide it into N = 8 subcarriers . Each subcarrier is then spaced by: Δf = Total Bandwidth N = 1024 8 kHz...

Online Simulator for ASK, FSK, and PSK

Try our new Digital Signal Processing Simulator!   •   Interactive ASK, FSK, and BPSK tools updated for 2025. Start Now Interactive Modulation Simulators Visualize binary modulation techniques (ASK, FSK, BPSK) in real-time with adjustable carrier and sampling parameters. 📡 ASK Simulator 📶 FSK Simulator 🎚️ BPSK Simulator 📚 More Topics ASK Modulator FSK Modulator BPSK Modulator More Topics Simulator for Binary ASK Modulation Digital Message Bits Carrier Freq (Hz) Sampling Rate (...

FastAPI Static Files – Overview

FastAPI Static Files Often, a web application needs to include resources that do not change, even when dynamic data is rendered. These resources are called static assets . Examples of static files include: Images ( .png , .jpg ) JavaScript files ( .js ) Stylesheets ( .css ) Installing Required Library To handle static files in FastAPI, you need the aiofiles library. pip install aiofiles Mounting Static Files FastAPI uses the StaticFiles class to serve static content. You mount a folder (usually named static ) so that all files inside it can be accessed via a URL. from fastapi import FastAPI from fastapi.staticfiles import StaticFiles app = FastAPI() app.mount("/static", StaticFiles(directory="static"), name="static") Example 1: Using an Image Place an image file (for example, fa-logo.png ) inside the static folder. main.py from fastapi import FastAPI, Request from fastapi.responses import HTMLRespon...

MATLAB Codes for Various types of beamforming | Beam Steering, Digital...

📘 How Beamforming Improves SNR 🧮 MATLAB Code 📚 Further Reading 📂 Other Topics on Beamforming in MATLAB ... MIMO / Massive MIMO Beamforming Techniques Beamforming Techniques MATLAB Codes for Beamforming... How Beamforming Improves SNR The mathematical [↗] and theoretical aspects of beamforming [↗] have already been covered. We'll talk about coding in MATLAB in this tutorial so that you may generate results for different beamforming approaches. Let's go right to the content of the article. In analog beamforming, certain codebooks are employed on the TX and RX sides to select the best beam pairs. Because of their beamforming gains, communication created through the strongest beams from both the TX and RX side enhances spectrum efficiency. Additionally, beamforming gain directly impacts SNR improvement. [Read more about Beamforming and How it improves SNR] Wireless...