我应该如何自动测试这个Matlab图?

时间:2013-06-25 02:56:04

标签: matlab graph plot scale

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轴?

1 个答案:

答案 0 :(得分:0)

我不认为自动缩放是这里的问题,而是事件的顺序。

您尝试在此行中“初始化”您的情节:

graph = plot(y);

但是,在上一行中,您定义了

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

因此,您看到的情节将是y=0处的直线。 graph的值为handle to the line object,在这种情况下为y=0行 在尝试设置绘图后,您输入一个循环,并在每次迭代时,向行句柄(graph(i) = y(i))添加一个值 - 但是您不绘制任何内容。如果在循环之后,您查看ygraph的值,则会看到新值 - 但它们从未被绘制过。
我怀疑你在这种情况下需要一个循环,也许试试这个:

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,它可能会产生问题如果你需要代码中的某个地方。