我有一个包含一些音频数据的numpy数组。 我正在做一些处理,并希望用白噪声替换数据的某些部分。 然而,噪音应该比更换部件短。 生成噪声不是问题,但我想知道用噪声替换原始数据的最简单方法是什么。 由于明显的尺寸不匹配,我第一次想到做data[10:110] = noise[0:10]不起作用。
用不同维度的另一部分替换numpy数组的一部分最简单的方法是什么?
编辑:数据是未压缩的PCM数据,可能长达一个小时,占用几百MB的内存。 我想避免在内存中创建任何额外的副本。
I have a 1D numpy array containing some audio data. I'm doing some processing and want to replace certain parts of the data with white noise. The noise should, however, be shorter then the replaced part. Generating the noise is not a problem, but I'm wondering what the easiest way to replace the original data with the noise is. My first thought of doing data[10:110] = noise[0:10] does not work due to the obvious dimension mismatch.
What's the easiest way to replace a part of a numpy array with another part of different dimension?
edit: The data is uncompressed PCM data that can be up to an hour long, taking up a few hundred MB of memory. I would like to avoid creating any additional copies in memory.
最满意答案
numpy数组对于应用程序的python列表有什么优势? 我认为numpy数组的一个缺点是它们不容易调整大小:
http://mail.python.org/pipermail/python-list/2008-June/1181494.html
你真的需要从缩短阵列的片段中回收内存吗? 如果没有,也许你可以使用蒙面数组:
http://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html
如果要用较短的噪声部分替换信号的一部分,请更换信号的第一个块,然后屏蔽掉移除信号的其余部分。
编辑:这是一些笨重的numpy代码,不使用掩码数组,并没有分配更多的内存。 它也不会为已删除的段释放任何内存。 想法是通过移动数组的其余部分来替换要删除的数据,在数组的末尾留下零(或垃圾)。
import numpy a = numpy.arange(10) # [0 1 2 3 4 5 6 7 8 9] ## Replace a[2:7] with length-2 noise: insert = -1 * numpy.ones((2)) new = slice(2, 4) old = slice(2, 7) #Just to indicate what we'll be replacing: a[old] = 0 # [0 1 0 0 0 0 0 7 8 9] a[new] = insert # [0 1 -1 -1 0 0 0 7 8 9] #Shift the remaining data over: a[new.stop:(new.stop - old.stop)] = a[old.stop:] # [0 1 -1 -1 7 8 9 7 8 9] #Zero out the dangly bit at the end: a[(new.stop - old.stop):] = 0 # [0 1 -1 -1 7 8 9 0 0 0]What advantage does a numpy array have over a python list for your application? I think one of the weaknesses of numpy arrays is that they are not easy to resize:
http://mail.python.org/pipermail/python-list/2008-June/1181494.html
Do you really need to reclaim the memory from the segments of the array you're shortening? If not, maybe you can use a masked array:
http://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html
When you want to replace a section of your signal with a shorter section of noise, replace the first chunk of the signal, then mask out the remainder of the removed signal.
EDIT: Here's some clunky numpy code that doesn't use masked arrays, and doesn't allocate more memory. It also doesn't free any memory for the deleted segments. The idea is to replace data that you want deleted by shifting the remainder of the array, leaving zeros (or garbage) at the end of the array.
import numpy a = numpy.arange(10) # [0 1 2 3 4 5 6 7 8 9] ## Replace a[2:7] with length-2 noise: insert = -1 * numpy.ones((2)) new = slice(2, 4) old = slice(2, 7) #Just to indicate what we'll be replacing: a[old] = 0 # [0 1 0 0 0 0 0 7 8 9] a[new] = insert # [0 1 -1 -1 0 0 0 7 8 9] #Shift the remaining data over: a[new.stop:(new.stop - old.stop)] = a[old.stop:] # [0 1 -1 -1 7 8 9 7 8 9] #Zero out the dangly bit at the end: a[(new.stop - old.stop):] = 0 # [0 1 -1 -1 7 8 9 0 0 0]更多推荐
发布评论