MATLAB: I am trying to solve following error in the code, Error using .* Matrix dimensions must agree.

clear all;
EbN0_dB = 0:2.5:30;
EbN0 = 10.^(EbN0_dB/10); % convert from dB scale
Analytical_BER_ASK = Q(sqrt(EbN0)); % BER of ASK
Analytical_BER_BPSK = Q(sqrt(2*EbN0)); % BER of QPSK and BPSK
L = 1000;
Numerical_BER_ASK = zeros(1,length(EbN0));
Numerical_BER_BPSK = zeros(1,length(EbN0));
for i=1:length(EbN0) % run simulation for each EbN0 value
ASK_error = 0; % to count number of error with ASK
BPSK_error = 0; % to count number of errors with BPSK
noOfL = 0; % to keep track number of bit sequences
while BPSK_error < 100
N0 = 1/(EbN0(i)); % with Eb = 1, N0 = 1/(EbN0)
s = randi([0,1],1,L); % random bit sequence of length L
ASK_TX_signal = s*sqrt(2); % ASK modulated signal
BPSK_RX_signal = 2*s - 1; % BPSK modulated signal
noise = sqrt(N0/2) * randn(1,L); % Gaussian noise;
% Generate the fading channel, it's a Gaussian random variables
channel = sqrt(1/2)*(randn(1,L) + 1i*randn(1,L));
ASK_RX_signal = channel.*ASK_TX_signal + noise; % receive ASK signal
BPSK_RX_signal = channel.*BPSK_RX_signal + noise; % receive BPSK signal
% Before doing demodulation, you do equalizer and co-phases the
% channel
ASK_RX_signal = conj(channel).*ASK_RX_signal;
BPSK_RX_signal = conj(channel).*BPSK_RX_signal;
% demodulate ASK signal with threshold at sqrt(2)/2
ASK_s = double(ASK_RX_signal > sqrt(2)/2);
% demodulate BPSK signal with thresshold at 0;
BPSK_s = double(BPSK_RX_signal > 0);
ASK_error = ASK_error + sum(ASK_s~=s); % count error with ASK
BPSK_error = BPSK_error + sum(BPSK_s~=s); % count error with BPSK
noOfL = noOfL + 1;
end
Numerical_BER_ASK(i) = ASK_error/L/noOfL;
Numerical_BER_BPSK(i) = BPSK_error/L/noOfL;
end
A_ASK = semilogy(EbN0_dB,Analytical_BER_ASK,'-');
hold on
A_BPSK = semilogy(EbN0_dB,Analytical_BER_BPSK,'--');
N_ASK = semilogy(EbN0_dB,Numerical_BER_ASK,'o');
N_BPSK = semilogy(EbN0_dB,Numerical_BER_BPSK,'s');
xlabel('Eb/N0 in dB');
ylabel('BER');
legend([A_ASK A_BPSK N_ASK N_BPSK],'Analytical ASK','Analytical BPSK',...
'Numerical ASK','Numerical BPSK');

Best Answer

If you got this code from someone else and it should be working (because it used to work with the exact same parameters), my guess is that the person wrote it for/on a recent version of MATLAB that supports implicit expansion, and you are trying to run it on an older version that requires explicit calls to BSXFUN for performing operations with implicit expansion (..).
On recent versions you can do this:
>> x = 1 : 5
x =
1 2 3 4 5
>> Y = repmat( 21:25, 4, 1 )
Y =
21 22 23 24 25
21 22 23 24 25
21 22 23 24 25
21 22 23 24 25
>> x .* Y
ans =
21 44 69 96 125
21 44 69 96 125
21 44 69 96 125
21 44 69 96 125
where you can observe an automatic expansion of x along dim 1, otherwise we could not perform this element-wise multiplication. On former version of MATLAB, we had to proceed as follows:
>> bsxfun( @times, x, Y )
ans =
21 44 69 96 125
21 44 69 96 125
21 44 69 96 125
21 44 69 96 125
If this is what is happening (based on your screenshots it could be), then you have to update the code and implement relevant calls to BSXFUN. For the line where you have the error, this should be:
BPSK_RX_signal = bsxfun( @times, channel, BPSK_RX_signal ) + noise; % receive BPSK signal
where both channel and noise are 8x1000 and BPSK_RX_signal is 1x1000.
If the code has never worked before, the error may be elsewhere.
EDIT: I don't understand how channel and noise can be 8x1000 with this code by the way, so the error should be elsewhere.
Related Question