# Simulate capture effect in Matlab with LoRa modulation

I have a Matlab code to generate a LoRa signal modulation and demodulation. Now I would like to simulate a capture effect with 2 signals of slighly different powers. I need some help on how to proceed in Matlab, here is our code:

LoRa_coding.m:

clear all; close all; clc;

BW = 125000; % 125kHz Bandwidth
Fs = 10^6;        % Sampling Frequency
inverse = 0;      % inverse = 1 for inverse chirps, inverse = 0 for normal chirps

% Case 1
SF = 7;
num_samples = Fs*(2^SF)/BW;  % Number of samples
[out_preamble1] = LoRa_Modulation(SF,BW,Fs,num_samples,0,inverse);

% Case 2
SF = 8;
num_samples = Fs*(2^SF)/BW;  % Number of samples
[out_preamble2] = LoRa_Modulation(SF,BW,Fs,num_samples,0,inverse);

% Case 3
SF = 9;
num_samples = Fs*(2^SF)/BW;  % Number of samples
[out_preamble3] = LoRa_Modulation(SF,BW,Fs,num_samples,0,inverse);

% Case 4
SF = 10;
num_samples = Fs*(2^SF)/BW;  % Number of samples
[out_preamble4] = LoRa_Modulation(SF,BW,Fs,num_samples,0,inverse);

% Case 5
SF = 11;
num_samples = Fs*(2^SF)/BW;  % Number of samples
[out_preamble5] = LoRa_Modulation(SF,BW,Fs,num_samples,0,inverse);

% Case 6
SF = 12;
num_samples = Fs*(2^SF)/BW;  % Number of samples
[out_preamble6] = LoRa_Modulation(SF,BW,Fs,num_samples,0,inverse);

outp = [out_preamble1 out_preamble2 out_preamble3 out_preamble4 out_preamble5 out_preamble6];
samples = length(out_preamble1)/4;
spectrogram(outp,samples,samples-1,samples*2,Fs,'yaxis');
title('Comparasion of LoRa Spreading Factors: SF 7 to SF 12');
grid on;
axis tight;


LoRa_decoding.m:

clear all; close all; clc;

%% Cockpit of the simulator

SF = 10;                     % Spreading Factor from 7 to 12
BW = 125000;                 % 125kHz
Fs = 125000;                 % Sampling Frequency
preamble_len = 8;            % Preamble length
sync_len = 2;                % Sync length
total_sym = 100;              % total symbols excluding preamble and sync from 1:100
num_samples = Fs*(2^SF)/BW;  % Number of samples
% 100 symbols to test the simulation :: choose 'total_sym'
symbols = [5,100,500,555,1000,200,300,567,100,50,400,222,666,444,777,555,111,999,525,455,345,456,34,678,234,672,123,67,382,588,200,300,1000,100,...
50,400,222,666,444,777,555,111,999,525,455,345,456,34,678,234,500,400,600,800,700,200,300,1000,100,50,400,222,666,444,777,555,111,999,...
525,455,345,456,34,678,234,672,123,67,382,588,200,300,1000,100,50,400,222,666,444,777,555,111,999,525,455,345,456,34,678,234];

lora_total_sym = preamble_len + sync_len + total_sym; % Total transmitted symbols

%% Preamble Generation
inverse = 0;
for i = 1:preamble_len
[out_preamble] = LoRa_Modulation(SF,BW,Fs,num_samples,0,inverse);
outp((i-1)*num_samples+1 : i*num_samples) = out_preamble;
end

%% Sync Symble Generation
inverse = 1;
for i = 1:sync_len
[out_sync] = LoRa_Modulation(SF,BW,Fs,num_samples,32,inverse);
outp = [outp out_sync];
end

%% Symble Generation
inverse = 0;
for i = 1:total_sym
[out_sym] = LoRa_Modulation(SF,BW,Fs,num_samples,symbols(i),inverse);
outp = [outp out_sym];
end

%% Reverse chirp generation for receiver
inverse = 1;

[out_reverse] = LoRa_Modulation(SF,BW,Fs,num_samples,0,inverse);
% Multiplying with the reverse chirp
for n = 1:1:lora_total_sym
decoded_out((n-1)*num_samples + 1 : n*num_samples) = (outp((n-1)*num_samples + 1 : n*num_samples).*out_reverse);
end

%% Calculating FFT
for m = 1:1:lora_total_sym
FFT_out(m,:) = abs((fft(decoded_out((m-1)*num_samples + 1 : m*num_samples))));
end

k=1;
for m = preamble_len+sync_len+1:1:lora_total_sym
[r,c] = max(FFT_out(m,:));
k = k+1;
end

%% Plotting

% Plotting the Spectrogram of Transmitted signal
figure(1);
samples = num_samples/4;
title('Decoded LoRa symbols');
spectrogram(decoded_out,samples,samples-1,samples,Fs,'yaxis');

figure(2);
samp_time = 0:1:num_samples-1;
for m = 1:1:lora_total_sym
plot(samp_time,FFT_out(m,:)); hold on;
end
grid on;


LoRa_Modulation.m:

% Matlab function to Modulate LoRa symbols

function out_preamble = LoRa_Modulation(SF,BW,Fs,num_samples,symbol,inverse)

%initialization
phase = 0;
Frequency_Offset = (Fs/2) - (BW/2);

shift = symbol;
out_preamble = zeros(1,num_samples);

for k=1:num_samples

%output the complex signal
out_preamble(k) = cos(phase) + 1i*sin(phase);

% Frequency from cyclic shift
f = BW*shift/(2^SF);
if(inverse == 1)
f = BW - f;
end

%apply Frequency offset away from DC
f = f + Frequency_Offset;

% Increase the phase according to frequency
phase = phase + 2*pi*f/Fs;
if phase > pi
phase = phase - 2*pi;
end

%update cyclic shift
shift = shift + BW/Fs;
if shift >= (2^SF)
shift = shift - 2^SF;
end
end
end


This is what I get when I try to transmit 2 signals of 2 symbols, we can clearly see the 4 symbols and this is not what we want. To do so, I added two Lora modulations with differents symbols like this:

outp = LoRa_Modulation(SF,BW,Fs,num_samples,symbols1(i),inverse) + LoRa_Modulation(SF,BW,Fs,num_samples,symbols2(i),inverse) 