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

    %
function sinexpofn(action)

    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;
			update(handles);
		case 'update'
			update(handles);
		case 'play'
			[signal, Fs] = calc_signal(handles);
			signal = normalize(signal);
			audiodata.data = signal';
			audiodata.Fs = Fs;
			play_audiodata(audiodata, handles.play);
        case {'fourier','sonogram','alias'}
			[signal, Fs] = calc_signal(handles);
			signal = normalize(signal);
			audiodata.data = signal';
			audiodata.Fs = Fs;
			if (max(signal) > 1.0)
				signal = normalize(signal);
			end
			switch action
				case 'fourier'
					fourierexpogui(audiodata);
				case 'sonogram'
					sonoexpogui(audiodata);
                case 'alias'
                    aliasexpogui(audiodata);
			end
			
	end
	set(f,'UserData',handles);


% --------------------------------------------------------------------
function update(handles)
	axes(handles.sin1);
	cla;
	axes(handles.sinsum);
	cla;

	Fs = 44100;
	duration = str2double(get(handles.durationtext,'String'));
	t = [0:1/Fs:duration];
	freq1 = str2double(get(handles.freq1,'String'));
	freq2 = str2double(get(handles.freq2,'String'));
	amp1 = str2double(get(handles.amp1,'String'));
	amp2 = str2double(get(handles.amp2,'String'));
	phase1 = str2double(get(handles.phase1,'String'))*pi/180;
	phase2 = str2double(get(handles.phase2,'String'))*pi/180;
	offset = str2double(get(handles.offset,'String'));
	sine1 = offset + amp1*sin(2*pi*freq1*t + phase1);
	sine2 = amp2*sin(2*pi*freq2*t + phase2);

	popup_sel_index = get(handles.popupmenu1, 'Value');
	switch popup_sel_index
		case 1
			% Sum
			signal = sine1 + sine2;
			legendtext = 'sine1 + sine2';
		case 2
			% Product
			signal = sine1.*sine2;
			legendtext = 'sine1*sine2';
	end

	axes(handles.sin1);
	if (get(handles.plot1box,'Value') & get(handles.plot2box,'Value'))
		plot(t,sine1,'b-',t,sine2,'k--');
		legend('sine1','sine2');
		hold on;
	elseif (get(handles.plot1box,'Value'))
		plot(t,sine1);
		hold on;
		legend('sine1');
	elseif (get(handles.plot2box,'Value')) 
		plot(t,sine2);
		hold on;
		legend('sine2');
	end

	if (get(handles.gridbox,'Value'))
		grid on;
	else
		grid off;
	end

	axes(handles.sinsum);
	plot(t,signal);
	legend(legendtext);
	xlabel('Time (s)');
	ylabel('Amplitude');
	if (get(handles.gridbox,'Value'))
		grid on;
	else
		grid off;
	end
	% Set plot properties
	set(handles.sin1,'XTickLabel',['']);
	set(handles.sin1,'YLim',[-1.5 1.5]);
	set(handles.sinsum,'YLim',[-2 2]);
	set(handles.sin1,'XLim',[0 duration]);
	set(handles.sinsum,'XLim',[0 duration]);
    

% --------------------------------------------------------------------
function [signal,Fs] = calc_signal(handles)
	Fs = 44100;
	freq1 = str2double(get(handles.freq1,'String'));
	freq2 = str2double(get(handles.freq2,'String'));
	amp1 = str2double(get(handles.amp1,'String'));
	amp2 = str2double(get(handles.amp2,'String'));
	phase1 = str2double(get(handles.phase1,'String'))*pi/180;
	phase2 = str2double(get(handles.phase2,'String'))*pi/180;
	offset = str2double(get(handles.offset,'String'));
	t = [0:1/Fs:2];
	sine1 = offset + amp1*sin(2*pi*freq1*t + phase1);
	sine2 = amp2*sin(2*pi*freq2*t + phase2);
	popup_sel_index = get(handles.popupmenu1, 'Value');
	switch popup_sel_index
		case 1
			% Sum
			signal = sine1 + sine2;
		case 2
			% Product
			signal = sine1.*sine2;
	end