用较短的数组替换部分numpy 1D数组(Replace part of numpy 1D array with shorter array)

编程入门 行业动态 更新时间:2024-10-27 09:35:26
用较短的数组替换部分numpy 1D数组(Replace part of numpy 1D array with shorter array)

我有一个包含一些音频数据的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]

更多推荐

本文发布于:2023-08-07 23:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   较短   numpy   Replace   array

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!