MATLAB: Fftshift isn’t workig

fftMATLAB

Hi,
I successfully got two deltas on 4/10 and on 2/10, but I cannot center the graph.
Could you help me?
t = 0:(1/fs):(10-1/fs);
S = 2*cos(3*pi*t/10).*cos(pi*t/10); %cos(4pit/10)+cos(2pit/10)
n = length(S);
X = fft(S);
f = (0:n-1)*(fs/n)*2; %multiply by pi*2, leave the pi aside
f80 = (0:n-1);
power = abs(X).^2/n;
stem(f,power)
Y = fftshift(X)
fshift = (-n/2:n/2-1)*(fs/n)
powershift = abs(Y).^2/n;
plot(fshift,powershift)
I attached pictures. In the second graph those are not even deltas.

Best Answer

I found the solution, I didn't post the frequency of sampling. But apparently fftshift just swaps X axis and then if there were no 0 between samples they get joined. So I had to add zeroes to the fft result in order for the fftshift to work properly, this is by extending the sampled rate of the original function. (Making sampling frequency higher extends repeating time in frequency domain vice versa making sampled range larger adds samples to the frequency domain in between the existing samples.)
Related Question