📄 pzfilterexpofn.m
字号:
pp = []; zz = []; for p=1:length(polepos) ppos=get(polepos(p),'Position'); pp(p)=complex(ppos(1),ppos(2)); end for z=1:length(zeropos) zp=get(zeropos(z),'Position'); zz(z)=complex(zp(1),zp(2)); end data.polepos = pp; data.zeropos = zz; data.gain = str2num(get(handles.gain,'String')); pzexpogui(data); case 'unselectall' set(findobj(gcf,'Selected','on'),'Selected','off'); case 'print' print_figure(f); case 'close' close_figure(f,figname(1:end-4)); return; end set(f,'UserData',handles);% --------------------------------------------------------------------function makePZPlot(handles) axes(handles.pz_plot); set(handles.pz_plot, 'DrawMode','fast', 'NextPlot','replacechildren',... 'Box','on', 'XLim', [-1.3 1.3], 'YLim', [-1.3 1.3]); cax=handles.pz_plot; % define a circle th = 0:pi/50:2*pi; xunit = cos(th); yunit = sin(th); rmax=1; tc = get(cax,'xcolor'); % now really force points on x/y axes to lie on them exactly inds = 1:(length(th)-1)/4:length(th); xunit(inds(2:2:4)) = zeros(2,1); yunit(inds(1:2:5)) = zeros(3,1); patch('xdata',xunit*rmax,'ydata',yunit*rmax, ... 'edgecolor',tc,'facecolor',get(gca,'color'),... 'ButtonDownFcn','pzfilterexpofn unselectall'); line([-1.1 1.1],[0 0]); line([0 0],[-1.1 1.1]); axis(rmax*[-1 1 -1.15 1.15]); axis image; axis off; zoom off;function handles = makeHzPlot(handles); if isfield(handles, 'audiodata') Fs = handles.audiodata.Fs; else Fs = handles.Fs; end axes(handles.Hz_plot); cla; handles.freqs=linspace(0,Fs/2,handles.npoints); handles.magline=line(handles.freqs,zeros(size(handles.freqs))); set(handles.magline,'EraseMode','xor'); axis normal; % Strange hack. set(handles.Hz_plot,'XLim',[0 round(Fs/2)]); set(handles.Hz_plot, 'DrawMode','fast', 'NextPlot','replacechildren',... 'Box','off'); xlabel('frequency (Hz)'); ylabel('Attenuation');% --------------------------------------------------------------------function handles = makeTimePlot(handles); handles.t = [0:1/handles.audiodata.Fs:(length(handles.audiodata.data)-1)/... handles.audiodata.Fs]; axes(handles.timeplot) plot(handles.t,handles.audiodata.data) maxtime = length(handles.t)/handles.audiodata.Fs; set(handles.timeplot,'XLim',[0 maxtime]); set(handles.timeplot,'YLim',[-1.0 1.0]); grid; xlabel('time (s)');function handles = makeSpecPlot(handles); axes(handles.specplot); handles.pos = get(gca,'Position'); % Save axes position if (get(handles.interpolate,'Value')) if (get(handles.dBcheckbox,'Value')) pcolor(handles.st,handles.bin,20*log10(abs(handles.spectrum))); else pcolor(handles.st,handles.bin,abs(handles.spectrum)); end shading interp; else if (get(handles.dBcheckbox,'Value')) imagesc(handles.t,handles.bin,20*log10(abs(handles.spectrum))); else imagesc(handles.t,handles.bin,abs(handles.spectrum)); end end axis xy; ylabel('Frequency (Hz)'); colormap('Jet'); set(handles.specplot,'XTickLabel',['']); h = colorbar('vert'); if (get(handles.dBcheckbox,'Value')) set(get(h, 'YLabel'), 'String', 'dB'); else set(get(h, 'YLabel'), 'String', 'Amplitude'); end set(handles.specplot,'Units','Characters'); sonopos = get(handles.specplot,'Position') set(h,'units','Characters', ... 'Position',[sonopos(1)+sonopos(3)+18 sonopos(2) 7 sonopos(4)]) switch computer case {'GLNX86','MAC'} %set(gca,'Position',handles.pos + [0 0.07 0 0]) set(gca,'Position',handles.pos) case 'PCWIN' set(gca,'Position',handles.pos) end handles.xlim_specplot_orig = get(handles.specplot,'XLim'); handles.ylim_specplot_orig = get(handles.specplot,'YLim'); handles.xlim_timeplot_orig = get(handles.timeplot,'XLim'); handles.ylim_timeplot_orig = get(handles.timeplot,'YLim'); handles.xlim_specplot = handles.xlim_specplot_orig; handles.ylim_specplot = handles.ylim_specplot_orig; handles.xlim_timeplot = handles.xlim_timeplot_orig; handles.ylim_timeplot = handles.ylim_timeplot_orig; handles.clim = get(handles.specplot,'CLim');% --------------------------------------------------------------------function ud = updateHzPlot(ud) polepos=findobj(ud.pz_plot,'Tag','Pole'); zeropos=findobj(ud.pz_plot,'Tag','Zero'); len=max(length(polepos),length(zeropos)); pp=zeros(1,len); % pp=1e-10*rand(1,len); for p=1:length(polepos) ppos=get(polepos(p),'Position'); pp(p)=ppos(1)+ppos(2)*i; end zz=zeros(1,len); % zz=1e-10*rand(1,len); for z=1:length(zeropos) zp=get(zeropos(z),'Position'); zz(z)=zp(1)+zp(2)*i; end ud.zz=zz;ud.pp=pp; [ud.num,ud.den]=zp2tf(zz',pp',1); [h,w]=freqz(ud.num,ud.den,ud.freqs,ud.Fs); gain = str2num(get(ud.gain,'String')); if get(ud.dB,'Value') % normalise so max = 0 dB logmag=20*log10(abs(h)); mag=logmag-max(logmag); else mag = gain*abs(h)./max(abs(h)); %mag = gain*abs(h); set(ud.Hz_plot,'YLim',[0 max(mag)*1.1]); end set(ud.magline,'YData',mag);function updateTimePlot(handles) axes(handles.timeplot) plot(handles.t,handles.audiodata) set(handles.timeplot,'XLim',handles.xlim_timeplot); set(handles.timeplot,'YLim',handles.ylim_timeplot); xlabel('time (s)'); grid;function updateSpecPlot(handles) axes(handles.specplot); if (get(handles.interpolate,'Value')) if (get(handles.dBcheckbox,'Value')) pcolor(handles.st,handles.bin,20*log10(abs(handles.spectrum))); else pcolor(handles.st,handles.bin,abs(handles.spectrum)); end shading interp; else if (get(handles.dBcheckbox,'Value')) imagesc(handles.t,handles.bin,20*log10(abs(handles.spectrum))); else imagesc(handles.t,handles.bin,abs(handles.spectrum)); end end set(handles.specplot,'XTickLabel',['']); axis xy; ylabel('Frequency (Hz)'); % Get Colormap contents = get(handles.colormap,'String'); cmap = colormap(lower(contents{get(handles.colormap,'Value')})); % Handle Inverse if (get(handles.inverse,'Value')) colormap(flipud(cmap)); else colormap(cmap); end set(handles.specplot,'XLim',handles.xlim_specplot); set(handles.specplot,'YLim',handles.ylim_specplot); set(handles.specplot,'CLim',handles.clim); h = colorbar('vert'); if (get(handles.dBcheckbox,'Value')) set(get(h, 'YLabel'), 'String', 'dB'); else set(get(h, 'YLabel'), 'String', 'Amplitude'); end set(handles.specplot,'Units','Characters'); sonopos = get(handles.specplot,'Position') set(h,'units','Characters', ... 'Position',[sonopos(1)+sonopos(3)+18 sonopos(2) 7 sonopos(4)]) switch computer case {'GLNX86','MAC'} %set(gca,'Position',handles.pos + [0 0.07 0 0]) set(gca,'Position',handles.pos) case 'PCWIN' set(gca,'Position',handles.pos) end % --------------------------------------------------------------------function [a,b] = designfilter(handles) order = str2double(get(handles.filtorder,'String')); contents = get(handles.filtermenu,'String'); cut1 = str2double(get(handles.cut1,'String')); cut2 = str2double(get(handles.cut2,'String')); cut1 = cut1/(handles.Fs/2); cut2 = cut2/(handles.Fs/2); switch (lower(contents{get(handles.filtermenu,'Value')})) case 'allpass' b = 1; case 'lowpass' b = fir1(order, cut1, 'low'); case 'highpass' b = fir1(order, cut1, 'high'); case 'bandpass' b = fir1(order, [cut1 cut2], 'bandpass'); case 'notch' b = fir1(order, [cut1 cut2], 'stop'); end a = 1;function ud = apply_filter(ud) polepos=findobj(ud.pz_plot,'Tag','Pole'); zeropos=findobj(ud.pz_plot,'Tag','Zero'); gain = str2num(get(ud.gain,'String')); len=max(length(polepos),length(zeropos)); pp=zeros(1,len); % pp=1e-10*rand(1,len); for p=1:length(polepos) ppos=get(polepos(p),'Position'); pp(p)=ppos(1)+ppos(2)*i; end zz=zeros(1,len); % zz=1e-10*rand(1,len); for z=1:length(zeropos) zp=get(zeropos(z),'Position'); zz(z)=zp(1)+zp(2)*i; end ud.zz=zz;ud.pp=pp; [ud.num,ud.den]=zp2tf(zz',pp',1); ud.audiodata2 = ud.audiodata; ud.audiodata.data = gain*filter(ud.num, ud.den, ud.audiodata.data); if max(abs(ud.audiodata.data))>1 ud.audiodata.data = normalize(ud.audiodata.data); end % Store old data for undo ud.spectrum2 = ud.spectrum; ud.bin2 = ud.bin; ud.st2 = ud.st; set(ud.undo,'String','Undo');function handles = undo(handles) hObject = handles.undo; if (strcmp(get(hObject,'String'),'Undo')) % Store old data for redo handles.spectrum3 = handles.spectrum; handles.bin3 = handles.bin; handles.st3 = handles.st; handles.audiodata3 = handles.audiodata; % Reload old data handles.spectrum = handles.spectrum2; handles.bin = handles.bin2; handles.st = handles.st2; handles.audiodata = handles.audiodata2; set(hObject,'String','Redo'); else handles.spectrum = handles.spectrum3; handles.bin = handles.bin3; handles.st = handles.st3; handles.audiodata = handles.audiodata3; set(hObject,'String','Undo'); endfunction ud = add_coef(ud,type) switch type case 'zero' string = 'o'; callbak = 'pzfilterexpofn selectzero'; tag = 'Zero'; case 'pole' string = 'x'; callbak = 'pzfilterexpofn selectpole'; tag = 'Pole'; end [x,y]=pol2cart(pi/(2+4*rand(1)),1.0*rand(1)); p1=text('Parent',ud.pz_plot,'Position',[x y],'String',string,... 'ButtonDownFcn',callbak,'EraseMode','xor',... 'Tag',tag,'HorizontalAlignment','center'); p2=text('Parent',ud.pz_plot,'Position',[x -y],'String',string,... 'ButtonDownFcn',callbak, 'EraseMode','xor',... 'Tag',tag,'HorizontalAlignment','center'); set(p1,'UserData',p2); set(p2,'UserData',p1);function ud = select_coef(ud,type) switch type case 'zero' buttonfn = 'pzfilterexpofn movezero'; case 'pole' buttonfn = 'pzfilterexpofn movepole'; end s=get(gcbo,'UserData'); set(gcf,'WindowButtonMotionFcn', buttonfn); set(gcf,'WindowButtonUpFcn','pzfilterexpofn releaseobject'); ud.currentcoef=gcbo; ud.partnercoef=s; set([findobj(gca,'Tag','Pole'); findobj(gca,'Tag','Zero')],'Selected','off'); set([ud.currentcoef ud.partnercoef],'Selected','on');function ud = move_coef(ud) cp=get(ud.pz_plot,'CurrentPoint'); if (cp(1)*cp(1)+cp(3)*cp(3)) < 1.1 set(ud.currentcoef,'Position',[cp(1) cp(3)]); set(ud.partnercoef,'Position',[cp(1) -cp(3)]); end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -