clear;
pkg load signal;

% To be included in an other file
function sig = modulate(msg, syms)
	nb_sample = columns(syms);
	sig  = zeros(1, nb_sample * length(msg));
	for i = 1:length(msg)
		sym_index = msg(i);
		l_index = 1 + (i-1) * nb_sample;
		h_index = i * nb_sample;
		sig(l_index:h_index) = syms(sym_index,:);
	end
endfunction

% Care Absolute Power 1 is SQRT(2)
function np = gennoise(power, len)
	np = (randn(1,len)+j*randn(1,len)) * sqrt(power);
endfunction

freq_step = 6.25; % 50Hz / 8
nb_freq = 8;

FS = 500;
nb_sample = 80; % Some information talk about 160ms symbol duration

% Generating the 8 frequencies
sym_array = zeros(nb_freq, nb_sample);
t = (1:nb_sample);

% TX Offset
tx_offset = 0;

% Generate a signal with power SQRT(2) by default
for i = 1:nb_freq
	fstart = 0 - (nb_freq-1) / 2;
	findex = (fstart + i) * freq_step;
	sym_freq = exp(j*2*pi*((findex+tx_offset)/FS)*t);
	sym_array(i,1:nb_sample) = sym_freq;
end

% FT8 is 79 symbol: 7 + 29 + 7 + 29 + 7
costas = [2 5 6 0 4 1 3];
costas = costas + 1; % Avoid 0 indexing
msg_len = 79;
data_block = 29;
msg = floor(rand(1, msg_len) * nb_freq + 1);

msg(1:7) = costas;
msg(37:43) = costas;
msg(73:79) = costas;

total_samples = msg_len * nb_sample;

signal = zeros(1,total_samples);
signal = modulate(msg, sym_array);

costas_mod = modulate(costas, sym_array);

% signal is now containing a fake frame of FT8
% needs to add noise power calculation
signoise = signal + gennoise(1, length(signal));

