首页 > 编程知识 正文

python实现傅里叶变换(python傅里叶变换函数)

时间:2023-05-03 10:59:35 阅读:102007 作者:182

1描述:

=====

1.1代码来源:感谢原作者并对代码进行修改、删除和批注。

https://github.com/VacantHusky/Fourier-visualization1.2直接效果图:四种傅立叶波形(智能笔、锯齿波、半圆波、三角波)的可视化效果。

1.3智能笔:

功能性调节

1.4锯齿波:

1.5半圆波:

1.6三角波:

2代码:

=====

2.1步骤1:导入模块

导入pygame、数学、时间、随机、操作系统

来自pygame.locals import *

从系统导入exit2.2步骤2:

#第2步:颜色初始化设置

白色=(255,255,255)

black=(0,0,0)

黄色=(200,200,0)

绿色=(0,255,0)

蓝色=(0,0,255)

红色=(255,0,0)

#每次随机打开圆圈的线条颜色都不一样。它来自这里,你可以随意添加和删除。

color _ list=[蓝色、红色、黄色、绿色、白色] 2.3步骤3:

#第三步:参数设置

#窗户的宽度和高度

窗宽=1800

窗口_小时=1200

一次性=1 #时间流量(默认为1)

比例=120 #比例(默认为120)

FPS=60 #帧速率

point _ size=8 #点的大小

Start _ xy=(300,int (window _ h/2)) #圆的位置

#波形图参数

B_xy=(600,start_xy[1]) #波形图原点坐标

B_scale=1 #波形比例

B_color=黄色#波形颜色

B_length=int(WINDOW_W/2) #右边波形图显示的长度,初始化为500,可通过2.4进行调整步骤4:

#第四步:定义正确的波形函数

Get _ wave(名称='智能笔',max_i=20):

'''

这里可以得到一些具体的波形。

:参数名:波形名:智能笔、锯齿波、半圆波、三角波。

:param max_i:你用几个三角函数来拟合?

:返回:该波形的参数列表

'''

max_i=max(max_i,1)

fourier_list=[]

If name=='智能笔' :

A_max=4/math.pi

对于范围(1,max_i):中的I

Fourier _ list . append([A _ max/(2*i-1),2 * I-1,0])

Elif名称=='锯齿波' :

A_max=1/math.pi

对于范围(1,max_i):中的I

傅立叶列表追加([A_max/i,I,0])

Elif名称=='半圆波' :

A_max=8/(math.pi**2)

对于范围(1,max_i):中的I

Fourier _ list . append([A _ max/(2*i-1)* * 2,2 * I-1,0)]

Elif名称=='三角波' :

A_max=8/(math.pi**2)

对于范围(1,max_i):中的I

Fourier _ list . append([A _ max *(1)* *(I-1))/(2*i-1)* * 2,2 * I-1,0])

else:

太阳神

ise TypeError('未知的类型') return fourier_list

2.5 第5步:自己增加的

#第5步:终端输入需要查看波形 print('请输入要查看的波形:聪明的钢笔,锯齿波,半圆波,三角波;请关闭窗口“×"退出') a = input("本次输入后查看的波形是:") fourier_list = get_wave(a)

2.6 第6步:如果报错,就是这一步,字体需要自己提前弄好

#第6步:窗口初始化设定 # 初始化pygame pygame.init() #os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (10, 40) # 创建一个窗口 screen = pygame.display.set_mode((WINDOW_W, WINDOW_H), pygame.DOUBLEBUF, 32) pygame.display.set_caption("傅里叶变换可视化") #显示中文字体,hwfs=华文仿宋,可自行下载相关字体,放在根目录下 font = pygame.font.Font('hwfs.ttf', 40) #字体变大后,注意上下行间距调整

2.7 第7步:核心一,没动

#第7步:画圆函数 class Circle(): x, y = 0, 0 r = 0 angle = 0 angle_v = 0 color = (0, 0, 0) #初始化主圆的波形颜色 father = None def __init__(self, r, angle_v, angle, color=None, father=None): self.r = r self.angle_v = angle_v self.angle = angle self.father = father if color is None: #主圆的波形颜色,随机选择 self.color = random.choice(color_list) else: self.color = color def set_xy(self, xy): self.x, self.y = xy def get_xy(self): return (self.x, self.y) def set_xy_by_angle(self): self.x = self.father.x + self.r * math.cos(self.angle) * scale self.y = self.father.y + self.r * math.sin(self.angle) * scale def run(self, step_time): if self.father is not None: self.angle += self.angle_v * step_time self.set_xy_by_angle() def draw(self, screen): color_an = tuple(map(lambda x: x // 3, self.color)) # 画圆 pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), point_size) # 画轨道 if self.father is not None: pygame.draw.circle(screen, color_an, (int(self.father.x), int(self.father.y)), max(int(self.r * scale), 1),1) pygame.draw.line(screen, self.color, (int(self.father.x), int(self.father.y)), (int(self.x), int(self.y)), 1)

2.8 第8步:

#第8步:圆内嵌套圆函数 class Boxin(): ys = [] def add_point(self, y): self.ys.append(y) if len(self.ys) > b_length: self.ys.pop(0) def draw(self, screen): # 画一个圆 pygame.draw.circle(screen, b_color, (b_xy[0], int(b_xy[1]+self.ys[-1] * scale)), point_size) bl = len(self.ys) for i in range(bl - 1): pygame.draw.line(screen, b_color, (b_xy[0] + int((bl - i) * b_scale), int(b_xy[1]+self.ys[i] * scale)), (b_xy[0] + int((bl - i - 1) * b_scale), int(b_xy[1]+self.ys[i + 1] * scale)), 1)

2.9 第9步:

#第9步:傅里叶圆的嵌套圆 fourier_list = sorted(fourier_list, key=lambda x: x[0], reverse=True) super_circle = Circle(0, 0, 0) super_circle.set_xy(start_xy) circle_list = [super_circle] for i in range(len(fourier_list)): p = fourier_list[i] circle_list.append(Circle(p[0], p[1], p[2], father=circle_list[i])) bx = Boxin() clock = pygame.time.Clock()

2.10 第10步:

#第10步:游戏主循环 while True: for event in pygame.event.get(): #退出设置 if event.type == pygame.QUIT: exit() #键盘参数设置 elif event.type == KEYDOWN: #空格键退出 if event.key == K_ESCAPE: exit() #方位:←,功能设定 elif event.key == K_LEFT and one_time>0.1: one_time *= 0.9 one_time = max(one_time,0.1) #按方位键:→,功能设定 elif event.key == K_RIGHT and one_time<10: one_time *= 1.1 #按+或者=,功能设定 elif (event.key == K_EQUALS or event.key == K_PLUS) and scale<800: scale *= 1.1 #按-,功能设定 elif event.key == K_MINUS and scale>0.001: scale *= 0.9 scale = max(scale,0.001) #按字母l或L,功能设定 elif event.key == K_l and b_scale<10: b_scale *= 1.1 #按字母k,功能设定 elif event.key == K_k and b_scale>0.1: b_scale *= 0.9 b_scale = max(b_scale,0.1) else: print(type(event.key),event.key) # 将背景图画上去 screen.fill(black) # 运行 for i, circle in enumerate(circle_list): circle.run(one_time / FPS) circle.draw(screen) last_circle = circle_list[-1] pygame.draw.line(screen, last_circle.color, (int(last_circle.x), int(last_circle.y)), (int(b_xy[0]), int(last_circle.y)), 1) # 画波形 bx.add_point((last_circle.y - b_xy[1]) / scale) bx.draw(screen) # 左上角画文字,注意字体变大后注意上下行间距调整 text_obj = font.render('可调节参数说明:', 1, red) screen.blit(text_obj, (10,10)) text_obj = font.render('←/→键:加速/减速', 11, white) screen.blit(text_obj, (10,55)) text_obj = font.render('+/-键:放大/缩小', 1, white) screen.blit(text_obj, (10,95)) text_obj = font.render('L/K键:波形图放大/缩小', 1, white) screen.blit(text_obj, (10,135)) #可变化的动态值 text_obj = font.render('注意变化后参数:', 1, red) screen.blit(text_obj, (int(WINDOW_W/2),10)) text_obj = font.render('时间速率:{:.3f},放大比例:{:.3f}'.format(one_time,scale), 1, green) screen.blit(text_obj, (int(WINDOW_W/2) ,55)) text_obj = font.render('波形图放大比例:{:.3f}'.format(b_scale), 1, green) screen.blit(text_obj, (int(WINDOW_W/2) ,95)) text_obj = font.render('FPS:{}'.format(clock.get_fps()), 1,green) screen.blit(text_obj, (int(WINDOW_W/2) ,135)) pygame.display.update() time_passed = clock.tick(FPS)

3 傅里叶:

=======

3.1 傅里叶:

傅里叶画像

3.2 (1768 –1830),法国著名数学家、物理学家,1817年当选为科学院院士,1822年任该院终身秘书。

3.3 傅里叶变换:他的主要成就。

可以参考外网的这篇文章,《傅里叶变换的意义和理解(通俗易懂)》。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。