数学动画引擎manim制作傅里叶变换画图视频

玩manim的up主都会日常拿傅里叶变换画图练手。作为manim新手,自然也是要来试一下。

关于利用傅立叶级数绘图的原理部分可参考下面这个链接:
http://www.jezzamon.com/fourier/zh-cn.html
或者 wiki百科傅里叶变换(这个是公式汇总)

首先生成svg图片,图片中包含需要制作的path路径,一般是一些直线和三阶贝塞尔曲线。然后引擎的get_shape方法可以直接处理svg图片生成shape对象,get_path方法通过shape生成程序中所需path对象(实际上为点集,供傅里叶变换采样使用。)

    def get_shape(self):
        shape = SVGMobject(self.file_name)
        return shape

    def get_path(self):
        shape = self.get_shape()
        path = shape.family_members_with_points()[0]
        path.set_height(self.height)
        path.set_fill(opacity=0)
        path.set_stroke(WHITE, 0)
        return path

接下来是制作傅里叶变换画图视频程序中主要用到的数学公式
c_n = \int ^1_0 e^{-2\pi int}f(t)dt
程序中通过上面生成的采样点做复变换后累加得到各参数,源码如下:

    def get_coefficients_of_path(self, path, n_samples=10000, freqs=None):
        if freqs is None:
            freqs = self.get_freqs()
        dt = 1 / n_samples
        ts = np.arange(0, 1, dt)
        samples = np.array([
            path.point_from_proportion(t)
            for t in ts
        ])
        samples -= self.center_point
        complex_samples = samples[:, 0] + 1j * samples[:, 1]

        result = []
        for freq in freqs:
            riemann_sum = np.array([
                np.exp(-TAU * 1j * freq * t) * cs
                for t, cs in zip(ts, complex_samples)
            ]).sum() * dt
            result.append(riemann_sum)
        return result

这里要特别注意的是程序对svg文件中的path格式要求还是比较严格的,如果绘图出错,多半是path格式的文件有点问题。

大致流程就是这样子了,最后来个成果展示。


来个死神小学生试一下(1000个向量)

再来个签名(1000个向量)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容