% senos.m - DFT

close all
clear all

%%%%%%%%%%%%%%%%%%%%% EJEMPLO 1 %%%%%%%%%%%%%%%%%%
% parametros
N1 = 128;
N2 = 512;

fs = 8000;
M = 100;
T = N2*M;

% vector de tiempo
t = ((1:T)-1)/fs;
% ventanas
win1 = hanning(N1);
win2 = hanning(N2);

% frecuencias
f1 = 2000;
f2 = 2150;

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

X1 = 4/N1 * fft(x(1:N1).*win1); 
X2 = 4/N2 * fft(x(1:N2).*win2); 

figure;
subplot(121)
%plot(0:1/N1:0.5,abs(X1(1:N1/2+1)),'-sk','LineWidth',2)
plot(0:1/N1:0.5,abs(X1(1:N1/2+1)),'-sk')
title(['DFT para N = ' num2str(N1) ' muestras'])
xlabel('Frecuencia')
ylabel('Magnitud')
axis([0 0.5 0 1])
subplot(122)
%plot(0:1/N2:0.5,abs(X2(1:N2/2+1)),'-sk','LineWidth',2)
plot(0:1/N2:0.5,abs(X2(1:N2/2+1)),'-sk')
title(['DFT para N = ' num2str(N2) ' muestras'])
xlabel('Frecuencia')
ylabel('Magnitud')
axis([0 0.5 0 1])

print -depsc 'senos_diferente_N.eps'

%%%%%%%%%%%%%%%%%%%%% EJEMPLO 2 %%%%%%%%%%%%%%%%%%
N = 512;
fs = 8000;

% frecuencias
f1 = 1000;
f2 = 2100;

% ventana
win = hanning(N);

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

X1 = 2/N * fft(x(1:N)); 
X2 = 4/N * fft(x(1:N).*win2); 

NP = N/4;
figure;
subplot(211)
%plot(t(1:NP), x1(1:NP), '-sk','LineWidth',2)
plot(t(1:NP), x1(1:NP), '-sk')
title('Sinusoide coincidente con base')
%xlabel('Tiempo (s)')
ylabel('Amplitud')
axis tight
subplot(212)
%plot(t(1:NP), x2(1:NP), '-sk','LineWidth',2)
plot(t(1:NP), x2(1:NP), '-sk')
title('Sinusoide no coincidente con base')
xlabel('Tiempo (s)')
ylabel('Amplitud')
axis tight

print -depsc 'senos_base_y_no.eps'

figure;
subplot(121)
%plot(0:1/N:0.5,abs(X1(1:N/2+1)),'-sk','LineWidth',2)
plot(0:1/N:0.5,abs(X1(1:N/2+1)),'-sk')
title(['DFT ventana Rectangular'])
xlabel('Frecuencia')
ylabel('Magnitud')
axis([0 0.5 0 1.01])
text(f1/fs+0.01, 0.95, 'coincidente')
text(f2/fs+0.01, 0.70, 'no coincidente')
subplot(122)
%plot(0:1/N:0.5,abs(X2(1:N/2+1)),'-sk','LineWidth',2)
plot(0:1/N:0.5,abs(X2(1:N/2+1)),'-sk')
title(['DFT ventana de Hanning'])
xlabel('Frecuencia')
ylabel('Magnitud')
axis([0 0.5 0 1.01])
text(f1/fs+0.01, 0.95, 'coincidente')
text(f2/fs+0.01, 0.70, 'no coincidente')

print -depsc 'senos_diferente_ventana.eps'


figure
subplot(2,2,3)
%plot(t(1:N), x(1:N), '-sk','LineWidth',2)
plot(t(1:N), x(1:N), '-k')
title('Ventana Rectangular')
xlabel('Tiempo (s)')
ylabel('Amplitud')
axis tight
subplot(2,2,4)
%plot(t(1:N), x(1:N).*win2, '-sk','LineWidth',2)
plot(t(1:N), x(1:N).*win2, '-k')
title('Ventana de Hanning')
xlabel('Tiempo (s)')
ylabel('Amplitud')
axis tight

print -depsc 'enventanado.eps'

%%%%%%%%%%%%%%%%%%%%% EJEMPLO 3 %%%%%%%%%%%%%%%%%%
NZ = 4*N; % zero padding
X1   = 2/N * fft(x(1:N)); 
X1zp = 2/N * fft(x(1:N),NZ); 
X2zp = 4/N * fft(x(1:N).*win2,NZ); 

x_zero_padded = [x(1:N).*win2; zeros(NZ,1)];

figure;
subplot(211)
plot(t(1:(N+NZ)), x_zero_padded/(max(abs(x_zero_padded))), '-k')
title('Relleno de ceros')
xlabel('Tiempo (s)')
ylabel('Amplitud')
axis tight

print -depsc 'zero_padding.eps'

figure;
subplot(211)
plot(0:1/N:0.5,abs(X1(1:N/2+1)),'-k')
title(['DFT ventana Rectangular'])
xlabel('Frecuencia')
ylabel('Magnitud')
axis([0 0.5 0 1.01])
text(f1/fs+0.01, 0.95, 'coincidente')
text(f2/fs+0.01, 0.70, 'no coincidente')
subplot(212)
plot(0:1/NZ:0.5,abs(X1zp(1:NZ/2+1)),'-k')
title(['DFT ventana Rectangular (agregado de ceros)'])
xlabel('Frecuencia')
ylabel('Magnitud')
axis([0 0.5 0 1.01])
text(f1/fs+0.01, 0.95, 'coincidente')
text(f2/fs+0.01, 0.70, 'no coincidente')


% subplot(122)
% %plot(0:1/NZ:0.5,abs(X2zp(1:NZ/2+1)),'-sk','LineWidth',2)
% plot(0:1/NZ:0.5,abs(X2zp(1:NZ/2+1)),'-k')
% title(['DFT ventana de Hanning (agregado de ceros)'])
% xlabel('Frecuencia')
% ylabel('Magnitud')
% axis([0 0.5 0 1.01])
% text(f1/fs+0.01, 0.95, 'coincidente')
% text(f2/fs+0.01, 0.70, 'no coincidente')

print -depsc 'senos_zero_padding.eps'

%%%%%%%%%%%%%%%%%%%%% EJEMPLO 4 %%%%%%%%%%%%%%%%%%
N = 512;
fs = 8000;

% frecuencias
f1 = 1000;
f2 = 1001;

% ventana
win = hanning(N);

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

X1 = 2/N * fft(x(1:N)); 
X2 = 4/N * fft(x(1:N).*win2); 

NP = N/4;
figure;
subplot(211)
%plot(t(1:NP), x1(1:NP), '-sk','LineWidth',2)
plot(t(1:NP), x1(1:NP), '-sk')
title('Sinusoides de frecuencia muy cercana')
%xlabel('Tiempo (s)')
ylabel('Amplitud')
axis tight
subplot(212)
%plot(t(1:NP), x2(1:NP), '-sk','LineWidth',2)
plot(t(1:NP), x2(1:NP), '-sk')
xlabel('Tiempo (s)')
ylabel('Amplitud')
axis tight

print -depsc 'senos_frecuencia_cercana.eps'
