Hi! I from trying for some time now to lower my BER from this equalizer using adaptive filters. My guess is that might be some problems with the synchronization part in the code. Thanks for the help!
clc;
clear;
close all;
% Set modulation parameters
Fc = 1000; % Carrier frequency
Fs = 8000; % Sampling frequency
dev = 50; % Frequency deviation
SNR_dB = 10;
durata_semnal = 5; %[s]
durata_simbol = 0.1; %[s]
% Generate random binary symbols
num_symbols = durata_semnal/durata_simbol; % Set the number of symbols
bits = randi([0 1], 1, num_symbols); % Generate random binary symbols
% Create a time axis for the plot
t = linspace(0, num_symbols, num_symbols*Fs/Fc+1);
t2 = linspace(0, num_symbols, num_symbols+1);
% Plot the binary signal
figure(1);
stairs(t2, [bits, bits(end)], 'LineWidth', 2);
xlim([0, num_symbols]);
ylim([-0.1, 1.1]);
xlabel('Time (symbols)');
ylabel('Amplitude');
title('Random Binary Signal');
% Generate carrier signals
carrier0 = cos(2*pi*Fc*t);
carrier1 = cos(2*pi*(Fc+dev)*t);
% Modulate binary signal using FSK
modulated_signal = zeros(1, length(t));
for n = 1:num_symbols
if bits(n) == 0
modulated_signal((n-1)*Fs/Fc+1:n*Fs/Fc) = carrier0((n-1)*Fs/Fc+1:n*Fs/Fc);
else
modulated_signal((n-1)*Fs/Fc+1:n*Fs/Fc) = carrier1((n-1)*Fs/Fc+1:n*Fs/Fc);
end
end
figure(2);
plot(t, modulated_signal, 'LineWidth', 2);
xlabel('Time (s)');
ylabel('Amplitude');
title('FSK Modulated Signal');
% Add noise to the modulated signal
modulated_signal_with_noise = awgn(modulated_signal, SNR_dB, 'measured');
% Plot the modulated signal
figure(3);
plot(t, modulated_signal_with_noise, 'LineWidth', 2);
xlabel('Time (s)');
ylabel('Amplitude');
title('FSK Modulated Signal with AWGN');
%========================================================================%
% Apply a raised cosine filter to the signal
rolloff = 0.5; % Rolloff factor of the filter
span = 10; % Span of the filter in symbols
sps = 4; % Samples per symbol
h = rcosdesign(rolloff, span, sps); % Create raised cosine filter
filtered_fm = filter(h, 1, modulated_signal_with_noise); % Filter the FM signal
% Plot the filtered signal
t_filtered = (0:length(filtered_fm)-1)/Fs; % Time axis of filtered signal
figure(4)
plot(t_filtered, filtered_fm, 'LineWidth', 2);
xlim([0, num_symbols/Fs]);
xlabel('Time (s)');
ylabel('Amplitude');
title('FM Modulated Signal with Noise and Raised Cosine Filtering');
%========================================================================%
sync_filter = ones(1, sps); % Matched filter for symbol detection
detected = conv(filtered_fm, sync_filter, 'valid'); % Apply matched filter
start_idx = find(detected == max(detected)); % Find index of maximum correlation
if isempty(start_idx) % Check if index is empty
start_idx = 1; % Set default value for start_idx
else % Check if index is negative or too large
start_idx = max(start_idx, 1); % Set index to 1 if negative
end_idx = start_idx + num_symbols*sps - 1; % Compute end index
if end_idx > length(filtered_fm) % Check if end index exceeds array length
end_idx = length(filtered_fm); % Set end index to array length
end
synced_fm = filtered_fm(start_idx:end_idx); % Synchronize signal
end
% Plot the synchronized signal
t_synced = (0:length(synced_fm)-1)/Fs; % Time axis of synchronized signal
figure(5)
plot(t_synced, synced_fm, 'LineWidth', 2);
xlim([0, num_symbols/Fs]);
xlabel('Time (s)');
ylabel('Amplitude');
title('Synchronized FM Modulated Signal with Noise and Raised Cosine Filtering');
eq_length = 6 * sps; % Lungimea filtrului
mu = 0.01 ; % Step size
d = bits(1:min(length(bits), length(synced_fm)));
eq = dsp.LMSFilter(eq_length, 'StepSize', mu); %implementarea algoritmului LMS
eq_output = zeros(1, length(modulated_signal_with_noise)); %vectorul care ne va ajuta sa stocam valorile de la iesirea semnalului dupa egalizare
for n = 1 :min(length(synced_fm), length(d))
% Extragerea unui segment din semnalul receptionat
start_index = round(n*sps - sps*sps +1);
end_index = round(n*sps);
start_index = max(start_index, 1);
end_index = min(end_index, length(filtered_fm));
x = filtered_fm(start_index:end_index);
% Actualizarea egalizorului
[~,err] = eq(d(n),x(min(n, length(x))));
eq_output(max(n*sps - sps*sps + 1, 1) : n*sps) = err';
end
figure(6)
subplot(1,1,1);
plot(t, eq_output, 'LineWidth', 2);
title('Semnalul egalizat');
xlabel('Timp (s)');
ylabel('Amplitudine');
% Demodulate the FSK modulated signal
% Create bandpass filters
bp_filter0 = designfilt('bandpassiir', 'FilterOrder', 6, ...
'HalfPowerFrequency1', Fc-0.5*dev, 'HalfPowerFrequency2', Fc+0.5*dev, ...
'SampleRate', Fs);
bp_filter1 = designfilt('bandpassiir', 'FilterOrder', 6, ...
'HalfPowerFrequency1', Fc+0.5*dev, 'HalfPowerFrequency2', Fc+1.5*dev, ...
'SampleRate', Fs);
% Filter the modulated signal with the bandpass filters
filtered0 = filter(bp_filter0, modulated_signal_with_noise);
filtered1 = filter(bp_filter1, modulated_signal_with_noise);
% Compute the energy of the filtered signals
energy0 = abs(filtered0).^2;
energy1 = abs(filtered1).^2;
% Calculate the moving average of energy for symbol detection
window_length = round(Fs/Fc);
energy0_avg = filter(ones(1, window_length)/window_length, 1, energy0);
energy1_avg = filter(ones(1, window_length)/window_length, 1, energy1);
% Initialize the detected symbols vector
detected_symbols = zeros(1, num_symbols);
% Compare the energies of the filtered signals to detect symbols
for n = 1:num_symbols
if energy0_avg(n*Fs/Fc) > energy1_avg(n*Fs/Fc)
detected_symbols(n) = 0;
else
detected_symbols(n) = 1;
end
end
% Calculate the error signal
semnal_iesire = abs(detected_symbols - bits);
% Plot the detected symbols
figure(7)
stairs(t2, [detected_symbols, detected_symbols(end)], 'LineWidth', 2);
xlim([0, num_symbols]);
ylim([-0.1, 1.1]);
xlabel('Time (symbols)');
ylabel('Amplitude');
title('Detected Binary Signal');
% Plot the error signal
figure(8)
stairs(t2, [semnal_iesire, semnal_iesire(end)], 'LineWidth', 2);
xlim([0, num_symbols]);
ylim([-0.1, 1.1]);
xlabel('Time (symbols)');
ylabel('Amplitude');
title('Error Signal');
% 1 indicates a bit error and a value of 0 indicates a correct bit
% Calculate the number of bit errors
bit_errors = sum(abs(detected_symbols - bits));
% Calculate the bit error rate (BER)
BER = bit_errors / num_symbols;
% Convert BER to percentage
BER_percentage = BER * 100;
% Display the BER and BER percentage
fprintf('Bit Error Rate (BER): %f\n', BER);
fprintf('Bit Error Rate (BER) in percentage: %f%%\n', BER_percentage);