import funkcje_do_lab3 as f3
f3.hide()
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:
f3.fig(f3.s1)
f3.fig(f3.s4)
EXPECTED RESULTS:
%pylab inline
print ''
print 'sinus1:'
f3.sinus1()
print ''
print 'sinus2:'
f3.sinus2()
print ''
print 'sinus3:'
f3.sinus3()
sinus4 (Not recommended importing!):
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)
f3.fig(f3.r)
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')
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')
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'?
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
f3.fig('binary_files.png')
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
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
4.Loading and saving binary files Matlab files *.mat
f3.fig('matlab_files.png')
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
f3.fig('play_audio.png')
import winsound
winsound.PlaySound("scale.wav", winsound.SND_ALIAS)
f3.fig('load_wave.png')
EXPECTED RESULTS:
from scipy.io.wavfile import read as read_wav
sampling_rate, data = read_wav('scale.wav')
print sampling_rate
print data
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(2np.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:
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)