{"id":449,"date":"2019-08-22T10:34:07","date_gmt":"2019-08-22T17:34:07","guid":{"rendered":"http:\/\/gantovnik.com\/bio-tips\/?p=449"},"modified":"2024-12-17T09:31:11","modified_gmt":"2024-12-17T17:31:11","slug":"smoothing-the-noise-in-real-world-data","status":"publish","type":"post","link":"https:\/\/gantovnik.com\/bio-tips\/2019\/08\/smoothing-the-noise-in-real-world-data\/","title":{"rendered":"#65 Smoothing the Noise in Real-world Data"},"content":{"rendered":"<p><img data-recalc-dims=\"1\" decoding=\"async\" class=\"alignnone  wp-image-1057\" src=\"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/08\/ex65.png?resize=861%2C574&#038;ssl=1\" alt=\"\" width=\"861\" height=\"574\" \/><\/p>\n<pre class=\"lang:python decode:true \">#Smoothing the noise in real-world data\n#This window rolls over the data and is used to compute the average over that window.\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport os\nos.chdir(r'D:\\projects\\wordpress\\ex65') \ndef moving_average(interval, window_size):\n    #Compute convoluted window for given size\n    window = np.ones(int(window_size)) \/ float(window_size)\n    return np.convolve(interval, window, 'same')\nt = np.linspace(-4, 4, 100)\ny = np.sin(t) + np.random.randn(len(t))*0.1\nplt.plot(t, y, \"k.\")\n# compute moving average\ny_av = moving_average(y, 10)\nplt.plot(t, y_av,\"r\")\n#xlim(0,1000)\nplt.xlabel(\"Time\")\nplt.ylabel(\"Value\")\nplt.grid(True)\nplt.savefig('ex65.png', dpi=300)\nplt.show()\n<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>#Smoothing the noise in real-world data #This window rolls over the data and is used to compute the average over that window. import matplotlib.pyplot as plt import numpy as np import os os.chdir(r&#8217;D:\\projects\\wordpress\\ex65&#8242;) def moving_average(interval, window_size): #Compute convoluted window for given size window = np.ones(int(window_size)) \/ float(window_size) return np.convolve(interval, window, &#8216;same&#8217;) t = np.linspace(-4, 4, [&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_post_was_ever_published":false,"_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":""},"categories":[2],"tags":[],"class_list":["post-449","post","type-post","status-publish","format-standard","hentry","category-python"],"modified_by":"gantovnik","jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8bH0k-7f","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":308,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/elementor-308\/","url_meta":{"origin":449,"position":0},"title":"# 54 Nonlinear Least Squares Fitting","author":"gantovnik","date":"2019-01-22","format":false,"excerpt":"import os import numpy as np import matplotlib.pyplot as plt os.chdir(r'D:\\\\data\\scripts\\wordpress\\ex54') os.getcwd() A,freq,tau=10,4,0.5 def f(t,A,freq,tau): return A*np.exp(-t\/tau)*np.cos(2*np.pi*freq*t) tmax,dt=1,0.01 t=np.arange(0,tmax,dt) yexact=f(t,A,freq,tau) y=yexact + np.random.randn(len(yexact))*2 plt.plot(t,yexact) plt.scatter(t,y,marker='o',facecolors='blue', edgecolors='black') def residuals(p,y,t): A,freq,tau=p return y-f(t,A,freq,tau) from scipy.optimize import leastsq p0=5,5,1 plsq=leastsq(residuals,p0,args=(y,t)) plsq[0][1] pfit = (plsq[0][0],plsq[0][1],plsq[0][2]) plt.plot(t,y,'o',c='k',label='Data') plt.plot(t,yexact,c='green',label='Exact') plt.plot(t,f(t,plsq[0][0],plsq[0][1],plsq[0][2]),c='red',label='Fit') plt.legend() plt.tight_layout() plt.savefig(\"ex54.png\", dpi=100) plt.show() plt.close()","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\/2019\/01\/example54-1.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example54-1.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example54-1.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":2053,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/01\/406-nonlinear-least-squares-fitting-using-leastsq-from-scipy-optimize-in-python\/","url_meta":{"origin":449,"position":1},"title":"#406 Nonlinear least squares fitting using leastsq from scipy.optimize in python","author":"gantovnik","date":"2024-01-14","format":false,"excerpt":"","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex406.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex406.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/01\/ex406.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":5,"url":"https:\/\/gantovnik.com\/bio-tips\/2016\/12\/example-1-interpolation\/","url_meta":{"origin":449,"position":2},"title":"Example 1: Interpolation","author":"gantovnik","date":"2016-12-08","format":false,"excerpt":"Example 1: Interpolation","rel":"","context":"In &quot;matplotlib&quot;","block_context":{"text":"matplotlib","link":"https:\/\/gantovnik.com\/bio-tips\/category\/matplotlib\/"},"img":{"alt_text":"example1","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2016\/12\/example1-300x200.png?resize=350%2C200","width":350,"height":200},"classes":[]},{"id":2889,"url":"https:\/\/gantovnik.com\/bio-tips\/2024\/07\/440-cycloid-on-interactive-figure-with-widgets-in-python\/","url_meta":{"origin":449,"position":3},"title":"#440 Cycloid on interactive figure with widgets in python","author":"gantovnik","date":"2024-07-21","format":false,"excerpt":"import numpy as np import matplotlib.pyplot as plt from matplotlib.widgets import Slider theta = np.linspace(0,2*np.pi) circ_x = lambda t: t + np.cos(theta) circ_y = 1 + np.sin(theta) cycl_x = lambda t: t - np.sin(t) cycl_y = lambda t: 1 - np.cos(t) t = 0 fig,ax = plt.subplots() plt.subplots_adjust(bottom=0.2) plt.ylim(0, 3)\u2026","rel":"","context":"In &quot;animation&quot;","block_context":{"text":"animation","link":"https:\/\/gantovnik.com\/bio-tips\/category\/animation\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex440.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex440.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2024\/07\/ex440.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]},{"id":1786,"url":"https:\/\/gantovnik.com\/bio-tips\/2023\/01\/340-lorenz-attractors-using-python\/","url_meta":{"origin":449,"position":4},"title":"#340 Lorenz attractors using python","author":"gantovnik","date":"2023-01-17","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\/2023\/01\/ex340_b.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2023\/01\/ex340_b.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":210,"url":"https:\/\/gantovnik.com\/bio-tips\/2019\/01\/2nd-order-runge-kutta-type-a\/","url_meta":{"origin":449,"position":5},"title":"#44 2nd-order Runge-Kutta type A using python","author":"gantovnik","date":"2019-01-12","format":false,"excerpt":"\u00a0","rel":"","context":"In &quot;numerical&quot;","block_context":{"text":"numerical","link":"https:\/\/gantovnik.com\/bio-tips\/category\/numerical\/"},"img":{"alt_text":"example44","src":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example44-1.png?resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example44-1.png?resize=350%2C200 1x, https:\/\/i0.wp.com\/gantovnik.com\/bio-tips\/wp-content\/uploads\/2019\/01\/example44-1.png?resize=525%2C300 1.5x"},"classes":[]}],"_links":{"self":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/449","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=449"}],"version-history":[{"count":2,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/449\/revisions"}],"predecessor-version":[{"id":6766,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/posts\/449\/revisions\/6766"}],"wp:attachment":[{"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/media?parent=449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/categories?post=449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gantovnik.com\/bio-tips\/wp-json\/wp\/v2\/tags?post=449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}