# HELP! How can i solve this array incompatible size issue

6 次查看（过去 30 天）
WeiQuan 2024-3-27

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 个评论显示 无隐藏 无
Fangjun Jiang 2024-3-27
see the output of size() prior to the error.
The size of the two matrix is different.
WeiQuan 2024-3-27
Hello Fangjun, are you able to guide me on that? How can i change the two matrix to be in same size?

### 采纳的回答

Chunru 2024-3-27
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 个评论显示 2更早的评论隐藏 2更早的评论
WeiQuan 2024-3-27
Hey Chunru may i know which parameters should i change to have a reasonable range ?
Chunru 2024-3-27
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).

### 类别

Help CenterFile Exchange 中查找有关 Error Detection and Correction 的更多信息

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by