%Demo-script for DSP-Lab session 3: DFT

N = 512;    % number of samples
f = 0.02;   % normalized frequency; 1 ^= f_s
%f = 16/512;

% sine wave; rectangular window 
n = 0:(N-1);
x = sin(2*pi*f*n) * (2/N);  % create sine-wave and normalize level

figure(1), clf, plot(n,x,'.-')

X = fft(x);

figure(2), clf, plot(n,20*log10(abs(X)),'.-'), axis([0,N,-80,10])

% sine wave; Hamming window
w = hamming(N)';    % change windows here! --> type help hamming to see 
%more windows
%w = flattopwin(N)';    % change windows here!
%w = blackman(N)';

x_h = x .* w *(N/sum(w));   % apply window and normalize

figure(1), hold on, plot(n,x_h,'rx-')
legend('original signal','windowed')

X_h = fft(x_h);

figure(2), hold on, plot(n,20*log10(abs(X_h)),'rx-')
legend('original signal','windowed')
