clear all
close all

%influence n
[freq1, Fplus1] = FFTsample(1000, 64);
[freq2, Fplus2] = FFTsample(1000, 128);
[freq3, Fplus3] = FFTsample(1000, 256);
figure
plot(freq1, Fplus1, freq2, Fplus2, freq3, Fplus3);
axis tight; grid on;
xlabel('Hz');
legend('n=64', 'n=128', 'n=256');

%influence Fs
[freq1, Fplus1] = FFTsample(1000, 64);
[freq2, Fplus2] = FFTsample(500, 64);
[freq3, Fplus3] = FFTsample(2000, 64);
figure
plot(freq1, Fplus1, freq2, Fplus2, freq3, Fplus3);
axis tight; grid on;
xlabel('Hz');
legend('Fs=1000', 'Fs=500', 'Fs=2000');

%influence if Fs, n for the same signal part
[freq1, Fplus1] = FFTsample(1000, 64);
[freq2, Fplus2] = FFTsample(500, 34);
[freq3, Fplus3] = FFTsample(2000, 128);
figure
plot(freq1, Fplus1, freq2, Fplus2, freq3, Fplus3);
axis tight; grid on;
xlabel('Hz');
legend('Fs=1000, n=64', 'Fs=500, n=32', 'Fs=2000, n=128');



function [freq, Fplus] = FFTsample(Fs, n)

%signal
dt=1/Fs;
t=(0:n-1)*dt;
f0=50;
f=sin(2*pi*f0*t);

%fft
F = fft(f);

%manual shifting procedure
Fplus = abs(F(1:end/2+1));
Fplus(2:end) = 2*Fplus(2:end); %1st point without *2 !!!
Fplus = Fplus/n;
freq = Fs*(0:n/2)/n;

end