In [2]:
import numpy as np
import scipy
In [ ]:
 
In [4]:
a = np.array([1,2,3,4,5,6,7,8]).reshape((4,2))
In [3]:
a
Out[3]:
array([[1, 2],
       [3, 4],
       [5, 6],
       [7, 8]])
In [6]:
a.sum(axis=1)
Out[6]:
array([ 3,  7, 11, 15])
In [7]:
 
In [9]:
 
In [11]:
a.flat = 0
In [12]:
a
Out[12]:
array([[0, 0],
       [0, 0],
       [0, 0],
       [0, 0]])
In [19]:
a = np.arange(0,1,1/(64*64))
In [20]:
a.shape
Out[20]:
(4096,)
In [21]:
a = a.reshape((64,64))
In [22]:
a
Out[22]:
array([[0.00000000e+00, 2.44140625e-04, 4.88281250e-04, ...,
        1.48925781e-02, 1.51367188e-02, 1.53808594e-02],
       [1.56250000e-02, 1.58691406e-02, 1.61132812e-02, ...,
        3.05175781e-02, 3.07617188e-02, 3.10058594e-02],
       [3.12500000e-02, 3.14941406e-02, 3.17382812e-02, ...,
        4.61425781e-02, 4.63867188e-02, 4.66308594e-02],
       ...,
       [9.53125000e-01, 9.53369141e-01, 9.53613281e-01, ...,
        9.68017578e-01, 9.68261719e-01, 9.68505859e-01],
       [9.68750000e-01, 9.68994141e-01, 9.69238281e-01, ...,
        9.83642578e-01, 9.83886719e-01, 9.84130859e-01],
       [9.84375000e-01, 9.84619141e-01, 9.84863281e-01, ...,
        9.99267578e-01, 9.99511719e-01, 9.99755859e-01]])
In [23]:
import matplotlib.pyplot as plt
In [24]:
plt.imshow(a)
Out[24]:
<matplotlib.image.AxesImage at 0x71a28c5f5c90>
No description has been provided for this image
In [25]:
b = a.copy()
np.random.shuffle(b)
plt.imshow(b)
Out[25]:
<matplotlib.image.AxesImage at 0x71a280852d90>
No description has been provided for this image
In [26]:
import os
In [27]:
os.curdir
Out[27]:
'.'
In [28]:
from glob import glob
In [29]:
glob("*")
Out[29]:
['kick1fpm.wav',
 'kick1.wav',
 'out.wav',
 'sine.py',
 'Untitled.ipynb',
 's16.wav',
 'idea.md',
 'tomono.py',
 's24.wav',
 'max.py']
In [30]:
from scipy.io import wavfile
In [31]:
kick1fpm = wavfile.read("kick1fpm.wav")
In [35]:
sr, data = kick1fpm
plt.plot(data)
Out[35]:
[<matplotlib.lines.Line2D at 0x71a268938450>]
No description has been provided for this image

Clip and Write¶

First we want to be able to write into a buffer, replacing what was there before. Then we want to do the same but add to it.

In [52]:
a = np.arange(0,10,0.01)
In [6]:
b = np.arange(100,200,10); b
Out[6]:
array([100, 110, 120, 130, 140, 150, 160, 170, 180, 190])
In [7]:
a[10:10+len(b)] = b
a[30:30+len(b)] += b
In [9]:
a[10:10+len(b)];a[30:30+len(b)]
Out[9]:
array([100.3 , 110.31, 120.32, 130.33, 140.34, 150.35, 160.36, 170.37,
       180.38, 190.39])
In [10]:
a[10:10+len(b)]
Out[10]:
array([100., 110., 120., 130., 140., 150., 160., 170., 180., 190.])

Overflow¶

What happens if our source array goes past the end?

In [42]:
a = np.zeros(40)
b = np.arange(10,20,1)
c = np.arange(100,130,10)
In [21]:
a[38:] = b
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[21], line 1
----> 1 a[38:] = b

ValueError: could not broadcast input array from shape (0,) into shape (2,)

So this fails. Thus when writing in, we need to detect this. We are working with 1-d arrays. So does len() work?

In [22]:
len(a)
Out[22]:
40

Yes.

In [50]:
def writeinto(target, source, position, mult=1.0):
    tlen = len(target)
    slen = len(source)
    wlen = min(tlen-position,slen)
    sslice = source[:wlen]
    print(sslice,tlen,slen,wlen)
    target[position:position+wlen] = sslice
    if mult != 1.0:
        target[position:position+wlen] = sslice*mult
    else:
        target[position:position+wlen] = sslice
def addinto(target, source, position, mult=1.0):
    tlen = len(target)
    slen = len(source)
    wlen = min(tlen-position,slen)
    sslice = source[:wlen]
    if mult != 1.0:
        target[position:position+wlen] += sslice*mult
    else:
        target[position:position+wlen] += sslice
In [51]:
writeinto(a,b,5,1)
[10 11 12 13 14 15 16 17 18 19] 40 10 10
In [44]:
a
Out[44]:
array([0. , 0. , 0. , 0. , 0. , 1. , 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7,
       1.8, 1.9, 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
       0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ,
       0. ])
In [45]:
addinto(a,c,7,4)
In [46]:
a
Out[46]:
array([  0. ,   0. ,   0. ,   0. ,   0. ,   1. ,   1.1, 401.2, 441.3,
       481.4,   1.5,   1.6,   1.7,   1.8,   1.9,   0. ,   0. ,   0. ,
         0. ,   0. ,   0. ,   0. ,   0. ,   0. ,   0. ,   0. ,   0. ,
         0. ,   0. ,   0. ,   0. ,   0. ,   0. ,   0. ,   0. ,   0. ,
         0. ,   0. ,   0. ,   0. ])
In [ ]: