title: Simple DFT Coefficients => Amplitude/Frequencies => Plot
body: <p>Im trying on DFT and FFT in Python with numpy and pyplot.</p>
  <p>My Sample Vector is</p>
  <pre><code>x = np.array([1,2,4,3]
  </code></pre>
  <p>The DFT coefficients for that vector are</p>
  <pre><code>K = [10+0j, -3+1j, 0+0j, -3-1j]
  </code></pre>
  <p>so basically we have 10, -3+i, 0 and -3-1i as DFT coefficients.</p>
  <p>My problem now is to get a combination of sin and cos to fit all 4 points.</p>
  <p>Let's assume we have a sample Rate of 1hz.</p>
  <p>This is my code :</p>
  <pre><code>from matplotlib import pyplot as plt
  import numpy as np
  
  x = np.array([1,2,4,3])
  
  fft = np.fft.fft(x)
  
  space = np.linspace(0,4,50)
  values = np.array([1,2,3,4])
  
  cos0 = fft[0].real * np.cos(0 * space)
  
  cos1 = fft[1].real * np.cos(1/4 * np.pi * space)
  sin1 = fft[1].imag * np.sin(1/4 * np.pi * space)
  
  res = cos0 + cos1 + sin1
  
  plt.scatter(values, x, label=&quot;original&quot;)
  plt.plot(space, cos0, label=&quot;cos0&quot;)
  plt.plot(space, cos1, label=&quot;cos1&quot;)
  plt.plot(space, sin1, label=&quot;sin1&quot;)
  plt.plot(space, res, label=&quot;combined&quot;)
  
  plt.legend()
  </code></pre>
  <p>As result i get the plot:</p>
  <p><a href="https://i.stack.imgur.com/2h1Il.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2h1Il.png" alt="img" /></a><br />
  <sub>(source: <a href="https://heeser-it.de/static/plot.png" rel="nofollow noreferrer">heeser-it.de</a>)</sub></p>
  <p>Why isnt the final curve hitting any point?</p>
  <p>I would appreciate your help. Thanks!</p>
  <p><strong>EDIT:</strong></p>
  <pre><code>N = 1000
  dataPoints = np.linspace(0, np.pi, N)
  function = np.sin(dataPoints)
  fft = np.fft.fft(function)
  
  F = np.zeros((N,))
  for i in range(0, N):
      F[i] = (2 * np.pi * i) / N
  F_sin = np.zeros((N,N))
  F_cos = np.zeros((N,N))
  
  res = 0
  for i in range(0, N):
      F_sin[i] = fft[i].imag / 500 * np.sin(dataPoints * F[i])
      F_cos[i] = fft[i].real / 500* np.cos(dataPoints * F[i])
      res = res + F_sin[i] + F_cos[i] 
  plt.plot(dataPoints, function)
  plt.plot(dataPoints, res)
  </code></pre>
  <p>my plot looks like:</p>
  <p><a href="https://i.stack.imgur.com/onnjS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/onnjS.png" alt="img" /></a><br />
  <sub>(source: <a href="https://heeser-it.de/static/plot2.png" rel="nofollow noreferrer">heeser-it.de</a>)</sub></p>
  <p>where do i fail?</p>