Matlab重新创建freqz,对x轴进行归一化并获得一半的绘图

时间:2015-04-24 21:15:51

标签: matlab plot matlab-figure

我有一个基本上在matlab中重新创建freqz命令的函数。我已经想出如何绘制我的频率响应的整个变换,但我只需要它的一半,我需要将它从pi标准化为1(其中0:pi代表我的x轴,我希望它去0:1)。代码在这里:

function freqrespplot(b, a)

hb = fft(b,512);
ha = fft(a,512);

magh = (abs(hb) ./ abs(ha));
angh = angle(hb ./ ha);

x = linspace(0,2*pi, 512);

subplot(2,1,1);
plot(x,magh,'g');
subplot(2,1,2);
plot(x,angh,'r');

end

在b = [1 2 2]的例子中,a = [0 1 .8],如下图:

freqrespplot([1 2 2],[0 1.8]); 大小 Correct total Magnitude, want 0 to pi normalized 0 to 1

和我相应的相图是 Want phase 0 to pi and normalized 0 to 1

我希望x轴(omega)在两种情况下从0变为1,并且只取一半图形对应于0到pi,如果可能的话,这样就是:

Desired Output

1 个答案:

答案 0 :(得分:1)

您只需要进行一些小的更改,在下面的代码中标有注释:

function freqrespplot(b, a)

hb = fft(b,512);
ha = fft(a,512);

magh = (abs(hb) ./ abs(ha));
angh = angle(hb ./ ha);

x = linspace(0,2, 513); %// 2, not 2*pi. And 513. Last value will be discarded
x = x(1:end-1); %// discard last value to avoid having 0 and 2*pi (they are the same)

subplot(2,1,1);
plot(x(1:end/2),magh(1:end/2),'g'); %// plot only half
subplot(2,1,2);
plot(x(1:end/2),angh(1:end/2),'r'); %// plot only half

end

使用您的示例ba生成

enter image description here

您可能希望包含一个额外的样本以到达图表的右边缘。我只评论与上述代码的差异:

function freqrespplot(b, a)

hb = fft(b,512);
ha = fft(a,512);

magh = (abs(hb) ./ abs(ha));
angh = angle(hb ./ ha);

x = linspace(0,2, 513);
x = x(1:end-1);

subplot(2,1,1);
plot(x(1:end/2+1),magh(1:end/2+1),'g'); %// plot only lower half plus one value
subplot(2,1,2);
plot(x(1:end/2+1),angh(1:end/2+1),'r'); %// plot only lower half plus one value

end

比较结果图(最右边的部分):

enter image description here