Clear Filters
Clear Filters

Strange problem in plotting

1 view (last 30 days)
moonman
moonman on 2 Oct 2011
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

Accepted Answer

Teja Muppirala
Teja Muppirala on 2 Oct 2011
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.
  1 Comment
moonman
moonman on 2 Oct 2011
yes ur last para identifies problem
It means i have made some mistake

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 2 Oct 2011
length(x) must be the same as length(t), and algebraically length(t) should be the same as Fs. I would, though, double check that that is what actually happens considering round-off in the colon function: length(t) could plausibly end up one sample short. linspace() is more reliable than colon for such purposes:
t = linspace(0,1,Fs+1);
t(end) = [];
With length(x) theoretically the same as Fs, then computing Fs/length(x) should give you 1, so it is not clear why you do not just use a step size of 1 ? If the colon operator does in fact sometimes drop the last sample due to round off, then that does not change the fact that the existing samples are 1/Fs apart, rather than 1/(Fs-1)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!