【问题标题】:How to apply CNN to Short-time Fourier Transform?如何将 CNN 应用于短时傅里叶变换?
【发布时间】:2019-05-24 13:30:29
【问题描述】:

所以我有一个代码,它返回 .wav 文件的短时傅里叶变换频谱。我希望能够获取,比如说一毫秒的频谱,并在其上训练一个 CNN。

我不太确定我将如何实现它。我知道如何格式化图像数据以输入 CNN,以及如何训练网络,但我不知道如何获取 FFT 数据并将其划分为小时间框架。

FFT 码(对不起,超长码):

rate, audio = wavfile.read('scale_a_lydian.wav')

audio = np.mean(audio, axis=1)

N = audio.shape[0]
L = N / rate

M = 1024

# Audio is 44.1 Khz, or ~44100 samples / second
# window function takes 1024 samples or 0.02 seconds of audio (1024 / 44100 = ~0.02 seconds)
# and shifts the window 100 over each time
# so there would end up being (total_samplesize - 1024)/(100) total steps done (or slices)

slices = util.view_as_windows(audio, window_shape=(M,), step=100) #slices overlap

win = np.hanning(M + 1)[:-1]
slices = slices * win #each slice is 1024 samples (0.02 seconds of audio)

slices = slices.T #transpose matrix -> make each column 1024 samples (ie. make each column one slice)


spectrum = np.fft.fft(slices, axis=0)[:M // 2 + 1:-1] #perform fft on each slice and then take the first half of each slice, and reverse

spectrum = np.abs(spectrum) #take absolute value of slices

# take SampleSize * Slices
# transpose into slices * samplesize
# Take the first row -> slice * samplesize
# transpose back to samplesize * slice (essentially get 0.01s of spectrum)

spectrum2 = spectrum.T
spectrum2 = spectrum2[:1]
spectrum2 = spectrum2.T

以下输出 FFT 频谱:

N = spectrum2.shape[0]
L = N / rate

f, ax = plt.subplots(figsize=(4.8, 2.4))

S = np.abs(spectrum2)
S = 20 * np.log10(S / np.max(S))

ax.imshow(S, origin='lower', cmap='viridis',
          extent=(0, L, 0, rate / 2 / 1000))
ax.axis('tight')
ax.set_ylabel('Frequency [kHz]')
ax.set_xlabel('Time [s]');
plt.show()

(请随时纠正我在 cmets 中的任何理论错误)

所以据我了解,我有一个 numpy 数组(频谱),每列是一个包含 510 个样本的切片(切成两半,因为每个 FFT 切片的一半是多余的(没用?)),每个样本都有频率列表?

上面的代码理论上输出0.01s的音频作为频谱,这正是我所需要的。这是真的,还是我的想法不对?

【问题讨论】:

    标签: python python-3.x conv-neural-network fft


    【解决方案1】:

    我建议您使用 Librosa 加载音频并在 1 行代码中进行一些预处理。您会希望所有音频文件都具有相同的采样率。此外,您还想在特定部分剪切音频以获得特定间隔。 您可以像这样加载音频:

    import librosa
    
    y, sr = librosa.load(audiofile, offset=10.0, duration=30.0, sr=16000)
    

    所以你的时间序列是 y。 从这里我会在音频上使用this CNN 的不错实现。 在这里,这个家伙正在使用他自己的库来执行基于 GPU 的 mel-spectrogram 计算。您只需要将 y 参数提供给网络。请参阅here 它是如何完成的。 或者,您可以删除该网络的第一层并预先计算您的梅尔谱图并将它们保存在某处。这些将是您对网络的输入。见here

    其他资源: Audio Classification : A Convolutional Neural Network Approach

    【讨论】:

      猜你喜欢
      • 2012-03-13
      • 2015-05-09
      • 2020-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多