% Spektrum von einem digitalen, unmoduliertem Signal: 
% (c) Elias K.

clear all;

%Define Signal Parameters
    sign_frequ = 1000;              % 1kHz Signal
    oversampl  = 10;                % Oversampling für höhere Auflösung im Spektrum, minimum 2
    stop_time = 1;                  % 1 s Signalbereich
    
    mod_frequ  = 17;                % Frequenz des Trägersignals

% Calculate further values
    sample_frequ = sign_frequ * oversampl;
    periods = stop_time * sign_frequ;
    n_points = periods * oversampl;

    time = [0:1/(n_points-1):1].';
    time = time * stop_time;

% Create Signal
    X = randi(2, periods, 1 ) * ones(1,oversampl) ; % ones(1,10) -> Oversampling;
    X = X/2; % Skalieren auf High = 1 und Low = 0.5
    X = X.';
    X = X(:); % 1D-Vektor aus dem 2D-Vektor machen
    
    mod_signal = sin(2*pi*time*mod_frequ);
    
    X = X.*mod_signal;

% Plot Signal raw
    figure();
    plot(time,X);
    title('Signal');
    xlabel('t (seconds)');
    ylabel('X(t)');
    ylim([-1.2 1.2]);

% Calculate and Plot FFT
    figure();
    Y = fft(X);
    % Y = fft(hanning(length(X)).*X);
    % Y = fft(hamming(length(X)).*X);

    P2 = abs(Y/n_points);
    P1 = P2(1:n_points/2+1);
    P1(2:end-1) = 2*P1(2:end-1);

    f = sample_frequ*(0:(n_points/2))/n_points;
    plot(f,P1) 
    title('Single-Sided Amplitude Spectrum of X(t)')
    xlabel('f (Hz)')
    ylabel('|P1(f)|')
    ylim([0 0.03]);

