SIGNAL PROCESSING
Laboratory #3

Importing Python packages
storing/loading: text, binary, Matlab and wave files

M. Kociński, P. Strumiłło, A. Borowska-Terka

Medical Electronics Division
Institute of Electronics


In [1]:
import funkcje_do_lab3 as f3
f3.hide()
Out[1]:
The raw code for this IPython notebook is by default hidden for easier reading. To toggle on/off the raw code, click here.

PURPOSE:

To learn how to import Python packages and how to load and save text, binary, Matlab and wave files files.

TASKS:
1.Learn four ways of importing Phyton packages inside scripts. Please create and run the following scripts in your Python folder: sinus1.py, sinus2.py, sinus3.py, sinus4.py:

In [2]:
f3.fig(f3.s1)
Out[2]:
In [3]:
f3.fig(f3.s4)
Out[3]:

EXPECTED RESULTS:

In [4]:
%pylab inline
print ''
print 'sinus1:'
f3.sinus1()
Populating the interactive namespace from numpy and matplotlib

sinus1:

In [5]:
print ''
print 'sinus2:'
f3.sinus2()

sinus2:

In [6]:
print ''
print 'sinus3:'
f3.sinus3()

sinus3:

sinus4 (Not recommended importing!):

In [7]:
from pylab import *
from numpy import *

x=arange(100)
y=sin(x*pi/50)

plot(x,y)
show()

2.Loading and saving files in ASCII format (the so called text files)

In [8]:
f3.fig(f3.r)
Out[8]:

Please note that saving and loading files takes place from the current directory.
For loading/saving files from other directories you need to specify a path to a file, e.g.
xx, yy1, yy2 = np.loadtxt('d:/Biomed2014_1/my_sin_cos.txt')

In [9]:
import numpy as np
x = np.arange(100)

y1 = np.sin(x*np.pi/50)
y2 = np.cos(x*np.pi/50)

np.savetxt('my_sin_cos.txt', (x,y1,y2),fmt='%.2f')
In [10]:
import os
files = os.listdir('.')   #all files in current directory
print 'Did we save our file my_sin_cos.txt?'
'my_sin_cos.txt' in files #is there our file 'my_sin_cos.txt'?
Did we save our file my_sin_cos.txt?

Out[10]:
True
In [11]:
import numpy as np

# load from file - case 1
xx, yy1, yy2 = np.loadtxt('my_sin_cos.txt')

#draw loaded data
#we can import some modules (modules or function from modules) also in the middle of the file
from pylab import plot, show, title, figure
figure(1)
plot(xx,yy1,xx,yy2)
title('Loaded data - case 1')
show()

3.Loading and saving binary files in Python native formats .npy and .npyz

In [12]:
f3.fig('binary_files.png')
Out[12]:
In [13]:
import numpy as np

b = np.arange(20.)
np.save('b.npy', b)
print 'The variable b:'
print b

print 'The content of file b2.npy:'
b2 = np.load('b.npy')
print b2
The variable b:
[  0.   1.   2.   3.   4.   5.   6.   7.   8.   9.  10.  11.  12.  13.  14.
  15.  16.  17.  18.  19.]
The content of file b2.npy:
[  0.   1.   2.   3.   4.   5.   6.   7.   8.   9.  10.  11.  12.  13.  14.
  15.  16.  17.  18.  19.]

In [14]:
cc = np.arange(5)
dd = np.arange(4,15,2.5)
ee = np.arange(100,110,2)
print 'cc:'
print cc
print 'dd:'
print dd
print 'ee:'
print ee
print ''
print 5*'*'
# names: a,b,c can be any valid key names
np.savez('multi_array.npz',a=cc, b=dd, c=ee)

#load from file
multi = np.load('multi_array.npz')

print 'List of arrays type in:'
print multi.files
print ''

# to select and print individual arrays type in:
print 'Array a:'
print multi['a']
print ''

# to assign to a new array
zz = multi['b']
print 'Array zz:'
print zz
cc:
[0 1 2 3 4]
dd:
[  4.    6.5   9.   11.5  14. ]
ee:
[100 102 104 106 108]

*****
List of arrays type in:
['a', 'c', 'b']

Array a:
[0 1 2 3 4]

Array zz:
[  4.    6.5   9.   11.5  14. ]

4.Loading and saving binary files Matlab files *.mat

In [15]:
f3.fig('matlab_files.png')
Out[15]:
In [16]:
from scipy.io import loadmat, savemat
from numpy import reshape
from pylab import plot

ecg=loadmat('ecg_all.mat')['ecg_s']
ecg=reshape(ecg,len(ecg))
plot(ecg)
show() 

#saving data in .mat files
savemat('ecg_py.mat',{'ecg_new':ecg})

5.Loading, saving and playing wave files *.wav

In [17]:
f3.fig('play_audio.png')
Out[17]:
In [18]:
import winsound
winsound.PlaySound("scale.wav", winsound.SND_ALIAS)
In [19]:
f3.fig('load_wave.png')
Out[19]:

EXPECTED RESULTS:

In [20]:
from scipy.io.wavfile import read as read_wav

sampling_rate, data = read_wav('scale.wav')
print sampling_rate
print data
11025
[     0   2970   5876 ..., -19999 -19250 -16986]

In [21]:
from scipy.io.wavfile import write as write_wav

write_wav('inv_scale.wav',sampling_rate,data[::-1])

winsound.PlaySound("inv_scale.wav", winsound.SND_ALIAS)

6.You can work with sounds (signals) interactively.
In this case run the command line of your operating system. Change your current directory to D:/Biomed2014_1.
Type in >>ipython notebook
By selecting icon [New notebook] create new file audio.ipynb
Type the following sequence of commands:

from IPython.display import Audio
import numpy as np
f1 = 320
f2 = 345
rate = 8000.0
L=3

times = np.linspace(0, L, rateL)
signal = np.cos(2
np.pif1times) + np.cos(2np.pif2*times)

Audio(data=signal, rate=rate)


By selecting icon [Run Cell] run your script. Alter frequency values f1, f2 and run script again.

EXPECTED RESULTS:

In [22]:
from IPython.display import Audio
import numpy as np
f1 = 320
f2 = 345
rate = 8000.0
L=3

times = np.linspace(0, L, rate*L)
signal = np.cos(2*np.pi*f1*times) + np.cos(2*np.pi*f2*times)

Audio(data=signal, rate=rate)
Out[22]:
In [22]: