Strange problem in plotting
Show older comments
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
More Answers (1)
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)
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!