import numpy as np
import pandas as pd
import scipy as sp
import matplotlib.pyplot as plt
import sounddevice as sd
import time
# 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] ;
groceryitem
groceryunit
# convert a list into an numpy array:
groceryunit=np.array(groceryunit)
groceryunit
myinfo=["lastnm","firstname","1234567","April",23,1998,]
myinfo
myinfo[0]
myinfo[4]
# make a 2-D np array with power formula
np.fromfunction(lambda i, j: (i+1)**2 + j, (5, 5), dtype=int)
[i**2 for i in range(10)] # just to make a Python list, not np.array:
(np.arange(8))*0.1
[i**2 for i in (np.arange(8))*0.1]
np.array([i**2 for i in (np.arange(8))*0.1])
np.array(["whatever" for i in range(10)])
np.array([(0.1*i)**2+0.25 for i in range(20)])
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)
sd.play(sig,srate)
np.arange(7)
np.arange(-12,37)
np.arange(0.8,3.6,0.12)
np.arange is similar to both Range[] and Table[] in Mathematica
plt.plot(np.arange(0.8,3.6,0.12))
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
sd.play(sigL2,srate)