the code are correct but display fails
Show older comments
%clc; clear; close all;
% Nom du fichier avec l'enregistrement
filename = "07-59-39_2480000000Hz.wav";
% Fréquence de coupure pour le filtrage passe-bas
lowpassFreq = 0.6e6; % [Hz]
% Mode BLE et index de canal
bleParam.Mode = 'LE1M';
bleParam.ChannelIndex = 39;
% Coefficients du filtre GFSK
BT = 0.5;
span = 4;
% Longueur de la section à traiter (à adapter selon taille du fichier)
StartIndex = 1;
EndIndex = 10e5; % exemple, à ajuster
% Lecture du signal
[rawSig, fs] = audioread(filename);
% Conversion en enveloppe complexe
signalComplex = complex(rawSig(:,1), rawSig(:,2));
% Décalage en fréquence (si besoin)
freqCenter = 2.48e9; % Hz, fréquence du canal
IF = 4; % décalage si besoin
t = (0:length(signalComplex)-1)' / fs;
signalShifted = signalComplex .* exp(-1j*2*pi*IF*t);
% Filtrage passe-bas
processed_signal = lowpass(signalShifted, lowpassFreq, fs);
% Paramètres selon le mode BLE
if strcmp(bleParam.Mode, 'LE1M')
bleParam.BitRate = 1e6;
bleParam.SamplesPerSymbol = fs / bleParam.BitRate;
bleParam.PreambleLength = 8;
bleParam.AccessAddressLength = 32;
bleParam.CRCLength = 24;
end
% Longueur du PDU
bleParam.PDUMinLen = 2; % en octets
bleParam.PDUMaxLen = 39; % en octets
% Adresse d’accès standard
bleParam.AccessAddressHex = '0x8E89BED6';
bleParam.AccessAddressBin = hexToBinaryVector(bleParam.AccessAddressHex, 32);
% Génération du préambule
bleParam.Preamble = ble.internal.preambleGenerator(bleParam.AccessAddressBin);
preambleVec = bleParam.Preamble(:).'; % colonne -> ligne
accessAddrVec = bleParam.AccessAddressBin(:).'; % colonne -> ligne
% Paramètres de modulation
sps = round(bleParam.SamplesPerSymbol); % échantillons par symbole
% Modulation du préambule + AA
bits = [preambleVec, accessAddrVec];
bleParam.RefSeq = gmskmod_custom(bits, BT, span, sps);
% Objets utiles
prbDet = comm.PreambleDetector('Preamble', bleParam.RefSeq);
agc = comm.AGC;
coarseFreqComp = comm.CoarseFrequencyCompensator;
pktCnt = 0; crcCnt = 0;
figure;
plot(abs(processed_signal));
title('Signal brut');
xlabel('Échantillons'); ylabel('Amplitude');
figure;
spectrogram(processed_signal(1:min(EndIndex,length(processed_signal))), 1024, 512, 1024, fs, 'centered');
title('Spectrogramme du signal');
displayFlag =true;
while length(processed_signal) > EndIndex
rcvSig = processed_signal(StartIndex:EndIndex);
% AGC + Compensation
rcvAGC = agc(rcvSig);
release(coarseFreqComp);
rcvComp = coarseFreqComp(rcvAGC);
% Détection du préambule
[~, dtMt] = prbDet(rcvComp);
[threshold, IndexMax] = max(dtMt);
startIdx = IndexMax - length(bleParam.RefSeq);
if startIdx < 1 || (startIdx + 2000 > length(rcvComp))
break;
end
if ~isempty(IndexMax)
figure;
subplot(2,1,1); plot(abs(rcvComp)); title('Signal compensé'); hold on;
plot(IndexMax, abs(rcvComp(IndexMax)), 'ro'); xline(startIdx, 'r--');
subplot(2,1,2); plot(dtMt); title('Corrélation du préambule');
else
disp('Aucun préambule détecté');
StartIndex = EndIndex + 1;
EndIndex = StartIndex + 2000;
continue;
end
% Extraire trame
rcvFrame = rcvComp(startIdx : startIdx + 2000); % taille à adapter
% Démodulation
bits = gfskdemod_custom(rcvFrame, fs, bleParam.BitRate);
figure;
stem(bits(1:100)); title('100 premiers bits démodulés');
% Déblanchiment
dewhitenStateLen = 6;
chBin = int2bit(bleParam.ChannelIndex, dewhitenStateLen)';
initState = [1 chBin];
dewhiten = bluetoothWhiten(InitialConditions=initState');
dewhitenedBits = dewhiten(bits);
release(dewhiten);
nbits = floor(length(dewhitenedBits) / 8) * 8;
dewhitenedBits = dewhitenedBits(1:nbits);
% Vérification CRC et décodage PDU
[status, cfgLLAdv] = bleLLAdvertisingChannelPDUDecode(dewhitenedBits);
if strcmp(status, 'Success')
crcCnt = crcCnt + 1;
pktCnt = pktCnt + 1;
fprintf("Type de PDU : %s\n", cfgLLAdv.PDUType);
fprintf("Adresse publicitaire : %s\n", cfgLLAdv.AdvertiserAddress);
payload = cfgLLAdv.Payload;
if length(payload) > 6*8
AdvertiserAddress = dec2hex(bit2int(payload(1:6*8), 48, 0), 12);
payload_int = bit2int(payload(6*8+1:end), 8, 0);
DataName_char = char(payload_int.');
fprintf("Adresse dans le payload : %s\n", AdvertiserAddress);
fprintf("Payload HEX : "); fprintf("%x", payload_int); fprintf("\n");
fprintf("Payload ASCII : %s\n\n", DataName_char);
end
end
StartIndex = EndIndex + 1;
EndIndex = StartIndex + 2000;
end
fprintf("Type de PDU : %s\n", cfgLLAdv.PDUType);
fprintf("Adresse publicitaire : %s\n", cfgLLAdv.AdvertiserAddress);
fprintf("Adresse dans le payload : %s\n", AdvertiserAddress);
fprintf("Payload HEX : "); fprintf("%x", payload_int); fprintf("\n");
fprintf("Payload ASCII : %s\n\n", DataName_char);
1 Comment
Rookie Programmer
on 19 Aug 2025
You likely need to add -> addpath(genpath('Path Name')), where 'Path Name' is the directory where"07-59-39_2480000000Hz.wav" is stored.
Answers (0)
Categories
Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!