HELP! How can i solve this array incompatible size issue

조회 수: 6 (최근 30일)
WeiQuan
WeiQuan 2024년 3월 27일
댓글: Chunru 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
Fangjun Jiang 2024년 3월 27일
see the output of size() prior to the error.
The size of the two matrix is different.
WeiQuan
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
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
WeiQuan
WeiQuan 2024년 3월 27일
Hey Chunru may i know which parameters should i change to have a reasonable range ?
Chunru
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).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Modulation에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by