淺談Python小波分析庫Pywavelets的一點(diǎn)使用心得
本文介紹了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:數(shù)據(jù)集,value:閾值,mode:比較模式默認(rèn)soft,substitute:替代值,默認(rèn)0,float類型 #data: [ 1. 1.5 2. 2.5 3. 3.5 4. ] #output:[ 6. 6. 0. 0.5 1. 1.5 2. ] #soft 因?yàn)閐ata中1小于2,所以使用6替換,因?yàn)閐ata中第二個(gè)1.5小于2也被替換,2不小于2所以使用當(dāng)前值減去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中數(shù)值小于閾值的替換為6,大于等于的不替換 print (pywt.threshold(data, 2, 'greater',6) ) print (data ) #data: [ 1. 1.5 2. 2.5 3. 3.5 4. ] #data中數(shù)值大于閾值的,替換為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)#選取小波函數(shù) a = data ca = []#近似分量 cd = []#細(xì)節(jié)分量 for i in range(5): (a, d) = pywt.dwt(a, w, mode)#進(jìn)行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))#重構(gòu) 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
將數(shù)據(jù)序列進(jìn)行小波分解,每一層分解的結(jié)果是上次分解得到的低頻信號(hào)再分解成低頻和高頻兩個(gè)部分。如此進(jìn)過N層分解后源信號(hào)X被分解為:X = D1 + D2 + … + DN + AN 其中D1,D2,…,DN分別為第一層、第二層到等N層分解得到的高頻信號(hào),AN為第N層分解得到的低頻信號(hào)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Django使用裝飾器限制對視圖的訪問及實(shí)現(xiàn)原理
除了可以在視圖處理中校驗(yàn)用戶身份以及驗(yàn)證用戶權(quán)限之外,Django還提供了便捷的裝飾器來完成這兩類校驗(yàn),下面介紹這兩個(gè)裝飾器的使用方法與實(shí)現(xiàn)原理,對Django裝飾器限制視圖訪問相關(guān)知識(shí)感興趣的朋友一起看看吧2022-10-10python采用django框架實(shí)現(xiàn)支付寶即時(shí)到帳接口
這篇文章主要介紹了python采用django框架實(shí)現(xiàn)支付寶即時(shí)到帳接口的相關(guān)資料,需要的朋友可以參考下2016-05-05python3-flask-3將信息寫入日志的實(shí)操方法
在本篇文章里小編給大家整理的是關(guān)于python3-flask-3將信息寫入日志的實(shí)操方法,有興趣的朋友們學(xué)習(xí)下。2019-11-11使用matplotlib修改坐標(biāo)軸,將y軸的間距設(shè)置為某一個(gè)值
這篇文章主要介紹了使用matplotlib修改坐標(biāo)軸,將y軸的間距設(shè)置為某一個(gè)值方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02Python爬蟲設(shè)置Cookie解決網(wǎng)站攔截并爬取螞蟻短租的問題
這篇文章主要介紹了Python爬蟲設(shè)置Cookie解決網(wǎng)站攔截并爬取螞蟻短租,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Python數(shù)據(jù)分析之缺失值檢測與處理詳解
在實(shí)際的數(shù)據(jù)處理中,缺失值是普遍存在的,如何使用 Python 檢測和處理缺失值,就是本文要講的主要內(nèi)容。感興趣的同學(xué)可以關(guān)注一下2021-12-12