{"id":3022,"date":"2024-07-25T04:22:59","date_gmt":"2024-07-25T11:22:59","guid":{"rendered":"https:\/\/gantovnik.com\/bio-tips\/?p=3022"},"modified":"2024-07-25T04:22:59","modified_gmt":"2024-07-25T11:22:59","slug":"442-the-partial-sums-of-the-fourier-series-for-the-square-wave-function-using-python","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/442-the-partial-sums-of-the-fourier-series-for-the-square-wave-function-using-python\/","title":{"rendered":"#442 The partial sums of the Fourier series for the square-wave function using python"},"content":{"rendered":"<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/442-the-partial-sums-of-the-fourier-series-for-the-square-wave-function-using-python\/ex442_1\/\" rel=\"attachment wp-att-3023\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_1.png?resize=640%2C480&#038;ssl=1\" alt=\"\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-3023\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_1.png 640w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_1-480x360.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 640px, 100vw\" \/><\/a><\/p>\n<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/442-the-partial-sums-of-the-fourier-series-for-the-square-wave-function-using-python\/ex442_2\/\" rel=\"attachment wp-att-3024\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_2.png?resize=640%2C480&#038;ssl=1\" alt=\"\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-3024\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_2.png 640w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_2-480x360.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 640px, 100vw\" \/><\/a><\/p>\n<p><a href=\"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/442-the-partial-sums-of-the-fourier-series-for-the-square-wave-function-using-python\/ex442_3\/\" rel=\"attachment wp-att-3025\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_3.png?resize=640%2C480&#038;ssl=1\" alt=\"\" width=\"640\" height=\"480\" class=\"alignnone size-full wp-image-3025\" srcset=\"https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_3.png 640w, https:\/\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex442_3-480x360.png 480w\" sizes=\"(min-width: 0px) and (max-width: 480px) 480px, (min-width: 481px) 640px, 100vw\" \/><\/a><\/p>\n<pre class=\"lang:python decode:true \" >import numpy as np\nimport matplotlib.pyplot as plt\nfrom matplotlib.widgets import Slider\n\nnmax = 5\npi = np.pi\nx = np.linspace(-2*pi, 2*pi, 1001)\n\ndef f(xarray):\n    y = np.zeros_like(xarray)\n    for ind, x in enumerate(xarray):\n        xmod = x%(2*pi)\n        if xmod&lt;pi:\n            y[ind] = 1\n        if x%pi==0:\n            y[ind]= np.nan\n    return y\n\ndef Fourier(x, nmax):\n    S = np.zeros_like(x)\n    for n in np.arange(1,nmax+1,2):\n        S += np.sin(n*x)\/n\n    return 0.5+ 2*S\/pi\n\nfig,ax = plt.subplots()\nplt.subplots_adjust(bottom=0.15)\nplt.plot(x, f(x),'r',lw=1.5)\nFfunc,= plt.plot(x, Fourier(x,nmax),'b',lw=0.5)\nplt.xlim([-2*pi, 2*pi])\nplt.ylim([-0.2, 1.2])\nplt.grid('on')\nplt.title(r'Fourier series')\naxn = plt.axes([0.15, 0.05, 0.7, 0.03])\nn_slide = Slider(axn, 'n', 1, 101, valstep = 2, valinit = nmax)\n\ndef update(val):\n    nmax = n_slide.val\n    Ffunc.set_ydata(Fourier(x,nmax))\n    fig.canvas.draw_idle()\n\nn_slide.on_changed(update)\nplt.show()<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider nmax = 5 pi = np.pi x = np.linspace(-2*pi, 2*pi, 1001) def f(xarray): y = np.zeros_like(xarray) for ind, x in enumerate(xarray): xmod = x%(2*pi) if xmod&lt;pi: y[ind] = 1 if x%pi==0: y[ind]= np.nan return y def Fourier(x, nmax): S = np.zeros_like(x) for n [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","_lmt_disableupdate":"yes","_lmt_disable":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[89,56,2],"tags":[137],"class_list":["post-3022","post","type-post","status-publish","format-standard","hentry","category-animation","category-math","category-python","tag-fourier-series"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-MK","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":58,"url":"https:\/\/gantovnik.com\/bio-tips\/2018\/12\/fourier-expansion-of-a-rectangular-wave\/","url_meta":{"origin":3022,"position":0},"title":"#7: Fourier expansion of a rectangular wave","author":"gantovnik","date":"2018-12-24","format":false,"excerpt":"import os import matplotlib.pyplot as plt import numpy as np os.chdir('\/home\/vg\/Downloads\/projects\/ex7') os.getcwd() plt.figure(figsize=(10,8)) N = 2**8 t = np.linspace(0,1,N) y = np.zeros(N) for n in range(1,32,2): y = y + 4\/(np.pi*n)*np.sin(2*np.pi*n*t*2) plt.plot(t,y) plt.axis([0,1,-1.4,1.4]) plt.grid() plt.xlabel('Time, sec') plt.ylabel('Value') plt.title('Fourier expansion of a rectangular wave') plt.savefig(\"example7.png\", dpi=100) plt.show() plt.close()","rel":"","context":"In &quot;math&quot;","block_context":{"text":"math","link":"https:\/\/gantovnik.com\/bio-tips\/category\/math\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/ex7.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/ex7.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/ex7.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2018\/12\/ex7.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2219,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/06\/431-hypermesh-python-script-to-create-nodes-using-coordinates-from-csv-file\/","url_meta":{"origin":3022,"position":1},"title":"#431 HyperMesh python script to create nodes using coordinates from csv file","author":"gantovnik","date":"2024-06-24","format":false,"excerpt":"Finally, I read how to write a script for HyperMesh in python. In this example, the script reads the coordinates of points from a cvs file. It feels like the python code is translated into a tcl script and then executed in HyperMesh. Because HyperMesh continues to record executed commands\u2026","rel":"","context":"In &quot;HyperMesh&quot;","block_context":{"text":"HyperMesh","link":"https:\/\/gantovnik.com\/bio-tips\/category\/hypermesh\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/06\/ex431.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":897,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/02\/161-euler-spiral-using-python\/","url_meta":{"origin":3022,"position":2},"title":"#161 Euler spiral using python","author":"gantovnik","date":"2021-02-14","format":false,"excerpt":"#161 Euler spiral using python The curve described by the parametric equations (x, y) = (S (t),C(t)) is called a clothoid (or Euler spiral) and has the property that its curvature is proportional to the distance along the path of the curve.","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/02\/example161.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/02\/example161.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/02\/example161.png?resize=525%2C300 1.5x"},"classes":[]},{"id":1002,"url":"https:\/\/gantovnik.com\/bio-tips\/2021\/10\/180-linear-regression-using-python\/","url_meta":{"origin":3022,"position":3},"title":"#180 Linear regression using python","author":"gantovnik","date":"2021-10-26","format":false,"excerpt":"","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex180.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex180.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2021\/10\/ex180.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":589,"url":"https:\/\/gantovnik.com\/bio-tips\/2020\/07\/86-stop-execution-of-python-script\/","url_meta":{"origin":3022,"position":4},"title":"#86 Stop execution of python script?","author":"gantovnik","date":"2020-07-28","format":false,"excerpt":"#86 Stop execution of python script?","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1723,"url":"https:\/\/gantovnik.com\/bio-tips\/2022\/12\/204-mandelbrot-fractal-using-python-2-2-2-2-2-2-2\/","url_meta":{"origin":3022,"position":5},"title":"#326 Vertical box plot using python","author":"gantovnik","date":"2022-12-14","format":false,"excerpt":"","rel":"","context":"In &quot;python&quot;","block_context":{"text":"python","link":"https:\/\/gantovnik.com\/bio-tips\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex326.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex326.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2022\/12\/ex326.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/3022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/comments?post=3022"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/3022\/revisions"}],"predecessor-version":[{"id":3027,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/3022\/revisions\/3027"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=3022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=3022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=3022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}