0

with this code i am only getting half og the fft spectrum from 0 to positive infinity . i am trying to mirror this along the y axis to get the other half which is symmetric to this one from 0 to negative infinity.

    Fs = 1000;   %sampling rate
    Ts = 1/Fs; %sampling time interval
    t = -10:Ts:10-Ts; %sampling period
    n = length(t); %number of samples
    y = heaviside(t)-heaviside(t-4); %the step curve

    matlabFFT = figure;  %create a new figure
    YfreqDomain = fft(y); %take the fft of our step funcion, y(t)
    y=abs(YfreqDomain);
    plot(y)
    xlabel('Sample Number')
    ylabel('Amplitude')
    title('Using the Matlab fft command')
    grid
    axis([-100,100,0,5000])
andrewsi
  • 10,807
  • 132
  • 35
  • 51
sam
  • 35
  • 1
  • 7

1 Answers1

2

That's normal behaviour. The FFT returns the spectrum in positive frequencies only (between 0 and Fs). You can use fftshift to correct that. The zero frequency will then be at the center of the x axis. So you should use

plot(fftshift(y))
axis([-100+1e4,100+1e4,0,5000])

enter image description here

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • Thank you very much this worked. now how can i make my x-axis(sample number) in term of pi instead of from(-100+1e4,100+1e4)....man you saved my life this project is due tomorow!! – sam Oct 31 '13 at 22:55
  • The complete frequency axis of the FFT with `fftshift` should be [-pi, pi]. So use `plot(linspace(-pi,pi,length(y)),fftshift(y))` and then something like `axis([-.03,.03,0,5000])` – Luis Mendo Oct 31 '13 at 23:54
  • Thanks luis i have one last question: i want to make my code dependable of the number of FFT points i want to graph so i can observe the difference it makes when you have more or less points. similar to what they did in this document: http://www.ele.uri.edu/~hansenj/projects/ele436/fft.pdf – sam Nov 01 '13 at 00:27
  • i did this 'YfreqDomain = fft(y,n)' where n is the number of samples 'n = 200; %number of samples' but i am only seeing a straight line what do you think i did wrong here? Thank you so much – sam Nov 01 '13 at 00:35
  • @LuisMendo I am having a problem with a similar issue here, and your nice simple solution here makes me wonder if you might be able to help :) https://stackoverflow.com/questions/52318628/matlab-fourier-coefficient-values-oscillating – eric Sep 13 '18 at 19:34
  • 1
    @neuronet I don't have much time right now, but it looks you got a good answer from Cris Luengo, who is very knowledgeable about signal processing – Luis Mendo Sep 13 '18 at 20:01