浅谈Python小波分析库Pywavelets的一点使用心得

yipeiwu_com7年前Python基础

本文介绍了Python小波分析库Pywavelets,分享给大家,具体如下:

# -*- coding: utf-8 -*- 
import numpy as np
import math
import matplotlib.pyplot as plt
import pandas as pd
import datetime 
from scipy import interpolate
from pandas import DataFrame,Series

import numpy as np 
import pywt 

data = np.linspace(1, 4, 7) 

# pywt.threshold方法讲解: 
#        pywt.threshold(data,value,mode ='soft',substitute = 0 ) 
#        data:数据集,value:阈值,mode:比较模式默认soft,substitute:替代值,默认0,float类型 

#data:  [ 1.  1.5 2.  2.5 3.  3.5 4. ] 
#output:[ 6.  6.  0.  0.5 1.  1.5 2. ] 
#soft 因为data中1小于2,所以使用6替换,因为data中第二个1.5小于2也被替换,2不小于2所以使用当前值减去2,,2.5大于2,所以2.5-2=0.5..... 

print(pywt.threshold(data, 2, 'soft',6))  


#data:  [ 1.  1.5 2.  2.5 3.  3.5 4. ] 
#hard data中绝对值小于阈值2的替换为6,大于2的不替换 
print (pywt.threshold(data, 2, 'hard',6)) 


#data:  [ 1.  1.5 2.  2.5 3.  3.5 4. ] 
#data中数值小于阈值的替换为6,大于等于的不替换 
print (pywt.threshold(data, 2, 'greater',6) )

print (data )
#data:  [ 1.  1.5 2.  2.5 3.  3.5 4. ] 
#data中数值大于阈值的,替换为6 
print (pywt.threshold(data, 2, 'less',6) )

[6. 6. 0. 0.5 1. 1.5 2. ]
[6. 6. 2. 2.5 3. 3.5 4. ]
[6. 6. 2. 2.5 3. 3.5 4. ]
[1. 1.5 2. 2.5 3. 3.5 4. ]
[1. 1.5 2. 6. 6. 6. 6. ]

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np
import matplotlib.pyplot as plt

import pywt
import pywt.data


ecg = pywt.data.ecg()

data1 = np.concatenate((np.arange(1, 400),
            np.arange(398, 600),
            np.arange(601, 1024)))
x = np.linspace(0.082, 2.128, num=1024)[::-1]
data2 = np.sin(40 * np.log(x)) * np.sign((np.log(x)))

mode = pywt.Modes.smooth


def plot_signal_decomp(data, w, title):
  """Decompose and plot a signal S.
  S = An + Dn + Dn-1 + ... + D1
  """
  w = pywt.Wavelet(w)#选取小波函数
  a = data
  ca = []#近似分量
  cd = []#细节分量
  for i in range(5):
    (a, d) = pywt.dwt(a, w, mode)#进行5阶离散小波变换
    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
    if i ==3:
      print(len(coeff))
      print(len(coeff_list))
    rec_d.append(pywt.waverec(coeff_list, w))

  fig = plt.figure()
  ax_main = fig.add_subplot(len(rec_a) + 1, 1, 1)
  ax_main.set_title(title)
  ax_main.plot(data)
  ax_main.set_xlim(0, len(data) - 1)

  for i, y in enumerate(rec_a):
    ax = fig.add_subplot(len(rec_a) + 1, 2, 3 + i * 2)
    ax.plot(y, 'r')
    ax.set_xlim(0, len(y) - 1)
    ax.set_ylabel("A%d" % (i + 1))

  for i, y in enumerate(rec_d):
    ax = fig.add_subplot(len(rec_d) + 1, 2, 4 + i * 2)
    ax.plot(y, 'g')
    ax.set_xlim(0, len(y) - 1)
    ax.set_ylabel("D%d" % (i + 1))


#plot_signal_decomp(data1, 'coif5', "DWT: Signal irregularity")
#plot_signal_decomp(data2, 'sym5',
#          "DWT: Frequency and phase change - Symmlets5")
plot_signal_decomp(ecg, 'sym5', "DWT: Ecg sample - Symmlets5")


plt.show()

72
5

将数据序列进行小波分解,每一层分解的结果是上次分解得到的低频信号再分解成低频和高频两个部分。如此进过N层分解后源信号X被分解为:X = D1 + D2 + … + DN + AN 其中D1,D2,…,DN分别为第一层、第二层到等N层分解得到的高频信号,AN为第N层分解得到的低频信号。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python十进制和二进制的转换方法(含浮点数)

本文介绍了python十进制和二进制的转换方法(含浮点数),分享给大家,也给自己留个笔记,具体如下: 我终于写完了 , 十进制转二进制的小数部分卡了我将近一个小时 上代码 #...

python打印9宫格、25宫格等奇数格 满足横竖斜相加和相等

python打印9宫格、25宫格等奇数格 满足横竖斜相加和相等

相信大家都做过九宫格的游戏,规则是要求填数字1-9在九个方格内,使横竖斜相加和相等。只填九个那可能有些简单,但是填25个,填49个,81个等奇数个那,这时候就要求我们去找其中的规律。找到...

在windows下使用python进行串口通讯的方法

Windows版本下的python并没有内置串口通讯的pyserial的库,所以需要自己下载。参照了网上的教程,有许多用的pip的安装方式,但是试了几个都没有用,所以想到用GitHub下...

关于Python中Inf与Nan的判断问题详解

大家都知道 在Python 中可以用如下方式表示正负无穷: float("inf") # 正无穷 float("-inf") # 负无穷 利用 inf(infinite) 乘以 0...

python奇偶行分开存储实现代码

python奇偶行分开存储实现代码

例子: 1:www.jb51.net 2:www.jb51.net 3:www.jb51.net 4:www.jb51.net 5:www.jb51.net 6:www.jb51.ne...