windowingEnabled = false;
VIDEO_PATH = 'C:\Users\hp\Desktop\test1.mp4';
ofdmMod = comm.OFDMModulator( ...
'NumGuardBandCarriers', numGuardBands, ...
'InsertDCNull', insertDCNull, ...
'CyclicPrefixLength', cyclicPrefixLen, ...
'Windowing', windowingEnabled, ...
'OversamplingFactor', oversamplingFact, ...
'NumTransmitAntennas', 1, ...
'PilotInputPort', false);
Fs_req = ofdmMod.FFTLength * scs * ofdmMod.OversamplingFactor;
scs = MAX_FS / (ofdmMod.FFTLength * ofdmMod.OversamplingFactor);
warning('SCS düşürüldü: %.1f kHz (Fs=%.2f MHz, Pluto limiti).', scs/1e3, MAX_FS/1e6);
Fs = ofdmMod.FFTLength * scs * ofdmMod.OversamplingFactor;
ofdmInfo0 = info(ofdmMod);
Ndata = ofdmInfo0.DataInputSize(1);
bitsPerOFDMsym = Ndata * bps;
inBits = load_file_as_bits(VIDEO_PATH);
fileLenBytes = uint64(numel(inBits)/8);
lenBytes = zeros(8,1,'uint8');
lenBytes(i) = uint8(bitand(bitshift(fileLenBytes, -8*(i-1)), 255));
lenBits = de2bi(lenBytes, 8, 'left-msb').';
payloadBitsNoPad = [lenBits; inBits];
NsymNeeded = ceil(numel(payloadBitsNoPad) / bitsPerOFDMsym);
ofdmMod.NumSymbols = NsymNeeded;
ofdmInfo = info(ofdmMod);
ofdmSize = ofdmInfo.DataInputSize;
totalBitsNeeded = prod(ofdmSize) * bps;
padBits = totalBitsNeeded - numel(payloadBitsNoPad);
txBits = [payloadBitsNoPad; false(padBits,1)];
txBits = payloadBitsNoPad(1:totalBitsNeeded);
totalBits = numel(txBits);
dataQAM = qammod(uint8(txBits), M, 'gray', 'InputType', 'bit', 'UnitAveragePower', true);
expectedLen = prod(ofdmSize);
if numel(dataQAM) ~= expectedLen
error('QAM veri uzunluğu (%d), beklenen OFDM giriş uzunluğuyla (%d) eşleşmiyor.', ...
numel(dataQAM), expectedLen);
dataQAM = reshape(dataQAM, ofdmSize);
ofdmModTrain = comm.OFDMModulator( ...
'NumGuardBandCarriers', numGuardBands, ...
'InsertDCNull', insertDCNull, ...
'CyclicPrefixLength', cyclicPrefixLen, ...
'Windowing', windowingEnabled, ...
'OversamplingFactor', oversamplingFact, ...
'NumTransmitAntennas', 1, ...
'PilotInputPort', false);
trainBits = randi([0 1], Ndata,1);
trainSym = 1 - 2*trainBits;
trainQAM = reshape(trainSym,[Ndata,1,1]);
trainWave = ofdmModTrain(trainQAM);
preambleWave = [trainWave; trainWave];
payloadWave = ofdmMod(dataQAM);
txWave = [preambleWave; payloadWave];
sa = spectrumAnalyzer('SampleRate', Fs);
cd = comm.ConstellationDiagram('ColorFading', true, ...
'ShowTrajectory', false, 'ShowReferenceConstellation', false);
showResourceMapping(ofdmMod);
plutoTx = sdrtx('Pluto', RadioID='usb:0');
plutoTx.CenterFrequency = txCenterHz;
plutoTx.BasebandSampleRate = Fs;
plutoTx.ShowAdvancedProperties = true;
plutoTx.FrequencyCorrection = 0;
transmitRepeat(plutoTx, txWave);
fprintf(['Transmission stopped.\n' ...
'--- OFDM/Link Özeti ---\n' ...
'GuardBands [L;U] : [%d;%d]\n' ...
'InsertDCNull : %d\n' ...
'OversamplingFactor : %d\n' ...
'Subcarrier Spacing : %.1f kHz\n' ...
'Fs (Baseband) : %.2f MHz\n' ...
'Active data subcarriers: %d\n' ...
'QAM order (M) : %d\n' ...
'Bits / OFDM symbol : %d\n' ...
'Payload bits : %d\n' ...
'Num OFDM symbols : %d\n' ...
'Total bits (TX stream) : %d\n'], ...
ofdmMod.FFTLength, numGuardBands(1), numGuardBands(2), insertDCNull, ...
cyclicPrefixLen, windowingEnabled, oversamplingFact, ...
scs/1e3, Fs/1e6, Ndata, M, bitsPerOFDMsym, ...
64, numel(inBits), padBits, NsymNeeded, totalBits);
Nfft = ofdmMod.FFTLength;
Ndata2 = inf0.DataInputSize(1);
fprintf('FFT=%d, Guard=[%d;%d], DCnull=%d -> Veri taşıyıcı: %d, Bit/sembol: %d\n', ...
Nfft, ofdmMod.NumGuardBandCarriers(1), ofdmMod.NumGuardBandCarriers(2), ...
ofdmMod.InsertDCNull, Ndata2, Ndata2*bps2);
function bits = load_file_as_bits(filePath)
fid = fopen(filePath,'rb');
assert(fid~=-1, 'Cannot open file: %s', filePath);
c = onCleanup(@() fclose(fid));
data = fread(fid, Inf, 'uint8=>uint8');
bits = de2bi(data, 8, 'left-msb').';
clc; clearvars; close all;
OUTPUT_PATH = 'C:\Users\hk\Desktop\rx_recovered.mp4';
windowingEnabled = false;
Fs = FFT_LEN * scs * oversamplingFact;
ofdmModTrain = comm.OFDMModulator( ...
'FFTLength', FFT_LEN, ...
'NumGuardBandCarriers', numGuardBands, ...
'InsertDCNull', insertDCNull, ...
'CyclicPrefixLength', cyclicPrefixLen, ...
'Windowing', windowingEnabled, ...
'OversamplingFactor', oversamplingFact, ...
'NumTransmitAntennas', 1, ...
'PilotInputPort', false);
ofdmDemod = comm.OFDMDemodulator( ...
'FFTLength', FFT_LEN, ...
'NumGuardBandCarriers', numGuardBands, ...
'InsertDCNull', insertDCNull, ...
'CyclicPrefixLength', cyclicPrefixLen, ...
'Windowing', windowingEnabled, ...
'OversamplingFactor', oversamplingFact, ...
'NumReceiveAntennas', 1, ...
'PilotOutputPort', false);
Ndata = info(ofdmModTrain).DataInputSize(1);
trainBits = randi([0 1], Ndata, 1);
trainSym = 1 - 2*trainBits;
trainQAM = reshape(trainSym, [Ndata, 1, 1]);
trainWave = ofdmModTrain(trainQAM);
preambleWave = [trainWave; trainWave];
symLen = FFT_LEN + cyclicPrefixLen;
plutoRx = sdrrx('Pluto','RadioID','usb:0', ...
'CenterFrequency', rxCenterHz, ...
'BasebandSampleRate', Fs, ...
'SamplesPerFrame', samplesPerFrame, ...
'OutputDataType', 'double', ...
'GainSource','Manual','Gain', rxGain);
nFrames = ceil(airtimeSec * Fs / samplesPerFrame);
rxBuf = zeros(nFrames * samplesPerFrame, 1);
if isempty(s); continue; end
rxBuf(idx:idx+numel(s)-1) = s;
if numel(rxBuf) < 2*symLen
error('Yetersiz örnek: Preamble''ın tamamı yakalanmamış görünüyor.');
corr = abs(conv(rxBuf, flipud(conj(preambleWave))));
[~, peakIdx] = max(corr);
startIdx = peakIdx - numel(preambleWave) + 1;
error('Preamble not found (startIdx < 1).');
fprintf('Preamble detected at sample %d\n', startIdx);
if startIdx + 2*L - 1 > numel(rxBuf)
error('Veri eksik: iki training sembolü tam değil.');
r1 = rxBuf(startIdx : startIdx + L - 1);
r2 = rxBuf(startIdx + L : startIdx + 2*L - 1);
phi = angle(sum(conj(r1) .* r2));
n = (0:numel(rxBuf)-1).';
rxBufCFO = rxBuf .* exp(-1j * cfoEst * n);
ofdmDemod.NumSymbols = 2;
preBlock = rxBufCFO(startIdx : startIdx + 2*symLen - 1);
Rpre = ofdmDemod(preBlock);
H_est = Rtrain ./ Strain;
H_est(abs(H_est) < 1e-12) = 1e-12;
payloadStart = startIdx + 2*symLen;
remain = numel(rxBufCFO) - payloadStart + 1;
numSyms = floor(remain / symLen);
error('Payload için yeterli sembol yok.');
ofdmDemod.NumSymbols = numSyms;
rxPayloadTrunc = rxBufCFO(payloadStart : payloadStart + numSyms*symLen - 1);
rxSymbols = ofdmDemod(rxPayloadTrunc);
rxSymbols = squeeze(rxSymbols);
eqSymbols = rxSymbols ./ H_est;
rxBits = qamdemod(rxQAMvec, M, 'gray', 'OutputType','bit', 'UnitAveragePower', true);
error('Yetersiz bit: 64-bit uzunluk başlığı alınamadı.');
lenBytesM = reshape(lenBits, 8, []).';
lenVals = uint8(bi2de(lenBytesM, 'left-msb'));
fileLen = fileLen + uint64(lenVals(i)) * uint64(2)^((i-1)*8);
fprintf('Recovered file length (header): %d bytes\n', fileLen);
payloadBits = rxBits(65:end);
maxBytes = floor(numel(payloadBits) / 8);
warning('Header %d byte diyor ama yalnizca %d byte mevcut. Kırpılıyor.', fileLen, maxBytes);
fileLen = uint64(maxBytes);
payloadBits = payloadBits(1:double(fileLen) * 8);
error('Payload boş: dosya çıkarılamadı.');
bytesMat = reshape(payloadBits, 8, []).';
bytes = uint8(bi2de(bytesMat, 'left-msb'));
fid = fopen(OUTPUT_PATH, 'wb');
assert(fid ~= -1, 'Cannot open output file: %s', OUTPUT_PATH);
fwrite(fid, bytes, 'uint8');
fprintf('Saved file: %s (bytes written: %d)\n', OUTPUT_PATH, numel(bytes));