% ventanas.m

close all
clear all

% parametros
N = 128;
%N = 512;
fs = 8000;
NFFT = 4*N; 
t = ((1:N)-1)/fs;
f1 = fs/4;
f  = ((0:NFFT/2)/NFFT)';

win1 = ones(N,1);
win2 = hanning(N);
win3 = blackman(N);
win4 = flattopwin(N);
win5 = hamming(N);

norm1 = 2/sum(win1);
norm2 = 2/sum(win2);
norm3 = 2/sum(win3);
norm4 = 2/sum(win4);
norm5 = 2/sum(win5);

x = sin(2*pi*f1.*t');

Xwin1 = norm1 * fft(x.*win1,NFFT);
Xwin2 = norm2 * fft(x.*win2,NFFT);
Xwin3 = norm3 * fft(x.*win3,NFFT);
Xwin4 = norm4 * fft(x.*win4,NFFT);
Xwin5 = norm5 * fft(x.*win5,NFFT);

figure
subplot(2,2,1)
plot(1:N,win1,'-k')
title('Ventana rectangular')
xlabel('Tiempo (muestras)')
ylabel('Amplitud')
ylim([0 1])
subplot(2,2,2)
plot(1:N,win2,'-k')
title('Ventana de Hanning')
xlabel('Tiempo (muestras)')
ylabel('Amplitud')
ylim([0 1])
subplot(2,2,3)
plot(f, 20*log10(abs(Xwin1(1:NFFT/2+1))), '-k')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
subplot(2,2,4)
plot(f, 20*log10(abs(Xwin2(1:NFFT/2+1))), '-k')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')

print -depsc 'rectangular_hanning.eps'

figure
%subplot(1,2,1)
subplot(2,1,1)
plot(f, 20*log10(abs(Xwin1(1:NFFT/2+1))), '-k')
hold on
plot(f, 20*log10(abs(Xwin2(1:NFFT/2+1))), '-r')
hold off
title('Rectangular vs Hanning - decaimiento lobulos secundarios')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
axis([0 0.5 -120 0])
legend('rectangular', 'hanning')
%subplot(1,2,2)
subplot(2,1,2)
plot(f, 20*log10(abs(Xwin1(1:NFFT/2+1))), '-k')
hold on
plot(f, 20*log10(abs(Xwin2(1:NFFT/2+1))), '-r')
hold off
title('Rectangular vs Hanning - ancho lobulo principal')
xlabel('Frecuencia')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
legend('rectangular', 'hanning')
axis([0.2 0.3 -80 0])

print -depsc 'rectangular_hanning_comparacion.eps'


figure
subplot(2,2,1)
plot(f, 20*log10(abs(Xwin1(1:NFFT/2+1))), '-k')
title('Ventana Rectangular')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
axis([0.15 0.35 -110 0])
subplot(2,2,2)
plot(f, 20*log10(abs(Xwin2(1:NFFT/2+1))), '-k')
title('Ventana de Hanning (o Hann)')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
axis([0.15 0.35 -110 0])
subplot(2,2,3)
plot(f, 20*log10(abs(Xwin3(1:NFFT/2+1))), '-k')
title('Ventana de Blackman')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
axis([0.15 0.35 -110 0])
subplot(2,2,4)
plot(f, 20*log10(abs(Xwin4(1:NFFT/2+1))), '-k')
title('Ventana flat-top')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
axis([0.15 0.35 -110 0])


print -depsc 'rectangular_hanning_blackman_flattop.eps'

figure
%subplot(1,2,1)
subplot(2,1,1)
plot(f, 20*log10(abs(Xwin2(1:NFFT/2+1))), '-k')
hold on
plot(f, 20*log10(abs(Xwin5(1:NFFT/2+1))), '-r')
hold off
title('Hanning vs Hamming - decaimiento lobulos secundarios')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
axis([0 0.5 -120 0])
legend('hanning', 'hamming')
%subplot(1,2,2)
subplot(2,1,2)
plot(f, 20*log10(abs(Xwin2(1:NFFT/2+1))), '-k')
hold on
plot(f, 20*log10(abs(Xwin5(1:NFFT/2+1))), '-r')
hold off
title('Hanning vs Hamming - ancho lobulo principal')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
legend('hanning', 'hamming')
axis([0.2 0.3 -80 0])

print -depsc 'hamming_hanning_comparacion.eps'


% frecuencias
f1 = 2050;
f2 = 2158;

x = sin(2*pi*f1.*t') + sin(2*pi*f2.*t'); 
x = x/2;

Xwin2 = norm2 * fft(x.*win2,NFFT);
Xwin5 = norm5 * fft(x.*win5,NFFT);

figure
subplot(1,2,1)
%plot(f, 20*log10(abs(Xwin2(1:NFFT/2+1))), '-k')
plot(f, abs(Xwin2(1:NFFT/2+1)), '-k')
hold on
plot(f, abs(Xwin5(1:NFFT/2+1)), '-r')
hold off
title('Hanning vs Hamming - frecuencias muy proximas')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
legend('Hanning', 'Hamming')
%axis([0.23 0.3 -20 0])
%subplot(1,2,2)
subplot(1,2,2)
plot(f, 20*log10(abs(Xwin2(1:NFFT/2+1))), '-k')
hold on
plot(f, 20*log10(abs(Xwin5(1:NFFT/2+1))), '-r')
hold off
%plot(f, abs(Xwin5(1:NFFT/2+1)), '-k')
title('Hanning vs Hamming - frecuencias muy proximas')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
axis([0.23 0.3 -20 0])
legend('Hanning', 'Hamming')

print -depsc 'hamming_hanning_frecuencias_proximas.eps'

figure
plot(1:N,win2,'-k',1:N,win3,'-g',1:N,win4,'-b',1:N,win5,'-r')
title('Ventanas en el tiempo')
xlabel('Tiempo (muestras)')
ylabel('Amplitud')
%ylim([0 1])
axis tight
legend('hanning', 'blackman', 'flat-top', 'hamming')

print -depsc 'ventanas_en_el_tiempo.eps'

%% sinusoides cercanas - espectro con cada ventana

% frecuencias
f1 = 0.20*fs/2;
f2 = 0.30*fs/2;

x = 0.025 * sin(2*pi*f1.*t') + sin(2*pi*f2.*t'); 
%x = x/2;

Xwin1 = norm1 * fft(x.*win1,NFFT);
Xwin2 = norm2 * fft(x.*win2,NFFT);
Xwin3 = norm3 * fft(x.*win3,NFFT);
Xwin4 = norm4 * fft(x.*win4,NFFT);

figure
subplot(2,2,1)
plot(f, 20*log10(abs(Xwin1(1:NFFT/2+1))), '-k')
title('Ventana Rectangular')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
axis tight
%axis([0.15 0.35 -110 0])
subplot(2,2,2)
plot(f, 20*log10(abs(Xwin2(1:NFFT/2+1))), '-k')
title('Ventana de Hanning (o Hann)')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
%axis([0.15 0.35 -110 0])
axis tight
subplot(2,2,3)
plot(f, 20*log10(abs(Xwin3(1:NFFT/2+1))), '-k')
title('Ventana de Blackman')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
%axis([0.15 0.35 -110 0])
axis tight
subplot(2,2,4)
plot(f, 20*log10(abs(Xwin4(1:NFFT/2+1))), '-k')
title('Ventana flat-top')
xlabel('Frecuencia')
ylabel('Magnitud (dB)')
%axis([0.15 0.35 -110 0])
axis tight

print -depsc 'rectangular_hanning_blackman_flattop_frecuencias_proximas.eps'
