numpy fft 备忘.
作者:互联网
主要参考以下注释网站
1 #encoding:utf-8 2 #https://blog.csdn.net/weixin_39949473/article/details/111337230 3 #https://blog.csdn.net/weixin_35756373/article/details/112713361 4 from numpy.fft import fft 5 6 import pylab 7 import matplotlib.pyplot as plt 8 import numpy as np 9 from matplotlib.animation import FuncAnimation 10 11 ph=r"d:\Users\Administrator\Documents\Kangaroo_test\lin.txt" 12 mtxt='' 13 with open(ph,'r') as f: 14 mtxt=f.read() 15 txt=mtxt.split('\n') 16 xn=[] 17 for i in txt: 18 if len(i)==0: 19 continue 20 sp,ep=i.split(r',') 21 sp=float(sp) 22 ep=float(ep) 23 xn.append(sp+ep*1j) 24 25 # xn=np.linspace(0.2, 3, 128) 26 # X = fft([i+1/i*1j for i in xn]) 27 28 X = fft(xn) 29 30 frames = len(X) # number of animation frames 31 n = frames # number of ciecles 32 coef = np.arange(frames) 33 34 factor = [] 35 for i in range(len(coef)): 36 factor.append(X[i] * np.exp(2 * np.pi * 1j * coef[i] * np.arange(frames) / frames) / frames) 37 pos = [[0 for i in range(frames)]] 38 for i in range(len(coef)): 39 pos.append(pos[-1] + factor[i]) 40 41 fig, ax = pylab.subplots() 42 lines = [] 43 circles = [] 44 def _init_lines(): 45 global lines 46 lines += [ax.plot([], [], alpha=1)[0] for i in range(n)] 47 48 def _init_circles(): 49 global circles,ax 50 for i in range(n): 51 radius = np.abs(pos[i+1][0] - pos[i][0]) 52 circle = plt.Circle((-1, -1), radius, alpha=0.6, fill=False) 53 circles.append(circle) 54 ax.add_artist(circle) 55 #### 56 circle_cyl=plt.Circle((-1, -1), 0.01, alpha=0.99, fill=True) 57 circles.append(circle_cyl) 58 ax.add_artist(circle_cyl) 59 60 61 path_x = [] 62 path_y = [] 63 64 flag=0 65 def init_frame(): 66 global flag 67 flag+=1 68 print ( flag ) 69 ax.clear() 70 viewZoom=1 71 ax.set(xlim=[-1*viewZoom, 1*viewZoom], ylim=[-1*viewZoom, 1*viewZoom]) 72 ax.axis('off') 73 _init_lines() 74 _init_circles() 75 global path 76 path = ax.plot([], [])[0] 77 78 def _render_lines(k): 79 for i in range(n): 80 p, q = pos[i][k], pos[i+1][k] 81 lines[i].set_data([p.real, q.real], [p.imag, q.imag]) 82 ### 83 circles[-1].center = (q.real, q.imag) 84 85 def _render_circles(k): 86 for i in range(n): 87 p = pos[i][k] 88 circles[i].center = (p.real, p.imag) 89 90 91 def render_frame(k): 92 _render_lines(k) 93 _render_circles(k) 94 p = pos[n][k] 95 path_x.append(p.real) 96 path_y.append(p.imag) 97 path.set_data(path_x, path_y) 98 99 100 fq= 1000/120 101 animation = FuncAnimation(fig, 102 render_frame, 103 frames=range(frames), 104 interval=fq, 105 repeat_delay=fq, 106 init_func=init_frame, 107 repeat=1) 108 pylab.show() 109 110
测试 txt点集文件
-0.049232844231,0.0992831516301 -0.0496022321778,0.057524373699 -0.0521786560248,0.0158532228812 -0.068789539469,0.00963270461548 -0.0977475272242,0.0397213672635 -0.127524661025,0.0690008407628 -0.157906922277,0.0976524479613 -0.18869270775,0.12587051679 -0.219689491178,0.153856795101 -0.224307764655,0.120997161149 -0.225060105945,0.0792422195054 -0.225812447236,0.0374872778613 -0.226564788527,-0.00426766378267 -0.227317129818,-0.0460226054267 -0.228069471109,-0.0877775470707 -0.2288218124,-0.129532488715 -0.229574153691,-0.171287430359 -0.230326494982,-0.213042372003 -0.231078836272,-0.254797313647 -0.231831177563,-0.296552255291 -0.232583518854,-0.338307196935 -0.233335860145,-0.380062138579 -0.234088201436,-0.421817080223 -0.234840542727,-0.463572021867 -0.23406682897,-0.50528613123 -0.236880672834,-0.546783712117 -0.263623481405,-0.541137647928 -0.281501103017,-0.503396002302 -0.29937872463,-0.465654356676 -0.301276260762,-0.430147827424 -0.290720562447,-0.389967229018 -0.287896876373,-0.348316887852 -0.287167934456,-0.306562834091 -0.28618557767,-0.264812670687 -0.285203220884,-0.223062507283 -0.284220864098,-0.181312343879 -0.283238507312,-0.139562180475 -0.282256150526,-0.0978120170714 -0.28127379374,-0.0560618536674 -0.280291436954,-0.0143116902634 -0.279309080168,0.0274384731405 -0.278326723382,0.0691886365445 -0.277344366596,0.110938799948 -0.27636200981,0.152688963352 -0.287757631673,0.146552932914 -0.307708741553,0.10986582613 -0.32837792291,0.0735784356736 -0.349762453472,0.0377080150188 -0.371858329362,0.00227155119055 -0.394660550348,-0.0327147763075 -0.418162605899,-0.0672348579366 -0.442356872926,-0.101273355528 -0.467234546841,-0.134815624914 -0.492785630149,-0.16784772941 -0.518999136371,-0.200356711936 -0.545862957331,-0.232330409182 -0.573364005207,-0.263757612789 -0.601488444217,-0.294628287112 -0.631048719569,-0.314645639541 -0.634556599816,-0.286484294791 -0.607600462821,-0.254588843457 -0.581434248834,-0.222042183578 -0.556048464678,-0.188883244674 -0.531430869407,-0.155149704265 -0.507567456896,-0.12087858525 -0.484442119061,-0.0861051445069 -0.462037250878,-0.0508630768635 -0.440334113567,-0.0151845071957 -0.419313019304,0.0209002208026 -0.398953680235,0.0573624128243 -0.379235309509,0.0941752393255 -0.360136936134,0.131313505097 -0.341637477218,0.16875379103 -0.323715962529,0.20647424773 -0.306351701784,0.244454416099 -0.303080639959,0.27216969355 -0.344297489058,0.265446347935 -0.385433726488,0.25824800967 -0.426385016882,0.250071629365 -0.466646351195,0.239088458028 -0.500376783807,0.250928123094 -0.532084644641,0.278106289523 -0.557011050704,0.303071103797 -0.515408527655,0.306708632128 -0.473864930648,0.310967734644 -0.43237779759,0.31574665731 -0.390945793209,0.320982378234 -0.349568416114,0.326633908111 -0.308246005095,0.332674005145 -0.275977030278,0.346765863936 -0.275977030278,0.388527582879 -0.275977030278,0.430289301823 -0.275977030278,0.472051020766 -0.275977030278,0.513812739709 -0.275977030278,0.555574458653 -0.274585061385,0.597295640806 -0.276287494939,0.638996464807 -0.28380957414,0.680029687615 -0.298748152592,0.718944440366 -0.297625382351,0.749216706773 -0.26445163379,0.723848546108 -0.231277885229,0.698480385444 -0.203246954404,0.674486673854 -0.221631295903,0.638945814015 -0.225088516339,0.59736723647 -0.224180733271,0.555624759081 -0.223651448882,0.513879137779 -0.223651448882,0.472117418835 -0.223651448882,0.430355699892 -0.223651448882,0.388593980948 -0.223651448882,0.346832262005 -0.18564482762,0.348936649945 -0.144519854358,0.356182199128 -0.104087521021,0.366517903668 -0.0692729471465,0.367962386462 -0.0350604139323,0.344013613212 0.00445577023738,0.341487072628 0.0459705228884,0.346020637648 0.0874950226481,0.350465435819 0.128999050686,0.355097138148 0.170467591851,0.36003610983 0.178377667108,0.395564070291 0.179421383971,0.437312744811 0.180465100834,0.479061419332 0.181508817697,0.520810093852 0.18255253456,0.562558768373 0.183966475911,0.60429175821 0.184631178645,0.646037965299 0.181137720451,0.687624866652 0.170481316598,0.72790635113 0.149028237553,0.762660512484 0.180839589147,0.757818732672 0.214013337708,0.732450572008 0.247187086269,0.707082411343 0.243573188398,0.67636623326 0.235023750768,0.635633713983 0.234741398521,0.59390838732 0.234827308111,0.552192822 0.233667707653,0.510447205513 0.232508107195,0.468701589026 0.231348506737,0.426955972539 0.230188906279,0.385210356052 0.258673896229,0.37633261733 0.300056973508,0.38194319334 0.341391180586,0.387901835963 0.382614699534,0.394577838952 0.423459794805,0.403216947781 0.459172135197,0.40280942253 0.492848993988,0.378113059416 0.52652585278,0.353416696303 0.497417022909,0.348068393529 0.455781212146,0.344848148577 0.414223176308,0.34073185564 0.372715368644,0.336135077215 0.331239447846,0.331257855768 0.289783583959,0.326213319875 0.248339490705,0.321072100158 0.254067457036,0.287739446127 0.269259135983,0.248838888364 0.284558925725,0.209980730522 0.299991652879,0.171175157398 0.315590655966,0.132436214085 0.331402601475,0.093783674034 0.347495964708,0.0552474156668 0.363978263373,0.0168762483552 0.381036631681,-0.0212420018369 0.399096145003,-0.0588928395245 0.419485291764,-0.0953381239738 0.440917750289,-0.131177779309 0.464220407337,-0.165822041994 0.491252284537,-0.197593868274 0.527339804956,-0.218127467711 0.567278890667,-0.230230553504 0.608197504829,-0.238539607222 0.642627620885,-0.250679250834 0.622996522734,-0.272809871626 0.581234803791,-0.272809871626 0.539473084847,-0.272809871626 0.497711365904,-0.272809871626 0.45594964696,-0.272809871626 0.426500656201,-0.259535502759 0.416546480979,-0.219024507111 0.40292185668,-0.179551691204 0.388220448469,-0.140463543528 0.372014234006,-0.101996705903 0.354975195035,-0.0638693868485 0.338275592217,-0.0255920137165 0.321849783944,0.0128036387221 0.305657129273,0.0512982980069 0.289670210902,0.0898787955416 0.273869230635,0.12853581791 0.258239301254,0.167262306804 0.2427688144,0.206052783687 0.229786640893,0.232281334092 0.229465406405,0.190520850647 0.229144171917,0.148760367203 0.228822937429,0.106999883758 0.228501702941,0.0652394003139 0.228180468453,0.0234789168694 0.227859233965,-0.0182815665751 0.227537999477,-0.0600420500195 0.227216764989,-0.101802533464 0.226895530501,-0.143563016909 0.226574296013,-0.185323500353 0.226253061525,-0.227083983797 0.225931827037,-0.268844467242 0.225610592549,-0.310604950686 0.225289358061,-0.352365434131 0.224968123573,-0.394125917575 0.224646889085,-0.43588640102 0.224325654597,-0.477646884464 0.22401876692,-0.519407436937 0.221584547955,-0.561059336029 0.19906468746,-0.5577052113 0.176377580266,-0.522643318364 0.153690473072,-0.487581425427 0.156860234921,-0.454667364926 0.170625243215,-0.415520134915 0.17303146128,-0.373896986135 0.171944802191,-0.332185386568 0.172414005371,-0.290426303514 0.172883208552,-0.248667220461 0.173352411732,-0.206908137407 0.173821614913,-0.165149054353 0.174290818093,-0.123389971299 0.174760021273,-0.0816308882456 0.175229224454,-0.0398718051919 0.175698427634,0.00188727786185 0.176167630815,0.0436463609156 0.176636833995,0.0854054439693 0.177106037175,0.127164527023 0.175207597851,0.158036780212 0.157791759241,0.120080803301 0.139487259007,0.0825454333286 0.120282464102,0.0454626820568 0.100167748065,0.00886548443653 0.079135924511,-0.0272121685761 0.0571819701317,-0.0627363416486 0.034303808625,-0.0976724143493 0.0105021376361,-0.131985987466 -0.0142191907165,-0.165642850631 -0.0398533927525,-0.198609781346 -0.0663904076162,-0.230854482667 -0.0938169117738,-0.26234599052 -0.122116493209,-0.293055170007 -0.151269855982,-0.322955120808 -0.178926865678,-0.331175816199 -0.160579153631,-0.298872831607 -0.134272076816,-0.266440128396 -0.108830808941,-0.233323919966 -0.0842698089707,-0.199549773486 -0.0605999511103,-0.165145122392 -0.0378286601426,-0.130139229087 -0.0159597203238,-0.0945625789711 0.00500660487594,-0.058446605189 0.0250735196126,-0.0218232127255 0.0442474576332,0.0152754650861 0.0625380255702,0.0528177239553 0.0799574435883,0.0907719915327 0.0965205884669,0.129107756396 0.112244492731,0.167795387013 0.127148063719,0.206806348429 0.141251949451,0.246113812629 0.154577811451,0.285691571557 0.136344377341,0.3006460966 0.0949585882514,0.295111774587 0.0542692499376,0.285835348353 0.0174923553402,0.280589908553 -0.0154723501092,0.306229123902 -0.0544066540584,0.31337194009 -0.0959340718555,0.309026886034 -0.137185643957,0.302536512272 -0.178219616031,0.29477925772 -0.21911126664,0.286300342756 -0.223651448882,0.248210729329 -0.223651448882,0.206449010385 -0.194387931168,0.181952281736 -0.158034744977,0.161398067816 -0.121812668148,0.14061346029 -0.0855984679436,0.119815071014
标签:circles,fft,pos,备忘,range,ax,frames,path,numpy 来源: https://www.cnblogs.com/chenyalin/p/15851765.html