Skip to content

[Bug]: FFT计算幅值是实际的2倍,偏移量问题 #4

@huayue0405

Description

@huayue0405

What happened

FFT计算
实际幅值为[0,10,20,30,0.2,1000]
计算幅值为[0, 19, 40, 59, 0, 1998]
考虑到0.2被整数量化为0,但其余幅值均差2倍

Reproduction steps

基础示例

欢迎使用CanMV IDE, 点击IDE左下角的绿色按钮开始执行脚本

from machine import FFT
import array
import math
from ulab import numpy as np
PI = 3.14159265358979323846264338327950288419716939937510

rx = []
def input_data():
for i in range(64):
data0 = 10 * math.cos(2 * PI * i / 64)
data1 = 20 * math.cos(2 * 2 * PI * i / 64)
data2 = 30 * math.cos(3 * 2 * PI * i / 64)
data3 = 0.2 * math.cos(4 * 2 * PI * i / 64)
data4 = 1000 * math.cos(5 * 2 * PI * i / 64)
rx.append((int(data0 + data1 + data2 + data3 + data4)))
input_data() #初始化需要进行FFT的数据,列表类型
#print(rx)
data = np.array(rx,dtype=np.int16) #把列表数据转换成数组
#print(data)
fft1 = FFT(data, 64, 0) #创建一个FFT对象,运算点数为64,偏移是0x555
res = fft1.run() #获取FFT转换后的数据
#print(res)

res = fft1.amplitude(res) #获取各个频率点的幅值
print(res)

#res = fft1.freq(64,38400) #获取所有频率点的频率值
#print(res)

Hardware board

k230 canmv

Software version

micropython v0.4

Bug frequency

all

Anything else

同时案例中的转换,为什么采用的是uint16
data = np.array(rx,dtype=np.uint16)

fft1 = FFT(data, 64, 0x555)
测试偏移量只支持16进制和8进制,是否在说明文档和案例中说明偏移的格式
同时,在进行偏移时FFT幅值为原来的1/2?

`from machine import FFT
import array
import math
from ulab import numpy as np
PI = 3.14159265358979323846264338327950288419716939937510

rx = []
def input_data():
for i in range(64):
data0 = 10 * math.sin(2 * PI * i / 64) +10
data1 = 0#20 * math.cos(2 * 2 * PI * i / 64)
data2 = 0#30 * math.cos(3 * 2 * PI * i / 64)
data3 = 0#0.2 * math.cos(4 * 2 * PI * i / 64)
data4 = 0# 1000 * math.cos(5 * 2 * PI * i / 64)
rx.append((int(data0 + data1 + data2 + data3 + data4)))
input_data() #初始化需要进行FFT的数据,列表类型
#print(rx)
data = np.array(rx,dtype=np.int16) #把列表数据转换成数组
#print(data)
fft1 = FFT(data, 64, 0xA) # 偏置10 ,16进制
res = fft1.run() #获取FFT转换后的数据
#print(res)

res = fft1.amplitude(res) #获取各个频率点的幅值
print(res)`

结果

[9, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
除直流分量接近,幅值仅为原有10的1/2

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions