百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

简单的基于小波分析的时间序列降噪方法(Python)

off999 2025-05-28 19:40 53 浏览 0 评论

import numpy as np
import pywt
import matplotlib.pyplot as plt


def denoise_signal(signal, wavelet='db4', level=6, noise_std=0.1):
    try:
        # Decompose signal into wavelet coefficients
        coeffs = pywt.wavedec(signal, wavelet, level=level)


        # Threshold the coefficients using universal threshold
        threshold = noise_std * np.sqrt(2 * np.log(len(signal)))
        denoised_coeffs = [pywt.threshold(c, threshold, mode='soft') for c in coeffs]


        # Reconstruct the signal from the denoised coefficients
        denoised_signal = pywt.waverec(denoised_coeffs, wavelet)


        # Ensure the reconstructed signal has the same length as the original signal
        denoised_signal = denoised_signal[:len(signal)]


        # Calculate SNR
        noise = signal - denoised_signal
        snr = 20 * np.log10(np.linalg.norm(signal) / np.linalg.norm(noise))


        return denoised_signal, snr
    except Exception as e:
        print(f"Error during denoising: {e}")
        return None, None


def plot_signals(original_signal, noisy_signal, denoised_signal):
    plt.figure(figsize=(12, 6))
    plt.plot(original_signal, label='Original signal')
    plt.plot(noisy_signal, label='Noisy signal')
    plt.plot(denoised_signal, label='Denoised signal')
    plt.legend()
    plt.title('Signal Denoising using Wavelet Transform')
    plt.xlabel('Sample Index')
    plt.ylabel('Amplitude')
    plt.grid(True)
    plt.show()


# Generate a noisy signal
np.random.seed(0)
original_signal = np.random.randn(1000)
noise = 0.1 * np.random.randn(1000)
noisy_signal = original_signal + noise


# Denoise the signal
denoised_signal, snr = denoise_signal(noisy_signal)




plot_signals(original_signal, noisy_signal, denoised_signal)




if snr is not None:
    print('SNR:', snr)
else:
    print('Denoising failed.')

import numpy as np
import pywt


def add_noise(signal, noise_std):
    noise = np.random.normal(0, noise_std, size=len(signal))
    noisy_signal = signal + noise
    return noisy_signal


def denoise_signal(signal):
    # Decompose signal into wavelet coefficients
    coeffs = pywt.wavedec(signal, 'db4', level=6)


    # Threshold the coefficients using universal threshold
    threshold = np.sqrt(2*np.log(len(signal)))
    denoised_coeffs = [pywt.threshold(c, threshold, mode='soft') for c in coeffs]


    # Reconstruct the signal from the denoised coefficients
    denoised_signal = pywt.waverec(denoised_coeffs, 'db4')


    # Calculate the signal-to-noise ratio (SNR)
    noise = signal - denoised_signal
    snr = 20*np.log10(np.linalg.norm(signal)/np.linalg.norm(noise))


    return denoised_signal, snr


# Generate a signal
signal = np.sin(2*np.pi*5*np.linspace(0, 1, num=1000))


# Add Gaussian noise to the signal
noise_std = 0.1
noisy_signal = add_noise(signal, noise_std)


# Denoise the signal and print the SNR
denoised_signal, snr = denoise_signal(noisy_signal)
print('SNR:', snr)

SNR: 3.5626499602068638

知乎学术咨询:https://www.zhihu.com/consult/people/792359672131756032?isMe=1

担任《Mechanical System and Signal Processing》等审稿专家,擅长领域:现代信号处理,机器学习,深度学习,数字孪生,时间序列分析,设备缺陷检测、设备异常检测、设备智能故障诊断与健康管理PHM等。



相关推荐

win7升级win10要留多少空间(windows7升级windows10需要多长时间)

win7电脑在系统已经激活并且开启系统更新的情况下,符合条件的系统会在右下角弹出windows10免费升级,直接点击确定就开始升级了。或者下载win10安助手,运行软件后会自动下载windows1...

国外比较开放的浏览器(国外比较开放的浏览器推荐)

1、打开控制面板。2、点击“检查防火墙状态”。3、点击左侧“高级设置”。4、选中“入栈规则”。5、右侧点击“新建规则”。6、选择“端口”。7、选择“TCP”,选中“特定端口”并输入你要开发的端口,或者...

一健ghost下载(一键ghost v2015.07.05)

你的是原版镜像,当然无法识别。。你可以使用微软usb工具。将镜像写入U盘或光盘。

纯净无毒的win7下载(有没有纯净的win7系统)

下面提供的是微软发布的Windows7各版本光盘ISO镜像下载地址,原始文件均来源自MSDN,和零-售彩盒版本光盘内容完全一致。请放心下载。(如果需要光盘的买家,请无视以下内容)下...

ie浏览器文件损坏怎么修复(ie浏览器破坏怎么恢复)

可以在浏览记录里面查到。重新下载一次就行了如果你在IE浏览器里面下载的文件被你不小心删掉了,而且这个文件对你来说很重要,你可以打开你的IE浏览器选择历史记录,在历史记录里面就可以找到相关的下载的地方,...

电脑没音量是什么原因(电脑没音量是什么原因造成的)

电脑突然没有声音可能是由于以下原因:1.音量设置被关闭或静音;2.音频驱动程序出现问题或需要更新;3.音频设备连接问题,例如音频插头没有插好或者有问题;4.操作系统的声音设置出现问题。解决方法包括检查...

电脑蓝屏u盘装系统教程(电脑蓝屏系统u盘怎么装系统)

dell电脑蓝屏用U盘重装系统方法,1在需要重装的电脑上插入启动U盘,开机并不断按下U盘启动快捷键。2在进入系统启动菜单中选择带有USB字样的选项并回车。3进入系统引导选项界面,选择Windows1...

系统之家官网纯净版(系统之家首页)

我现在用的就是系统之家的win7,以前用的是系统之家纯净版的xp,纯净版的好处就是没有预装的软件,缺点就是有些东西要自己装,包括有些软件必须的东西。装个游戏又要装这个又要装那个的很麻烦,我现在都是安装...

声卡正常但是麦克风没有声音

1、检查声卡、连接线,以及音箱等设备是否连接正常;运行杀毒软件进行杀毒;2、右击“我的电脑”----“属性”---“硬件”----“设备管理器”,打开“声音、视频和游戏控制器”有无问题,即看前面有没有...

coreldraw教程自学(coreldraw零基础教学视频)

1、CDR是矢量软件,国内印刷业和部分设计用的较多;2、基础入门操作:A、看你用在具体的哪个细分行业,如印刷排版方面、户外室内喷绘方面、图形设计方面、字体设计等等……输出的要求不一样,其他基本一样...

cad2010序列号和密钥激活码免费

回答如下:不可以,CAD2010序列号和密钥是两个不同的概念。序列号是一个唯一的标识符,用于识别软件产品的副本。而密钥是一个加密字符串,用于验证软件副本的合法性。在安装CAD2010时,需要输入正确的...

笔记本电脑联想好还是惠普好

惠普和联想为全球名列前茅的电脑品牌,其中惠普为美国品牌,联想为中国品牌。联想作为一个后起之秀,当年通过收购thankpad品牌,走向全球。电脑中又分出多种风格的子品牌,有的注重游戏性能,有的注重轻薄商...

手机和电脑怎么连接(苹果手机和电脑怎么连接)
  • 手机和电脑怎么连接(苹果手机和电脑怎么连接)
  • 手机和电脑怎么连接(苹果手机和电脑怎么连接)
  • 手机和电脑怎么连接(苹果手机和电脑怎么连接)
  • 手机和电脑怎么连接(苹果手机和电脑怎么连接)
如何安装路由器步骤(安装路由器操作步骤)

路由器安装设置共分两个步骤,依次是”路由器与光猫“串联,“宽带账号写入路由器”即可,方法如下:?步骤一首先将路由器与光猫串联,用网线一端连接至“光猫”的LAN端口,另一端连接至“路由器”的WAN端口“...

iphone序列号验机官网验机(iphone序列号验机官网验机免费)
  • iphone序列号验机官网验机(iphone序列号验机官网验机免费)
  • iphone序列号验机官网验机(iphone序列号验机官网验机免费)
  • iphone序列号验机官网验机(iphone序列号验机官网验机免费)
  • iphone序列号验机官网验机(iphone序列号验机官网验机免费)

取消回复欢迎 发表评论: