I'm trying to write a simple script that should plot the spectrum in scilab, to test it I use a sinus function with 440hz so that I get my dirac in this position , my problem is that it doesn't work and I don't understand why? here's the code :

Fs = 8000;
f = 440;
t= 0:1/Fs:1;
y = sin(2*%pi*f*t);

nf = 1024; // number of point in the DFT
Y = fft(y)
f = Fs/2  * linspace(0,1,nf/2+1);

and this is what I get : image

any idea why I get this ?


Your fft vector Y has the same length as your input signal y, because you just specified nf without letting fft() know what your desired FFT-length is. This is why your peak does not appear at 440 Hz. Your vector Y does not correspond to the frequencies in f. It's just a matter of correct scaling of the x-axis.

EDIT: I do not know scilab, so I don't know how to pass the desired FFT-length to fft(). If you can't do that, you just need to make your time domain signal have the desired FFT-length.

  • $\begingroup$ where does the 4000 come from? and what about the magnitude ? $\endgroup$
    – Engine
    Mar 4 '14 at 11:02
  • $\begingroup$ Please edit your question to make clear what it is that is unexpected for you. I thought you were talking about the location of the peak, which is not at 440 Hz. This is what I tried to explain in my answer. $\endgroup$
    – Matt L.
    Mar 4 '14 at 11:05
  • $\begingroup$ indeed ,but I though you could help me with that one ! $\endgroup$
    – Engine
    Mar 4 '14 at 11:07
  • $\begingroup$ Sure, but what do you mean? The 4000 on the x-axis or on the y-axis? And what are your expectations concerning the magnitude (and why)? $\endgroup$
    – Matt L.
    Mar 4 '14 at 11:12
  • 1
    $\begingroup$ No, the FFT computes the discrete Fourier transform (DFT), and in this case you get an impulse with height $N/2$ (where $N$ is the FFT length). However, you only get a discrete impulse in the frequency domain if your signal frequency lies on the grid of FFT frequencies (i.e. $N$ is an integer multiple of the period of the time signal). Hope this helps. $\endgroup$
    – Matt L.
    Mar 4 '14 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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