如何从FFT结果计算dBm?

2024-06-15 03:50:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我计算了一个4Hz的正弦波,应用FFT计算了振幅,振幅是一个500长度的数组,我想把数组中的每个元素转换成dBm形式,然后画一个谱图。然而,我似乎无法正确计算

我看到了一般公式:

valueDBFS=20np.log10(绝对值))

所以我试着用它,结果却只有负面的结果

以下是我的完整代码(已编辑):

# 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()

Tags: oftheimportfftnumpytimeasnp
1条回答
网友
1楼 · 发布于 2024-06-15 03:50:18

振幅的最大值为1,log10(1)为0,其他所有值都将小于该值-例如log10(0.9)=-00458

因此,您的部分代码工作正常,在您的示例中,日志应该是负的!-尝试这样定义振幅:

amplitude1 = 100 * np.sin(2*np.pi*signal1Frequency*time)

这应该会带来很多积极的结果

相关问题 更多 >