Math_Music
查看代码
#REmoo的优化任务
#1、公式写在<formula_set>类中,统一管理 --- Finished 2022.8.15 12:39
#2、建立<sample_set>类,统一管理数据样本 --- Finished 2022.8.15 13:03
#3、统一化manim动画流程 ---Finished 2022.8.16 11:29
#1、流程:配置全局参数-导入公式-导入sample-定义坐标图像-
#2、整理每段Video Clip的animation部分
#4、提高代码重用性
from manim import *
import librosa
import numpy as np
import pickle #写出读取打包 序列化二进制list
# 文件都是以二进制的方式储存在硬盘上
# 那么如果有一个int :13 存在硬盘,转成二进制即可
# 如果我们有一个list [1,2,3,4,5]呢 甚至是 ["hello",12,1.11,Dot()]
# 那么我们就要序列化list 打包好--使用pickle
# 所有文件都是序列化的,方式不同。我用list写出到文件,我就用list的方式读取,中间过程需要序列化。
#import librosa
#import pickle
#samp = [1,2,3,1.11]
#with open('test.iiiiii','rb') as pickle_file:
# #序列化samp对象,以二进制的方式写到文件
# pickle.dump(samp,pickle_file)
###################################################
#
#import librosa
#import pickle
#with open('winter.libyy','rb') as pickle_file:
# #反序列化对象
# samp_read = pickle.load(pickle_file)
# print(samp_read)
#>>> samp_read = [1,2,3,1.11]
#
# # ##生成wave文件的数据集样本文件##
# class UsingLibrosa():
# #利用librosa读取wave文件
# yy44,fs44 = librosa.load('winter.wav',sr=44100)
# #写出数据样本文件
# with open('winter.libyy','wb') as pickle_file:
# #序列化对象
# pickle.dump(yy44, pickle_file)
class sys:
frame_window = 2048
def Create_Dot_Set(self,axes,sample,Vg,dot_radius=0.02,dot_color=BLUE_A,run_time=1):
dots_set = []
_len_sample = int(len(sample))
for x in range(_len_sample):
dots_set.append(Dot(radius=dot_radius,color=dot_color,fill_opacity=0.8))
dots_set[x].move_to(axes.c2p(x,sample[x]))
self.add(dots_set[x])
Vg.add(dots_set[x])
return dots_set
##数据类##
class sample_set:
def get_winter_wav():
with open('winter.libyy','rb') as pickle_file:
#反序列化对象
sample = pickle.load(pickle_file)
return sample,max(sample)
def get_sam():
pass
# WAVE_FILE, SAM_RATE = librosa.load(r'003.wav')
# NUM_DOTS = len(WAVE_FILE) # number of discrete point
# DURATION = NUM_DOTS/SAM_RATE # duration of the audio (second)
# SAMPLE_DENSITY = 1 / 1 # the density of point
# data = WAVE_FILE[1000:1030:int(1/SAMPLE_DENSITY)]
#len_data = len(data)
##公式类##
class formula_set(MathTex):
formula_001 = MathTex(r"F\left(\omega_{k}\right) \equiv \int_{-\infty}^{\infty} f(t) e^{-2 \pi i k t} \mathrm{~d} t, \quad k \in(-\infty, \infty)")
formula_002 = [MathTex(
r"\hat{f}_{k}=\sum_{j=0}^{n-1} f_{j} e^{-i 2 \pi k j / n}"
),
MathTex(
r"k=0,1,2,...,n-1"
) ]
formula_004 = MathTex(
r"\left[\begin{array}{l}\
f_{0} \\\
f_{1} \\\
f_{2} \\\
f_{3} \\\
f_{4} \\\
f_{5} \\\
f_{6} \\\
f_{7} \\\
f_{8} \\\
f_{9} \\\
\end{array}\right]"
)
formula_006 = [
MathTex(
r"\left[\begin{array}{l}\
e^{-i 2 \pi(2(0)) / 9 }\\\
e^{-i 2 \pi(2(1)) / 9 }\\\
e^{-i 2 \pi(2(2)) / 9 }\\\
e^{-i 2 \pi(2(3)) / 9 }\\\
e^{-i 2 \pi(2(4)) / 9 }\\\
e^{-i 2 \pi(2(5)) / 9 }\\\
e^{-i 2 \pi(2(6)) / 9 }\\\
e^{-i 2 \pi(2(7)) / 9 }\\\
e^{-i 2 \pi(2(8)) / 9 }\\\
e^{-i 2 \pi(2(9)) / 9 }\\\
\end{array}\right]"
),
MathTex(r"e^{-i 2 \pi(2(0)) / 19")
]
#Almost Fourier Transform - 直坐标到极坐标,然后扭圈圈
class ContinuousFourier(Scene):
def construct(self):
#### Global variables ####
## Duration of sound wave
second = 3
y_scale = 0.5
a = TAU/second*y_scale
Da = DecimalNumber(a)
#function
f = lambda x: y_scale*np.cos(3*2*PI*x)+y_scale
#### manim_graph ####
axes = Axes(
x_range=[0 , second , 1],
y_range=[0, 1.1, 1],
x_length=(7+1/9)*2*1.8,
y_length=4*3/4,
axis_config={"include_numbers": True},
)
polar_axix = PolarPlane(radius_max=1,size=5)
normal_graph = axes.plot(f,color=YELLOW)
polar_graph = polar_axix.plot_parametric_curve(
lambda t : [
+np.cos(a*t) * (f(t)),
-np.sin(a*t) * (f(t)),
0
],t_range=[0,second],color=BLUE
)
group1 = VGroup(axes,normal_graph)
group1.scale(0.5).to_edge(UP)
axes_label = axes.get_axis_labels()
upfunc = lambda obj : obj.become(polar_axix.plot_parametric_curve(
lambda t : [
+np.cos(Da.get_value()*t) * (f(t)),
-np.sin(Da.get_value()*t) * (f(t)),
0
],
t_range=[0,second],
color=BLUE
))
polar_graph.add_updater(upfunc)
group2 = VGroup(polar_axix,polar_graph)
group2.to_edge(DL)
########################################################
# Start Animation #
########################################################
self.play(Write(Da))
self.add(axes,polar_axix,axes_label,normal_graph)
self.play(ReplacementTransform(normal_graph,polar_graph),
run_time=3.5,
path_arc = -TAU*2/3)
self.wait()
self.play(ChangeDecimalToValue(Da,5),runtime=3)
self.wait()
class DFT_preparation(Scene):
def construct(self):
# After zoom in in Lib 散点图作为背景,虚化。前面显示以下。
tex = Text("Dicrete Fourier Transform")
formula = VGroup(*formula_set.formula_002)
formula.arrange(DOWN,aligned_edge=LEFT,buff=0.8)
second = 1
y_scale = 0.5
f = lambda x: y_scale*np.cos(30 * x)+y_scale
ax = Axes(
x_range=[0 , second , 1],
y_range=[0, 1.1, 1],
x_length=(7+1/9)*2*1.8,
y_length=4*3/4,
axis_config={"include_numbers": True},
).to_edge(DOWN)
wave = ax.plot(f,color=YELLOW)
group = VGroup(ax,wave).scale(0.5)
########################################################
# Start Animation #
########################################################
self.play(FadeIn(tex))
self.wait()
self.play(tex.animate.to_edge(UL),FadeIn(formula))
self.wait()
self.play(FadeOut(formula),FadeIn(group))
self.wait()
class PlotDots(Scene):
def Create_Dot_Set(self,axes,sample,Vg,run_time=1):
dots_set = []
_len_sample = int(len(sample))
for x in range(_len_sample):
dots_set.append(Dot(radius=0.02,color=BLUE_A,fill_opacity=0.8))
dots_set[x].move_to(axes.c2p(x,sample[x]))
self.add(dots_set[x])
Vg.add(dots_set[x])
##如果需要逐个点展示,请取消下行注释即可
# self.play(FadeIn(dots_set[x]),run_time=run_time/_len_sample)
return dots_set
def construct(self):
########################################################
# GET SAMPLE #
########################################################
sample,_abs_big_ = sample_set.get_winter_wav()
windows_dot_num = 2048
shift = 10000
sample_part=sample[shift:shift+windows_dot_num]
_big_ = max([abs(max(list(sample_part))),abs(min(list(sample_part)))])
print("样本点总数量:"+str(len(sample))+"\n当前节选样本区域:"+str(shift)+","+str(shift+windows_dot_num))
#### manim_graph ####
axes = Axes(x_range=(0,windows_dot_num+1,512),
y_range=(-_big_,_big_,round((_big_)/2,2)),
axis_config={"include_numbers": True},)
#np.hanning(windows_dot_num)
ax_TOTAL = VGroup(axes)
dots_set = self.Create_Dot_Set(axes,sample_part,ax_TOTAL,run_time=1)
formula_004 = formula_set.formula_004.to_edge(LEFT).scale(0.5)
########################################################
# Start Animation #
########################################################
self.add(axes)#Add coordinates()
self.play(Write(formula_004),ax_TOTAL.animate.next_to(formula_004,RIGHT))
_tc = 0 #temp Int_counter
_ts=[] #temp Animation_set
for dot in dots_set:
if _tc <= 9 * 2:
_ts.append(Transform(dot,formula_004[0][18+_tc:20+_tc]))
_tc = _tc +2
self.play(*_ts,run_time=3)
self.wait()
class FormulaTest(Scene):
def construct(self):
formula_002 = formula_set.formula_002[0].shift(UP)
formula_003 = formula_set.formula_002[1].next_to(formula_002, DOWN)
formula_002[0][11:13].color=YELLOW
########################################################
# Start Animation #
########################################################
self.add(formula_002[0],formula_003)
self.play(Write(formula_002[0]), Write(formula_002[1]), run_time=2)
self.play(formula_002[0][11:13].animate.set_color(YELLOW))
self.play(Wiggle(formula_002[0][11:13]))
self.play(Indicate(formula_002[0][11:13]))
self.wait()
#连续图像上打点,然后扭动
class DFT_Illustration(Scene):
def construct(self):
#### Global variables ####
## Duration of sound wave
second = 3
## Winding frequency
a = 1
Da = DecimalNumber(a)
## Ini Dot
NUM_OF_DOT = 12
#### formulas ####
formula_004 = formula_set.formula_004.to_edge(RIGHT)
formula_006 = formula_set.formula_006
formula_006[0].next_to(formula_004,LEFT)
#### functions ####
f = lambda x: 0.5*np.cos(3*2*PI*x)+0.5
Wind_f =lambda t : [
+np.cos(Da.get_value()*t) * (f(t)),
-np.sin(Da.get_value()*t) * (f(t)),
0
]
#### manim_graph ####
ax = PolarPlane(radius_max=1,size=7)
#### Curve Mobject ####
def _get_curve_graph_(func):
return ax.plot_parametric_curve(
func,
t_range=[0,second],
color=PINK)
curve = _get_curve_graph_(Wind_f)
#### Dot Mobject ####
def _set_plot_dot_(D_set,curve):
for value in range(NUM_OF_DOT):
position = ax.i2gp(graph=curve,x=value*PI/NUM_OF_DOT)
D_set.append(Dot(color=YELLOW).move_to(position))
_dot_set = []
_set_plot_dot_(_dot_set,curve)
########################################################
# UPDATE FUNCTION #
########################################################
def upfunc(curve):
curve.become(_get_curve_graph_(Wind_f))
def up_dots(curve):
new_dot_set = []
_set_plot_dot_(new_dot_set,curve)
for dot,new_dot in zip(_dot_set,new_dot_set):
dot.move_to(new_dot)
self.add(*_dot_set)
####Add updater####
curve.add_updater(up_dots)
curve.add_updater(upfunc)
########################################################
# Start Animation #
########################################################
#self.play(Write(formula_004),Write(formula_006[0]))
#self.play(FadeOut(formula_004,formula_006[0]))
self.add(ax, curve)
self.play(ChangeDecimalToValue(Da,15,rate_func=rate_functions.linear),run_time=20)
class DFT_RealData(Scene):
def Create_DotSet_Polar(self,axes,sample,Vg,freq=0,run_time=1):
dots_set = []
_len_sample = int(len(sample))
for x in range(_len_sample):
dots_set.append(Dot(radius=0.02,color=BLUE_A,fill_opacity=0.8))
dots_set[x].move_to(axes.polar_to_point(sample[x],x/_len_sample*freq))
Vg.add(dots_set[x])
return dots_set
def construct(self):
########################################################
# GET SAMPLE #
########################################################
sample,_abs_big_ = sample_set.get_winter_wav()
windows_dot_num = 512
shift = 10000
sample_part=abs(sample[shift:shift+windows_dot_num])
_big_ = max([abs(max(list(sample_part))),abs(min(list(sample_part)))])
print("number of sample:"+str(len(sample))+"\ncurrent interval:"+str(shift)+","+str(shift+windows_dot_num))
#### manim_graph ####
polar_axix = Axes(x_range=(-_big_,_big_),
y_range=(-_big_,_big_),
x_length=8,y_length=8,
axis_config={"include_numbers": True})
graph_total = VGroup(polar_axix)
polar_data = self.Create_DotSet_Polar(polar_axix, sample_part,graph_total)
########################################################
# Start Animation #
########################################################
self.add(polar_axix,*polar_data)
旋转圈数 = 3
播放倍数 = 1
_精细度_ = config.frame_rate*旋转圈数/播放倍数
for x in range(int(_精细度_)+1):
new_dots=self.Create_DotSet_Polar(polar_axix, sample_part,graph_total,(x)/(_精细度_)*TAU*旋转圈数)#即绕一个圈中间有多少帧
count=-1;ani_set=[]
for obj in polar_data:
count=count+1
ani_set.append(Transform(obj,new_dots[count]))
self.play(*ani_set,run_time=1/config.frame_rate,rate_func=rate_functions.linear)
class STFT(Scene):
def construct(self):
self.wait()
# formula_004a = MathTex(r"e^{-i 2 \pi(2(0)) / 19")
# self.add_subcaption("Hello sub!", duration=1)
# self.play(Write(formula_004a))
class M_Shift(Scene):
def Create_Dot_Set(self,axes,sample,Vg,run_time=1):
dots_set = []
_len_sample = int(len(sample))
for x in range(_len_sample):
dots_set.append(Dot(radius=0.02,color=BLUE_A,fill_opacity=0.8))
dots_set[x].move_to(axes.c2p(x,sample[x]))
self.add(dots_set[x])
Vg.add(dots_set[x])
return dots_set
def Create_DotSet_Polar(self,axes,sample,Vg,freq=0,run_time=1):
dots_set = []
_len_sample = int(len(sample))
for x in range(_len_sample):
dots_set.append(Dot(radius=0.02,color=BLUE_A,fill_opacity=0.8))
dots_set[x].move_to(axes.polar_to_point(sample[x],x/_len_sample*freq))
Vg.add(dots_set[x])
return dots_set
#方案:
#1、先往左边移动,然后Transform到新的点集
#2、直接Transform,但是就是担心transform大量Mobject消耗时间
#3、删除点集合,但是方法失效
def construct(self):
########################################################
# GET SAMPLE #
########################################################
sample,_abs_big_ = sample_set.get_winter_wav()
windows_dot_num = 4096
shift = 10000
sample_part=sample[shift:shift+windows_dot_num]
_big_ = max([abs(max(list(sample_part))),abs(min(list(sample_part)))])
print("number of sample:"+str(len(sample))+"\ncurrent interval:"+str(shift)+","+str(shift+windows_dot_num))
#### manim_graph ####
axes = Axes(x_range=(0,windows_dot_num+1,512),
y_range=(-_big_,_big_,round((_big_)/2,2)),
axis_config={"include_numbers": True},)
text_pos = VGroup(Text('Time Domain x_range:',font_size=22),
Text('['+str(shift)+','+str(shift+windows_dot_num)+']',font_size=22))
text_pos.arrange(DOWN,aligned_edge=ORIGIN,buff=0.2).to_edge(UP)
#np.hanning(windows_dot_num)
ax_TOTAL = VGroup(axes).next_to(text_pos,DOWN)
dots_set = self.Create_Dot_Set(axes,sample_part,ax_TOTAL)
########################################################
# Start Animation #
########################################################
self.add(ax_TOTAL,text_pos)#Add coordinates()
self.wait()
Math_Music的更多相关文章
随机推荐
- docker-compose 搭建 Prometheus+Grafana监控系统
有关监控选型之前有写过一篇文章: 监控系统选型,一文轻松搞定! 监控对象 Linux服务器 Docker Redis MySQL 数据采集 1).prometheus: 采集数据 2).node-ex ...
- 六张图详解LinkedList 源码解析
LinkedList 底层基于链表实现,增删不需要移动数据,所以效率很高.但是查询和修改数据的效率低,不能像数组那样根据下标快速的定位到数据,需要一个一个遍历数据. 基本结构 LinkedList 是 ...
- 二:动手实操SpringBoot-使用Spring Initializr创建项目
使用 Spring Initializr 初始化 Spring Boot 项目 Spring Initializr 从本质上说就是一个Web应用程序,它能为你构建Spring Boot项目结构. 虽然 ...
- TopoLVM: 基于LVM的Kubernetes本地持久化方案,容量感知,动态创建PV,轻松使用本地磁盘
正文 研发测试场景下,一般追求的是一键快速起环境,横向动态复制,一人一套,随起随用,用完即走.作为使用方,其不用关心实际的物理资源是怎样的,环境起在哪里,只要声明自己的使用需求即可.但作为方案构建者以 ...
- Linux 安装Apche服务
用yum 进行在线安装apche服务 yum install -y httpd 我这边是centos7 需要开启一下端口: 1 firewall-cmd --zone=public --add-por ...
- 什么是pytorch?
Pytorch是基于python的科学计算包,为两类受众提供服务 作为Numpy的替换,让你可以使用GPU的算力 作为一个深度学习计算平台提供最大的计算灵活性与速度 开始体验pytorch的基础功能 ...
- 集合容器和Hash表
集合容器 集合相当于一个容器,在我们使用Arraylist的时候添加参数相当与放了一个容器中.这里面的元素是可以重复的 在HashSet中添加元素是没有重复的,我们来写一个测试看一下 public s ...
- Harbor-私有镜像仓库的安装部署
Harbor 安装条件 官网给出了安装需要的最低硬件和软件的条件:https://goharbor.io/docs/2.0.0/install-config/installation-prereqs/ ...
- eclipse使用小记录
(手动狗头)之前用eclipse的时候左侧的project栏不知道为什么整没了....记录一下 1.击Window--how View--other 2.Project Explorer,就可以了
- day08 集合API | 遍历_ | 泛型 |增强For循环
集合(续) 集合间的操作 集合提供了如取并集,删交集,判断包含子集等操作 package collection; import java.util.ArrayList; import java.uti ...