最近几天我一直在玩 scipy 音频。下面的代码根据它需要一个音频文件和一个脉冲响应来工作。然后应用卷积并保存文件。到目前为止,一切都很好。
当重新导入文件并对其应用反卷积时,重新生成初始文件。它仍然可以完美运行。
但是,当任何其他文件传递给反卷积函数时,返回的数组包含大量的 NaN 和 0。
即使我加载文件并通过卷积函数运行它,脉冲响应为单个 1(平面滤波器)。为了获得通过相同过程的文件。
在反卷积后尝试将文件写入磁盘时,具有 NaN 和零的数组最终以错误结束。
有任何想法吗?提示?
我是编码新手,这是我的第一篇文章。请温柔一点。
import scipy
import numpy as np
import librosa
from scipy import signal
def main():
#samplerate
sr = 44100
#file path
dryFile = "/Users/davidhefti/Dropbox/DPsync/dh prog/python/thinkDsp2.0/audio2/original2_01.wav"
irFile = "/Users/davidhefti/Dropbox/DPsync/dh prog/python/thinkDsp2.0/audio2/IR2_01.wav"
convolutionOut = "/Users/davidhefti/Dropbox/DPsync/dh prog/python/thinkDsp2.0/dump/convolved1.wav"
filtered = "/Users/davidhefti/Dropbox/DPsync/dh prog/python/thinkDsp2.0/dump/convolved1.wav"
deConvolutionOut = "/Users/davidhefti/Dropbox/DPsync/dh prog/python/thinkDsp2.0/dump/DEconvolved1.wav"
do_Convolution(dryFile, irFile, convolutionOut, sr)
do_Deconvolution(filtered, irFile, deConvolutionOut, sr)
"""FUNCTIONS"""
def print_properties(name, array):
print("Name {}".format(name))
print("Shape: {}".format(array.shape))
print("Type: {}".format(array.dtype))
print("Min Value: {}".format(array.min()))
print("Max Value: {}".format(array.max()))
print()
def do_Convolution(sourcePath, irPath, outPath, samplerate):
"""load INFILE 1"""
source, sSR = librosa.load(sourcePath, sr=samplerate)
source64 = source.astype(np.float64)
print_properties("input file before convolution", source64)
"""load IR"""
ir, irSR = librosa.load(irPath, sr=samplerate)
ir64 = ir.astype(np.float64)
print_properties("Impulse response", ir64)
"""CONVOLVE AND WRITE 64bit"""
convolve = signal.convolve(source64, ir64)
print_properties("CONVOLUTION", convolve)
librosa.output.write_wav(outPath, convolve, sr=samplerate)
def do_Deconvolution(sourcePath, irPath, outPath, samplerate):
"""load convolutioned file NEEDS TO BE 64 BIT MONO?"""
relSR, reloaded = scipy.io.wavfile.read(sourcePath)
print_properties("reloaded", reloaded)
"""Load IR"""
ir, irSR = librosa.load(irPath, sr=samplerate)
"""DECONVOLVE"""
deconvolve, remainder = signal.deconvolve(reloaded, ir)
print_properties("DECONVOLUTION", deconvolve)
librosa.output.write_wav(outPath, deconvolve, samplerate)
if __name__ == "__main__":
main()