拙网论坛

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 330|回复: 0

FFT to spectrum in decibel

[复制链接]

949

主题

1001

帖子

3736

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
3736
发表于 2019-9-19 14:03:22 | 显示全部楼层 |阅读模式
https://dsp.stackexchange.com/questions/32076/fft-to-spectrum-in-decibel

Definitely you will have to calibrate your system. You need to know what is the relationship between dBFS (Decibel Full-Scale) and dB scale you want to measure.

In case of digital microphones, you will find sensitivity given in dBFS. This corresponds to dBFS level, given 94 dB SPL (Sound Pressure Level). For example this microphone for input 94dBSPL will produce signal at −26dBFS. Therefore the calibration factor for spectrum will be equal to 94+26=120dB.

Secondly, keep in mind scaling of the spectrum while doing windowing and DFT itself. More specifically, given amplitude spectrum (abs(sp)):

Total energy of the signal is spread over frequencies below and above Nyquist frequency. Naturally we are interested only in half of the spectrum. That is why, it important to multiply it by 2 and ignore everything above the Nyquist frequency.

Whenever doing windowing, it is necessary to compensate for loss of energy due to multiplication by that window. This is defined as division by sum of window samples (sum(win)). In case of rectangular window (or now window), it is as simple as division by N, where N is the DFT length.

Here is some example source code in Python. I am sure that you can take it from here.
  1. #!/usr/bin/env python

  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import scipy.io.wavfile as wf

  5. plt.close('all')


  6. def dbfft(x, fs, win=None, ref=32768):
  7.     """
  8.     Calculate spectrum in dB scale
  9.     Args:
  10.         x: input signal
  11.         fs: sampling frequency
  12.         win: vector containing window samples (same length as x).
  13.              If not provided, then rectangular window is used by default.
  14.         ref: reference value used for dBFS scale. 32768 for int16 and 1 for float

  15.     Returns:
  16.         freq: frequency vector
  17.         s_db: spectrum in dB scale
  18.     """

  19.     N = len(x)  # Length of input sequence

  20.     if win is None:
  21.         win = np.ones(1, N)
  22.     if len(x) != len(win):
  23.             raise ValueError('Signal and window must be of the same length')
  24.     x = x * win

  25.     # Calculate real FFT and frequency vector
  26.     sp = np.fft.rfft(x)
  27.     freq = np.arange((N / 2) + 1) / (float(N) / fs)

  28.     # Scale the magnitude of FFT by window and factor of 2,
  29.     # because we are using half of FFT spectrum.
  30.     s_mag = np.abs(sp) * 2 / np.sum(win)

  31.     # Convert to dBFS
  32.     s_dbfs = 20 * np.log10(s_mag/ref)

  33.     return freq, s_dbfs


  34. def main():
  35.     # Load the file
  36.     fs, signal = wf.read('Sine_440hz_0dB_10seconds_44.1khz_16bit_mono.wav')

  37.     # Take slice
  38.     N = 8192
  39.     win = np.hamming(N)
  40.     freq, s_dbfs = dbfft(signal[0:N], fs, win)

  41.     # Scale from dBFS to dB
  42.     K = 120
  43.     s_db = s_dbfs + K

  44.     plt.plot(freq, s_db)
  45.     plt.grid(True)
  46.     plt.xlabel('Frequency [Hz]')
  47.     plt.ylabel('Amplitude [dB]')
  48.     plt.show()

  49. if __name__ == "__main__":
  50.     main()
复制代码
Lastly, there is no better source on this type of spectrum scaling than brilliant publication by G. Heinzel et al. Just keep in mind that if you want to proper RMS scaling, then full-scale sinosuidal signal will be −3dBFS.

You might also find my previous answer being helpful.


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|抱朴守拙BBS

GMT+8, 2025-5-26 12:13 , Processed in 0.183605 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表