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

    %
function fseriesexpofn(action,datastruct)
    if nargin < 1
        action='init';
    end

	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.freqplot);
			reset(handles.phasplot);
			reset(handles.timeplot);
			set(handles.A,'String','1');
			set(handles.T,'String','0.1');
			set(handles.tau,'String','0.01');
			set(handles.offset,'String','0');
			handles.audiodata = gensquare(handles);
			handles = makeTimePlot(handles);
			handles = makeSpecPlot(handles);
			set(handles.freqzoom,'Value',1);
            set(handles.ampzoom,'Value',0.5);
		case 'square'
			handles.audiodata = gensquare(handles);
			handles = makeTimePlot(handles);
			handles = makeSpecPlot(handles);
			fseriesexpofn 'freqzoom';
			fseriesexpofn 'ampzoom';
		case 'playsound'
			if isfield(handles, 'audiodata')
				audiodata = handles.audiodata;
				if (max(abs(audiodata.data)) > 1.0)
					audiodata.data = normalize(audiodata.data);
				end
				play_audiodata(audiodata, handles.play);
			end
		case 'logfreq'
			if (get(handles.logfreq,'Value'))
				set(handles.freqplot,'XScale','log');
			else
				set(handles.freqplot,'XScale','linear');
			end
		case {'db'}
			if isfield(handles, 'audiodata')
				handles = makeSpecPlot(handles);
				if (get(handles.dB,'Value'))
					axes(handles.freqplot)
					ylabel('Log Magnitude (dB)');
				else
					axes(handles.freqplot)
					ylabel('Amplitude');
				end
				fseriesexpofn 'freqzoom';
				fseriesexpofn 'ampzoom';
			end
		case 'entire'
			if isfield(handles, 'audiodata')
				handles = makeSpecPlot(handles);
				fseriesexpofn 'freqzoom';
				fseriesexpofn 'ampzoom';
			end
		case 'stem'
			if isfield(handles, 'audiodata')
				handles = makeSpecPlot(handles);
				fseriesexpofn 'freqzoom';
				fseriesexpofn 'ampzoom';
			end
        case {'sonogram', 'alias'}
            if isfield(handles,'audiodata'),
                audiodata = handles.audiodata;
			    switch action
					case 'sonogram'
				    	sonoexpogui(audiodata);
					case 'alias'
                    	aliasexpogui(audiodata);
			    end
            end
		case 'freqzoom'
            if isfield(handles,'audiodata')
                val = get(handles.freqzoom,'Value');
                if val == 0,
                    val = 0.001;
                end
                Fs = handles.audiodata.Fs;
				if get(handles.entire,'Value')
                	set(handles.freqplot,'XLim',[-val*Fs/2 val*Fs/2])
                	set(handles.phasplot,'XLim',[-val*Fs/2 val*Fs/2])
				else
                	set(handles.freqplot,'XLim',[0 val*Fs/2])
                	set(handles.phasplot,'XLim',[0 val*Fs/2])
				end
            end
        case 'ampzoom'
            if isfield(handles,'audiodata')
                val = get(handles.ampzoom,'Value');
                if val == 0,
                    val = 0.001;
                end
                %if get(handles.dB,'Value'),
                %    set(handles.freqplot,'YLim',[-100 (2*val*100-40)])
                %else
                %    set(handles.freqplot,'YLim',[0 val*200])
                %end
            end
		case 'print'
			print_figure(f);
		case 'close'
			close_figure(f,figname(1:end-4));
			return;

	end
	set(f,'UserData',handles);

% --------------------------------------------------------------------
function handles = makeTimePlot(handles);
	axes(handles.timeplot);
	cla;
	handles.t = [0:1/handles.audiodata.Fs:(length(handles.audiodata.data)-1)/...
			handles.audiodata.Fs];
	maxtime = length(handles.t)/handles.audiodata.Fs;
	plot(handles.t,handles.audiodata.data)
	xlabel('time (s)');
	set(handles.timeplot, 'XLim',[0 maxtime], 'YLim', [-1.1 1.1]);
	grid;
 
function handles = makeSpecPlot(handles);
	signal = repmat(handles.audiodata.data,20,1);
	fftsize = length(signal);
	shape = 'Rectangular';

	signal = signal.*get_windowdata(fftsize, shape);
	f = fftshift(fft(signal));
	if (get(handles.dB,'Value'))
		fmag = 20*log10(1e-10+abs(f)) - max(20*log10(1e-10+abs(f)));
	else
		fmag = abs(f)./max(abs(f));
	end

	%% Cheat
	%fmag(find(fmag < 0.000001)) = 0;
	%fmaginds = find(fmag);
	%% Choose one for every three
	%fmagtemp = zeros(1,length(fmag));
	%mags = fmagtemp(fmaginds(2:3:end)) = fmag(fmaginds(2:3:end));
	%fmag = fmagtemp;

	freqs = linspace(-handles.audiodata.Fs/2,handles.audiodata.Fs/2,fftsize);
	axes(handles.freqplot);
	if get(handles.stem,'Value')
		handles.sp = stem(freqs,fmag);
	else
		handles.sp = plot(freqs,fmag);
	end
	axis xy; grid;
	xlabel('');
	set(handles.freqplot,'XTickLabel','')
	ylabel('Magnitude');

	axes(handles.phasplot);
	%if get(handles.stem,'Value')
	%	handles.ph = stem(freqs,unwrap(angle(f)));
	%else
	%	handles.ph = plot(freqs,unwrap(angle(f)));
	%end
	handles.ph = plot(freqs,unwrap(angle(f)));
	axis xy; grid;
	ylabel('Phase (rad)');
	xlabel('Frequency (kHz)');

	if get(handles.entire,'Value')
		set(handles.freqplot, ...
			'XLim',[-handles.audiodata.Fs/2 handles.audiodata.Fs/2]);
		set(handles.phasplot, ...
			'XLim',[-handles.audiodata.Fs/2 handles.audiodata.Fs/2]);
	else
		set(handles.freqplot,'XLim',[0 handles.audiodata.Fs/2]);
		set(handles.phasplot,'XLim',[0 handles.audiodata.Fs/2]);
	end

	if (get(handles.logfreq,'Value'))
		set(handles.freqplot,'XScale','log');
		set(handles.phasplot,'XScale','log');
	else
		set(handles.freqplot,'XScale','linear');
		set(handles.phasplot,'XScale','linear');
	end


function audiodata = gensquare(handles)
	% Create square wave
	A = str2num(get(handles.A,'String'));
	T = str2num(get(handles.T,'String'));
	tau = str2num(get(handles.tau,'String'));
	offset = str2num(get(handles.offset,'String'));

	Fs = 50000;
	t = [0:1/Fs:T];
	signal = zeros(floor(Fs*T),1)+offset;
	tau = floor(T*Fs*tau);
	signal(1:tau) = A+offset;
	audiodata.data = signal;
	audiodata.Fs = Fs;