clear;clc;close all

set(0,'defaulttextinterpreter','Latex');

%fs = 30 pre Obr.2.5 a 2.8
%fs = 15 pre Obr.2.7 a 2.8

fs =30;
fa =13;
t = 1/fs;
x=0:0.001:2;
x2 = 0:t:2;
y2 = sin(2*pi*fa*x2);
y = sin(2*pi*fa*x);

figure(1) %Obr 2.5
plot(x,y,'k','LineWidth',2);
hold on
plot(x2,y2,'ok','MarkerSize',10,'MarkerFaceColor','black')
xlim([-0.1 2.1])
ylim([-1.1 1.1])
xlabel('t[s]');
set(gca,'FontSize',25)

N = length(y2);
N2 = 2*2^nextpow2(N);

ff = fft(y2,N2);
fff = ff(1:N2/2);
fff = fff/max(fff);

xff=fs*(0:N2/2-1)/N2;
figure(2) %Obr 2.6
plot(xff,abs(fff),'k','LineWidth',2)
xlabel('f[Hz]');
ylh = get(gca,'ylabel');
gyl = get(ylh);                                                        
ylp = get(ylh, 'Position');
set(ylh, 'Rotation',0, 'Position',ylp, 'VerticalAlignment','middle', 'HorizontalAlignment','right')
ylabel('$\left|X_k\right|$');
set(gca,'FontSize',25)
xlim([xff(1) xff(end)])


fs =15;
fa =13;
t = 1/fs;
x=0:0.001:2;
x2 = 0:t:2;
y2 = sin(2*pi*fa*x2);
y = sin(2*pi*fa*x);

figure(3) %Obr 2.7
plot(x,y,'k','LineWidth',2);
hold on
plot(x2,y2,'ok','MarkerSize',10,'MarkerFaceColor','black')
xlim([-0.1 2.1])
ylim([-1.1 1.1])
xlabel('t[s]');
set(gca,'FontSize',25)

N = length(y2);
N2 = 2*2^nextpow2(N);

ff = fft(y2,N2);
fff = ff(1:N2/2);
fff = fff/max(fff);

xff=fs*(0:N2/2-1)/N2;
figure(4) %Obr 2.8
plot(xff,abs(fff),'k','LineWidth',2)
xlabel('f[Hz]');
ylh = get(gca,'ylabel');
gyl = get(ylh);                                                        
ylp = get(ylh, 'Position');
set(ylh, 'Rotation',0, 'Position',ylp, 'VerticalAlignment','middle', 'HorizontalAlignment','right')
ylabel('$\left|X_k\right|$');
set(gca,'FontSize',25)
xlim([xff(1) xff(end)])