MicroPython内核开发笔记书内软件用例 :浮点数相关实验
简 介: 本文给出了 MicroPython内核开发笔记:书内嵌入实验任务 中的浮点数运算软件用例部分内容。
关键词
: MicroPython,MM32F3277,FFT
- 本书稿内容隶属于 MicroPython内核开发笔记:书内嵌入实验任务 中的内容。
软件用例:
这部分书稿内容包括有:
- 基本的浮点数运算;
- FFT运算。
- 书稿内位置:
§01 书稿内容
一、基础实验
1、基本浮点数、复数运算
可以在 MicroPython中 进行浮点数、复数的运算。下面给出了浮点数基本运算:
from math import *
a = 1.7
b = 3.4
print(a+b)
print(a*b)
print(a**b)
print(exp(a))
print(sin(b))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
运算结果输出:
5.1
5.78
6.074722
5.473948
-0.2555412
- 1
- 2
- 3
- 4
- 5
使用浮点数的函数库,需要引用 cmath 。下面给出了关于浮点数的相关运算示例:
from cmath import *
a = 1+1j
b = 0.5-1j
print(a+b)
print(a*b)
print(a**b)
print(abs(a))
print(phase(a))
exp(a)
log(b)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
输出结果:
(1.5+0j)
(1.5-0.5j)
(2.60549+0.1202649j)
1.414214
0.7853982
(1.468694+2.287355j)
(0.1115718-1.107149j)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2、基本函数库
通过如下命令,可以查看 math 中可以使用的数学函数:
import math
dir(math)
- 1
- 2
如下是命令输出,可以看到常见到的数学函数都包括。
['__name__', 'pow', 'acos', 'asin', 'atan', 'atan2', 'ceil', 'copysign', 'cos', 'degrees', 'e', 'exp', 'fabs', 'floor', 'fmod', 'frexp', 'isfinite', 'isinf', 'isnan', 'ldexp', 'log', 'modf', 'pi', 'radians', 'sin', 'sqrt', 'tan', 'trunc']
- 1
在 cmath 中包含有的函数库有:
['__name__', 'cos', 'e', 'exp', 'log', 'phase', 'pi', 'polar', 'rect', 'sin', 'sqrt']
- 1
二、实现FFT运算
FFT运算是信号频谱分析最常用到的算法,针对 MicroPython所支持的浮点数、复数运算,下面给出了DIF(时域抽取快速傅里叶变换)递归算法。并且测试了长度为256的举行信号的傅里叶变换。
from math import *
from cmath import *
def FFT(P):
n = len(P)
if n == 1: return P
ye = FFT(P[0::2])
yo = FFT(P[1::2])
y = [0]*n
w = exp(-1j*2*pi/n)
n2 = n//2
for j in range(n//2):
wj = w ** j
yow = [a*wj for a in yo]
y[j] = ye[j] + yow[j]
y[j+n2] = ye[j] - yow[j]
return y
LEN = 256
oneLEN = 10
p1 = [1] * oneLEN + [0] * (LEN - oneLEN)
y = FFT(p1)
yabs = [abs(yy) for yy in y]
print(yabs)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
下面给出了矩形信号傅里叶变换的幅度谱数据。在MM32F3277单片机中运行时间为 0.81秒。
[10.0, 9.97517, 9.900895, 9.777838, 9.607072, 9.390103, 9.128836, 8.825552, 8.482898, 8.103848, 7.691679, 7.249937, 6.782396, 6.293025, 5.785944, 5.265385, 4.735648, 4.201056, 3.665917, 3.134478, 2.610882, 2.099133, 1.603052, 1.126244, 0.672064, 0.2435851, 0.1564261, 0.5255356, 0.8616611, 1.163087, 1.428479, 1.656888, 1.847759, 2.000919, 2.116586, 2.195351, 2.238167, 2.246329, 2.221461, 2.165483, 2.08059, 1.96922, 1.834023, 1.677828, 1.503605, 1.314431, 1.113455, 0.9038545, 0.6888052, 0.4714402, 0.2548177, 0.04188573, 0.1645496, 0.3618536, 0.5475908, 0.7195493, 0.8757596, 1.014518, 1.134397, 1.234257, 1.313254, 1.370846, 1.406789, 1.421134, 1.414214, 1.386666, 1.339365, 1.273443, 1.190256, 1.09137, 0.9785301, 0.8536346, 0.7187103, 0.5758811, 0.4273379, 0.2753101, 0.1220333, 0.03027992, 0.1794671, 0.3234483, 0.460248, 0.5880249, 0.7050903, 0.8099316, 0.9012283, 0.9778691, 1.038963, 1.083848, 1.1121, 1.12353, 1.11819, 1.096367, 1.058574, 1.005545, 0.9382177, 0.8577236, 0.7653668, 0.6626039, 0.5510257, 0.432332, 0.3083075, 0.1807963, 0.05167642, 0.07716717, 0.2038682, 0.3266045, 0.4436239, 0.5532675, 0.6539917, 0.7443892, 0.8232089, 0.8893699, 0.9419779, 0.9803374, 1.003956, 1.012556, 1.006073, 0.9846593, 0.9486776, 0.898699, 0.8354924, 0.760011, 0.6733827, 0.576894, 0.4719654, 0.3601391, 0.2430533, 0.1224208, 0.0, 0.1224198, 0.2430533, 0.3601391, 0.4719653, 0.5768936, 0.6733835, 0.7600111, 0.8354924, 0.8986997, 0.9486781, 0.9846594, 1.006073, 1.012556, 1.003957, 0.9803378, 0.9419788, 0.8893703, 0.823209, 0.7443897, 0.653992, 0.5532677, 0.4436243, 0.3266048, 0.2038684, 0.07716741, 0.05167619, 0.1807961, 0.3083073, 0.4323315, 0.551025, 0.6626033, 0.7653668, 0.8577241, 0.9382182, 1.005545, 1.058574, 1.096366, 1.118189, 1.123529, 1.112099, 1.083847, 1.038961, 0.9778674, 0.9012262, 0.8099288, 0.7050877, 0.5880218, 0.4602449, 0.3234443, 0.1794629, 0.03027497, 0.1220383, 0.2753156, 0.4273441, 0.5758879, 0.7187173, 0.8536419, 0.9785377, 1.091378, 1.190264, 1.273451, 1.339374, 1.386675, 1.414214, 1.421124, 1.406779, 1.370837, 1.313245, 1.234248, 1.134388, 1.014509, 0.8757509, 0.7195406, 0.5475829, 0.3618463, 0.1645427, 0.04189257, 0.2548237, 0.4714459, 0.6888097, 0.9038591, 1.113459, 1.314435, 1.503608, 1.677831, 1.834026, 1.969222, 2.080592, 2.165484, 2.221462, 2.24633, 2.238166, 2.19535, 2.116585, 2.000918, 1.847759, 1.65689, 1.42848, 1.163088, 0.8616615, 0.5255361, 0.1564267, 0.2435843, 0.6720631, 1.126243, 1.603051, 2.099132, 2.610881, 3.134475, 3.665913, 4.201052, 4.735641, 5.265379, 5.785938, 6.293019, 6.78239, 7.24993, 7.691671, 8.103838, 8.482887, 8.825541, 9.128823, 9.390092, 9.607058, 9.777821, 9.900881, 9.975152]
>>>
- 1
- 2
下面是将幅度谱绘制出来的频谱波形,可以验证计算数据的正确性。通过测试可以知道,在当前MM32F3277中的 MicroPython最多支持长度为256的FFT。超过256长度,单片机内存不够了。
▲ 256长度的矩形信号对应的FFT结果的幅度谱
※ 总 结 ※
本文给出了 MicroPython内核开发笔记:书内嵌入实验任务 中的浮点数运算软件用例部分内容。
■ 相关文献链接:
● 相关图表链接:
文章来源: zhuoqing.blog.csdn.net,作者:卓晴,版权归原作者所有,如需转载,请联系作者。
原文链接:zhuoqing.blog.csdn.net/article/details/125549606
- 点赞
- 收藏
- 关注作者
评论(0)