gpt4 book ai didi

python - 如何使用 Python 将 scipy.signal.spectrogram 反转为音频?

转载 作者:行者123 更新时间:2023-12-04 02:38:47 25 4
gpt4 key购买 nike

我有:

import librosa
from scipy import signal
import scipy.io.wavfile as sf

samples, sample_rate = sf.read(args.file)
nperseg = int(sample_rate * 0.001 * 20)
frequencies, times, spectrogram = signal.spectrogram(samples,
sample_rate,
nperseg=nperseg,
window=signal.hann(nperseg))

audio_signal = librosa.griffinlim(spectrogram)
print(audio_signal, audio_signal.shape)

sf.write('test.wav', audio_signal, sample_rate)

但是,这会产生(接近)空的声音文件。

最佳答案

正如@DrSpill 提到的,scipy.io.wav.readscipy.io.wav.write订单错误,还有来自 librosa 的导入不正确。这应该这样做:

import librosa
import numpy as np
import scipy.signal
import scipy.io.wavfile

# read file
file = "temp/processed_file.wav"
fs, sig = scipy.io.wavfile.read(file)
nperseg = int(fs * 0.001 * 20)

# process
frequencies, times, spectrogram = scipy.signal.spectrogram(sig,
fs,
nperseg=nperseg,
window=scipy.signal.hann(nperseg))
audio_signal = librosa.core.spectrum.griffinlim(spectrogram)
print(audio_signal, audio_signal.shape)

# write output
scipy.io.wavfile.write('test.wav', fs, np.array(audio_signal, dtype=np.int16))
备注:
当我听到它时,生成的文件有一个加速的速度,我认为这是由于您的处理,但经过一些调整它应该可以工作。
一个不错的选择是只使用 librosa , 像这样:
import librosa
import numpy as np

# read file
file = "temp/processed_file.wav"
sig, fs = librosa.core.load(file, sr=8000)

# process
abs_spectrogram = np.abs(librosa.core.spectrum.stft(sig))
audio_signal = librosa.core.spectrum.griffinlim(abs_spectrogram)

print(audio_signal, audio_signal.shape)

# write output
librosa.output.write_wav('test2.wav', audio_signal, fs)

关于python - 如何使用 Python 将 scipy.signal.spectrogram 反转为音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60377585/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com