DWT


In [1]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

In [6]:
import pywt

In [2]:
def tf(signal, fs, w=256, wtime=False, poverlap=None, ylim=None, colorbar=False, vmin=None, vmax=None):
    dt = 1./fs
    n = signal.size
    t = np.arange(0.0, n*dt, dt)
        
    if wtime:
        # Then the window length is time so change to samples
        w *= fs
        w = int(w)
        
    if poverlap:
        # Then overlap is a percentage
        noverlap = int(w * poverlap/100.)
    else:
        noverlap = w - 1
        
    plt.figure(figsize=(12,8))
    plt.subplot(211)
    plt.plot(t, signal)
    ax1 = plt.subplot(212)
    Pxx, freqs, bins, im = plt.specgram(signal, NFFT=w, Fs=fs, noverlap=noverlap, cmap='Greys', vmin=vmin, vmax=vmax)
    if colorbar: plt.colorbar()
    if ylim:
        ax1.set_ylim((0,ylim))
    plt.show()

In [3]:
synthetic = np.loadtxt('benchmark_signals/synthetic.txt')

In [5]:
tf(synthetic, 800, w=128, ylim=256, vmin=-30)



In [149]:
print pywt.wavelist('sym')


['sym2', 'sym3', 'sym4', 'sym5', 'sym6', 'sym7', 'sym8', 'sym9', 'sym10', 'sym11', 'sym12', 'sym13', 'sym14', 'sym15', 'sym16', 'sym17', 'sym18', 'sym19', 'sym20']

In [216]:
cA6, cD7, cD6, cD5, cD4, cD3, cD2, cD1 = pywt.wavedec(synthetic, "sym16", level=7)

In [223]:
c= pywt.wavedec(synthetic, "sym20", level=16) 
plt.plot(c[1])


Out[223]:
[<matplotlib.lines.Line2D at 0x10964b5d0>]

In [151]:
cD7.shape


Out[151]:
(94,)

In [152]:
cD1.shape


Out[152]:
(4111,)

In [153]:
synthetic.shape[0]/2.


Out[153]:
4096.0

In [186]:
plt.plot(cD7)


Out[186]:
[<matplotlib.lines.Line2D at 0x1093bc190>]

In [155]:
64*2**6


Out[155]:
4096

In [156]:
c1 = np.repeat(cD1,1)[:4096]
c2 = np.repeat(cD2,2)[:4096]
c3 = np.repeat(cD3,4)[:4096]
c4 = np.repeat(cD4,8)[:4096]
c5 = np.repeat(cD5,16)[:4096]
c6 = np.repeat(cD6,32)[:4096]

In [157]:
i1 = np.repeat(c1[:,np.newaxis], 64, axis=1)
i2 = np.repeat(c2[:,np.newaxis], 32, axis=1)
i3 = np.repeat(c3[:,np.newaxis], 16, axis=1)
i4 = np.repeat(c4[:,np.newaxis], 8, axis=1)
i5 = np.repeat(c5[:,np.newaxis], 4, axis=1)
i6 = np.repeat(c6[:,np.newaxis], 2, axis=1)
i1.shape


Out[157]:
(4096, 64)

In [158]:
spec = np.zeros((4096, 126))

In [159]:
spec[:, :2] = i6
spec[:, 2:6] = i5
spec[:, 6:14] = i4
spec[:, 14:30] = i3
spec[:, 30:62] = i2
spec[:, 62:] = i1

In [160]:
plt.figure(figsize=(14,7))
plt.imshow(np.abs(spec.T), aspect="auto", origin="lower", cmap="Greys")
plt.colorbar()
plt.show()



In [224]:
def specdwt(signal, wavelet, level):
    length = signal.shape[0]/2.
    coeffs = pywt.wavedec(signal, wavelet, level=level)[1:]
    
    exp = []
    for i, c in enumerate(reversed(coeffs)):
        exp.append(np.repeat(c,2**i)[:length])
    
    exp2 = []
    cols = 0
    for i, e in enumerate(reversed(exp)):
        repeats = 2**(i+1)
        exp2.append(np.repeat(e[:,np.newaxis], repeats, axis=1))
        cols += repeats
        
    spec = np.zeros((length, cols))
    
    end = 0
    for i, e in enumerate(exp2):
        start = end
        end = start + 2**(i+1)
        spec[:, start:end] = e
    
    plt.figure(figsize=(14,7))
    plt.imshow(np.abs(spec.T), aspect="auto", origin="lower", cmap="Greys", interpolation="none")
    plt.colorbar()
    plt.show()

In [225]:
l = [1,2,3,4,5,6]
for i, a in enumerate(reversed(l)):
    print i, a


0 6
1 5
2 4
3 3
4 2
5 1

In [226]:
specdwt(synthetic, "sym5", level=12)



In [242]:
[phi, psi, x] = pywt.Wavelet('db2').wavefun(level=12)
plt.plot(x, psi)
plt.show()



In [ ]: