Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Numpy isn't the right tool really to calculate fourier series components, as your data has to be discretely sampled. You really want to use something like Mathematica or should be using fourier transforms.</p> <p>To roughly do it, let's look at something simple a triangle wave of period 2pi, where we can easily calculate the Fourier coefficients (c_n = -i ((-1)^(n+1))/n for n>0; e.g., c_n = { -i, i/2, -i/3, i/4, -i/5, i/6, ... } for n=1,2,3,4,5,6 (using Sum( c_n exp(i 2 pi n x) ) as Fourier series).</p> <pre><code>import numpy x = numpy.arange(0,2*numpy.pi, numpy.pi/1000) y = (x+numpy.pi/2) % numpy.pi - numpy.pi/2 fourier_trans = numpy.fft.rfft(y)/1000 </code></pre> <p>If you look at the first several Fourier components:</p> <pre><code>array([ -3.14159265e-03 +0.00000000e+00j, 2.54994550e-16 -1.49956612e-16j, 3.14159265e-03 -9.99996710e-01j, 1.28143395e-16 +2.05163971e-16j, -3.14159265e-03 +4.99993420e-01j, 5.28320925e-17 -2.74568926e-17j, 3.14159265e-03 -3.33323464e-01j, 7.73558750e-17 -3.41761974e-16j, -3.14159265e-03 +2.49986840e-01j, 1.73758496e-16 +1.55882418e-17j, 3.14159265e-03 -1.99983550e-01j, -1.74044469e-16 -1.22437710e-17j, -3.14159265e-03 +1.66646927e-01j, -1.02291982e-16 -2.05092972e-16j, 3.14159265e-03 -1.42834113e-01j, 1.96729377e-17 +5.35550532e-17j, -3.14159265e-03 +1.24973680e-01j, -7.50516717e-17 +3.33475329e-17j, 3.14159265e-03 -1.11081501e-01j, -1.27900121e-16 -3.32193126e-17j, -3.14159265e-03 +9.99670992e-02j, </code></pre> <p>First neglect the components that are near 0 due to floating point accuracy (~1e-16, as being zero). The more difficult part is to see that the 3.14159 numbers (that arose before we divide by the period of a 1000) should also be recognized as zero, as the function is periodic). So if we neglect those two factors we get:</p> <pre><code>fourier_trans = [0,0,-i,0,i/2,0,-i/3,0,i/4,0,-i/5,0,-i/6, ... </code></pre> <p>and you can see the fourier series numbers come up as every other number (I haven't investigated; but I believe the components correspond to [c0, c-1, c1, c-2, c2, ... ]). I'm using conventions according to wiki: <a href="http://en.wikipedia.org/wiki/Fourier_series" rel="noreferrer">http://en.wikipedia.org/wiki/Fourier_series</a>. </p> <p>Again, I'd suggest using mathematica or a computer algebra system capable of integrating and dealing with continuous functions.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload