{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}{\f1\fnil\fcharset238 Calibri;}} {\colortbl ;\red0\green0\blue255;\red0\green176\blue80;\red247\green150\blue70;} {\*\generator Msftedit 5.41.21.2509;}\viewkind4\uc1\pard\sa200\sl276\slmult1\qc\lang9\ul\b\f0\fs22 Signal Processing laboratory\ulnone\b0\par Robert Kawecki\par \par \pard\sa200\sl276\slmult1 Numpy library description: {\field{\*\fldinst{HYPERLINK "http://wiki.scipy.org/Tentative_NumPy_Tutorial"}}{\fldrslt{\ul\cf1 http://wiki.scipy.org/Tentative_NumPy_Tutorial}}}\f0\fs22\par \pard\sl240\slmult1\b Basic Python commands to operate with folders:\b0\par \cf2\tab # check current working directory\par \tab # pwd\cf0\par print os.getcwd()\par \par \cf2\tab # change directory to D\par \tab # cd D:/\cf0\par os.chdir('D:/')\par \par \cf2\tab # check current working directory\par \tab # pwd\cf0\par print os.getcwd()\par \par \cf2\tab # make a Biomed directory\par \tab # mkdir Biomed\cf0\par os.mkdir("Biomed")\par \par \cf2\tab # check a content of the D drive\par \tab # ls\cf0\par \cf2\tab # Example: '.' lists files and directories in the current folder.\par \tab # Example2: 'D:\\canopy' lists files and directories in the D:\\canopy folder.\cf0\par print os.listdir('.')\par \par \b Examples of chosen basic commands to create arrays:\b0\par from numpy import * \cf2 # sign '*' means that you import all commands/methods from numpy \tab\tab\tab\tab library\par \cf0 import os\tab\tab\cf2 # imports standard operating system methods. See more: \tab\tab\tab\tab\tab\cf0{\field{\*\fldinst{HYPERLINK "https://docs.python.org/3/library/os.html"}}{\fldrslt{\ul\cf1 https://docs.python.org/3/library/os.html}}}\cf2\f0\fs22\par \cf0 a = arange(15)\tab\tab\cf2 # generates array with N=15 elements, starting from 0 till N-1.\cf0\par print a \tab\tab\tab\cf2 #Output: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]\cf0\par b = a.reshape(3,5)\tab\cf2 # reshapes 1x15 matrix to the matrix which has 3 rows and 5 columns\cf0\par print b\tab\tab\tab\cf2 #Output: \tab [[ 0 1 2 3 4]\par \tab\tab\tab \tab\tab [ 5 6 7 8 9]\par \tab\tab\tab\tab\tab [10 11 12 13 14]]\par \cf0 print zeros( 10 )\tab\cf2 #Output: [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.], 0 with '.' means that this is a \tab\tab\tab\tab float value. See more: \tab\tab\tab\tab\tab\tab\tab\tab\tab\tab\cf0{\field{\*\fldinst{HYPERLINK "https://www.tutorialspoint.com/python/python_variable_types.htm"}}{\fldrslt{\ul\cf1 https://www.tutorialspoint.com/python/python_variable_types.htm}}}\cf2\f0\fs22\par \cf0 print ones( 10 )\tab\tab\cf2 #Output: [ 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]\par \par \cf0 print arange(2,24,3)\cf2\tab #Output: [ 2 5 8 11 14 17 20 23], Generates array from 2 to 24. Each \tab\tab\tab\tab successive element in the array is greater than the previous by 3.\par \tab\tab\tab # arange( FROM , TO , STEP ) - step parameter can be also negative\par \par \cf0 t = array([[1.1, 4.56, 8.999],[2,3,4],[6.78,2.44,5.55]]) \par print 't=',t \tab\tab\cf2 #t= [[ 1.1 4.56 8.999]\par \pard\li2160\sl240\slmult1 [ 2. 3. 4. ]\par [ 6.78 2.44 5.55 ]]\cf0\par \pard\sl240\slmult1 print 't.min()=',t.min()\tab\cf2 #t.min()= 1.1\cf0\par print 't.max()=',t.max()\tab\cf2 #t.max()= 8.999\cf0\par print 't.min(axis=0)=',t.min(axis=0)\tab\cf2 #t.min(axis=0)= [ 1.1 2.44 4. ]\cf0\par print 't.min(axis=1)=',t.min(axis=1)\tab\cf2 #t.min(axis=1)= [ 1.1 2. 2.44]\cf0\par print 't.max(axis=0)=',t.max(axis=0)\tab\cf2 #t.max(axis=0)= [ 6.78 4.56 8.999]\cf0\par print 't.max(axis=1)=',t.max(axis=1)\tab\cf2 #t.max(axis=1)= [ 8.999 4. 6.78 ]\cf0\par print 't.prod()=', t.prod()\tab\tab\cf2 #t.prod()= 99466.1995825\cf0\par print 'average(t)=',average(t)\tab\tab\cf2 #average(t)= 4.26988888889\cf0\par print 't.std()=',t.std()\tab\tab\tab\cf2 #t.std()= 2.37165064152\cf0\par print 't.var()=',t.var()\tab\tab\tab\cf2 #t.var()= 5.62472676543\cf0\par print 't.round()=',t.round()\tab\tab\cf2 #t.round()= [[ 1. 5. 9.]\par \tab\tab\tab\tab\tab\tab [ 2. 3. 4.]\par \tab\tab\tab\tab\tab\tab [ 7. 2. 6.]]\cf0\par print 't.clip(1,3)=',t.clip(1,3)\tab\tab\cf2 #t.clip(1,3)= [[ 1.1 3. 3. ]\par \tab\tab\tab\tab\tab\tab [ 2. 3. 3. ]\par \tab\tab\tab\tab\tab\tab [ 3. 2.44 3. ]]\cf0\par print 'floor(t)=',floor(t)\tab\tab\tab\cf2 #floor(t)= [[ 1. 4. 8.]\par \tab\tab\tab\tab\tab\tab [ 2. 3. 4.]\par [ 6. 2. 5.]]\cf0\par print 'ceil(t)=',ceil(t)\tab\tab\tab\cf2 #ceil(t)= [[ 2. 5. 9.]\par \tab\tab\tab\tab\tab\tab [ 2. 3. 4.]\par \tab\tab\tab\tab\tab\tab [ 7. 3. 6.]]\cf0\par print 'flipud(t)=',flipud(t)\tab\tab\cf2 #flipud(t)= [[ 6.78 2.44 5.55 ]\par \tab\tab\tab\tab\tab\tab [ 2. 3. 4. ]\par \tab\tab\tab\tab\tab\tab [ 1.1 4.56 8.999]]\cf0\par print 'fliplr(t)=',fliplr(t)\tab\tab\tab\cf2 #fliplr(t)= [[ 8.999 4.56 1.1 ]\par \tab\tab\tab\tab\tab\tab [ 4. 3. 2. ]\par \tab\tab\tab\tab\tab\tab [ 5.55 2.44 6.78 ]]\cf0\par print 'rot90(t)=',rot90(t)\tab\tab\cf2 #rot90(t)= [[ 8.999 4. 5.55 ]\par \tab\tab\tab\tab\tab\tab [ 4.56 3. 2.44 ]\par \tab\tab\tab\tab\tab\tab [ 1.1 2. 6.78 ]]\par \par \cf0\b Defining functions:\par Tutorial: {\field{\*\fldinst{HYPERLINK "http://www.diveintopython.net/getting_to_know_python/indenting_code.html"}}{\fldrslt{\ul\cf1 http://www.diveintopython.net/getting_to_know_python/indenting_code.html}}}\f0\fs22\par or if you prefer polish: {\field{\*\fldinst{HYPERLINK "https://pl.wikibooks.org/wiki/Zanurkuj_w_Pythonie/Wci\f1\'eacia_kodu"}}{\fldrslt{\ul\cf1 https://pl.wikibooks.org/wiki/Zanurkuj_w_Pythonie/Wci\lang1045\f1\'eacia_kodu\f1 }}}\f1\fs22\par \lang9\f0\par \b0 def motion(a,t):\tab\tab\cf2 #type def before the function name. you can type input args in (), ':' at \tab\tab\tab\tab the end of the definition\par \cf3\tab """Here enter help description for your function\par \tab Syntax: v,s=motion(a,t)""" \cf2 #Function description here- optional but useful\par \par \tab # Some operations below\cf0\par \tab print "The result of the function motion(a,t) for a = ",a," t = ",t, ": ", \par \tab v=a*t \tab\tab\cf2 # velocity\cf0\par \tab s=(a*t**2)/2. \tab\tab\cf2 #distance\par \tab #Functions in python can return one, two or many parameters, lists, arrays, tuples etc.\cf0\par \tab return v,s\par \cf2 #Function execution\par \cf0 print "#### FUNCTION: MOTION #####"\par a=5\par t=10\par print "Motion: ", motion(a,t)\par \par \b Ploting arrays / signals:\par \b0 import matplotlib.pyplot as plt\par import os\cf2\par \par \cf0 plt.cla()\cf2 #clears an axis, i.e. the currently active axis in the current figure. It leaves the other axes untouched.\par \cf0 plt.clf()\cf2 #clears the entire current figure with all its axes, but leaves the window opened, such that it may be reused for other plots.\par \cf0 plt.close() \cf2 #closes a window, which will be the current window, if not specified otherwise.\par \par \cf0 dt=0.5\par t=arange(0,10,dt)\cf2 # time scale\par \cf0 a=9.81\cf2 # acceleration\par \cf0 v=a*t \cf2 # velocity\par \cf0 s=(a*t**2)/2. \cf2 # distance\par \cf0\par plt.figure(1)\tab\cf2 #new windows\cf0\par plt.plot(t,v,'.') \tab\cf2 #plot velocity parameter. '.' arg means that plot line style will be dotted.\par \cf0 plt.title('Velocity') \cf2 #plot title shown on the output figure\cf0\par plt.xlabel('t[s]')\tab\tab\cf2 # add labels to axes\cf0\par plt.ylabel('v[m/s]')\cf2\par \cf0 plt.legend() \tab\cf2\tab #adds legend\cf0\par plt.grid()\tab\cf2\tab #adds grid\cf0\par plt.show() \tab\cf2\tab #displays windows to the user\par \par # Example 2, printing sinus\par \cf0 from numpy import *\par import os\par from matplotlib.pyplot import *\par x = arange(100)\par y=sin(x*pi/50.)\par plot(x,y)\par show()\cf2\par \par \cf0\b Playing sounds:\b0\par from scipy.io.wavfile import read as read_wav\par from scipy.io.wavfile import write as write_wav\par import winsound\par \par sampling_rate, data = read_wav('noise_voice.wav')\tab\cf2 # File is included in signals.zip folder.\cf0\par print sampling_rate\par print data\par print "#######################################"\par write_wav('male_voice.wav',sampling_rate,data[::-1])\tab\cf2 # This method \par \cf0 winsound.PlaySound("male_voice.wav", winsound.SND_ALIAS)\tab\cf2 # Play .wav file\par \cf0 print "#######################################"\par \par \b Plotting signals with the defined frequency:\par \par \b0 import pylab\par import numpy\par N=2000\par A=10\par f1=10\par f2=20\par f3=25\par fs=1000.\par t = numpy.arange(2000)/fs\par y1=A*numpy.sin(2*numpy.pi*f1*t)\par pylab.plot(t,y1)\par pylab.show()\par y2=A*numpy.sin(2*numpy.pi*f1*t)+A*numpy.sin(2*numpy.pi*f2*t)+A*numpy.sin(2*numpy.pi*f3*t)\par pylab.plot(t,y2)\par pylab.show()\par \b\par Scalling A/D converter values into voltage\par \par Example script:\par \b0 from scipy.io import loadmat, savemat\par from numpy import *\par from pylab import *\par import os\par print os.getcwd()\par fs = 360.\par N=2000\par ecg=loadmat('ecg_mit.mat')['ecg_mit']\par ecg=(5.*(reshape(ecg,len(ecg)) - 1024.))/1024.\par t= arange(N)/fs\par plot(t,ecg[:N])\par show() \par \b\par Using n-bit A/D convertes\par {\field{\*\fldinst{HYPERLINK "https://en.wikipedia.org/wiki/Analog-to-digital_converter"}}{\fldrslt{\ul\cf1 https://en.wikipedia.org/wiki/Analog-to-digital_converter}}}\f0\fs22 Chapter: Resolution\par \par \b0 from scipy.io import loadmat, savemat\par from numpy import *\par from pylab import *\par import os\par \par ecg=loadmat('ecg_mit.mat')['ecg_mit']\par ecg=reshape(ecg,len(ecg))\par \par def quantize_ecg(vec,b):\par \par ecg1=(vec[:2000]>>b)<