Featured post
python - Replace part of numpy 1D array with shorter array -
i have 1d numpy array containing audio data. i'm doing processing , want replace parts of data white noise. noise should, however, shorter replaced part. generating noise not problem, i'm wondering easiest way replace original data noise is. first thought of doing data[10:110] = noise[0:10]
not work due obvious dimension mismatch.
what's easiest way replace part of numpy array part of different dimension?
edit: data uncompressed pcm data can hour long, taking few hundred mb of memory. avoid creating additional copies in memory.
what advantage numpy array have on python list application? think 1 of weaknesses of numpy arrays not easy resize:
http://mail.python.org/pipermail/python-list/2008-june/1181494.html
do need reclaim memory segments of array you're shortening? if not, maybe can use masked array:
http://docs.scipy.org/doc/numpy/reference/maskedarray.generic.html
when want replace section of signal shorter section of noise, replace first chunk of signal, mask out remainder of removed signal.
edit: here's clunky numpy code doesn't use masked arrays, , doesn't allocate more memory. doesn't free memory deleted segments. idea replace data want deleted shifting remainder of array, leaving zeros (or garbage) @ end of array.
import numpy = numpy.arange(10) # [0 1 2 3 4 5 6 7 8 9] ## replace a[2:7] length-2 noise: insert = -1 * numpy.ones((2)) new = slice(2, 4) old = slice(2, 7) #just indicate we'll 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 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 dangly bit @ end: a[(new.stop - old.stop):] = 0 # [0 1 -1 -1 7 8 9 0 0 0]
- Get link
- X
- Other Apps
Comments
Post a Comment