We not know the size of your frame and your sampe rate, a quick look in your frequency domain code say me that are you getting 256 points from FFT, this may not be sufficient for accurate results, try split your signal in bigger frames to pass to your FFT !
For time domain the size of your frame is important too !
Here one simple autocorrelation example:
f = 500;
Fs=8000
frame= 0.9*sin(2*pi*f/Fs*(1:4096));
X = xcorr(frame,160,'coeff');
X=X(160+1:2*160+1);
[Xmax,i]=max(X(8:end));
%Period
P = (8+i-2)
%Pitch Test, this result need be near or equal f
Fs/P
trying find pitches between 50 and 1000 hertz, I just use a pure sinusoid with frame size = 4096 to show you how can be done in time domain!