Clear Filters
Clear Filters

HELP! How can i solve this array incompatible size issue

12 views (last 30 days)
I was trying to combine two signals and caculate the bit error rate and plot the graph but it shows error at line 30 with Arrays have incompatible sizes for this operation. Can anyone help me please. I am new to matlab. Below is the code.
clc;
clear all;
close all;
% Parameters
fs = 1000; % Sampling frequency
fc = 100; % Carrier frequency for BPSK modulation
T = 1; % Duration of the signals
t = 0:1/fs:T-1/fs; % Time vector
% SNR for channel in dB
SNRdB = 0 : 32;
% Get SNR Valur, SNRdB = 10*log(10) (signalpower/noise power)
SNR = 10.^(SNRdB/10);
%Data bits
N = 10^5;
x = randsrc(1,N,[0,1]);
%BPSK data generation
bpsk_modulated = 1-2*x;
% Generate Interference Signal (e.g., sinusoidal interference)
interference_frequency = 50; % Frequency of the interference signal
interference_amplitude = 0.5; % Amplitude of the interference signal
interference_signal = interference_amplitude * sin(2*pi*interference_frequency*t);
% Combine BPSK Signal with Interference
size(bpsk_modulated)
ans = 1x2
1 100000
size(interference_signal)
ans = 1x2
1 1000
x_BPSK = bpsk_modulated + interference_signal;
Arrays have incompatible sizes for this operation.
%AWGN noise
n = randn(1,N);
for k=1:length(SNR);
%Assume that noise follow the gaussian distribution, the (0,1). here
%the veriance (sigma squre = 1 and mean = 0), so that noise power = 1
y = (sqrt(SNR(k)) * x_BPSK) + n;
% We need to find which bits has changed with noise ( -1 -> +1 , +1 ->
% -1) in the transmitted channel
noisy_bits = y.*x_BPSK;
% Get indices of the currupted bits by noise
index_currupted = find((noisy_bits)<0);
% Get no of currupted bits by noise
NumOfError_bits(k) = length(find(index_currupted));
end
%BER calculation
ber= NumOfError_bits/N;
%Simulation results
figure;
%Practical
prac = semilogy(SNRdB, ber, 'b*-','linewidth', 1);
hold on;
%Theoritical
theoritical = qfunc(sqrt(SNR));
theo = semilogy(SNRdB,theoritical,' r+-','linewidth',1);
xlabel("SNR in dB");
ylabel("Bit Error Rate(BER)");
legend ([prac theo], {'Practical','Theoritical'});
grid on;
datacursormode on;
  2 Comments
Fangjun Jiang
Fangjun Jiang on 27 Mar 2024
see the output of size() prior to the error.
The size of the two matrix is different.
WeiQuan
WeiQuan on 27 Mar 2024
Hello Fangjun, are you able to guide me on that? How can i change the two matrix to be in same size?

Sign in to comment.

Accepted Answer

Chunru
Chunru on 27 Mar 2024
Make the change: N = length(t);
If you need larger N, thant T in your code to a larger value.
% Parameters
fs = 1000; % Sampling frequency
fc = 100; % Carrier frequency for BPSK modulation
T = 1; % Duration of the signals
t = 0:1/fs:T-1/fs; % Time vector
% SNR for channel in dB
SNRdB = 0 : 32;
% Get SNR Valur, SNRdB = 10*log(10) (signalpower/noise power)
SNR = 10.^(SNRdB/10);
%Data bits
%N = 10^5;
N = length(t);
x = randsrc(1,N,[0,1]);
%BPSK data generation
bpsk_modulated = 1-2*x;
% Generate Interference Signal (e.g., sinusoidal interference)
interference_frequency = 50; % Frequency of the interference signal
interference_amplitude = 0.5; % Amplitude of the interference signal
interference_signal = interference_amplitude * sin(2*pi*interference_frequency*t);
% Combine BPSK Signal with Interference
%whos
x_BPSK = bpsk_modulated + interference_signal;
%AWGN noise
n = randn(1,N);
for k=1:length(SNR);
%Assume that noise follow the gaussian distribution, the (0,1). here
%the veriance (sigma squre = 1 and mean = 0), so that noise power = 1
y = (sqrt(SNR(k)) * x_BPSK) + n;
% We need to find which bits has changed with noise ( -1 -> +1 , +1 ->
% -1) in the transmitted channel
noisy_bits = y.*x_BPSK;
% Get indices of the currupted bits by noise
index_currupted = find((noisy_bits)<0);
% Get no of currupted bits by noise
NumOfError_bits(k) = length(find(index_currupted));
end
%BER calculation
ber= NumOfError_bits/N;
%Simulation results
figure;
%Practical
prac = semilogy(SNRdB, ber, 'b*-','linewidth', 1);
hold on;
%Theoritical
theoritical = qfunc(sqrt(SNR));
theo = semilogy(SNRdB,theoritical,' r+-','linewidth',1);
xlabel("SNR in dB");
ylabel("Bit Error Rate(BER)");
legend ([prac theo], {'Practical','Theoritical'});
grid on;
datacursormode on;
  4 Comments
WeiQuan
WeiQuan on 27 Mar 2024
Hey Chunru may i know which parameters should i change to have a reasonable range ?
Chunru
Chunru on 27 Mar 2024
You can set N=1e9 if you have >8GB or RAM (adjust it according to your hardware). In this case, plot your BER curve up to 1e-9 (or so).

Sign in to comment.

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!