简单的基于小波分析的时间序列降噪方法(Python)
off999 2025-05-28 19:40 7 浏览 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等。
相关推荐
- 还不会安装python?快来看看怎么快速安装
-
下载安装包一、访问Python官网1、在浏览器输入python.org点击Enter键显示这个页面即可PS:因为是国外网站访问会慢一点,等待个10秒左右2、点击Downloads按钮(D...
- 安装python后这几个目录很重要
-
各位网友好,关于拍摄环境之前有视频安装已经做过介绍了,安装完拍摄环境之后初学者经常有在运行过程当中的会遇到的问题,为了快速的去排查gatson环境的问题,了解python安装之后的一些关键目录以及主要...
- 简单的基于小波分析的时间序列降噪方法(Python)
-
importnumpyasnpimportpywtimportmatplotlib.pyplotaspltdefdenoise_signal(signal,wavelet=...
- 每天一个 Python 库:logging 用法精讲,高效简洁的输出日志
-
一、为什么你必须掌握logging?你是否还在用print()调试程序?简单场景OK,但当项目逐渐复杂,print就显得力不从心:无法区分信息等级不带时间,无法定位日志时间点不易写入日...
- [python] 轻量级定时任务调度库schedule使用指北
-
schedule是一款专为简化定时任务调度而设计的Python库,它通过直观的语法降低了周期性任务的实现门槛。作为进程内调度器,它无需额外守护进程,轻量且无外部依赖,适合快速搭建自动化任务。不过,该库...
- 解决 Python 中 schedule 模块安装与使用问题的完整指南
-
schedule是一个轻量级的Python定时任务调度库,适用于简单的周期性任务管理。以下是安装、基本使用、常见问题解决及最佳实践的完整指南。1.安装schedule通过pip安装bas...
- Python 的日历模块,你会用吗
-
Python的日历模块是一个内置库,它提供了广泛的功能来处理与日历相关的任务。无论您是管理计划、处理基于日期的逻辑,还是创建日历可视化,此模块都可以简化复杂的操作。为什么使用calendar模块?c...
- Python中测试代码执行时间的利器
-
简介作为Python开发者,我们都希望代码运行更快一些,今天给大家介绍一个实用的工具模块timeit,它可以帮我们精确测量Python代码的执行时间。赶快来看看吧!timeit模块是py...
- Python定时任务管理指南:让你的代码按时工作!
-
大家好!今天我要和大家分享一个非常实用的Python技能-定时任务管理。想让你的程序在每天固定时间自动执行?或者周期性地完成某些任务?跟着我一起学习,让你的代码成为一位守时的好帮手!一、什么是定时...
- deepseek+wps/excel:时间类公式复杂难度测试
-
如图,题目是对科目间隔作出判断并最确定每个时间是否有效。测试目标:1、理解分人分科目的时间作判定;结果:完全理解。2、使用循环函数,特别是调用累积器的能力;结果:能选出合适的函数组合,但具体参数调用会...
- Python基于Prophet实现时间序列数据趋势周期特征提取项目实战
-
说明:这是一个机器学习实战项目(附带数据+代码+文档+代码讲解),如需数据+代码+文档+代码讲解可以直接到文章最后获取。1.项目背景Prophet是Facebook开源的一个用于时间序列预测的库,它主...
- 一节课的时间快速掌握Python基础知识
-
对于初学者来说,掌握它的基础语法和核心功能是非常重要的。下面是一个循序渐进的学习步骤,帮助你轻松入门:Python是一种简单易学,容易上手,功能也非常强大,所很多人想学会它。个人感觉还是先学下C...
- python教程从基础到精通,第9课—日期与时间
-
Hello,小伙伴们,祝大家五.一玩得快乐!刚学习完了七大数据类型,今天咱们来学习日期与时间的表示方法。Python标准库中提供了时间和日期的支持:calendar:日历相关;time、datetim...
- 碎片时间学Python-09循环
-
循环是指重复完成同一个动作。重复有限次数,则为有限循环,否则为无限循环。Python中的循环结构通常有两种:while循环和for循环。while循环`while`循环在条件为真时重复执行代码块。#...
- Python 实现【响应报文时间】
-
defdecode_max_resp_time(m):ifm<128:returnmelse:mant=m&0x0F...
你 发表评论:
欢迎- 一周热门
- 最近发表
- 标签列表
-
- python计时 (73)
- python安装路径 (56)
- python类型转换 (75)
- python进度条 (54)
- python的for循环 (56)
- python串口编程 (60)
- python写入txt (51)
- python读取文件夹下所有文件 (59)
- java调用python脚本 (56)
- python操作mysql数据库 (66)
- python字典增加键值对 (53)
- python获取列表的长度 (64)
- python接口 (63)
- python调用函数 (57)
- python qt (52)
- python人脸识别 (54)
- python斐波那契数列 (51)
- python多态 (60)
- python命令行参数 (53)
- python匿名函数 (59)
- python打印九九乘法表 (65)
- centos7安装python (53)
- python赋值 (62)
- python异常 (69)
- python元祖 (57)