传感数据分析——高通滤波与低通滤波

传感数据分析——高通滤波与低通滤波

文章目录

  • 传感数据分析——高通滤波与低通滤波
  • 前言
  • 一、运行环境
  • 二、Python实现
  • 总结


前言

对于传感信号而言,我们可以提取其中的高频信息和低频信息,低频信息往往是信号的趋势,高频信息往往是一些突变或异常的信号,根据实际需求分离信号中的高低频特征具有实际意义。本文将使用scipy库中的signal模块实现高低通滤波器的设计,并采用计算周期特征,以下直接上代码。


本文正文内容

一、运行环境

系统: Windows 10 / Ubuntu 20.04
编程语言: Python 3.8
文本编译器: Vscode
所需库:matplotlib >= 2.2.2 , numpy >= 1.19.5, scipy >= 1.1.0

二、Python实现

代码如下(示例):

# @copyright all reseved
# @author: Persist_Zhang
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

# 生成一个模拟信号
fs = 1000  # 采样频率
t = np.arange(0, 1, 1/fs)  # 时间向量
f1 = 50  # 低频信号频率
f2 = 120  # 高频信号频率
x = np.sin(2 * np.pi * f1 * t) + np.sin(2 * np.pi * f2 * t)  # 模拟信号

# 设计低通滤波器
nyquist = 0.5 * fs
low = 40 / nyquist
b, a = signal.butter(4, low, btype='low')

# 应用低通滤波器
y_low = signal.filtfilt(b, a, x)

# 设计高通滤波器
high = 60 / nyquist
b, a = signal.butter(4, high, btype='high')

# 应用高通滤波器
y_high = signal.filtfilt(b, a, x)

# 计算周期特征
fft_x = np.fft.fft(x)
fft_y_low = np.fft.fft(y_low)
fft_y_high = np.fft.fft(y_high)

# 绘制结果
plt.figure()
plt.subplot(3, 1, 1)
plt.plot(t, x)
plt.title('Original Signal')
plt.subplot(3, 1, 2)
plt.plot(t, y_low)
plt.title('Low-Frequency Signal')
plt.subplot(3, 1, 3)
plt.plot(t, y_high)
plt.title('High-Frequency Signal')
plt.savefig('./figure/filtered_signals.png')
plt.show()
# 绘制频谱
plt.figure()
plt.subplot(3, 1, 1)
plt.plot(np.abs(fft_x))
plt.title('The spectrum of the original signal')
plt.subplot(3, 1, 2)
plt.plot(np.abs(fft_y_low))
plt.title('The spectrum of the signal after low-pass filtering')
plt.subplot(3, 1, 3)
plt.plot(np.abs(fft_y_high))
plt.title('The spectrum of the signal after high-pass filtering')
plt.savefig('./figure/filtered_signals_spectrum.png')
plt.show()


结果图

传感数据分析——高通滤波与低通滤波_第1张图片
上图为信号的高低频频谱,下图为原始信号与高频信号(设定大于120Hz)和低频信号(设定为小于50Hz)图。
传感数据分析——高通滤波与低通滤波_第2张图片


总结

以上就是本文关于传感信号分析中高低频滤波器设计的内容,全部代码见上,还望多多收藏点赞,后续将会更新与分享更多传感数据处理的代码。

你可能感兴趣的:(传感数据,Python,数据分析,数据分析,数据挖掘)