clc;
clear all;

qam_sig = load('matlab\4QAM_signal.mat','m');
qam_sig = qam_sig.m;

qam_sig = awgn(qam_sig,100,'measured');

% Interne Abtastrate in Samples. 
% Wenn pro Symbol 100 Samples sind, und hier 25 eingestellt wird, dann wird
% somit 4 mal pro Symbol abgetastet.
sample_freq = 25;

% Sample start verzögerung
sample_offset = 0;

% Anzahl Samples pro Symbol
sample_per_symbol = 100;
sample_count = length(qam_sig);
symbol_count = sample_count / sample_per_symbol;

% - Frequenz einstellung
local_phaseshift = deg2rad(0);
local_freq = 2; % Rad / sec
rads_per_range = (2*pi) * local_freq; % Wie viele Umdrehungen

osc_range = rads_per_range/(sample_per_symbol):rads_per_range/(sample_per_symbol):rads_per_range * symbol_count;
local_sin = sin(osc_range+local_phaseshift);
local_cos = cos(osc_range+local_phaseshift);

% Multipliziere Sin und Cos mit QAM
I_mix = local_cos .* qam_sig;
Q_mix = local_sin .* qam_sig;

%Manuelle Sample-Schleife
sample_points = [];
for i=1:sample_freq:sample_count
    % gehe durch jedes x-te sample durch. Abtastrate durch sample_freq
    % bestimmt.
    sample_points = [sample_points i+sample_offset];
    %I_direct_mixed = qam_sig(i); 
    
end

%anzahl plots
plot_fig_1 = 4;
figure_1 = figure(1);

% Original QAM_Signal
subplot(plot_fig_1,1,1);
plot(1:sample_count,qam_sig);
title('QAM-Input Signal');
drawSampleLines(sample_count,sample_points);

% Lokale Sinus Cosinus
subplot(plot_fig_1,1,2);
plot(1:sample_count,local_sin,1:sample_count,local_cos);
title('Local Sin (blue) and Cos');

% I und Q mix
subplot(plot_fig_1,1,3);
plot(1:sample_count,I_mix,1:sample_count,Q_mix);
title('(Perfect) I_m_i_x (blue) and Q_m_i_x');


% Show Sample Points
tmp = subplot(plot_fig_1,1,4);
ylim([0,2]);
cla(tmp); % Subplot löschen, damit die linien verschwinden welche eventuell
%noch aus früheren plots vorhanden sind. Denn clear all löscht diese nicht!

%Variable um Position in der Phase zu speichern
phase_pos = 0;
I_direct = [];
I_x_points = [];
Q_direct = [];
Q_x_points = [];
old_sample = 0;
for i=1:1:sample_count
    % Prüfen ob bei aktueller Position ein Abtastung vorhanden ist!
    if ismember(i,sample_points) == true
        %Code für den ADC Interrupt!
        switch phase_pos
            case 0
                I_direct = [I_direct qam_sig(i)-old_sample];
                I_x_points = [I_x_points i];
            case 1
                Q_direct = [Q_direct qam_sig(i)-old_sample];
                Q_x_points = [Q_x_points i];
            case 2
                I_direct = [I_direct -qam_sig(i)+old_sample];
                I_x_points = [I_x_points i];
            case 3
                Q_direct = [Q_direct -qam_sig(i)+old_sample];
                Q_x_points = [Q_x_points i];
        end
        old_sample = qam_sig(i);
        phase_pos = phase_pos + 1;
        if phase_pos == 4
            phase_pos = 0;
        end
    end
end

plot(I_x_points,I_direct,Q_x_points,Q_direct);
drawSampleLines(sample_count,sample_points);
title('I (blue) and Q through sampling the QAM-Signal with 4x Symbolfrequency');

function drawSampleLines(sample_count,sample_points)
    for i=1:1:sample_count
        % Prüfen ob bei aktueller Position ein Abtastung vorhanden ist!
        if ismember(i,sample_points) == true
            line([i,i],[-2,2],'Color','magenta','LineStyle',':');
        end
    end
end


