Skip to content

armgit / manim

Public Imported from GitHub
0

A community-maintained Python framework for creating mathematical animations.

Scoped out WhiteComplexExponentialExpression of fourier

Grant Sanderson9b400c322e2d
diff --git a/active_projects/fourier.py b/active_projects/fourier.py
index 2eb848699..f51029f99 100644
--- a/active_projects/fourier.py
+++ b/active_projects/fourier.py
@@ -708,10 +708,10 @@ class FourierMachineScene(Scene):
},
"time_label_t" : 3.4,
"circle_plane_config" : {
- "x_radius" : 2.5,
- "y_radius" : 2.5,
- "x_unit_size" : 0.8,
- "y_unit_size" : 0.8,
+ "x_radius" : 2.1,
+ "y_radius" : 2.1,
+ "x_unit_size" : 1,
+ "y_unit_size" : 1,
},
"frequency_axes_config" : {
"number_line_config" : {
@@ -747,6 +747,7 @@ class FourierMachineScene(Scene):
"rate_func" : None,
"run_time" : 5,
},
+ "default_num_v_lines_indicating_periods" : 20,
}
def get_time_axes(self):
@@ -772,6 +773,7 @@ class FourierMachineScene(Scene):
circle_plane = NumberPlane(**self.circle_plane_config)
circle_plane.to_corner(DOWN+LEFT)
circle = DashedLine(ORIGIN, TAU*UP).apply_complex_function(np.exp)
+ circle.scale(circle_plane.x_unit_size)
circle.move_to(circle_plane.coords_to_point(0, 0))
circle_plane.circle = circle
circle_plane.add(circle)
@@ -947,7 +949,9 @@ class FourierMachineScene(Scene):
self.remove(v_line.polarized_mobject)
self.play(FadeOut(VGroup(v_line, v_line.polarized_mobject)))
- def get_v_lines_indicating_periods(self, freq, n_lines = 20):
+ def get_v_lines_indicating_periods(self, freq, n_lines = None):
+ if n_lines is None:
+ n_lines = self.default_num_v_lines_indicating_periods
period = np.divide(1., max(freq, 0.01))
v_lines = VGroup(*[
DashedLine(ORIGIN, 1.5*UP).move_to(
@@ -1216,8 +1220,7 @@ class DrawFrequencyPlot(WrapCosineGraphAroundCircle, PiCreatureScene):
def show_center_of_mass_dot(self):
color = self.center_of_mass_color
- dot = Dot(self.get_pol_graph_center_of_mass())
- dot.highlight(color)
+ dot = self.get_center_of_mass_dot()
dot.save_state()
arrow = Vector(DOWN+2*LEFT, color = color)
arrow.next_to(dot.get_center(), UP+RIGHT, buff = SMALL_BUFF)
@@ -1234,7 +1237,6 @@ class DrawFrequencyPlot(WrapCosineGraphAroundCircle, PiCreatureScene):
self.play(FadeOut(arrow), FadeOut(self.pi_creature))
self.wait()
- self.center_of_mass_dot = dot
self.generate_center_of_mass_dot_update_anim()
self.center_of_mass_label = words
@@ -1289,9 +1291,9 @@ class DrawFrequencyPlot(WrapCosineGraphAroundCircle, PiCreatureScene):
def draw_full_frequency_plot(self):
graph = self.graph
- fourier_graph = get_fourier_transform_graph(graph)
+ fourier_graph = self.get_fourier_transform_graph(graph)
fourier_graph.save_state()
- fourier_graph_update = self.get_fouier_graph_drawing_update_anim(
+ fourier_graph_update = self.get_fourier_graph_drawing_update_anim(
fourier_graph
)
v_line = DashedLine(
@@ -1362,7 +1364,7 @@ class DrawFrequencyPlot(WrapCosineGraphAroundCircle, PiCreatureScene):
new_graph = self.get_cosine_wave(
self.signal_frequency, shift_val = 0
)
- new_fourier_graph = get_fourier_transform_graph(new_graph)
+ new_fourier_graph = self.get_fourier_transform_graph(new_graph)
for mob in graph, time_axes, fourier_graph:
mob.save_state()
@@ -1421,6 +1423,14 @@ class DrawFrequencyPlot(WrapCosineGraphAroundCircle, PiCreatureScene):
##
+ def get_center_of_mass_dot(self):
+ dot = Dot(
+ self.get_pol_graph_center_of_mass(),
+ color = self.center_of_mass_color
+ )
+ self.center_of_mass_dot = dot
+ return dot
+
def get_pol_graph_center_of_mass(self):
pg = self.graph.polarized_mobject
result = center_of_mass([
@@ -1441,7 +1451,7 @@ class DrawFrequencyPlot(WrapCosineGraphAroundCircle, PiCreatureScene):
)
self.fourier_graph_dot_anim.update(0)
- def get_fouier_graph_drawing_update_anim(self, fourier_graph):
+ def get_fourier_graph_drawing_update_anim(self, fourier_graph):
fourier_graph_copy = fourier_graph.copy()
max_freq = self.frequency_axes.x_max
def update_fourier_graph(fg):
@@ -1451,7 +1461,10 @@ class DrawFrequencyPlot(WrapCosineGraphAroundCircle, PiCreatureScene):
0, freq/max_freq
)
return fg
- return UpdateFromFunc(fourier_graph, update_fourier_graph)
+ self.fourier_graph_drawing_update_anim = UpdateFromFunc(
+ fourier_graph, update_fourier_graph
+ )
+ return self.fourier_graph_drawing_update_anim
def generate_center_of_mass_dot_update_anim(self):
self.center_of_mass_dot_anim = UpdateFromFunc(
@@ -1470,14 +1483,17 @@ class DrawFrequencyPlot(WrapCosineGraphAroundCircle, PiCreatureScene):
ChangeDecimalToValue(freq_label, new_freq),
self.get_frequency_change_animation(
self.graph, new_freq
- ),
- self.get_period_v_lines_update_anim(),
+ )
]
- anims += added_anims
+ if hasattr(self, "v_lines_indicating_periods"):
+ anims.append(self.get_period_v_lines_update_anim())
if hasattr(self, "center_of_mass_dot"):
anims.append(self.center_of_mass_dot_anim)
if hasattr(self, "fourier_graph_dot"):
anims.append(self.fourier_graph_dot_anim)
+ if hasattr(self, "fourier_graph_drawing_update_anim"):
+ anims.append(self.fourier_graph_drawing_update_anim)
+ anims += added_anims
self.play(*anims, **kwargs)
def create_pi_creature(self):
@@ -1605,8 +1621,8 @@ class ShowLowerFrequency(DrawFrequencyPlot):
self.wait()
#Show fourier graph
- fourier_graph = get_fourier_transform_graph(graph)
- fourier_graph_update = self.get_fouier_graph_drawing_update_anim(
+ fourier_graph = self.get_fourier_transform_graph(graph)
+ fourier_graph_update = self.get_fourier_graph_drawing_update_anim(
fourier_graph
)
x_coord_label = TextMobject(
@@ -1624,14 +1640,12 @@ class ShowLowerFrequency(DrawFrequencyPlot):
self.play(Write(x_coord_label))
self.change_frequency(
self.signal_frequency,
- added_anims = [fourier_graph_update],
run_time = 10,
rate_func = smooth,
)
self.wait()
self.change_frequency(
self.frequency_axes.x_max,
- added_anims = [fourier_graph_update],
run_time = 15,
rate_func = smooth,
)
@@ -1654,12 +1668,18 @@ class ShowLinearity(DrawFrequencyPlot):
"sum_color": GREEN,
"low_freq" : 2.0,
"high_freq" : 3.0,
+ "circle_plane_config" : {
+ "x_radius" : 2.5,
+ "y_radius" : 2.7,
+ "x_unit_size" : 0.8,
+ "y_unit_size" : 0.8,
+ },
}
def construct(self):
self.remove(self.pi_creature)
self.show_sum_of_signals()
self.show_winding_with_sum_graph()
- self.point_out_two_spikes()
+ self.show_vector_rotation()
def show_sum_of_signals(self):
low_freq, high_freq = self.low_freq, self.high_freq
@@ -1743,8 +1763,8 @@ class ShowLinearity(DrawFrequencyPlot):
self.center_of_mass_dot = dot
self.generate_center_of_mass_dot_update_anim()
- fourier_graph = get_fourier_transform_graph(graph)
- fourier_graph_update = self.get_fouier_graph_drawing_update_anim(
+ fourier_graph = self.get_fourier_transform_graph(graph)
+ fourier_graph_update = self.get_fourier_graph_drawing_update_anim(
fourier_graph
)
x_coord_label = TextMobject(
@@ -1781,13 +1801,18 @@ class ShowLinearity(DrawFrequencyPlot):
for freq in freqs:
self.change_frequency(
freq,
- added_anims = [fourier_graph_update],
run_time = 8,
rate_func = bezier([0, 0, 1, 1]),
)
- def point_out_two_spikes(self):
- pass
+ def show_vector_rotation(self):
+ self.fourier_graph_drawing_update_anim = Animation(Mobject())
+ self.change_frequency(self.low_freq)
+ self.play(*self.get_vector_animations(
+ self.graph, draw_polarized_graph = False,
+ run_time = 20,
+ ))
+ self.wait()
class ShowCommutativeDiagram(ShowLinearity):
CONFIG = {
@@ -2144,11 +2169,11 @@ class FilterOutHighPitch(AddingPureFrequencies, ShowCommutativeDiagram):
time_graph.underlying_function,
t_min = 0, t_max = 30,
)
- def alt_fourier_func(t):
- bell = smooth(t)*0.3*np.exp(-0.8*(t-0.9)**2)
- return bell + (smooth(t/3)+0.2)*fourier_func(t)
+ # def alt_fourier_func(t):
+ # bell = smooth(t)*0.3*np.exp(-0.8*(t-0.9)**2)
+ # return bell + (smooth(t/3)+0.2)*fourier_func(t)
fourier_graph = frequency_axes.get_graph(
- alt_fourier_func, num_graph_points = 150,
+ fourier_func, num_graph_points = 150,
)
fourier_graph.highlight(RED)
frequency_axes.graph = fourier_graph
@@ -2176,6 +2201,7 @@ class FilterOutHighPitch(AddingPureFrequencies, ShowCommutativeDiagram):
self.frequency_axes = frequency_axes
self.fourier_graph = fourier_graph
self.spike_rect = spike_rect
+ self.to_fourier_arrow = arrow
def filter_out_high_pitch(self):
fourier_graph = self.fourier_graph
@@ -2203,7 +2229,38 @@ class FilterOutHighPitch(AddingPureFrequencies, ShowCommutativeDiagram):
self.wait()
def mention_inverse_transform(self):
- pass
+ time_axes = self.time_axes
+ time_graph = self.time_graph
+ fourier_graph = self.fourier_graph
+ frequency_axes = self.frequency_axes
+ f_min = frequency_axes.x_min
+ f_max = frequency_axes.x_max
+
+ filtered_graph = time_axes.get_graph(
+ lambda t : time_graph.underlying_function(t)-np.cos(TAU*3*t)
+ )
+ filtered_graph.highlight(BLUE_C)
+
+ to_fourier_arrow = self.to_fourier_arrow
+ arrow = to_fourier_arrow.copy()
+ arrow.rotate(TAU/2, about_edge = LEFT)
+ arrow.shift(MED_SMALL_BUFF*LEFT)
+ inv_fourier_words = TextMobject("Inverse Fourier \\\\ transform")
+ inv_fourier_words.next_to(arrow, LEFT)
+ VGroup(arrow, inv_fourier_words).highlight(MAROON_B)
+
+ self.play(
+ GrowArrow(arrow),
+ Write(inv_fourier_words)
+ )
+ self.wait()
+ self.play(
+ time_graph.fade, 0.9,
+ ReplacementTransform(
+ fourier_graph.copy(), filtered_graph
+ )
+ )
+ self.wait()
##
@@ -2219,6 +2276,209 @@ class FilterOutHighPitch(AddingPureFrequencies, ShowCommutativeDiagram):
for n in 5, 7, 10, 12
]
+class AskAboutInverseFourier(TeacherStudentsScene):
+ def construct(self):
+ self.student_says("Inverse Fourier?")
+ self.change_student_modes("confused", "raise_right_hand", "confused")
+ self.wait(2)
+
+class ApplyFourierToFourier(DrawFrequencyPlot):
+ CONFIG = {
+ "time_axes_config" : {
+ "y_min" : -1.5,
+ "y_max" : 1.5,
+ "x_max" : 5,
+ "x_axis_config" : {
+ "numbers_to_show" : range(1, 5),
+ "unit_size" : 2.5,
+ },
+ },
+ "frequency_axes_config" : {
+ "y_min" : -0.6,
+ "y_max" : 0.6,
+ },
+ "circle_plane_config" : {
+ "x_radius" : 1.5,
+ "y_radius" : 1.35,
+ "x_unit_size" : 1.5,
+ "y_unit_size" : 1.5,
+ },
+ "default_num_v_lines_indicating_periods" : 0,
+ "signal_frequency" : 2,
+ }
+ def construct(self):
+ self.setup_fourier_display()
+ self.swap_graphs()
+
+ def setup_fourier_display(self):
+ self.force_skipping()
+ self.setup_graph()
+ self.show_center_of_mass_dot()
+ self.introduce_frequency_plot()
+ self.draw_full_frequency_plot()
+ self.time_axes.remove(self.time_axes.labels)
+ self.remove(self.beats_per_second_label)
+ VGroup(
+ self.time_axes, self.graph,
+ self.frequency_axes, self.fourier_graph,
+ self.x_coord_label,
+ self.fourier_graph_dot,
+ ).to_edge(UP, buff = MED_SMALL_BUFF)
+ self.revert_to_original_skipping_status()
+
+ def swap_graphs(self):
+ fourier_graph = self.fourier_graph
+ time_graph = self.graph
+ wound_up_graph = time_graph.polarized_mobject
+ time_axes = self.time_axes
+ frequency_axes = self.frequency_axes
+
+ f_max = self.frequency_axes.x_max
+ new_fourier_graph = time_axes.get_graph(
+ lambda t : 2*fourier_graph.underlying_function(t)
+ )
+ new_fourier_graph.match_style(fourier_graph)
+
+ self.remove(fourier_graph)
+ self.play(
+ ReplacementTransform(
+ fourier_graph.copy(),
+ new_fourier_graph
+ ),
+ ApplyMethod(
+ time_graph.shift, 3*UP+10*LEFT,
+ remover = True,
+ ),
+ )
+ self.play(
+ wound_up_graph.next_to, SPACE_WIDTH*LEFT, LEFT,
+ remover = True
+ )
+ self.wait()
+
+ self.graph = new_fourier_graph
+ wound_up_graph = self.get_polarized_mobject(new_fourier_graph, freq = 0)
+ double_fourier_graph = frequency_axes.get_graph(
+ lambda t : 0.25*np.cos(TAU*2*t)
+ ).highlight(PINK)
+ self.fourier_graph = double_fourier_graph
+ self.remove(self.fourier_graph_dot)
+ self.get_fourier_graph_drawing_update_anim(double_fourier_graph)
+ self.generate_fourier_dot_transform(double_fourier_graph)
+ self.center_of_mass_dot.highlight(PINK)
+ self.generate_center_of_mass_dot_update_anim()
+ def new_get_pol_graph_center_of_mass():
+ result = DrawFrequencyPlot.get_pol_graph_center_of_mass(self)
+ result -= self.circle_plane.coords_to_point(0, 0)
+ result *= 25
+ result += self.circle_plane.coords_to_point(0, 0)
+ return result
+ self.get_pol_graph_center_of_mass = new_get_pol_graph_center_of_mass
+
+ self.play(
+ ReplacementTransform(self.graph.copy(), wound_up_graph),
+ ChangeDecimalToValue(
+ self.winding_freq_label[1], 0.0,
+ run_time = 0.2,
+ )
+ )
+ self.change_frequency(5.0, run_time = 15, rate_func = None)
+ self.wait()
+
+ ##
+
+ def get_cosine_wave(self, freq, **kwargs):
+ kwargs["shift_val"] = 0
+ kwargs["scale_val"] = 1.0
+ return DrawFrequencyPlot.get_cosine_wave(self, freq, **kwargs)
+
+class WhiteComplexExponentialExpression(DrawFrequencyPlot):
+ CONFIG = {
+ "signal_frequency" : 2.0,
+ "default_num_v_lines_indicating_periods" : 0,
+ }
+ def construct(self):
+ self.remove(self.pi_creature)
+ self.setup_plane()
+ self.setup_graph()
+ self.show_winding_with_both_coordinates()
+ self.show_plane_as_complex_plane()
+ self.show_eulers_formula()
+ self.reference_other_video()
+ self.show_winding_graph_expression()
+
+ def setup_plane(self):
+ circle_plane = ComplexPlane(
+ unit_size = 2,
+ y_radius = SPACE_HEIGHT+LARGE_BUFF
+ )
+ circle_plane.shift(DOWN)
+ circle = DashedLine(ORIGIN, TAU*UP)
+ circle.apply_complex_function(
+ lambda z : R3_to_complex(
+ circle_plane.number_to_point(np.exp(z))
+ )
+ )
+ circle_plane.add(circle)
+
+ time_axes = self.get_time_axes()
+ time_axes.add_to_back(BackgroundRectangle(
+ time_axes,
+ fill_opacity = 0.9,
+ buff = MED_SMALL_BUFF,
+ ))
+ time_axes.scale(0.7)
+ time_axes.to_corner(UP+LEFT, buff = 0)
+ time_axes.set_stroke(color = WHITE, width = 1)
+
+ self.add(circle_plane)
+ self.add(time_axes)
+
+ self.circle_plane = circle_plane
+ self.time_axes = time_axes
+
+ def setup_graph(self):
+ plane = self.circle_plane
+ graph = self.graph = self.get_cosine_wave(
+ freq = self.signal_frequency,
+ scale_val = 0.5,
+ shift_val = 0.75,
+ )
+ freq = 0.1
+ pol_graph = self.get_polarized_mobject(graph, freq = freq)
+ wps_label = self.get_winding_frequency_label()
+ ChangeDecimalToValue(wps_label[0], freq).update(1)
+ wps_label.add_to_back(BackgroundRectangle(wps_label))
+ wps_label.next_to(plane.coords_to_point(0, 1), DOWN)
+ wps_label.to_edge(LEFT)
+ self.get_center_of_mass_dot()
+ self.generate_center_of_mass_dot_update_anim()
+
+ self.add(graph, pol_graph, wps_label)
+
+ def show_winding_with_both_coordinates(self):
+ #TODO, tie dashed lines to dot
+
+ self.change_frequency(
+ 2.0, run_time = 15,
+ rate_func = bezier([0, 0, 1, 1])
+ )
+ self.wait()
+
+ def show_plane_as_complex_plane(self):
+ pass
+
+ def show_eulers_formula(self):
+ pass
+
+ def reference_other_video(self):
+ pass
+
+ def show_winding_graph_expression(self):
+ pass
+
+
+
diff --git a/topics/complex_numbers.py b/topics/complex_numbers.py
index 7670d0960..c411cd201 100644
--- a/topics/complex_numbers.py
+++ b/topics/complex_numbers.py
@@ -166,11 +166,11 @@ def complex_string(complex_num):
class ComplexPlane(NumberPlane):
CONFIG = {
- "color" : BLUE,
- "unit_size" : 1,
- "line_frequency" : 1,
+ "color" : BLUE,
+ "unit_size" : 1,
+ "line_frequency" : 1,
"faded_line_frequency" : 0.5,
- "number_scale_factor" : 0.5,
+ "number_scale_factor" : 0.5,
}
def __init__(self, **kwargs):
digest_config(self, kwargs)
diff --git a/topics/geometry.py b/topics/geometry.py
index b67d93f14..2c242b819 100644
--- a/topics/geometry.py
+++ b/topics/geometry.py
@@ -76,8 +76,6 @@ class Dot(Circle):
self.shift(point)
self.init_colors()
-
-
class AnnularSector(VMobject):
CONFIG = {
"inner_radius" : 1,
@@ -128,10 +126,6 @@ class AnnularSector(VMobject):
# arc_center = inner_arc_start_point - inner_arc.radius * radial_unit_vector
return arc_center
-
-
-
-
class Sector(AnnularSector):
CONFIG = {
@@ -147,8 +141,6 @@ class Sector(AnnularSector):
def radius(self,new_radius):
self.outer_radius = new_radius
-
-
class Annulus(Circle):
CONFIG = {
"inner_radius": 1,
@@ -167,7 +159,6 @@ class Annulus(Circle):
inner_circle.flip()
self.add_subpath(inner_circle.points)
-
class Line(VMobject):
CONFIG = {
"buff" : 0,