python - 如何从 FFT 结果计算 dBm?
问题描述
我计算了一个 4Hz 的正弦波,应用 FFT 并计算了幅度,幅度是一个长度为 500 的数组,我想将该数组中的每个元素转换为 dBm 形式,并绘制一个频谱图。但是我似乎无法正确计算。
我看到了那个通用公式:
valueDBFS = 20 np.log10(abs(value))
所以我尝试使用它,但我只得到负面结果..
这是我的完整代码(已编辑):
# Python example - Fourier transform using numpy.fft method
import numpy as np
import matplotlib.pyplot as plotter
from os import times
from PIL import Image
import numpy as np
# How many time points are needed i,e., Sampling Frequency
samplingFrequency = 100
# At what intervals time points are sampled
samplingInterval = 1 / samplingFrequency
# Begin time perod of the signals
beginTime = 0
# End time period of the signals
endTime = 10
# Frequency of the signals
signal1Frequency = 4
signal2Frequency = 70
# Time points
time = np.arange(beginTime, endTime, samplingInterval)
# Create two sine waves
amplitude1 = 100 * np.sin(2*np.pi*signal1Frequency*time)
fourierTransform = np.fft.fft(amplitude1)
fourierTransform = fourierTransform[range(int(len(amplitude1)/2))] # Exclude sampling frequency
tpCount = len(amplitude1)
values = np.arange(int(tpCount/2))
timePeriod = tpCount/samplingFrequency
frequencies = values/timePeriod
valueDBFS = 20*np.log10(abs(fourierTransform))
print(valueDBFS)
#SPECTROGRAM
w, h = 500, 500
data = np.zeros((h, w, 3), dtype=np.uint8)
time = time[:len(time)//2]
for i in range(500):
for j in range(500):
color = abs(fourierTransform)[i]
data[i,j] = [color, color, color]
img = Image.fromarray(data, 'RGB')
img.show()
解决方案
幅度的最大值为 1,log10(1) 为 0,其他所有值都将小于该值 - 例如 log10(0.9) = -0,0458。
所以你的那部分代码工作正常,你的例子中的日志应该是负面的!- 尝试像这样定义你的振幅:
amplitude1 = 100 * np.sin(2*np.pi*signal1Frequency*time)
这应该会产生很多积极的结果。
推荐阅读
- flutter - Overalyed 图像不会在颤动中出现在左下角
- php - 如果值等于,则使用 OR 语句回显一次
- python - 无法使用 importlib 在 python 3.8.8 中导入文件
- java - 是否为 spring mvc rest api 或 spring boot api 对应用程序服务器的每个新请求创建了新的 Service、Repository 和 Component 实例?
- scala - 如何指定传递给 Scala 映射(高阶)函数的函数的参数?
- swift - 为 Objective-C Pod 添加 SPM 支持:提供包含目录
- angular - 如何在下拉列表中显示来自数据源的第一项
- android - 使用 Navigation 组件通过 App Bar/Toolbar 打开 Fragments
- javascript - 为什么第一次点击不起作用,但在javascript中第二次点击有效?
- python-3.x - 如何在将项目添加到画布时阻止滚动条向下滚动