How to compute dBm from FFT results?

1.1k Views Asked by At

I computed a sinewave of 4Hz, applied FFT and calculated the amplitude, the amplitude is an array of 500 length, I want to convert each element in that array to dBm form, and draw a spectrogram. however I can't seem to get the calculation right.

I saw that general formula:

valueDBFS = 20np.log10(abs(value))

so I tried using it and I get only negative results..

Here is my full code (edited):

# 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

There are 1 best solutions below

8
On

The maximum value of your amplitude is 1, and log10(1) is 0, everything else will be less than that - for example log10(0.9) = -0,0458.

So that part of your code works fine, the logs should be negative in your example! - Try defining your amplitude like this:

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

That should give plenty of positive results.