Python scipy - wav audio deconvolution NaN

时间:2017-04-21 19:38:41

标签: python audio scipy nan deconvolution

过去几天我一直在玩scipy音频。 以下代码的工作方式是它需要一个音频文件和一个脉冲响应。然后应用卷积并保存文件。到现在为止还挺好。

重新导入文件并对其应用解卷积时,重新生成初始文件。它仍然完美无缺。

,但 当任何其他文件传递给deconvolution函数时,返回的数组包含大量的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()

0 个答案:

没有答案