MATLAB: Strange problem in plotting

spectrum

I have solved this equation x=(4+cos(1000*pi*t+pi/3)).*(sin(500*pi*t+pi/4)) by hand and the amplitude comes to be 2 at 250 Hz. when i plot this in matlab from range -500 to 500, it matches my hand solved answer. But when i change the range from -1000 to 1000, it shows amplitude 1.8. Why it is so
Code for -500 to 500
Fs = 1000;
t = 0:1/Fs:1-(1/Fs);
x=(4+cos(1000*pi*t+pi/3)).*(sin(500*pi*t+pi/4));
xdft = (1/length(x))*fft(x);
freq = -500:(Fs/length(x)):500-(Fs/length(x));
plot(freq,abs(fftshift(xdft)))
xlabel('Freq(Hz)-------->')
ylabel('Amplitude')
Now code from -1000 to 1000
Fs = 2000;
t = 0:1/Fs:1-(1/Fs);
x=(4+cos(1000*pi*t+pi/3)).*(sin(500*pi*t+pi/4));
xdft = (1/length(x))*fft(x);
freq = -1000:(Fs/length(x)):1000-(Fs/length(x));
plot(freq,abs(fftshift(xdft)))
xlabel('Freq(Hz)-------->')
ylabel('Amplitude')
Why it is so

Best Answer

Are you sure you did your hand calculation correctly? I think the second result is correct.
The result you are seeing is due to aliasing. Your signal x has a 250Hz component of about 3.6 (= 1.8*2), and 750Hz component of exactly 0.5 (=0.25*2).
In the first plot, you have sampled the signal at 1000Hz, which means any component over half that (500Hz) will get picked up as a lower frequency signal. In this case the 750 Hz signal ends up getting picked up as a 750-500 = 250 Hz signal. So the peaks at 250Hz end up being 1.8+0.2 = 2, which is not correct.
In the second plot, you are sampling at 2000Hz, and you can see all frequencies up to 2000/2 = 1000Hz. In that case, the 750Hz component is correctly identified, and you get two frequency peaks, one at 250Hz with amplitude 1.8, and one at 750Hz with amplitude 0.2.