% Listing 16.6 for Figure 16.6 of the book 
% "Principles of System Identification: Theory and Practice"

% Arun K. Tangirala
% December 06, 2015
% Freely distributed under no warranty

%% Generate signal
kvec2 = (0:108)'; f0 = 0.04;
xk2 = sin(2*pi*f0*kvec2);
N2 = length(xk2); fvec2 = (0:1/N2:0.5-1/N2);

%% Apply Hann and Blackman windows
win_fun1 = window(@hanning,N2); xk2w1 = win_fun1.*xk2;
cg_win1 = sum(win_fun1); ssqwin1 = sum(win_fun1.^2);
win_fun2 = window(@blackman,N2); xk2w2 = win_fun2.*xk2;
cg_win2 = sum(win_fun2); ssqwin2 = sum(win_fun2.^2);

%% DFT
xk2w1f = fft(xk2w1-mean(xk2w1));
xk2w2f = fft(xk2w2-mean(xk2w2));

%% Plot spectrum
figure; stem(fvec2,abs(xk2w1f(1:floor(end/2))).^2/ssqwin1);
figure; stem(fvec2,abs(xk2w2f(1:floor(end/2))).^2/ssqwin2);