Python:通过加窗的高通 FIR 滤波器

标签 python filter windowing

我想通过 Python 中的窗口创建一个基本的高通 FIR 滤波器。

我的代码在下面,并且是故意惯用的 - 我知道你(很可能)可以用Python中的一行代码来完成这个,但我正在学习。我使用了带有矩形窗口的基本 a sinc 函数:我的输出适用于加法 (f1+f2) 但不适用于乘法 (f1*f2) 的信号,其中 f1=25kHz 且 f2=1MHz。

我的问题是:我是否误解了一些基本的东西或者我的代码是否错误? 总之,我想只提取高通信号(f2=1MHz)并过滤掉其他所有信号。我还提供了 (f1+f2) 和 (f1*f2) 生成内容的屏幕截图:

enter image description here

import numpy as np
import matplotlib.pyplot as plt

# create an array of 1024 points sampled at 40MHz
# [each sample is 25ns apart]
Fs = 40e6
T  = 1/Fs
t  = np.arange(0,(1024*T),T)

# create an ip signal sampled at Fs, using two frequencies 
F_low  = 25e3 #  25kHz
F_high = 1e6  #  1MHz
ip = np.sin(2*np.pi*F_low*t) + np.sin(2*np.pi*F_high*t)
#ip = np.sin(2*np.pi*F_low*t) * np.sin(2*np.pi*F_high*t)
op = [0]*len(ip)


# Define -
# Fsample = 40MHz
# Fcutoff = 900kHz,
# this gives the normalised transition freq, Ft
Fc = 0.9e6
Ft = Fc/Fs
Length       = 101
M            = Length - 1
Weight       = []
for n in range(0, Length):
    if( n != (M/2) ):
        Weight.append( -np.sin(2*np.pi*Ft*(n-(M/2))) / (np.pi*(n-(M/2))) )
    else:
        Weight.append( 1-2*Ft )




for n in range(len(Weight), len(ip)):
    y = 0
    for i in range(0, len(Weight)):
        y += Weight[i]*ip[n-i]
    op[n] = y


plt.subplot(311)
plt.plot(Weight,'ro', linewidth=3)
plt.xlabel( 'weight number' )
plt.ylabel( 'weight value' )
plt.grid()

plt.subplot(312)
plt.plot( ip,'r-', linewidth=2)
plt.xlabel( 'sample length' )
plt.ylabel( 'ip value' )
plt.grid()

plt.subplot(313)
plt.plot( op,'k-', linewidth=2)
plt.xlabel( 'sample length' )
plt.ylabel( 'op value' )
plt.grid()
plt.show()

最佳答案

你误解了一些基本的东西。加窗 sinc 滤波器旨在分离线性组合频率;即通过加法组合的频率,而不是通过乘法组合的频率。请参阅chapter 5科学家和工程师指南 数字信号处理了解更多详细信息。

基于 scipy.signal 的代码将提供与您的代码类似的结果:

from pylab import *
import scipy.signal as signal

# create an array of 1024 points sampled at 40MHz
# [each sample is 25ns apart]
Fs = 40e6
nyq = Fs / 2
T  = 1/Fs
t  = np.arange(0,(1024*T),T)

# create an ip signal sampled at Fs, using two frequencies 
F_low  = 25e3 #  25kHz
F_high = 1e6  #  1MHz
ip_1 = np.sin(2*np.pi*F_low*t) + np.sin(2*np.pi*F_high*t)
ip_2 = np.sin(2*np.pi*F_low*t) * np.sin(2*np.pi*F_high*t)

Fc = 0.9e6
Length = 101

# create a low pass digital filter
a = signal.firwin(Length, cutoff = F_high / nyq, window="hann")

# create a high pass filter via signal inversion
a = -a
a[Length/2] = a[Length/2] + 1

figure()
plot(a, 'ro')

# apply the high pass filter to the two input signals
op_1 = signal.lfilter(a, 1, ip_1)
op_2 = signal.lfilter(a, 1, ip_2)

figure()
plot(ip_1)
figure()
plot(op_1)
figure()
plot(ip_2)
figure()
plot(op_2)

脉冲响应:

Impulse Response

线性组合输入:

Linearly Combined Input

过滤后的输出:

Linearly Combined Output

非线性组合输入:

Non-linearly Combined Input

过滤后的输出:

Non-linearly Combined Output

关于Python:通过加窗的高通 FIR 滤波器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23341566/

相关文章:

Python 脚本打开 bash 提示终止脚本

适用于 Maya 的 Python : Why can't I use a variable in concatination with a wildcard?

python - 如何让python import在编译后起作用?

html - 从哪里开始过滤用户的输入以给出结果

javascript - D3 - 如何使用不透明度过滤正确的值?

javascript - 如何从另一个多维数组中的对象属性创建一个包含所有首字母的数组?

sql-server - TSQL 用窗口和 CTE 替换 "Quirky Update"计算

python - 找到数组低于特定阈值的第一个索引(并保持低于一段时间)

python - 在 View 中初始化表单时是否可以指定特定的表单字段?

c# - 列出/详细说明不同的窗口是否可以同步并数据绑定(bind)到同一个集合?