Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python高通滤波器_Python_Scipy_Signal Processing - Fatal编程技术网

Python高通滤波器

Python高通滤波器,python,scipy,signal-processing,Python,Scipy,Signal Processing,我使用以下代码在python中实现了高通过滤器: from scipy.signal import butter, filtfilt import numpy as np def butter_highpass(cutoff, fs, order=5): nyq = 0.5 * fs normal_cutoff = cutoff / nyq b, a = butter(order, normal_cutoff, btype='high', analog=False)

我使用以下代码在python中实现了高通过滤器:

from scipy.signal import butter, filtfilt
import numpy as np

def butter_highpass(cutoff, fs, order=5):
    nyq = 0.5 * fs
    normal_cutoff = cutoff / nyq
    b, a = butter(order, normal_cutoff, btype='high', analog=False)
    return b, a

def butter_highpass_filter(data, cutoff, fs, order=5):
    b, a = butter_highpass(cutoff, fs, order=order)
    y = filtfilt(b, a, data)
    return y

rawdata = np.loadtxt('sampleSignal.txt', skiprows=0)
signal = rawdata
fs = 100000.0

cutoff = 100
order = 6
conditioned_signal = butter_highpass_filter(signal, cutoff, fs, order)
我将此滤波器应用于100 kHz的电压信号,它在截止频率>=60 Hz时工作良好。但它在下面不起作用。我想切断所有低于10赫兹的频率。有没有提示我的错误在哪里?我观察到的是,滤波器的阶数越低,截止频率就越低


我希望这能帮助您:

import numpy as np
import pandas as pd
from scipy import signal
import matplotlib.pyplot as plt
def sine_generator(fs, sinefreq, duration):
    T = duration
    nsamples = fs * T
    w = 2. * np.pi * sinefreq
    t_sine = np.linspace(0, T, nsamples, endpoint=False)
    y_sine = np.sin(w * t_sine)
    result = pd.DataFrame({ 
        'data' : y_sine} ,index=t_sine)
    return result

def butter_highpass(cutoff, fs, order=5):
    nyq = 0.5 * fs
    normal_cutoff = cutoff / nyq
    b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
    return b, a

def butter_highpass_filter(data, cutoff, fs, order=5):
    b, a = butter_highpass(cutoff, fs, order=order)
    y = signal.filtfilt(b, a, data)
    return y

fps = 30
sine_fq = 10 #Hz
duration = 10 #seconds
sine_5Hz = sine_generator(fps,sine_fq,duration)
sine_fq = 1 #Hz
duration = 10 #seconds
sine_1Hz = sine_generator(fps,sine_fq,duration)

sine = sine_5Hz + sine_1Hz

filtered_sine = butter_highpass_filter(sine.data,10,fps)

plt.figure(figsize=(20,10))
plt.subplot(211)
plt.plot(range(len(sine)),sine)
plt.title('generated signal')
plt.subplot(212)
plt.plot(range(len(filtered_sine)),filtered_sine)
plt.title('filtered signal')
plt.show()

由于我的声誉很低,我无法对你的问题发表评论——“截止和过滤顺序之间的关系是什么?”这不是对你最初问题的回答

对于FIR滤波器,对于给定截止频率,对于高阶滤波器,脉冲响应图的斜率(| H(f)| vs f)更陡。因此,要在不需要的频率范围内实现更高的衰减,请增加滤波器阶数。但是,当滤波器阶数很高时,脉冲响应是一个理想的盒函数,会发生什么呢?您将看到类似符号间干扰(数字通信中的ISI)的效果。当截止频率与采样频率之比变小时,这种效应的强度增加(想想频域中盒函数的宽度与sinc函数主瓣的宽度之间的关系)

当我试图在TI DSP微控制器上实现一个非常窄带的低通IIR滤波器时,我第一次观察到了这一点。TI库将滤波器实现为级联双四元结构,以处理众所周知的截断效应。这仍然没有解决问题,因为问题不仅仅是因为截断。我解决这个问题的方法是使用抗混叠滤波器,然后对输入信号进行下采样,然后使用我想要的低通IIR滤波器


据我所知,您正在实现一个HPF,它是在频域中转换的LPF。希望这能回答你的一些问题。让我知道下采样是否适用于您。

我添加了验证上述代码的函数,以便您可以查看顺序和截止频率之间的关系。代码主要来自


你在哪里设置你的通带?你有可以粘贴的配方吗?以及您的
filt()
函数。还有你的
butter()
function@Daniel李:我编辑了我的问题。butter()和filtfilt()函数来自scipy.signal。您能举例说明如何使用它、观察到的行为是什么样的以及预期的行为是什么吗?寻求调试帮助的问题需要清楚的问题陈述。这里可以正常工作,那里不可以正常工作还不够清楚。@moooeeep:我添加了一个样本信号和一个如何应用过滤器的示例。谢谢你的帮助!现在我有点困惑。切断和秩序之间有什么关系?没有关系。“截止”是指截止频率。“顺序”决定了过滤器的精度。这就是我的想法。但是如果你把订单设置为“30”。结果变化很大。对于高阶,低截止频率不起作用。它取决于许多因素。您可以使用Matlab滤波器设计工具包设计并尝试您想要的。因为我认为LMS滤波器是去除极低频的最好方法。你知道如何用mp3文件代替正弦波吗。我想删除mp3和wav文件中低于某个阈值的所有频率
import scipy
import sys  
from scipy import signal
from scipy import pi
from scipy.io.wavfile import write
import matplotlib.pyplot as plt
import numpy as np    
from scipy.signal import butter, lfilter, freqz   


# ----- ----- ----- -----    
def butter_highpass(cutoff, fs, order=5):
    nyq = 0.5 * fs
    normal_cutoff = cutoff / nyq
    b, a = signal.butter(order, normal_cutoff, btype='high', analog=False)
    return b, a

def butter_highpass_filter(data, cutoff, fs, order=5):
    b, a = butter_highpass(cutoff, fs, order=order)
    y = signal.filtfilt(b, a, data)
    return y


# ----- -----    
# (1)
def foo(sel):
    if (sel == 1):
        # Filter requirements.
        order = 6
        fs = 300.0  # sample rate, Hz
        cutoff = 10  # desired cutoff frequency of the filter, Hz

        # Get the filter coefficients so we can check its frequency response.
        b, a = butter_highpass(cutoff, fs, order)

        # Plot the frequency response.
        w, h = freqz(b, a, worN=8000)
        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("High Filter Frequency Response")
        plt.xlabel('Frequency [Hz]')
        plt.grid()

        # Demonstrate the use of the filter.
        # First make some data to be filtered.
        T = 0.5  # seconds
        n = int(T * fs)  # total number of samples
        t = np.linspace(0, T, n, endpoint=False)
        # "Noisy" data.  We want to recover the 20 Hz signal from this.
        data = np.sin(1.2 * 2 * np.pi * t) + 1.5 * np.cos(5 * 2 * np.pi * t) + 0.5 * np.sin(20.0 * 2 * np.pi * t)

        # Filter the data, and plot both the original and filtered signals.
        y = butter_highpass_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()
    else:
        print ('Please, choose among choices, thanks.')


# ----- -----
def main():
    sel = int (sys.argv[1])
    foo(sel)


# ----- ----- ----- ----- ----- -----
if __name__ == '__main__':
    main()