def test_perfect_reconstruction(families, wavelets, modes, epsilon, dtype): for wavelet in wavelets: for pmode, mmode in modes: print "Wavelet: %-8s Mode: %s" % (wavelet, pmode), w = pywt.Wavelet(wavelet) data_size = range(2, 40) + [100, 200, 500, 1000, 2000, 10000, 50000, 100000] ok, over = 0, 0 for N in data_size: data = numpy.asarray(numpy.random.random(N), dtype) # compute dwt coefficients pa, pd = pywt.dwt(data, wavelet, pmode) # compute reconstruction rec = pywt.idwt(pa, pd, wavelet, pmode) if len(data) % 2: rec = rec[:len(data)] rms_rec = rms(data, rec) if rms_rec > epsilon: if not over: print print '[RMS_REC > EPSILON] for Mode: %s, Wavelet: %s, Length: %d, rms=%.3g' % (pmode, wavelet, len(data), rms_rec, ) over += 1 else: ok += 1 if not over: print "- RMSE for all %d cases was under %s" % (len(data_size), epsilon)
def plot(data, w, title): w = pywt.Wavelet(w) a = data ca = [] cd = [] if DWT: for i in xrange(5): (a, d) = pywt.dwt(a, w, mode) ca.append(a) cd.append(d) else: for a, d in pywt.swt(data, w, 5): ca.append(a) cd.append(d) pylab.figure() ax_main = pylab.subplot(len(ca) + 1, 1, 1) pylab.title(title) ax_main.plot(data) pylab.xlim(0, len(data) - 1) for i, x in enumerate(ca): #print len(data), len(x), len(data) / (2**(i+1)) lims = -(len(data) / (2.**(i + 1)) - len(x)) / 2. ax = pylab.subplot(len(ca) + 1, 2, 3 + i * 2) ax.plot(x, 'r') if DWT: pylab.xlim(0, len(x) - 1) else: pylab.xlim(w.dec_len * i, len(x) - 1 - w.dec_len * i) pylab.ylabel("A%d" % (i + 1)) for i, x in enumerate(cd): ax = pylab.subplot(len(cd) + 1, 2, 4 + i * 2) ax.plot(x, 'g') pylab.xlim(0, len(x) - 1) if DWT: pylab.ylim(min(0, 1.4 * min(x)), max(0, 1.4 * max(x))) else: #SWT pylab.ylim( min( 0, 2 * min(x[w.dec_len * (1 + i):len(x) - w.dec_len * (1 + i)])), max( 0, 2 * max(x[w.dec_len * (1 + i):len(x) - w.dec_len * (1 + i)]))) pylab.ylabel("D%d" % (i + 1))
def plot(data, w, title): print title w = pywt.Wavelet(w) a = data ca = [] cd = [] for i in xrange(5): (a, d) = pywt.dwt(a, w, mode) ca.append(a) cd.append(d) rec_a = [] rec_d = [] for i, coeff in enumerate(ca): coeff_list = [coeff, None] + [None]*i rec_a.append(pywt.waverec(coeff_list, w)) for i, coeff in enumerate(cd): coeff_list = [None, coeff] + [None]*i rec_d.append(pywt.waverec(coeff_list, w)) pylab.figure() ax_main = pylab.subplot(len(rec_a)+1,1,1) pylab.title(title) ax_main.plot(data) pylab.xlim(0, len(data)-1) for i, y in enumerate(rec_a): #print len(data), len(x), len(data) / (2**(i+1)) ax = pylab.subplot(len(rec_a)+1, 2, 3+i*2) ax.plot(y, 'r') pylab.xlim(0, len(y)-1) pylab.ylabel("A%d" % (i+1)) for i, y in enumerate(rec_d): ax = pylab.subplot(len(rec_d)+1, 2, 4+i*2) ax.plot(y, 'g') pylab.xlim(0, len(y)-1) #pylab.ylim(min(0,1.4*min(x)), max(0,1.4*max(x))) pylab.ylabel("D%d" % (i+1))
def plot(data, w, title): print title w = pywt.Wavelet(w) a = data ca = [] cd = [] for i in xrange(5): (a, d) = pywt.dwt(a, w, mode) ca.append(a) cd.append(d) rec_a = [] rec_d = [] for i, coeff in enumerate(ca): coeff_list = [coeff, None] + [None] * i rec_a.append(pywt.waverec(coeff_list, w)) for i, coeff in enumerate(cd): coeff_list = [None, coeff] + [None] * i rec_d.append(pywt.waverec(coeff_list, w)) pylab.figure() ax_main = pylab.subplot(len(rec_a) + 1, 1, 1) pylab.title(title) ax_main.plot(data) pylab.xlim(0, len(data) - 1) for i, y in enumerate(rec_a): #print len(data), len(x), len(data) / (2**(i+1)) ax = pylab.subplot(len(rec_a) + 1, 2, 3 + i * 2) ax.plot(y, 'r') pylab.xlim(0, len(y) - 1) pylab.ylabel("A%d" % (i + 1)) for i, y in enumerate(rec_d): ax = pylab.subplot(len(rec_d) + 1, 2, 4 + i * 2) ax.plot(y, 'g') pylab.xlim(0, len(y) - 1) #pylab.ylim(min(0,1.4*min(x)), max(0,1.4*max(x))) pylab.ylabel("D%d" % (i + 1))
def test_perfect_reconstruction(families, wavelets, modes, epsilon, dtype): for wavelet in wavelets: for pmode, mmode in modes: print "Wavelet: %-8s Mode: %s" % (wavelet, pmode), w = pywt.Wavelet(wavelet) data_size = range( 2, 40) + [100, 200, 500, 1000, 2000, 10000, 50000, 100000] ok, over = 0, 0 for N in data_size: data = numpy.asarray(numpy.random.random(N), dtype) # compute dwt coefficients pa, pd = pywt.dwt(data, wavelet, pmode) # compute reconstruction rec = pywt.idwt(pa, pd, wavelet, pmode) if len(data) % 2: rec = rec[:len(data)] rms_rec = rms(data, rec) if rms_rec > epsilon: if not over: print print '[RMS_REC > EPSILON] for Mode: %s, Wavelet: %s, Length: %d, rms=%.3g' % ( pmode, wavelet, len(data), rms_rec, ) over += 1 else: ok += 1 if not over: print "- RMSE for all %d cases was under %s" % (len(data_size), epsilon)
times_idwt = [[] for i in range(len(wavelets))] repeat = 3 for j, size in enumerate(sizes): if size > 500000: print "Warning, too big data size may cause page swapping if computer does not have enough memory." data = numpy.ones((size, ), numpy.float64) print("%d/%d" % (j + 1, len(sizes))).rjust(6), str(size).rjust(9), for i, w in enumerate(wavelets): min_t1, min_t2 = 9999., 9999. for _ in xrange(repeat): t1 = clock() (a, d) = pywt.dwt(data, w, mode) t1 = clock() - t1 min_t1 = min(t1, min_t1) t2 = clock() a0 = pywt.idwt(a, d, w, mode) t2 = clock() - t2 min_t2 = min(t2, min_t2) times_dwt[i].append(min_t1) times_idwt[i].append(min_t2) print '.', gc.collect() print for j, (times, name) in enumerate([(times_dwt, 'dwt'), (times_idwt, 'idwt')]):
def __init__(self): self.filter_bank = self.dec_lo, self.dec_hi, self.rec_lo, self.rec_hi data = [1,2,3,4,5,6] ############################################################################ print "Case 1 (custom filter bank - Haar wavelet)" myBank = FilterBank() # pass the user supplied filter bank as argument myWavelet = pywt.Wavelet(name="UserSuppliedWavelet", filter_bank=myBank) #print myWavelet.get_filters_coeffs() print "data:", data a, d = pywt.dwt(data, myWavelet) print "a:", a print "d:", d print "rec:", pywt.idwt(a, d, myWavelet) ############################################################################ print "-" * 75 print "Case 2 (Wavelet object as filter bank - db2 wavelet)" # builtin wavelets can also be treated as filter banks with theirs # filter_bank attribute builtinWavelet = pywt.Wavelet('db2') myWavelet = pywt.Wavelet(name="UserSuppliedWavelet", filter_bank=builtinWavelet) print "data:", data
times_idwt = [[] for i in range(len(wavelets))] repeat = 3 for j, size in enumerate(sizes): if size > 500000: print "Warning, too big data size may cause page swapping if computer does not have enough memory." data = numpy.ones((size,), numpy.float64) print ("%d/%d" % (j + 1, len(sizes))).rjust(6), str(size).rjust(9), for i, w in enumerate(wavelets): min_t1, min_t2 = 9999.0, 9999.0 for _ in xrange(repeat): t1 = clock() (a, d) = pywt.dwt(data, w, mode) t1 = clock() - t1 min_t1 = min(t1, min_t1) t2 = clock() a0 = pywt.idwt(a, d, w, mode) t2 = clock() - t2 min_t2 = min(t2, min_t2) times_dwt[i].append(min_t1) times_idwt[i].append(min_t2) print ".", gc.collect() print