I'm trying to calculate the spectrum magnitude and phase of a signal, but when I plot them I get pretty messy results:

Time domain and spectrum

The signal is displayed on the right, then I have the magnitude and phase of the fft in the centre and to the left accordingly.

I would have expected to see just few peaks in both the spectrums but instead there are a lot of peaks and it gets really messy.

This is my code:

T = mean(diff(t))
fs = 1/T


title ('Time domain');  
plot((0:9999)/10,abs(fft(sig))); % magnitude spectrum
xlabel('Frequency (Hz)')
plot((0:9999)/10,angle(fft(sig))); % phase spectrum
xlabel('Frequency (Hz)')

Am I missing something or is it supposed to be like this?

I tried already rounding off to zero values that are close to zero, but that did not change anything.


closed as unclear what you're asking by Marcus Müller, MBaz, lennon310, Stanley Pawlukiewicz, Matt L. Apr 9 at 10:23

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    $\begingroup$ We can't really tell without having access to your actual signal. In general this looks like two sine waves with some pink or brown noise added. Perfectly normal. What do you expect to be different here? $\endgroup$ – Hilmar Mar 24 at 12:41
  • $\begingroup$ since matlab hasn't had an FFT bug that I've ever heard of: this is your signal's phase and magnitude spectrum! It's just what it is, no matter whether you consider that messy or not. $\endgroup$ – Marcus Müller Mar 24 at 13:36
  • 1
    $\begingroup$ if you look at your fft magnitude, you might notice that the values are symmetric around the center. this implies that your data is real and you are plotting negative and positive frequencies. if so, phase often needs to be unwrap $\endgroup$ – Stanley Pawlukiewicz Mar 24 at 13:37

If you round off tiny magnitudes to zero (to remove numerical noise), you should also zero the matching phase result bins. The phase of noise is usually just random.


Not the answer you're looking for? Browse other questions tagged or ask your own question.