关于小波软硬阈值折中法去躁的python编程,什么结果都没出?请求帮助
#-*-coding:utf-8-*-importpywtimportnumpyasnpimportpandasaspdimportxlrdimportmatplotli...
# -*- coding: utf-8 -*-
import pywt
import numpy as np
import pandas as pd
import xlrd
import matplotlib
import matplotlib.pyplot as plt
import math
from pandas import read_csv
# 导入数据
f = open('F:\鸟击按日风险值.csv',encoding='utf-8',errors='ignore')
data= pd.read_csv(f)
print(data,type(data))
# 去躁前的图
x1 = data['发生日期']
y_values = data['风险值']
print(x1,y_values)
plt.plot(x1, y_values)
plt.show()
# datafram 转为list列表
datafram = read_csv('F:\鸟击按日风险值.csv', nrows = 2965, usecols = [1,], engine='python')
#nrows:读取行数,usecols=[n,]:仅读取第n列,usecols=[a,b,c]:读取a、b、c列
dataset = datafram .values
List = []
for k in dataset:
for j in k:
List.append(j)
print(datafram [0:2965])
print(dataset[0:2965])
print('list:',List[0:2965])
# 小波分解 3层
db1 = pywt.Wavelet( 'db1')
[ca3,cd3,cd2,cd1] = pywt.wavedec( List ,db1)
print('ca3:',ca3)
print('cd3:',cd3)
print('cd2:',cd2)
print('cd1:',cd1)
#小波系数进行阈值处理
coeffs= pywt.wavedec(List,3,'db1')
def sgn(num):
if (num > 0.0):
return 1.0
elif (num == 0.0):
return 0.0
else:
return -1.0
a=0.5
thcoeffs = []
for i in range(1, len(coeffs)):
tmp = coeffs[i].copy()
Sum = 0.0
for j in coeffs[i]:
Sum = Sum + abs(j)
N = len(coeffs[i])
Sum = (1.0 / float(N)) * Sum
sigma = (1.0 / 0.6745) * Sum
lamda = sigma * math.sqrt(2.0 * math.log(float(N), math.e))
for k in range(len(tmp)):
if (abs(tmp[k]) >= lamda):
tmp[k] = sgn(tmp[k]) * (abs(tmp[k]) - a * lamda)
else:
tmp[k] = 0.0
thcoeffs.append(tmp)
# 小波重构
[cA, cH, cV, cD] =pywt.waverec(coeffs,thcoeffs,mode='symmetric',axis=-1)
# meta = pywt.waverec(coeffs, db1)
# 画图
print(data,type(data))
x1 = data['发生日期']
y_values = a
print(x1,a)
plt.plot(x1, a)
plt.show() 展开
import pywt
import numpy as np
import pandas as pd
import xlrd
import matplotlib
import matplotlib.pyplot as plt
import math
from pandas import read_csv
# 导入数据
f = open('F:\鸟击按日风险值.csv',encoding='utf-8',errors='ignore')
data= pd.read_csv(f)
print(data,type(data))
# 去躁前的图
x1 = data['发生日期']
y_values = data['风险值']
print(x1,y_values)
plt.plot(x1, y_values)
plt.show()
# datafram 转为list列表
datafram = read_csv('F:\鸟击按日风险值.csv', nrows = 2965, usecols = [1,], engine='python')
#nrows:读取行数,usecols=[n,]:仅读取第n列,usecols=[a,b,c]:读取a、b、c列
dataset = datafram .values
List = []
for k in dataset:
for j in k:
List.append(j)
print(datafram [0:2965])
print(dataset[0:2965])
print('list:',List[0:2965])
# 小波分解 3层
db1 = pywt.Wavelet( 'db1')
[ca3,cd3,cd2,cd1] = pywt.wavedec( List ,db1)
print('ca3:',ca3)
print('cd3:',cd3)
print('cd2:',cd2)
print('cd1:',cd1)
#小波系数进行阈值处理
coeffs= pywt.wavedec(List,3,'db1')
def sgn(num):
if (num > 0.0):
return 1.0
elif (num == 0.0):
return 0.0
else:
return -1.0
a=0.5
thcoeffs = []
for i in range(1, len(coeffs)):
tmp = coeffs[i].copy()
Sum = 0.0
for j in coeffs[i]:
Sum = Sum + abs(j)
N = len(coeffs[i])
Sum = (1.0 / float(N)) * Sum
sigma = (1.0 / 0.6745) * Sum
lamda = sigma * math.sqrt(2.0 * math.log(float(N), math.e))
for k in range(len(tmp)):
if (abs(tmp[k]) >= lamda):
tmp[k] = sgn(tmp[k]) * (abs(tmp[k]) - a * lamda)
else:
tmp[k] = 0.0
thcoeffs.append(tmp)
# 小波重构
[cA, cH, cV, cD] =pywt.waverec(coeffs,thcoeffs,mode='symmetric',axis=-1)
# meta = pywt.waverec(coeffs, db1)
# 画图
print(data,type(data))
x1 = data['发生日期']
y_values = a
print(x1,a)
plt.plot(x1, a)
plt.show() 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询