[英]How to design a zero-phase bandpass filter for 1 to 20 Hz in matlab?
I am using Matlab to filter ECG data. 我正在使用Matlab筛选ECG数据。 There is noise in the data and I have tried using a Butterfilter.
数据中有噪音,我尝试使用Butterfilter。 The problem is that there a higher power butter filter, anything above 3 makes the data disappear.
问题在于,有一个更高功率的黄油过滤器,大于3的任何值都会使数据消失。
I have looked through Matlab's Help guide on filter design but I am unclear and still confused on how to actually use it and implement it. 我浏览了Matlab关于滤波器设计的帮助指南,但不清楚,仍然对如何实际使用和实现它感到困惑。
Goal: Filter ECG data with zero phase distortion, filter all outside 1 to 20 Hz 目标:过滤零相位失真的ECG数据,过滤1至20 Hz以外的所有信号
My code right now: 我现在的代码:
%takes in one channel of data from the dataMatrix, plots it, filters, and plots again in a new figure
channel = 150;
x = dataMatrix(:,channel);
plot(x)
rawTitle = sprintf('Raw Channel %d ', channel);
title(rawTitle)
figure
[b_pass a_pass] = butter(3, [1/1000 20/1000], 'bandpass');
plot(filtfilt(b_pass, a_pass, x))
channelTitle=sprintf('Filtered Channel %d ', channel);
title(channelTitle)
Let's start with the basics of designing the Butterworth-filter in Matlab. 让我们从在Matlab中设计Butterworth滤波器的基础开始。 Here, we can only define a filter using normalized frequencies.
在这里,我们只能使用归一化频率定义滤波器。 Therefore we need to know the sampling frequency
fs
to determine the normalized frequency fn
like this: fn = f/fs
. 因此,我们需要知道采样频率
fs
以便像这样确定归一化频率fn
: fn = f/fs
。 Then we need the order order
and the two cutoff-frequencies fc1
and fc2
. 然后,我们需要定单
order
和两个截止频率fc1
和fc2
。 The second argument of butter
takes 'frequencies' from 0 to 1 where 1 corresponds to the Nyquist-rate which is the half of the sampling frequency fs
. butter
的第二个参数将“频率”从0设为1,其中1对应于奈奎斯特速率,即采样频率fs
的一半。 Therefore we have to divide fs
by 2 in the second argument. 因此,我们必须在第二个参数中将
fs
除以2。
[b,a] = butter(order, [fc1,fc2]/(fs/2), 'bandpass');
Now we can go into your code and apply the filter. 现在,我们可以进入您的代码并应用过滤器。 Note that
filtfilt
is zero-phase and doubles the order of the original filter. 请注意,
filtfilt
是零相位的,并且使原始滤波器的阶数加倍。 We can take some sample data from here ( fs
assumed to be 500Hz) and see if it works as expected. 我们可以从此处获取一些样本数据(假设
fs
为500Hz),看看它是否按预期工作。
% load sample data and assign it to x
load('ecg.mat');
fs = 500;
x = ecg;
% define to use OP's code
channel = 150;
% plot the raw data
figure
plot(x)
rawTitle = sprintf('Raw Channel %d ', channel);
title(rawTitle)
% design filter
[b_pass,a_pass] = butter(3,[1,20]/(fs/2), 'bandpass');
% filter data and plot it
figure
plot(filtfilt(b_pass, a_pass, x))
channelTitle=sprintf('Filtered Channel %d ', channel);
title(channelTitle)
This is the result: 结果如下:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.