Python:如何将两个有符号的int16数组求和成一个而没有溢出

问题描述 投票:0回答:1

我在字符串中有几个int16流,我希望它们加在一起(没有溢出),并将其作为int16字符串返回。背景正在将多个wave文件混合到一个流中。

decodeddata1 = numpy.fromstring(data, numpy.int16)
decodeddata2 = numpy.fromstring(data2, numpy.int16)
newdata = decodeddata1 + decodeddata2
return newdata.tostring()

是否可以使用numpy进行此操作,或者是否还有另一个库?

[在python中处理每个单个值太慢,导致结结。

最重要的是性能,因为此代码用于提供音频的回调方法中。

@编辑:

test input data:
a = np.int16([20000,20000,-20000,-20000])
b = np.int16([10000,20000,-10000,-20000])
print a + b -->   [ 30000 -25536 -30000  25536]

但我想保持最高水平:[30000 40000 -30000 -40000]

python performance numpy audio wav
1个回答
0
投票

怎么样?它不会溢出。但是如果发生溢出,我不知道您的期望是什么。

decodeddata1 = numpy.fromstring(data, numpy.int16)
decodeddata2 = numpy.fromstring(data2, numpy.int16)
newdata = numpy.sum([decodeddata1, decodeddata2], dtype=numpy.int16)
© www.soinside.com 2019 - 2024. All rights reserved.