2015-01-16 73 views
0

我有這個代碼,它將三次諧波添加到基本信號,然後使用一個濾波器回收基波。我必須修改此代碼以添加第3次,第5次和第7次諧波,然後將其濾除,但我不知道如何使濾波器達到此目的。如何使用MATLAB濾除諧波(DSP)?

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); 

回答

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 
+0

我真的很感謝你的努力,你解決了我的問題的最好辦法。最好的祝福 –