%zasada konstrukcji okien hanninga/blackmana 
clear all; close all

N = 7;
n = -N:N;

NF=5*(2*N+1);

hw = ones(size(n));                 %okno prostokatna

    
dw = 2*pi/(2*N+1)/30;               %krok czestotliwosci - tak dobrany aby "ladnie" pokazac zera widm R, RL, RR
wk = -pi: dw: pi;                    %wektor czestotliwosci
R = []; RL = []; RR = [];
for k=1:length(wk)
    w = wk(k);
    R0(k) = sin((2*N+1)*w/2)/sin(w/2);
	R(k)  = sum(hw.*exp(-j*(w+0)*n));
    RL(k) = sum(hw.*exp(-j*(w+2*pi/(2*N+1))*n));
    RR(k) = sum(hw.*exp(-j*(w-2*pi/(2*N+1))*n));
end
R = real(R); RL = real(RL); RR = real(RR); %widma tych okien s� rzeczywiste, ale dokladno�� oblicze�
    
%    
figure; set(gcf,'DefaultLineLineWidth',2.0);
set(gca,'FontSize',14,'LineWidth',1.0); % opis osi: etykiety + linia ramki
plot(wk/pi, R, 'k', wk/pi, RL, 'g', wk/pi, RR, 'r'); title('widma oknien prost.'); 
legend('R', 'RL', 'RR');
xlabel('$\omega/\pi$','Interpreter','latex','Fontsize',14);    grid on; 
k0 = -floor((2*N+1)/2) : floor((2*N+1)/2);
hold on; plot(2*k0/(2*N+1), zeros(size(k0)), 'or');
    
%    
figure; set(gcf,'DefaultLineLineWidth',2.0);
set(gca,'FontSize',14,'LineWidth',2.0); % opis osi: etykiety + linia ramki
plot(wk/pi, R+0.5*(RL+RR)); title('suma widm: R+0.5*(RL+RR)');
xlabel('$\omega/\pi$','Interpreter','latex','Fontsize',14);    grid on; 
hold on; plot(2*k0/(2*N+1), zeros(size(k0)), 'or');

%Hamming    
%hw = hw + 0.5 * (hL+hR) 
hr = ones(2*N+1, 1);
hn = 0.5*ones(size(n)) + 0.5*cos(2*pi/(2*N+1).*n);          %Haning
hm = 0.54*ones(size(n)) + 0.46*cos(2*pi/(2*N+1).*n);        %Hamming
hb = 0.42+0.5*cos(2*pi/(2*N+1)*n)+0.08*cos(4*pi/(2*N+1)*n); %Blackman
    
figure; set(gcf,'DefaultLineLineWidth',2.0);
n = -N:N;
plot(n, hr, n, hn, n, hm, n, hb); title('okno czasowe'); 
xlabel('n');    grid on; 
legend('Rect', 'Hanning', 'Hamming', 'Blackman'); grid on; 
    
    
    

for k=1:length(wk)
    w = wk(k);
	Hn(k)  = sum(hn.*exp(-j*w*n));
    Hm(k)  = sum(hm.*exp(-j*w*n));
    Hb(k)  = sum(hb.*exp(-j*w*n));
end
Hn = real(Hn); Hm = real(Hm);  Hb = real(Hb); 
    
R = R/max(R); Hn = Hn/max(Hn); Hm = Hm/max(Hm); Hb = Hb/max(Hb);
    
figure; set(gcf,'DefaultLineLineWidth',2.0);
plot(wk/pi, R, wk/pi, Hn, wk/pi, Hm, wk/pi, Hb); title('widmo okna'); 
xlabel('$\omega/\pi$','Interpreter','latex','Fontsize',14);    grid on; 
legend('Rect', 'Hanning', 'Hamming', 'Blackman'); grid on; 
hold on; plot(2*pi*k0/(2*N+1)/pi, zeros(size(k0)), 'or');
    
    
        

RdB  = 20*log10(abs(R));
HndB = 20*log10(abs(Hn));
HbdB = 20*log10(abs(Hb));
figure; set(gcf,'DefaultLineLineWidth',2.0);
plot(wk/pi, RdB, wk/pi, HndB, wk/pi, HbdB); 
axis([-1, 1, -70, 0]); title('modul widma okna [dB]'); 
xlabel('$\omega/\pi$','Interpreter','latex','Fontsize',14);    grid on; 
ylabel('dB');
legend('Rect', 'Hanning', 'Blackman'); grid on; 
%hold on; plot(2*pi*k0/(2*N+1), zeros(size(k0)), 'or');            
    


