用python实现滤波器_python实现低通滤波器代码

本文展示了如何使用Python实现低通滤波器,包括理想的低通滤波器和巴特沃斯低通滤波器。通过傅里叶变换、滤波器设计和逆变换,对图像进行滤波处理,去除高频噪声,实现图像模糊效果。
摘要由CSDN通过智能技术生成

低通滤波器实验代码,这是参考别人网上的代码,所以自己也分享一下,共同进步

# -*- coding: utf-8 -*-

import numpy as np

from scipy.signal import butter, lfilter, freqz

import matplotlib.pyplot as plt

def butter_lowpass(cutoff, fs, order=5):

nyq = 0.5 * fs

normal_cutoff = cutoff / nyq

b, a = butter(order, normal_cutoff, btype='low', analog=False)

return b, a

def butter_lowpass_filter(data, cutoff, fs, order=5):

b, a = butter_lowpass(cutoff, fs, order=order)

y = lfilter(b, a, data)

return y # Filter requirements.

order = 6

fs = 30.0 # sample rate, Hz

cutoff = 3.667 # desired cutoff frequency of the filter, Hz # Get the filter coefficients so we can check its frequency response.

b, a = butter_lowpass(cutoff, fs, order) # Plot the frequency response.

w, h = freqz(b, a, worN=800)

plt.subplot(2, 1, 1)

plt.plot(0.5*fs*w/np.pi, np.abs(h), 'b')

plt.plot(cutoff, 0.5*np.sqrt(2), 'ko')

plt.axvline(cutoff, color='k')

plt.xlim(0, 0.5*fs)

plt.title("Lowpass Filter Frequency Response")

plt.xlabel('Frequency [Hz]')

plt.grid() # Demonstrate the use of the filter. # First make some data to be filtered.

T = 5.0 # seconds

n = int(T * fs) # total number of samples

t = np.linspace(0, T, n, endpoint=False) # "Noisy" data. We want to recover the 1.2 Hz signal from this.

data = np.sin(1.2*2*np.pi*t) + 1.5*np.cos(9*2*np.pi*t) + 0.5*np.sin(12.0*2*np.pi*t) # Filter the data, and plot both the original and filtered signals.

y = butter_lowpass_filter(data, cutoff, fs, order)

plt.subplot(2, 1, 2)

plt.plot(t, data, 'b-', label='data')

plt.plot(t, y, 'g-', linewidth=2, label='filtered data')

plt.xlabel('Time [sec]')

plt.grid()

plt.legend()

plt.subplots_adjust(hspace=0.35)

plt.show()

实际代码,没有整理,可以读取txt文本文件,然后进行低通滤波,并将滤波前后的波形和FFT变换都显示出来

# -*- coding: utf-8 -*-

import numpy as np

from scipy.signal import butter, lfilter, freqz

import matplotlib.pyplot as plt

import os

def butter_lowpass(cutoff, fs, order=5):

nyq = 0.5 * fs

normal_cutoff = cutoff / nyq

b, a = butter(order, normal_cutoff, btype='low', analog=False)

return b, a

def butter_lowpass_filter(data, cutoff, fs, order=5):

b, a = butter_lowpass(cutoff, fs, order=order)

y = lfilter(b,

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值