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

时间:2015-01-16 23:23:53

标签: matlab signals processing signal-processing digital

我有这个代码,它将三次谐波添加到基本信号,然后使用滤波器收回基波。我必须修改这段代码才能添加第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); 

1 个答案:

答案 0 :(得分:0)

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

%% 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

最后,如果你想把它作为一个过滤器,你需要一个更复杂的过滤器。您可以根据极点和零点设计自己的(这可能是预期的,如果这是一个类项目,它听起来像是这样)。或者,您可以使用允许您输入任意响应的过滤器设计命令之一。如果要使用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