2013-06-25 54 views
-1
function [h,w,y,graph] = lowpassFIR(sample) 

%Calculates Finite Impulse Response low pass filter coefficient 
%using the windowing methods as well 

passEdge = 100; 
StopbandAtt = 20; 
passbandRip =.05; 
transWidth = 10; 
Fs = sample; 





%Step One: select number of coefficients% 
deltaF = transWidth/Fs; 

%Normalize for each window 


rectN = round(0.9/deltaF); 

hannN = round(3.1/deltaF); 
hammN = round(3.3/deltaF); 
blackN = round(5.5/deltaF); 

rectN = 1:rectN 

%rectPos = round(rectN/2); 
%rectNeg = round((rectPos*-1)); 


%For the Vector Array 
%rect = rectNeg:rectPos; 

deltaSum= passEdge + (transWidth/2); 
deltaF2= deltaSum/Fs; 

h=zeros(size(1:rectN(end))); 
w=zeros(size(1:rectN(end))); 
y=zeros(size(1:rectN(end))); 
graph = plot(y) 
for i = 1:rectN(end) 

    %iterate through each value and plug into function in for loop 
    %each output of the function will be stored into another array 
    h(i) = 2*deltaF2*(sin(i*2*pi*deltaF2))/(2*i*pi*deltaF2); 
    w(i) = 0.5 + 0.5*cos(2*pi*i/rectN(end)); 
    y(i) = h(i)*w(i); 
    graph(i) = y(i); 
end 

從代碼中可以看出,我試圖從圖表中得到圖表結果....但是當它輸出時,我在命令窗口中獲取了這些值,但是該圖顯示了一條直線@零。如何在這裏自動縮放y軸?我應該如何自動設置這個Matlab圖?

回答

0

我不認爲汽車的規模是這裏的問題,而是事件的順序。

你嘗試「初始化」在這一行你的情節:

graph = plot(y); 

然而,前行,你定義

y=zeros(size(1:rectN(end))); 

因此,你看到的情節將是一條直線在y=0。 的值是handle to the line object,在這種情況下,您的行爲y=0
試圖建立的情節後,就進入一個循環,並在每次迭代中,你將值添加到您的線路手柄(graph(i) = y(i)) - 但你不積任何東西。如果在循環之後,你看y或的值,你會看到你的新值 - 但它們從不被繪製。
我懷疑你需要在這種情況下一個循環,也許嘗試這個:

I = 1:rectN(end); 
h = 2.*deltaF2.*(sin(I.*2.*pi.*deltaF2))./(2.*I.*pi.*deltaF2); 
w = 0.5 + 0.5.*cos(2.*pi.*I./rectN(end)); 
y = h.*w; 
graph = plot(y); 

需要注意的是重要的element wise multiplication.*

最後一個評論:你不應該使用i(或j爲),因爲它們也被用作Matlab中的imaginary unit,如果你需要在你的代碼中某處,它可能會產生問題。

+0

如果我刪除了循環......它只輸出一個值,因爲它不會重複... – user2514874

+0

@ user2514874你如何消除環路?你使用我答案中的代碼嗎? – Schorsch

相關問題