【问题标题】:spectrogram by fft using python使用 python 的 fft 频谱图
【发布时间】:2017-11-22 12:30:45
【问题描述】:

我试图理解一段代码,在我看来它试图先应用过滤器然后计算 FFT。 我不明白它是怎么做到的。谁能给我解释一下。 代码如下:

# Parameters to create the spectrogram
N = 160000  # No. of frames in .wav file
K = 512  
step = 4
wind = 0.5 * (1 - np.cos(np.array(range(K)) * 2 * np.pi / (K - 1)))  # 0.5*2*sin(o/2), creation of filter window
ffts = []
def wav_to_floats(file):
    s = wave.open(file, 'r')
    str_sig = s.readframes(s.getnframes())
    y = np.fromstring(str_sig, np.short)
    s.close()    
    return y

for file_index in range(len(label)):
    test_flag = label.iloc[file_index]['fold'] # 0 - training data, 1 - test data
    fname = label.iloc[file_index]['filename']

    #-------------from here i dont understand mainly------------ 
    spectogram = []
    s = wav_to_floats(essential_folder+'src_wavs/'+fname+'.wav')
    for j in range(int((step*N/K) - step)):
        vec = s[j * K/step : (j+step) * K/step] * wind
        spectogram.append(abs(fft(vec, K)[:K / 2]))

    ffts.append(np.array(spectogram))

【问题讨论】:

    标签: python audio signal-processing fft


    【解决方案1】:

    首先,它将文件从wav转换为float(s = wav_to_floats(essential_folder+'src_wavs/'+fname+'.wav')` ,因为要计算 fft 你需要一个浮点数。之后,它在信号和窗口之间进行卷积(可能是加窗滤波器)

    for j in range(int((step*N/K) - step)):
            vec = s[j * K/step : (j+step) * K/step] * wind
    

    获取 fft 的模数(因为 fft 为您提供了一个复数,其中包含有关模数和相位的信息)并将该向量添加到 ffts

    【讨论】:

      猜你喜欢
      • 2010-11-21
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 2017-01-10
      • 2013-03-12
      相关资源
      最近更新 更多