フィルターのクリア

Why BER consistently simulated as 0.5?

1 回表示 (過去 30 日間)
Kim Yi NG
Kim Yi NG 2023 年 11 月 20 日
コメント済み: Kim Yi NG 2023 年 11 月 28 日
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%% Direct Sequence Spread Spectrum %%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Single User %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simulation Parameters
numBits = 5e6; % Number of bits
SNR_dB = 0:5:40; % Signal-to-Noise Ratio (dB) range
numSNR = length(SNR_dB);
BER = zeros(1, numSNR);
%K=2;
% Simulation Loop
for i = 1:numSNR % Originally: for i = 1:numSNR
% Generate random bits
dataBits = randi([0, 1], 1, numBits);
% PN Sequence Generation (e.g., m-sequence)
sequenceLength = numBits; % or any other desired length
pnSequence = generatePNSequence(sequenceLength); % Implement this function
% Spread the data using PN sequence
spreadData = spreadDataWithPN(dataBits, pnSequence); % Implement this function
% BPSK Modulation
modulatedSignal = bpskModulation(spreadData); % Implement this function
% Add AWGN
noisySignal = addAWGN(modulatedSignal, SNR_dB(i)); % Implement this function
% Rayleigh Fading Channel
Rayleigh_fadedSignal = applyRayleighFading(noisySignal); % Implement this function
% Rician Fading Channel
%Rician_fadedSignal = applyRicianFading(noisySignal,K); % Use Either Rayleigh or Rician Only
% BPSK Demodulation
demodulatedBits = bpskDemodulation(Rayleigh_fadedSignal); % Implement this function
% Calculate Bit Error Rate (BER)
BER(i) = sum(demodulatedBits ~= dataBits) / numBits;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%% Theoretical Simulation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Theoretical Rayleigh Fading
theoreticalBER_Rayleigh = qfunc(sqrt(2 * 10.^(SNR_dB / 10))); % Assuming BPSK
% Theoretical Rician Fading
%theoreticalBER_Rician = 0.5 * exp(-K / (1 + K) * 2 * 10.^(SNR_dB / 10) / (1 + K));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%% Plotting of Figure %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Plot BER vs SNR
figure;
semilogy(SNR_dB, BER, 'o-', 'LineWidth', 2, 'DisplayName', 'Simulated-Rayleigh');
hold on;
semilogy(SNR_dB, theoreticalBER_Rayleigh, 'r--', 'LineWidth', 2, 'DisplayName', 'Theoretical-Rayleigh');
hold on;
%semilogy(SNR_dB, theoreticalBER_Rician, 'g--', 'LineWidth', 2, 'DisplayName', 'Theoretical-Rician');
%hold on;
grid on;
xlabel('SNR (dB)');
ylabel('Bit Error Rate (BER)');
legend('Location', 'best');
title('DSSS with Rayleigh Fading Simulation');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%% Debugging of the Component %%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure;
subplot(2, 1, 1);
plot(real(noisySignal(1:100)), 'LineWidth', 2);
title('Real Part of Noisy Signal (before fading)');
subplot(2, 1, 2);
plot(real(Rayleigh_fadedSignal(1:100)), 'LineWidth', 2);
title('Real Part of Faded Signal');
% Debug information
disp(['SNR: ', num2str(SNR_dB(i))]);
disp(' ');
% Inside spreadDataWithPN function
disp(['Spread Data (first 10): ', num2str(spreadData(1:10))]);
disp(' ');
% Inside bpskModulation function
disp(['Modulated Signal (first 10): ', num2str(modulatedSignal(1:10))]);
disp(' ');
% Inside Noisy Signal
disp(['Noisy Signal (first 10): ', num2str(noisySignal(1:10))]);
disp(' ');
% Inside Received Signal
disp(['Received Signal (first 10): ', num2str(Rayleigh_fadedSignal(1:10))]);
disp(' ');
% Inside Real Part of Reeived Signal
disp(['Real Part of Received Signal (before demodulation): ', num2str(real(Rayleigh_fadedSignal(1:10)))]);
disp(' ');
% Inside bpskDemodulation function
disp(['Demodulated Bits (first 10): ', num2str(demodulatedBits(1:10))]);
disp(' ');
% Display Simulated BER at each SNR point
disp('Simulated BER at each SNR point:');
disp(BER);
disp(' ');
% Display Demodulation Threshold
disp(['Demodulation Threshold: ', num2str(0)]);
disp(['Mean of Received Signal: ', num2str(mean(real(Rayleigh_fadedSignal)))]);
disp(['Variance of Received Signal: ', num2str(var(real(Rayleigh_fadedSignal)))]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% Function For all the Component %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Function for PN Sequence Generator
function pnSequence = generatePNSequence(sequenceLength)
% Generate a PN sequence using a shift register
% sequenceLength: length of the PN sequence
% Define the shift register
shiftRegister = ones(67, 100); % You can adjust the length as needed
pnSequence = zeros(1, sequenceLength);
% Generate the PN sequence
for i = 1:sequenceLength
feedback = xor(shiftRegister(3), shiftRegister(10));
pnSequence(i) = shiftRegister(10);
shiftRegister = circshift(shiftRegister, [0, 1]);
shiftRegister(1) = feedback;
end
end
% Function for Spreading Sequence ( Chipping Code )
function spreadData = spreadDataWithPN(dataBits, pnSequence)
% Spread data bits using a PN sequence
% Repeat the PN sequence to match the length of dataBits
numRepeats = ceil(length(dataBits) / length(pnSequence));
repeatedPN = repmat(pnSequence, 1, numRepeats);
% Take only the required length
repeatedPN = repeatedPN(1:length(dataBits));
% XOR the data with the PN sequence
spreadData = xor(dataBits, repeatedPN);
% Ensure spreadData is in the range [0, 1]
spreadData = mod(spreadData, 2);
end
% Function for BPSK Modulation
function modulatedSignal = bpskModulation(dataBits)
% BPSK modulation: 0 -> -1, 1 -> 1
modulatedSignal = 2 * dataBits - 1;
end
% Function for AWGN Channel
function noisySignal = addAWGN(modulatedSignal, snr_dB)
% Add AWGN to the signal based on specified SNR in dB
% Convert SNR from dB to linear scale
snr = 10^(snr_dB / 10);
% Calculate the noise power
noisePower = 1 / snr;
% Generate Gaussian noise with the same length as the signal
noise = sqrt(noisePower) * randn(size(modulatedSignal));
% Add the noise to the signal
noisySignal = modulatedSignal + noise;
end
% Function for Rayleigh Fading Channel
function Rayleigh_fadedSignal = applyRayleighFading(noisySignal)
% Apply Rayleigh fading to the signal
% Generate complex Gaussian random variables for Rayleigh fading
fadingChannel = sqrt(1/2)*(randn(size(noisySignal)) + 1i * randn(size(noisySignal)));
% Multiply the signal with the fading channel
Rayleigh_fadedSignal = noisySignal .* fadingChannel;
end
% Function For Rician Fading Channel
function Rician_fadedSignal = applyRicianFading(signal, K)
% Apply Rician fading to the signal
% Generate complex Gaussian random variables for Rician fading
fadingChannel = sqrt(K / (K + 1)) * (randn(size(signal)) + 1i * randn(size(signal))) + sqrt(1 / (K + 1));
% Multiply the signal with the fading channel
Rician_fadedSignal = signal .* fadingChannel;
end
% Function for BPSK Demodulation
function demodulatedBits = bpskDemodulation(Rayleigh_fadedSignal)
% BPSK demodulation: map the sign of the real part to 1 or 0
% Demodulate based on the sign of the real part
demodulatedBits = (sign(real(Rayleigh_fadedSignal)) + 1) / 2;
threshold = 1 * max(abs(real(Rayleigh_fadedSignal)));
end
I have this code above, when i run the simulation, my BER result consistently appear to be 0.5. i need help on where is the error that i have, as from the understanding of the DSSS system, it should be decreasing BER when increase SNR.
  1 件のコメント
Kim Yi NG
Kim Yi NG 2023 年 11 月 22 日
can anyone help on this?

サインインしてコメントする。

採用された回答

Pratik
Pratik 2023 年 11 月 28 日
Hi Kim,
As per my understanding, you want to implement Direct Sequence Spread Spectrum and want to plot the Bit Error Rate (BER) vs Signal to Noise Ratio (SNR). However, the plot is not as expected and the value of BER is nearly 0.5 for all values of SNR.
After inspecting the provided code, it can be observed that “despreading” process is missing. “Despreading” is done by mixing the spread signal with the same PN sequence that was used for spreading. It is mathematically a correlation of the transmitted spreading sequence with the spreading sequence.
Please consider modifying the code after the demodulation of signal as following:
recbits = spreadDataWithPN(demodulatedBits, pnSequence); % De-spread the demodulated bits using the PN sequence.
BER(i) = (sum(recbits ~= dataBits)/numBits); % Calculate the Bit Error Rate (BER) for the current iteration
Hope this helps!
  1 件のコメント
Kim Yi NG
Kim Yi NG 2023 年 11 月 28 日
Yes i having the problem on the plotting of BER vs SNR, as my simulated BER consistently at 0.5 where it does not match the Theoretically BER.
Thank you for assisting me on the coding, i did tried the code you provide, the result is the same.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBPSK についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by