-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwavelets.py
46 lines (33 loc) · 943 Bytes
/
wavelets.py
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
37
38
39
40
41
42
43
44
45
46
##### WAV Wavelet extraction #####
import scipy.io.wavfile
import matplotlib.pyplot as plt
import pywt
import numpy as np
(rate, data) = scipy.io.wavfile.read("Boiling.wav")
# For debugging uncomment
#print data
# Using the Daubechies2 coefficients
(ca, cd) = pywt.dwt(data,'db2')
# Selecting the soft threshold -- Less computational time
cat = pywt.threshold(ca, np.std(ca)/2, 'soft')
cdt = pywt.threshold(cd, np.std(cd)/2, 'soft')
# Using the Daubechies2 coefficients
data_rec = pywt.idwt(cat, cdt, 'db2')
# For debugging uncomment
#print data_rec
plt.close('all')
plt.subplot(211)
# Original coefficients
plt.plot(ca, '--*b')
plt.plot(cd, '--*r')
# Thresholded coefficients
plt.plot(cat, '--*c')
plt.plot(cdt, '--*m')
plt.legend(['ca','cd','ca_thresh', 'cd_thresh'], loc=0)
plt.grid('on')
plt.subplot(212)
plt.plot(data)
plt.plot(data_rec, 'r')
plt.legend(['original signal', 'reconstructed signal'])
plt.grid('on')
plt.show()