%%%%%%%%%%%%%%%%%%%%%%%%%%%%%55
% file: demo_addNoise.m
% author: Jakob Möderl
% date: 03.11.2023
% description:
%   demo_addNoise is an example script how to use the provided addNoise.m
%   function to get noise samples of the dataset


% Load breathing data to get the median energy
load('UWBCarGraz_6.5GHz-0.5GHz_breathing.mat');

% subtract mean over slow time
cir_tilde_breathing = getCIRTilde(cir);

% calculate median energy
% this is the reference value the noise variance is compared against to
% calculate the SNR
Es_breathing_median = median(sigEnergy(cir_tilde_breathing));



% fix random seed
rng(13);

% Pick a random sample from the breathing class
sample_idx = randi(size(cir_tilde_breathing, 3));


% SNRs (in dB) to plot
SNR_dB = [10,0,-10];


% Color ranges for plots (in dB)
color_lims = [25, 55];

% plot original sample
figure;
subplot(2, 2, 1);
imagesc(t(:, sample_idx), tau*c, 20*log10(abs(cir_tilde_breathing(:, :, sample_idx))), color_lims);
colormap(flipud(virdis_white));
title('Original Sample');
xlabel('t in s');
ylabel('\tau in ns');
cb = colorbar;
ylabel(cb, ['$$|\tilde{\textbf{R}}|$$ in dB'], 'Interpreter', 'LaTeX');

% loop over SNRs
for ii=1:numel(SNR_dB)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% add noise to the sample
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
noisySample = addNoise(cir_tilde_breathing(:, :, sample_idx), SNR_dB(ii), Es_breathing_median);

% you can also Add noise to all samples in parallel using
% cir_tilde_breathing_noise = addNoise(cir_tilde_breathing, SNR_dB(ii), Es_breathing_median);

% plot the noisy version of the sample
subplot(2, 2, ii+1);
imagesc(t(:, sample_idx), tau*c, 20*log10(abs(noisySample)), color_lims);
colormap(flipud(virdis_white));
title(sprintf('SNR=%ddB', SNR_dB(ii)));
xlabel('t in s');
ylabel('\tau in ns');
cb = colorbar;
ylabel(cb, ['$$|\tilde{\textbf{R}}|$$ in dB'], 'Interpreter', 'LaTeX');
end



