clear all
close all
clc

dt = 1e-4;
t = 0 : dt : 100-dt; % sec
M = length(t);
f = 500; % Hz
x = 1.5*cos(2*pi*f*t);

figure
plot(t(1:100)*1e3,x(1:100), "linewidth", 2)
xlabel('Χρόνος (msec)');
ylabel('Πλάτος (V)');
set(gca, "fontsize", 16);
hold on
h = quiver([2;4],[1.45;1.45], [2;-2],[0;0],0, "linewidth",1, "color", "red");
set (h, "maxheadsize", 0.06);
text(3,1.25, "T = 2 msec\nF = 500 Hz", "fontsize", 16, "horizontalalignment", "center");
hold off
axis([0 10 -1.5 1.5]);
grid on

print -dpng "-S450,150" sin.png

dF = 1/(M*dt);
Fmin = -0.5 / dt;
Fmax = 0.5 / dt - dF ;

X = fft(x);

figure
Fp = linspace(0, Fmax, M/2);
Fm = linspace(Fmin, -dF, M/2);
F = [Fp Fm];

subplot(2, 1, 1);
plot([1:M]*dF-5000, X/1e3);
%plot([1:M], X);
Fx = text([-5000:500:4999], -5 * ones(1,20), ...
    {'0' '0.5' '1' '1.5' '2' '2.5' '3' '3.5' '4' '4.5' ...
      '-5' '-4.5' '-4' '-3.5' '-3' '-2.5' '-2' '-1.5' '-1' '-0.5'});
set(Fx, 'HorizontalAlignment','center',...
      'VerticalAlignment','middle', "fontsize", 16)
xlabel('Συχνότητα (kHz)');
ylabel('Φάσμα (kV/Hz)');
text(0, 60, "Ανεστραμμένο Φάσμα\n(χωρίς fftshift)", 'color', [1 0 0], "fontsize", 16, 'HorizontalAlignment', 'center');
set(gca, "fontsize", 16);
grid on
% Remove the default labels
set(gca,'XTickLabel','')
set (gca, "xaxislocation", "origin")
axis([1*dF-5000 M*dF-5000 0 80]);

X = fftshift(X);
F = linspace(Fmin, Fmax, M);

subplot(2, 1, 2);
plot(F/1e3, abs(X)/1e3)
xlabel('Συχνότητα (kHz)');
ylabel('Φάσμα (kV/Hz)');
text(-3, 60, "Κανονικό Φάσμα\n(με fftshift)", 'color', [1 0 0], "fontsize", 16, 'HorizontalAlignment', 'center');
set(gca, "fontsize", 16, 'xtick', (Fmin: 500: Fmax)/1e3);
grid on
set (gca, "xaxislocation", "origin")
axis([(1*dF-5000)/1e3 (M*dF-5000)/1e3 0 80]);

print -dpng "-S450,250" sinSpec.png