function [ Gn] = spa_b( u_all, y_all, window_size)
%SPA_B - spectral analys - Bartlett method
%   [INPUT]
%   u_all - input vector to system
%   y_all - output vector from system
%   window_size - size of window
%   [OUTPUT]
%   Gn - transfer of estimated system in complex variables

len_main = window_size*2 - 1;   
if window_size > length(u_all)
    window_size = length(u_all);
end
num_repeat = floor(length(u_all)/window_size);

Phi_uy = complex(zeros(1,len_main));
Phi_uu = complex(zeros(1,len_main));
Ruu = zeros(1,len_main);
Ruy = zeros(1,len_main);

% Bartlett window

% V.1 - less filtring
W = bartlett(len_main)';

% V.2 - more filtring
% W = zeros(1,len_main);
% W(ceil(length(W)*(1/3)) : ceil(length(W)*(2/3))) = bartlett(ceil(length(W)*(2/3)) - ceil(length(W)*(1/3)) + 1)'; 


for j=1:num_repeat,
   % Sample form all values - number of values = window size
   u = u_all((j-1)*window_size+1:j*window_size);                              
   y = y_all((j-1)*window_size+1:j*window_size); 

   % for positive way of correlation and autocorrelation
   for k=1:window_size, Ruu(k + window_size - 1)=1/window_size * u(1:end-(k-1)) * u(k:end)';end   % Autocorrelation of input
   for k=1:window_size, Ruy(k + window_size - 1)=1/window_size * u(1:end-(k-1)) * y(k:end)';end   % Correlation of output and input
   
   % for negative way of correlation and autocorrelation
   for k=1:(window_size-1), Ruu(window_size - k)=1/window_size * u((k+1):end) * u(1:end - k)';end   % Autocorrelation of input
   for k=1:(window_size-1), Ruy(window_size - k)=1/window_size * u((k+1):end) * y(1:end - k)';end   % Correlation of output and input

   % fft
   Phi_uy = Phi_uy + fft(Ruy.*W); 
   Phi_uu = Phi_uu + fft(Ruu.*W);
end

Gn = Phi_uy ./ Phi_uu;  % Transfer
end