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')
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]:
In [151]:
cD7.shape
Out[151]:
In [152]:
cD1.shape
Out[152]:
In [153]:
synthetic.shape[0]/2.
Out[153]:
In [186]:
plt.plot(cD7)
Out[186]:
In [155]:
64*2**6
Out[155]:
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]:
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
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 [ ]: