📄 dfirplt.m
字号:
function dfirplt
% DFIRPLT
% Support file for DFIRGUI
% obtain necessary handles
f = gcf;
ui = get(f,'userdata');
filter_uis = ui(3:4);
method_uis = ui(5:6);
window_uis = ui(7:8);
edge_edt = ui(11);
attn_edt = ui(13);
samp_uis = ui(19:20);
length_uis = ui(31:32);
cutoff_uis = ui(34:35);
harris_uis = ui(37:38);
beta_uis = ui(40:41);
ripple_uis = ui(43:44);
ap_des = ui(50);
as_des = ui(52);
psl_des = ui(95);
resp_axs = ui(73);
resp_line = ui(74);
resp_line2 = ui(75);
fixed_line = ui(76);
phase_axs = ui(22);
phase_line = ui(23);
lin_btn = ui(33);
pz_axes = ui(85);
uc_line = ui(88);
z_line = ui(86);
p_line = ui(87);
sig_axs = ui(90);
sig_line = ui([91,92]);
%%%%%
if get(ui(18),'userdata')==1
set(f,'currentaxes',ui(73));
zoom out,zoom reset
set(f,'currentaxes',ui(22));
zoom out,zoom reset
set(f,'currentaxes',ui(85));
zoom out,zoom reset
set(f,'currentaxes',ui(90));
zoom out,zoom reset
end
%%%%%%
% get the values
filter_type = get(filter_uis(2),'value');
method_type = get(method_uis(2),'value');
window_type = get(window_uis(2),'value');
length_n = str2num(get(length_uis(2),'string'));
attn = eval(['[',get(attn_edt,'string'),']'],'[]');
if length(attn)~=2
errordlg('Need 2 values for attenuation.','Input Error');
return;
end
if diff(attn)<=0
errordlg('Attenuation values must be in increasing order.','Input Error');
return;
end
attnp=attn(1);attns=attn(2);
edge = eval(['[',get(edge_edt,'string'),']'],'[]');
if any(filter_type == [1:2]),
if length(edge) ~= 2,
errordlg('LP & HP filters need 2 band edges.','Input Error');
return;
end
else
if length(edge) ~= 4,
errordlg('BP & BS filters need 4 band edges.','Input Error');
return;
end
end
if filter_type == 1
edgep=edge(1);edges=edge(2);ty1='lp';
elseif filter_type == 2
edgep=edge(2);edges=edge(1);ty1='hp';
elseif filter_type == 3
edgep=edge(2:3);edges=[edge(1) edge(4)];ty1='bp';
elseif filter_type == 4
edges=edge(2:3);edgep=[edge(1) edge(4)];ty1='bs';
end
cutoff = str2num(get(cutoff_uis(2),'string'));
harris = str2num(get(harris_uis(2),'string'));
beta = str2num(get(beta_uis(2),'string'));
ripple = str2num(get(ripple_uis(2),'string'));
sampf = str2num(get(samp_uis(2),'string'));
if method_type == 1, % optimal
if isempty(sampf)
errordlg('Sampling frequency not specified.','Input Error');
return;
elseif sampf<2*max([edgep edges])
errordlg('Sampling frequency not high enough.','Input Error');
return;
end
if isempty(length_n)
errordlg('Length not specified','Input Error');
return;
end
elseif method_type == 2, % window based
if isempty(sampf)
errordlg('Sampling frequency not specified.','Input Error');
return;
elseif sampf<2*max([edgep edges])
errordlg('Sampling frequency not high enough.','Input Error');
return;
end
if isempty(length_n)
errordlg('Length not specified','Input Error');
return;
end
if isempty(cutoff)
errordlg('Cutoff not specified.','Input Error');
return;
elseif cutoff>=0.5
errordlg('Need Fc < 0.5','Input Error');
return;
end
if window_type == 4 & isempty(beta)
errordlg('Beta not specified.','Input Error');
return;
end
if window_type == 5 & isempty(ripple)
errordlg('Ripple not specified.','Input Error');
return;
end
if window_type == 6 & isempty(harris)
errordlg('Harris type (0 to 7) not specified.','Input Error');
return;
end
elseif method_type == 3, % window based
if isempty(length_n)
errordlg('Length not specified.','Input Error');
return;
end
if window_type == 4 & isempty(beta)
errordlg('Beta not specified.','Input Error');
return;
end
if window_type == 5 & isempty(ripple)
errordlg('Ripple not specified.','Input Error');
return;
end
if window_type == 6
if isempty(harris)
errordlg('Harris type (0 to 7) not specified.','Input Error');
return;
elseif harris~=round(harris)
errordlg('Harris type (0 to 7) must be an integer.','Input Error');
return;
end
end
end
% make sure N is odd for some cases
if (window_type == 5 & method_type>1)| filter_type == 4 | method_type == 3,
if ~rem(length_n,2),
errordlg('N must be odd.','Input Error');
return;
end
end
% if all checks out, do the plot
if method_type == 1, % optimal
hwn = dfiropt(filter_type,length_n,[attnp,attns],sampf,edgep,edges);
samp_f=sampf;
else % window and halfband
if any(window_type == [(1:3)]),
p1 = [];
elseif window_type == 4, % kaiser
p1 = beta;
elseif window_type == 5 % chebyshev
p1 = ripple;
else % Harris window
p1 = harris;
end
if method_type == 2, % window based
hwn = dfirwin(filter_type,length_n,cutoff,sampf,edgep,edges,window_type,p1);
samp_f=sampf;
else
[hwn,samp_f] = dfirhalf(filter_type,length_n,edgep,edges,window_type,p1);
set(ui(79),'string',num2str(samp_f,5));
end
end
%Strip leading/trailing zeros
hwns=hwn;
lhn=length(hwn);
if abs(hwn(1))<10*eps
hwn=hwn(2:lhn-1);
lhn=lhn-2;
end
lwn=lhn-1;
fac=max([4,fix(400/lwn)]);
df = 0.5/lwn/fac;
f = 0:df:0.5; % frequency array
j = sqrt(-1);
z = exp(j*2*pi*f); % to find H(z)
HD = polyval(hwn,z); % spectrum
H=abs(HD);
Hmax=max(H);
hwn=hwn/Hmax; % Normalize impulse response
hwns=hwns/Hmax;
set(ui(14),'userdata',hwns);
Hdb = 20*log10(H/Hmax); % Normalized spectrum to 0dB
f1 = [edgep edges]/samp_f;
z = exp(j*2*pi*f1);
H1 = polyval(hwn,z); % Already normalized above
H1dB = 20*log10(abs(H1));
if get(lin_btn,'value')==1
aslinear=10^(-attns/20);
set(resp_line2,'xdata',f1*samp_f,'ydata',abs(H1)/Hmax);
set(resp_axs,'xlim',[0 0.5*samp_f],'ylim',[0 1]);
set(resp_line,'xdata',f*samp_f,'ydata',H/Hmax);
set(fixed_line,'xdata',[0 0.5*samp_f],'ydata',[aslinear,aslinear]);
set(ui(15),'userdata',[f(:)*samp_f H(:)/Hmax]);
else
%%%% Avoid warning divide by zero
Hdb=H/Hmax;
i=find(Hdb==0);
Hdb(i)=10*eps*ones(size(i));
Hdb=20*log10(Hdb);
set(resp_line2,'xdata',f1*samp_f,'ydata',H1dB);
set(fixed_line,'xdata',[0 0.5*samp_f],'ydata',-[attns attns]);
set(resp_axs,'xlim',[0 0.5*samp_f],'ylim',[-2*attns max(Hdb)]);
set(resp_line,'xdata',f*samp_f,'ydata',Hdb);
set(ui(15),'userdata',[f(:)*samp_f Hdb(:)]);
end
%phase
th=180*angle(HD)/pi;th=th.*(abs(th)>1e-6);Pmin=min(th);Pmax=max(th);
set(phase_axs,'xlim',[0 0.5*samp_f],'ylim',[Pmin Pmax]);
set(phase_line,'xdata',f*samp_f,'ydata',th);
set(ui(16),'userdata',th(:));
if length(H1) == 2,
set(ap_des,'string',num2str(-H1dB(1),3));
set(as_des,'string',num2str(-H1dB(2),4));
else
set(ap_des,'string',[num2str(-H1dB(1),3),', ',num2str(-H1dB(2),3)]);
set(as_des,'string',[num2str(-H1dB(3),4),', ',num2str(-H1dB(4),4)]);
end
%Pole-zero plot
fr=0:1/360:1;
phi=fr*2*pi;
cir=exp(sqrt(-1)*phi);
set(uc_line,'xdata',real(cir),'ydata',imag(cir))
zz=roots(hwn);
rmin=min([real(zz);-1]);rmax=max([real(zz);1]);
imin=min([imag(zz);-1]);imax=max([imag(zz);1]);
%set(pz_axes,'xlim',[rmin, rmax],'ylim',[imin, imax]);
maxx=max(abs([rmax,imax,rmin,imin]));
set(pz_axes,'xlim',[-maxx, maxx],'ylim',[-maxx, maxx]);
set(p_line,'xdata',0,'ydata',0);
set(z_line,'xdata',real(zz),'ydata',imag(zz));
dtstem(sig_line,0:lwn,hwn);
set(sig_axs,'xlim',[0,lwn],'ylim',1.1*[min(hwn),max(hwn)]);
ptt=get(sig_axs,'title');
tf = abs(HD);
HdB = 20*log10(tf+(tf==0))-5000*(tf==0);
HdB=HdB-20*log10(max(tf));
cutoff=edgep/samp_f;
if length(cutoff)==1,
fc=cutoff;
%if filter_type == 2,fc=0.5-fc;end
else
f0=0.5*sum(cutoff);fc=max(cutoff)-f0;
end
AP=[];AS=[];TW=[];
mx=max(tf)-1;
if mx>0,AP=-20*log10((1-mx)/(1+mx));end
if filter_type==1
i=find(f>=fc);H=HdB(i);d=diff(H);
i=find(d>=0);
if ~isempty(i)
iw=i(1);
H2=H(iw:length(H));
AS=-max(H2);
end
elseif filter_type==2
i=find(f<=fc);H=HdB(i);H=fliplr(H);d=diff(H);
i=find(d>=0);
if ~isempty(i)
iw=i(1);
H2=H(iw:length(H));
AS=-max(H2);
end
elseif filter_type==3
i=find(f>cutoff(2));H=HdB(i);
d=diff(H);i=find(d>=0);
if ~isempty(i),iw=i(1);H2=H(iw:length(H));AS1=-max(H2);
else
AS1=[];%AS1=-H(length(H));
end
i=find(f<cutoff(1));H=HdB(i);H=fliplr(H);
d=diff(H);i=find(d>=0);
if ~isempty(i),iw=i(1);H2=H(iw:length(H));AS2=-max(H2);
else
AS2=[];%AS2=-H(length(H));
end
if ~isempty(AS1) | ~isempty(AS2)
%AS=min([AS1,AS2]);
AS=[AS1,AS2];
end
else
i=find(f>cutoff(1) & f<f0);H=HdB(i);
d=diff(H);i=find(d>=0);
if ~isempty(i),
iw=i(1);H2=H(iw:length(H));AS1=-max(H2);
else
AS1=[];%AS1=-H(length(H));
end
i=find(f>f0 & f<cutoff(2));H=HdB(i);H=fliplr(H);
d=diff(H);i=find(d>=0);
if ~isempty(i),
iw=i(1);H2=H(iw:length(H));AS2=-max(H2);
else
AS2=[];%AS2=-H(length(H));
end
if ~isempty(AS1) | ~isempty(AS2)
%AS=min([AS1,AS2]);
AS=[AS1,AS2];
end
end
if length(AS) == 1,
set(psl_des,'string',num2str(AS,4));
elseif length(AS)==2
set(psl_des,'string',[num2str(AS(1),4),', ',num2str(AS(2),4)]);
else
set(psl_des,'string',[]);
end
if ~isempty(AS)
AM=-min(AS);
set(ui(93),'xdata',[0 0.5*samp_f],'ydata',[AM,AM]);
end
%SAVE??
%set(ui(1),'userdata',hwn);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -