离散小波变换 python_CDF 9/7离散小波变换(卷积)
實際上,我自己解決了這個問題,通過比較系數,然后重建,與這個提升實現的代碼:
基本上,我
1) 使邊界條件對稱,而不是周期性的
2) 必須以一定的方式抵消卷積(和上采樣)以使其對齊。在
下面是代碼,以防其他人遇到問題。我覺得這仍然過于復雜化了,特別是因為它在任何地方都沒有真正的文檔記錄,但至少它是有效的。這也包括我用來對照那個參考進行測試的“開關”,我必須修改Haar小波使其工作。在import random
import math
length = int()
array = list()
row = list()
scaleCoefficients = list()
waveletCoefficients = list()
reconstruction = list()
switch = False
def upsample1(lst, index):
if (index % 2 == 0):
return lst[index/2]
else:
return 0.0
def upsample2(lst, index):
if (index % 2 == 0):
return 0.0
else:
return lst[index/2]
## Generate a random list of floating point numbers
if (not switch):
length = 128
for i in range(length):
array.append(random.random())
else:
length = 32
for i in range(32):
array.append(5.0+i+.4*i*i-.02*i*i*i)
## First Part Just Calculates the Filters
## CDF 9/7 Wavelet
DWTAnalysisLowpass = [.026749, -.016864, -.078223, .266864, .602949, .266864, -.078223, -.016864, .026749]
for i in range(len(DWTAnalysisLowpass)):
DWTAnalysisLowpass[i] = math.sqrt(2.0) * DWTAnalysisLowpass[i]
DWTAnalysisHighpass = [.091272, -.057544, -0.591272, 1.115087, -.591272, -.057544, .091272]
for i in range(len(DWTAnalysisHighpass)):
DWTAnalysisHighpass[i] = DWTAnalysisHighpass[i]/math.sqrt(2.0)
DWTSynthesisLowpass = [-.091272, -.057544, 0.591272, 1.115087, .591272, -.057544, -.091272]
for i in range(len(DWTSynthesisLowpass)):
DWTSynthesisLowpass[i] = DWTSynthesisLowpass[i]/math.sqrt(2.0)
DWTSynthesisHighpass = [.026749, .016864, -.078223, -.266864, .602949, -.266864, -.078223, .016864, .026749]
for i in range(len(DWTSynthesisHighpass)):
DWTSynthesisHighpass[i] = math.sqrt(2.0) * DWTSynthesisHighpass[i]
## Haar Wavelet
## c = 1.0/math.sqrt(2)
## DWTAnalysisLowpass = [c,c]
## DWTAnalysisHighpass = [c, -c]
## DWTSynthesisLowpass = [-c, c]
## DWTSynthesisHighpass = [c, c]
# Do the forward transform. We can skip every other sample since they would
# be removed in the downsampling anyway
for i in range(0,length,2):
newVal = 0.0
## Convolve the next j elements by the low-pass analysis filter
for j in range(len(DWTAnalysisLowpass)):
index = i + j - len(DWTAnalysisLowpass)/2
if(index >= length):
index = 2*length - index - 2
elif (index < 0):
index = -index
newVal = newVal + array[index]*DWTAnalysisLowpass[j]
# append the new value to the list of scale coefficients
scaleCoefficients.append(newVal)
newVal = 0.0
# Convolve the next j elements by the high-pass analysis filter
for j in range(len(DWTAnalysisHighpass)):
index = i + j - len(DWTAnalysisHighpass)/2 + 1
if(index >= length):
index = 2*length - index - 2
elif (index < 0):
index = -index
newVal = newVal + array[index]*DWTAnalysisHighpass[j]
# append the new value to the list of wavelet coefficients
waveletCoefficients.append(newVal)
# Do the inverse transform
for i in range(length):
newVal = 0.0
# convolve the upsampled wavelet coefficients with the high-pass synthesis filter
for j in range(len(DWTSynthesisHighpass)):
index = i + j - len(DWTSynthesisHighpass)/2
if(index >= length):
index = 2*length - index - 2
elif (index < 0):
index = -index
newVal = newVal + upsample2(waveletCoefficients, index)*DWTSynthesisHighpass[j]
# convolve the upsampled scale coefficients with the low-pass synthesis filter, and
# add it to the previous convolution
for j in range(len(DWTSynthesisLowpass)):
index = i + j - len(DWTSynthesisLowpass)/2
if(index >= length):
index = 2*length - index - 2
elif (index < 0):
index = -index
newVal = newVal + upsample1(scaleCoefficients, index)*DWTSynthesisLowpass[j]
reconstruction.append(newVal)
print ("Sums: ")
print sum(reconstruction)
print sum(array)
print ("Original Signal: ")
print array
if (not switch):
print ("Wavelet Coefficients: ")
for i in range(len(scaleCoefficients)):
print ("sc[" + str(i) + "]: " + str(scaleCoefficients[i]))
for i in range(len(waveletCoefficients)):
print ("wc[" + str(i) + "]: " + str(waveletCoefficients[i]))
print ("Reconstruction: ")
print reconstruction
總結
以上是生活随笔為你收集整理的离散小波变换 python_CDF 9/7离散小波变换(卷积)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab实现单纯型法解线性规划_【运
- 下一篇: websocket python爬虫_p