title: How to properly scale frequency axis in Fast Fourier Transform?
body: <p>I am trying some sample code taking the FFT of a simple sinusoidal function. Below is the code</p>
  
  <pre><code>import numpy as np
  from matplotlib import pyplot as plt
  
  N = 1024
  limit = 10
  x = np.linspace(-limit, limit, N)
  dx = x[1] - x[0]
  y = np.sin(2 * np.pi * 5 * x) + np.sin(2 * np.pi * x)
  Y = np.abs(np.fft.fft(y) ** 2)
  z = fft.fftshift(np.fft.fftfreq(N, dx))
  plt.plot(z[int(N/2):], Y[int(N/2):])
  plt.show()
  </code></pre>
  
  <p>From the function that is given, <img src="https://latex.codecogs.com/gif.latex?y%20%3D%20%5Csin%2810%5Cpi%20x%29%20&plus;%20%5Csin%282%5Cpi%20x%29" alt="formula">, it is clear there should be two spikes at frequencies 1 and 5. However, when I run this code, I get the following plot.</p>
  
  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="https://i.stack.imgur.com/MHeHlm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/MHeHlm.png" alt="enter image description here"></a></p>
  
  <p>Clearly the spikes are not where they should be. Additionally, I have noticed that the frequency scaling is sensitive to the number of points <code>N</code> as well as the interval limits that I make <code>limit</code>. As an example, setting <code>N = 2048</code> gives the following plot.</p>
  
  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="https://i.stack.imgur.com/aSblxm.png" rel="noreferrer"><img src="https://i.stack.imgur.com/aSblxm.png" alt="enter image description here"></a></p>
  
  <p>As you can see, the locations of the spikes have changed. Now keeping <code>N = 1024</code> and setting <code>limit = 100</code> also changes the result.</p>
  
  <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="https://i.stack.imgur.com/6Lay8m.png" rel="noreferrer"><img src="https://i.stack.imgur.com/6Lay8m.png" alt="enter image description here"></a></p>
  
  <p><strong>How can I make it so the frequency axis is properly scaled at all times?</strong></p>