Tutorial 2 - support

Han Q Le (c) -copyrighted 

In [2]:
import numpy as np
import pandas as pd
import scipy as sp
import matplotlib.pyplot as plt
import sounddevice as sd
import time

Section 1 - example of array data

In [1]:
# These are just simple Python lists, not np.array
groceryitem = ["juice", "eggs", "fruits", "vegetables", "milk", 
"cereals", "coffee", "tea", "ice cream", "napkins", "foils"
               ,"storage bags", "toothpaste", "shampoo", "detergent"];
groceryunit = [3, 4, 12, 8, 2, 6, 1, 2, 3, 5, 2, 10, 4, 3, 2] ;
In [2]:
groceryitem
Out[2]:
['juice',
 'eggs',
 'fruits',
 'vegetables',
 'milk',
 'cereals',
 'coffee',
 'tea',
 'ice cream',
 'napkins',
 'foils',
 'storage bags',
 'toothpaste',
 'shampoo',
 'detergent']
In [3]:
groceryunit
Out[3]:
[3, 4, 12, 8, 2, 6, 1, 2, 3, 5, 2, 10, 4, 3, 2]
In [4]:
# convert a list into an numpy array:
groceryunit=np.array(groceryunit)
In [5]:
groceryunit
Out[5]:
array([ 3,  4, 12,  8,  2,  6,  1,  2,  3,  5,  2, 10,  4,  3,  2])
In [6]:
myinfo=["lastnm","firstname","1234567","April",23,1998,]
In [7]:
myinfo
Out[7]:
['lastnm', 'firstname', '1234567', 'April', 23, 1998]
In [9]:
myinfo[0]
Out[9]:
'lastnm'
In [10]:
myinfo[4]
Out[10]:
23
In [ ]:
# make a 2-D np array with power formula
In [4]:
np.fromfunction(lambda i, j: (i+1)**2 + j, (5, 5), dtype=int)
Out[4]:
array([[ 1,  2,  3,  4,  5],
       [ 4,  5,  6,  7,  8],
       [ 9, 10, 11, 12, 13],
       [16, 17, 18, 19, 20],
       [25, 26, 27, 28, 29]])
In [14]:
[i**2 for i in range(10)] # just to make a Python list, not np.array:
Out[14]:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
In [26]:
(np.arange(8))*0.1
Out[26]:
array([0. , 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7])
In [27]:
[i**2 for i in (np.arange(8))*0.1]
Out[27]:
[0.0,
 0.010000000000000002,
 0.04000000000000001,
 0.09000000000000002,
 0.16000000000000003,
 0.25,
 0.3600000000000001,
 0.4900000000000001]
In [29]:
np.array([i**2 for i in (np.arange(8))*0.1])
Out[29]:
array([0.  , 0.01, 0.04, 0.09, 0.16, 0.25, 0.36, 0.49])
In [30]:
np.array(["whatever" for i in range(10)])
Out[30]:
array(['whatever', 'whatever', 'whatever', 'whatever', 'whatever',
       'whatever', 'whatever', 'whatever', 'whatever', 'whatever'],
      dtype='<U8')
In [31]:
np.array([(0.1*i)**2+0.25 for i in range(20)])
Out[31]:
array([0.25, 0.26, 0.29, 0.34, 0.41, 0.5 , 0.61, 0.74, 0.89, 1.06, 1.25,
       1.46, 1.69, 1.94, 2.21, 2.5 , 2.81, 3.14, 3.49, 3.86])

Section 2 - sound signal examples

single note

In [4]:
srate=11025;tau=0.35; f=440.;
tarr=np.array([i/srate for i in range(srate)]);
sig=np.array([np.exp(-t/tau)*np.cos(2*np.pi*f*t) for t in tarr]);
plt.plot(sig)
Out[4]:
[<matplotlib.lines.Line2D at 0x24f2231f240>]
In [6]:
sd.play(sig,srate)

illustration of linear array

In [5]:
np.arange(7)
Out[5]:
array([0, 1, 2, 3, 4, 5, 6])
In [6]:
np.arange(-12,37)
Out[6]:
array([-12, -11, -10,  -9,  -8,  -7,  -6,  -5,  -4,  -3,  -2,  -1,   0,
         1,   2,   3,   4,   5,   6,   7,   8,   9,  10,  11,  12,  13,
        14,  15,  16,  17,  18,  19,  20,  21,  22,  23,  24,  25,  26,
        27,  28,  29,  30,  31,  32,  33,  34,  35,  36])
In [7]:
np.arange(0.8,3.6,0.12)
Out[7]:
array([0.8 , 0.92, 1.04, 1.16, 1.28, 1.4 , 1.52, 1.64, 1.76, 1.88, 2.  ,
       2.12, 2.24, 2.36, 2.48, 2.6 , 2.72, 2.84, 2.96, 3.08, 3.2 , 3.32,
       3.44, 3.56])

np.arange is similar to both Range[] and Table[] in Mathematica

In [8]:
plt.plot(np.arange(0.8,3.6,0.12))
Out[8]:
[<matplotlib.lines.Line2D at 0x29e5fe90be0>]

multiple musical notes

In [18]:
srate=11025;tau=0.35; 
freq=[261.63, 261.63, 392, 392, 440, 440, 392];
tdur=[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1]
tdarr=[list(np.linspace(0.,td,np.int32(td*srate))) for td in tdur]
siglong=[[np.exp(-t/tau)*np.cos(2*np.pi*freq[i]*t) for t in tdarr[i]] for i in range(len(freq))];
sigL2=[];
for u in siglong :
    sigL2=sigL2+u
In [20]:
sd.play(sigL2,srate)
In [ ]: