如何使用 MATLAB 滤除谐波(DSP)?

How to filter out harmonics (DSP) using MATLAB?

我有这段代码,它将三次谐波添加到基波信号中,然后使用滤波器取回基波。我必须修改此代码以添加 3 次、5 次和 7 次谐波,然后将它们过滤掉,但我不知道如何让过滤器做到这一点。

t = [0:199];
A1 = 100;
s1 = A1*sin(2*pi*(1/40)*t); % fundamental
A3 = A1/3;
s3 = A3*sin(2*pi*(1/40)*3*t); %3rd harmonic
s13 = s1 + s3; 
% A5 = A1/5;  
% s5 = A5*sin(2*pi*(1/40)*5*t); 5th
% A7 = A1/7;
% s7 = A7*sin(2*pi*(1/40)*7*t); 7th

% filter
[b13, a13] = ellip(6,0.5,20,[5.7/40 6.3/40],'stop') %elliptic filter
h13 = impz(b13,a13,length(s13)); %impulse
y13 = filter(b13,a13,s13); 

消除这些谐波的最简单方法是简单地使用低通滤波器...这将消除截止频率以上的所有频率内容。这不再是一个陷波滤波器,就像你展示的那样,但它肯定会消除那些谐波:

%% lowpass IIR filter example
fs_Hz = 1;  %your sample rate appears to be 1 Hz
fund_Hz = 1/40;  %this is your fundamental
cutoff_Hz = 1.5*fund_Hz;  %choose cutoff
[b,a] = butter(3,fund_Hz/(fs_Hz/2));  %lowpass by default
y = filter(b,a,s13);  %apply filter

如果低通滤波器过滤太多,那么听起来你的问题是你不知道如何做多陷波滤波器。没关系。您可以选择通过一个接一个地应用一系列陷波滤波器来陷波我们的谐波...

%% apply IIR notch filters in series
fs_Hz = 1;  %your sample rate
fund_Hz =1/40;  %your fundamental frequency
y = x;  %initialize your output
for Iharm = 3:2:7  %will do 3rd, 5th, 7th
    [b, a] = ellip(6,0.5,20,[(Iharm-0.3) (Iharm+0.3)]*fund_Hz/(fs_hz/2),'stop');
    y = filter(b,a,y);  %apply the filter onto the previous output
end

最后,如果您想将所有内容作为一个过滤器来完成,您将需要一个复杂得多的过滤器。您可以根据极点和零点设计您自己的(这可能是预期的,如果这是一个 class 项目,听起来就是这样)。或者,您可以使用允许您输入任意响应的滤波器设计命令之一。如果您想使用 IIR 滤波器(而不是 FIR 滤波器),请查找 yulewalk 命令。你会像这样使用它(我有一段时间没有使用它,所以这可能不正确...

%% yulewalk example
f_Hz = 1;  %your sample rate
f_fund_Hz = 1/40;  %your fundamental

%define desired response at different frequencies
w = 0.3;  %width of each notch
f_Hz = [3-w 3 3+w  5-w 5 5+w  7-w 7 7+w]*f_fund_Hz; %3rd, 5th, 7th harmonics
resp = [ 1  0  1    1  0  1    1  0  1 ];  %notch each harmonic
f_Hz = [0; f_Hz(:); fs/2];  %must start at 0 Hz and end at Nyquist
resp = [1; resp(:); 1];  %add start and end points

%create and apply the filter
N = 3*3; %filter order...play with this...N=3 per notch?
[b,a]=yulewalk(N,f_Hz/(fs_Hz/2),resp);  %create filter
y = filter(b,a,x);  %apply filter