% Lösung DFT

fs  = 1000;
k   = -5:1:7;
x_k = [2, 1, 0, 1, 2, 3, 2, 1, 0, 1, 2, 3, 2];
N   = length(x_k);

subplot(2,1,1);
stem(k, x_k);
xlabel('k'); ylabel('x(k)'); grid on;

Xcomp = zeros(1,13);
Xmag  = zeros(1,13);

for m=0:1:12
    for n=1:1:13
        Xcomp(m+1) = Xcomp(m+1) + x_k(n) * (cos(2*pi*n*m/N) - i*sin(2*pi*n*m/N));
    end
    
    Xmag(m+1) = abs(Xcomp(m+1));
end

f = (0:1:(N-1))/N*fs;

subplot(2,1,2);
stem(f, Xmag./N);
xlabel('f (Hz)'); ylabel('Xmag'); grid on;