gpt4 book ai didi

Python:从 STFT 重建音频文件

转载 作者:行者123 更新时间:2023-12-03 01:40:16 27 4
gpt4 key购买 nike

作为一个简单的实验,我想计算 stft音频文件:

sample_rate, samples = wav.read(file)

f, t, Zxx = stft(samples, sample_rate)
_, reconstructed = istft(Zxx, sample_rate)
padded_samples = np.zeros_like(reconstructed)
padded_samples[:len(samples)] = samples
print (np.sum(padded_samples - reconstructed))

输出: -1.37309940428 .很小,不是吗?鉴于 samples形状为 (9218368,) .
test_file = os.path.join(temp_folder, 'reconstructed.wav')
wav.write(test_file, sample_rate, reconstructed)

重建的文件听起来很糟糕。原声在噪音下几乎听不见。我犯了错误,还是根本无法从 STFT 恢复音频文件?

关于如何将音频文件转换为某种可处理的数据,然后从中重建它,您还有其他建议吗?还有哪些其他类型的数据结构可用于处理音频文件?

谢谢你。

编辑:

正如沃伦所建议的:
print (samples.shape)
print (samples.dtype)
print (reconstructed.dtype)

输出:
(9218368,)
int16
float64

根据 scipy docs int 和 float input 在编写 wav 文件时具有不同的含义。我尝试将重构的转换为 np.int16:
rounded_reconstructed = np.rint(reconstructed).astype(np.int16)

test_file = os.path.join(temp_folder, 'reconstructed.wav')
wav.write(test_file, sample_rate, rounded_reconstructed)

结果与原版几乎没有区别。感谢您的帮助。

最佳答案

正如 Warren 所建议的那样:

print (samples.shape)
print (samples.dtype)
print (reconstructed.dtype)

输出:
(9218368,)
int16
float64

根据 the scipy docs intfloat写入 wav 文件时,输入具有不同的含义。我尝试转换 reconstructednp.int16 :
rounded_reconstructed = np.rint(reconstructed).astype(np.int16)

test_file = os.path.join(temp_folder, 'reconstructed.wav')
wav.write(test_file, sample_rate, rounded_reconstructed)

结果与原版几乎没有区别。感谢您的帮助。

关于Python:从 STFT 重建音频文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47983897/

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