www.gusucode.com > 声音的处理有:LPC,FFT,共振峰,频谱源码程序 > siganlandsystemusingMatlab/SSUM/fdeqexpo/fdeqexpofn.m

    function fdeqexpofn(action,datastruct)
% This code borrows heavily from MAD polezero demonstration
    if nargin < 1
        action='init';
    end

	% Maybe 'apply to white noise' button
	% How do you set the plots so that one zooms and the other doesn't????
	% Phase in radians, +- pi
    % Fix impulse response, zoom in...

	name = mfilename;
	figname = [name(1:end-2) '_fig'];
	f=findobj('Tag',figname);
    handles = get(f,'UserData');

    switch action
		case 'help'
			display_help(figname);
        case 'init'
			setdefaults;
			reset(handles.freq_plot);
			reset(handles.phase_plot);
			reset(handles.impulse_plot);
			handles.npoints = 256;
			handles = makePlots(handles);
			handles = updatePlots(handles);
			linkedzoom([handles.freq_plot, handles.phase_plot],'onx');
		case 'reset'
			set(handles.b_n,'String','1');
			set(handles.a_n,'String','1');
			updateEq(handles);
			handles = updatePlots(handles);
		case 'readinput'
			setdefaults;
			reset(handles.freq_plot);
			reset(handles.phase_plot);
			reset(handles.impulse_plot);
            handles.Fs = 44100;
			handles.npoints = 256;
			handles = makePlots(handles);
			if isfield(datastruct,'polepos')
                for i=1:length(datastruct.zeropos)/2,
                    x = real(datastruct.zeropos(i*2-1));
                    y = imag(datastruct.zeropos(i*2-1));
                end
                for i=1:length(datastruct.polepos)/2,
                    x = real(datastruct.polepos(i*2-1));
                    y = imag(datastruct.polepos(i*2-1));
                end
			end
			handles = updatePlots(handles);
		case {'a_n','b_n'}
			updateEq(handles);
			handles = updatePlots(handles);
		case 'averager'
			% Create GUI to input number
			N = getNgui;
			val = 1/N;
			b_n = ones(1,N)./N;
			b_n = num2str(b_n,'%2.3f ');
			set(handles.b_n,'String',b_n);
			set(handles.a_n,'String','1');
			updateEq(handles);
			handles = updatePlots(handles);
		case 'db'
			axes(handles.freq_plot);
			if get(handles.dB,'Value')
				set(handles.freq_plot,'YLim',[-80 1]);
    			ylabel('Normalized Attenuation (dB)');
			else
				%set(handles.freq_plot,'YLim',[0 1.05]);
                %set(handles.freq_plot,'YLimMode','auto');
    			ylabel('Normalized Attenuation');
			end
			fdeqexpofn 'plotfreqz'
		case 'unwrap'
			if get(handles.unwrap,'Value')
				set(handles.phase_plot,'YLimMode','auto');
			else
				set(handles.phase_plot,'YLim',[-pi pi]);
			end
			fdeqexpofn 'plotfreqz'
		case 'grid'
			if get(handles.grid,'Value')
				axes(handles.freq_plot);
				grid on;
				axes(handles.phase_plot);
				grid on;
				axes(handles.impulse_plot);
				grid on;
			else
				axes(handles.freq_plot);
				grid off;
				axes(handles.phase_plot);
				grid off;
				axes(handles.impulse_plot);
				grid off;
			end
		case 'logf'
			if get(handles.logf,'Value')
				set(handles.freq_plot,'XScale','log');
				set(handles.phase_plot,'XScale','log');
			else
				set(handles.freq_plot,'XScale','linear');
				set(handles.phase_plot,'XScale','linear');
			end
		case 'loadvowel'
			axes(handles.pz_plot);
            cla;
            delete(findobj(gcf,'Selected','on'))
            makePZPlot(handles);
            bws=[0.03 0.07 0.1];    %formant freqs from Rabiner & Juang
            switch datastruct
                case 'beet'
                    freqs=[270 2290 3010];
                case 'bit'
                    freqs=[390 1990 2550];
                case 'bet'
                    freqs=[530 1840 2480];
                case 'bat'
                    freqs=[660 1720 2410];
                case 'bart'
                    freqs=[730 1090 2440];
                case 'bort'
                    freqs=[570 840 2410];
                case 'but'
                    freqs=[440 1020 2240];
                case 'boot'
                    freqs=[300 870 2240];
                case 'bert'
                    freqs=[490 1350 1690];
                otherwise
            end 
            % convert freqs and bws to PZ locations
            angs=2*pi*freqs/44100;
            for fr=1:length(freqs)
                [x,y]=pol2cart(angs(fr),1-bws(fr));
            end     
			fdeqexpofn 'plotfreqz'
		case 'plotfreqz'
			handles = updatePlots(handles);
			fdeqexpofn 'timezoom';
		case {'pzexpo','pzfilterexpo'}
			a_n = [str2num(get(handles.a_n,'String'))];
			b_n = [str2num(get(handles.b_n,'String'))];
            gain = str2num(get(handles.gain,'String'));
			[b_n,a_n] = eqtflength(b_n,a_n);
			[z,p,k] = tf2zp(b_n,a_n);
			data.polepos = p;
			data.zeropos = z;
            data.gain = gain;
			switch action
				case 'pzexpo'
					pzexpogui(data);
				case 'pzfilterexpo'
					pzfilterexpogui(data);
			end
		case 'convexpo'
			a_n = [str2num(get(handles.a_n,'String'))];
            b_n = [str2num(get(handles.b_n,'String'))];
            [b_n,a_n] = eqtflength(b_n,a_n);
			data.impulse = filter(b_n,a_n,[1 zeros(1,49)]);
			convexpogui(data);
		case 'timezoom'
			if isfield(handles,'imp')
                val = get(handles.timezoom,'Value');
                if val == 0,
                    val = 0.001;
                end
                set(handles.impulse_plot,'XLim',[0 val*100])
    			imp = get(handles.implot(1),'YData');
				set(handles.impulse_plot,'YLim', ...
						[min(imp(1:ceil(val*100+1))) ...
						1.1*max(imp(1:ceil(val*100+1)))]);
            end
		case 'print'
			print_figure(f);
		case 'close'
			close_figure(f, figname(1:end-4));
			return;
	end
	set(f,'UserData',handles);

% --------------------------------------------------------------------
function handles = makePlots(handles);
	axes(handles.freq_plot);
	handles.freqs=linspace(0,1,handles.npoints);
	handles.freqline=line(handles.freqs,zeros(size(handles.freqs)));
	set(handles.freqline,'EraseMode','xor');
	axis normal;	% Strange hack.
	%set(handles.freq_plot,'YLim',[0 1.05],'XLim',[0 0.5]);
    set(handles.freq_plot,'XLim',[0 0.5]);
	set(handles.freq_plot, 'DrawMode','fast', 'NextPlot','replacechildren',...
		'Box','off');
	set(handles.freq_plot,'XTick',[],'XTickLabel','');
    ylabel('Normalized Attenuation');
	grid;

	axes(handles.phase_plot);
	handles.phaseline=line(handles.freqs,zeros(size(handles.freqs)));
	set(handles.phaseline,'EraseMode','xor');
	axis normal;	% Strange hack.
	set(handles.phase_plot,'YLim',[-pi pi],'XLim',[0 0.5]);
	set(handles.phase_plot, 'DrawMode','fast', 'NextPlot','replacechildren',...
		'Box','off');
	xlabel('Normalized Frequency ({\varpi})');
    ylabel('Phase (radians)');
	set(handles.freq_plot,'XTick',get(handles.phase_plot,'XTick'));
	grid;

	
	axes(handles.impulse_plot);
	handles.imp = zeros(1,500);
	%handles.implot = plot(handles.imp);
	handles.implot = stem(handles.imp);
	set(handles.implot,'EraseMode','xor');
	set(handles.impulse_plot,'XLim',[-1 100]);
	set(handles.impulse_plot, 'DrawMode','fast', 'NextPlot','replacechildren');
	xlabel('Sample');
	grid;
	%zoom on;
	

% --------------------------------------------------------------------
function ud = updatePlots(ud)
	a_n = [str2num(get(ud.a_n,'String'))];
	b_n = [str2num(get(ud.b_n,'String'))];
    gain = str2num(get(ud.gain,'String'));
    [h,w]=freqz(b_n,a_n,ud.npoints,'whole');
	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);
	end
    set(ud.freqline,'YData',mag);
    if get(ud.dB,'Value')
    else
        set(ud.freq_plot,'YLim',[0 max(mag)*1.1])
    end

	if get(ud.unwrap,'Value')
	    set(ud.phaseline,'YData',unwrap(angle(h)));
		set(ud.phase_plot,'YLim',[min(unwrap(angle(h)))-1 max(unwrap(angle(h)))+1]);
	else
	    set(ud.phaseline,'YData',angle(h));
	end
	imp = filter(b_n, a_n, [1, zeros(1,499)]);	
  set(ud.implot,'YData',imp(1:500));
% 	temp = get(ud.implot,'YData');
% 	temp(2:3:end) = imp(1:500);
%   set(ud.implot,'YData',temp);
	set(ud.impulse_plot,'YLim',[min(imp(1:100)) 1.1*max(imp(1:100))]);
	

% --------------------------------------------------------------------
function showImpulse(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
    [b_k,a_k]=zp2tf(zz',pp',1);

function updateEq(handles)
	a_n = [str2num(get(handles.a_n,'String'))];
	b_n = [str2num(get(handles.b_n,'String'))];

	fde = ['y[n] = ' num2str(b_n(1)) 'x[n] + '];
	for i=2:length(b_n)
		fde = [fde num2str(b_n(i)) 'x[n-' num2str(i-1) '] + '];
	end
	
	for i=2:length(a_n)
		fde = [fde num2str(-a_n(i)) 'y[n-' num2str(i-1) '] + '];
	end

	fde = fde(1:end-2);
	set(handles.equationbox,'String',fde);