that_one_nigga_you_k

FFT_movendo

Jan 18th, 2025
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.02 KB | None | 0 0
  1.  
  2. from manim import *
  3.  
  4. config.pixel_height = 1920
  5. config.pixel_width = 1080
  6. config.frame_height = 16
  7. config.frame_width = 9
  8. config.frame_rate = 60
  9.  
  10. class fft(MovingCameraScene):
  11.     center_point = ORIGIN
  12.     slow_factor = 0.1
  13.  
  14.     def get_fft_coefficients_of_path(self, path, n_samples=2048, n_freqs = 256):
  15.         dt = 1 / n_samples
  16.         ts = np.arange(0, 1, dt)
  17.         samples = np.array([
  18.             path.point_from_proportion(t)
  19.             for t in ts
  20.         ])
  21.         samples -= self.center_point
  22.         complex_samples = samples[:, 0] + 1j * samples[:, 1]
  23.        
  24.         coeffs = np.fft.fft(complex_samples)
  25.         freqs  = np.fft.fftfreq(n_samples, 1/n_samples)
  26.  
  27.         sfreqs = zip(coeffs,freqs)
  28.         p = sorted(sfreqs, key=lambda f: abs(f[1]))
  29.         cs = [a[0]/n_samples for a in p]
  30.         fs = [a[1] for a in p]
  31.         return cs[:n_freqs], fs[:n_freqs]
  32.    
  33.     def get_path(self):
  34.         svg_mob = SVGMobject("/home/narciso/Pictures/eu.svg").scale(1)
  35.         svg_mob.scale_to_fit_height(6)
  36.         path = svg_mob #.family_members_with_points()[0]
  37.         path.set_fill(opacity=0)
  38.         path.set_stroke(WHITE, 1)
  39.         return path
  40.        
  41.     def construct(self):
  42.         #npl = NumberPlane( x_range=[-config.frame_width / 2, config.frame_width / 2], y_range=[-config.frame_height / 2, config.frame_height / 2], axis_config={"stroke_color": WHITE},)
  43.         #npl.add_coordinates()
  44.         #self.add(npl)
  45.  
  46.         paths  = self.get_path()
  47.         for path in paths:
  48.             coefs, freqs = self.get_fft_coefficients_of_path(path, n_samples=2048, n_freqs=5) #n_samples and n_freqs give the detail
  49.  
  50.             self.play(Create(path))
  51.             self.wait(2)
  52.  
  53.             vectorsCircles = VGroup()
  54.             origin = ORIGIN
  55.             strokewidth = 4
  56.             for i in range(len(freqs)):
  57.                 print("{:3.0f}: abs = {:5.3f}  Z = {:-5.3f} + {:-5.3f}j".format(
  58.                     freqs[i],
  59.                     np.abs(coefs[i]),
  60.                     np.real(coefs[i]),
  61.                     np.imag(coefs[i])))
  62.                 dummy = Line(
  63.                         start = ORIGIN,
  64.                         end   = [np.real(coefs[i]), np.imag(coefs[i]), 0],
  65.                         stroke_width = strokewidth
  66.                 )
  67.                 circ = Circle(radius=np.abs(coefs[i])).set_stroke(width=0.5*strokewidth, color=RED)
  68.                 strokewidth *= 0.9
  69.                 vectorsCircles += VGroup(dummy, circ).shift(origin)
  70.                 origin = dummy.get_end()
  71.  
  72.             self.play(FadeIn(vectorsCircles), run_time = 0.5)
  73.             self.wait()
  74.  
  75.             dot = always_redraw(lambda: Dot(vectorsCircles[-1][0].get_end(), radius=0.04, color=GREEN))
  76.  
  77.             trace = VMobject().set_points([vectorsCircles[-1][0].get_end()]).set_color(YELLOW)
  78.             self.add(trace,dot)
  79.  
  80.             def vectorsUpdater(mobj, dt):
  81.                 origin = mobj[0][0].get_end()
  82.                 for i in range(1, len(freqs)):
  83.                     mobj[i][0].rotate(2*PI*dt*freqs[i]*self.slow_factor, about_point=mobj[i][0].get_start())
  84.                     mobj[i].shift(origin - mobj[i][0].get_start())
  85.                     origin = mobj[i][0].get_end()
  86.                 trace.add_smooth_curve_to(mobj[-1][0].get_end())    
  87.             vectorsCircles.add_updater(vectorsUpdater)
  88.  
  89.             self.wait(1/self.slow_factor)
  90.             self.camera.frame.save_state()
  91.  
  92.             vectorsCircles.remove_updater(vectorsUpdater)
  93.             self.play(self.camera.frame.animate.scale(0.2).move_to(dot))
  94.             vectorsCircles.add_updater(vectorsUpdater)
  95.  
  96.             def update_camera(mob):
  97.                 mob.move_to(dot.get_center())
  98.  
  99.             self.camera.frame.add_updater(update_camera)
  100.             self.slow_factor=1/30
  101.  
  102.             self.wait(1/self.slow_factor)
  103.  
  104.             self.camera.frame.remove_updater(update_camera)
  105.  
  106.             self.play(Restore(self.camera.frame))
  107.             vectorsCircles.remove_updater(vectorsUpdater)
  108.         self.wait(2)
  109.  
Add Comment
Please, Sign In to add comment