Hello,
I understand you are facing "Index exceeds matrix dimensions." error because you are using wrong function names.
- Correct MATLAB Functions: use rcosdesign for designing a raised cosine filter instead of any incorrect or untranslated function names
- Correct the loop for adding noise and synthesizing the modulating signal.
- Correct the implementation of noise addition to the signal, ensuring that the white Gaussian noise (wgn) function is used correctly with respect to signal power and SNR.
Please refer below for the corrected code:
clc;
clear;
% Initial data
F = 100; % Pulse repetition frequency, Hz
alpha = 0.5; % Rolloff factor
U0 = 1; % Carrier amplitude, V
f0 = 100000; % Carrier frequency, Hz
Fs = 10 * f0; % Sampling frequency, Hz
T = 1 / F; % Pulse repetition period, s
Ts = 1 / Fs; % Sampling period, s
F = 140: 0.1: 160; % Array
M = length(F);
j = complex(0, 1);
% Adding noise
SNR = -15; % Noise addition, dB
% Calculation of the raised cosine pulse
delay = 3; % Delay
g = rcosdesign(alpha, delay, 200*F, 'sqrt'); % Raised cosine pulse
g = g / max(g);
N1 = length(g);
t1 = 0: (1 / (200 * F)) : (N1-1) * (1 / (200 * F));
% 2. Synthesis of random code
Nsym = 100; % Number of symbols
p = ceil(2. * rand(1, Nsym));
p = p-1;
% ---------------------------------- DQPSK Modulation ------------
% 3. Calculation of symbols a
a = zeros(1, Nsym);
for n = 1: Nsym % Calculating A and B
if p(n) == 0
a(n) = -1;
end
if p(n) == 1
a(n) = 1;
end
end
n = 1: Nsym;
% Plot 1)
stem(n, a);
% xlim([0, 20]);
ylim([-2, 2]);
xlabel('n');
ylabel('a');
grid on;
% 4. Synthesis of the modulating signal
N = N1 * Nsym;
t = 0: Ts: (N-1) * Ts;
Sa = zeros(1, N);
for n = 1: Nsym
for k = 1: N1
Sa(k + (n-1) * 200 * F) = a(n) * g(k) + Sa(k + (n-1) * 200 * F);
end
end
% Figure 2)
plot(t, Sa);
title('Plot of the modulating signal of symbols a');
xlim([0, 10]);
ylim([-2, 2]);
xlabel('t, s');
ylabel('Sa(t), V');
grid on;
% 5. Balanced modulation
Ug = U0 * cos(2 * pi * f0 * t); % Carrier oscillation
Uw = Sa .* Ug; % Radio signal
P = mean(Uw .* Uw); % Power in W
PF = 10 * log10((max(Uw .* Uw)) / P); % Peak factor
Ps = 10 * log10(P); % Power in dB
Noise = wgn(1, N, Ps-SNR);
Uw = Uw + Noise; % Adding noise
% Figure (3)
plot(t, Uw);
xlim([0, 20]);
ylim([-2, 2]);
xlabel('t, s');
ylabel('Uw(t), V');
grid on;
% ---------------------------------- BPSK Demodulation ------------
% 6. Balanced demodulation
t = 0: Ts: (N-1) * Ts;
Uda = Uw .* cos(2 * pi * f0 * t);
Sfa = filter(g, 1, Uda);
Sfa = Sfa / max(Sfa) * max(Sa);
t = 0: (1 / (200 * F)) : (N-1) * (1 / (200 * F));
% Figure (4)
subplot(2, 1, 1);
plot(t, Sfa);
title('Plot of the demodulated signal a');
xlim([0, 0.23]);
ylim([-2, 2]);
xlabel('t, s');
ylabel('Sa(t), V');
grid on;