Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 根据定义的FFT长度、块大小和波形文件窗口计算FFT_Python 3.x_Scipy_Fft_Wave_Soundfile - Fatal编程技术网

Python 3.x 根据定义的FFT长度、块大小和波形文件窗口计算FFT

Python 3.x 根据定义的FFT长度、块大小和波形文件窗口计算FFT,python-3.x,scipy,fft,wave,soundfile,Python 3.x,Scipy,Fft,Wave,Soundfile,我有一个32位浮点.wav文件,以48KHz的采样率记录了5秒。我想得到一个完整5秒的FFT,应用汉宁窗,没有重叠,FFT长度为8192。在获取FFT时,我意识到我在FFT计算中犯了一个错误 我使用以下代码执行此操作: # Imports import matplotlib.pyplot as plt import numpy as np import math as math import soundfile as sf import scipy.fftpack as fftpack im

我有一个32位浮点.wav文件,以48KHz的采样率记录了5秒。我想得到一个完整5秒的FFT,应用汉宁窗,没有重叠,FFT长度为8192。在获取FFT时,我意识到我在FFT计算中犯了一个错误

我使用以下代码执行此操作:

# Imports
import matplotlib.pyplot as plt
import numpy as np
import math as math
import soundfile as sf  
import scipy.fftpack as fftpack
import scipy.io.wavfile as wf


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

    Returns:
        freq: frequency vector
        s_db: spectrum in dB scale
    """
    N = len(x)  # Length of input sequence
    if win is None:
        win = np.ones(1, N)
    if len(x) != len(win):
            raise ValueError('Signal and window must be of the same length')
    x = x * win
    # Calculate real FFT and frequency vector
    sp = np.fft.rfft(x)
    freq = np.arange((N / 2) + 1) / (float(N) / fs)
    # Scale the magnitude of FFT by window and factor of 2,because we are using half of FFT spectrum.
    s_mag = np.abs(sp) * 2 / np.sum(win)
    # Convert to dBFS using 20*log10(val/max)
    s_dbfs = 20 * np.log10(s_mag/ref)      
    return freq, s_dbfs

def main():
    # Read from wav file
    data, samplerate = sf.read('Signal Analyzer_5s_55_1_L1F4_APxRecordedAudio.wav')
    # Scaling factor
    K = 120
    # Calculation
    N = 8192
    win = np.hanning(N)
    # Frequency domain
    freq, s_dbfs = dbfft(data[0:N],samplerate, win)
    # Scale from dbFS to dB
    s_db = s_dbfs + K
    # Time domain
    Time = np.linspace(0, len(data) / samplerate, num=len(data))
    # Amp_time = 20 * np.log10 (abs(data) / 0.00002)    # ref = 1 
    Amp_time = (20*np.log10((data/50))) + K + 20*np.log10(math.sqrt(2))      # reference of sound i.e 20 log10(P rms_value/P ref)+120 dB TODO
  

    # Plot
    #fig, axes = plt.subplots(nrows=2, ncols=1)
    plt.subplot(2,1,1)
    plt.plot(Time, Amp_time)
    plt.grid('on')
    plt.minorticks_on
    plt.xlabel('Time [s]')
    plt.ylabel('Instantaneous Level [dBFS]')
    plt.xlim([0,5])
    plt.subplot(2,1,2)
    plt.plot(freq, s_db)
    plt.grid('on')
    plt.minorticks_on
    plt.xlabel('Frequency [Hz]')
    plt.ylabel('Amplitude [dB]')
    plt.xscale('log')
    plt.xlim([10,10000])
    plt.show()
    
if __name__ == "__main__":
    main()

在代码编写过程中,我看到我只对前8192个样本进行FFT,并对整个240000(5秒)样本进行平均FFT,块大小为8192,带有汉宁窗口。我是否应该每8192秒对5秒(88次FFT)进行多次FFT,并平均振幅以得到结果FFT?是否有一种有效的方法来执行此操作?

也许您想了解或

你也许可以开始。例如,绘制一个光谱图