www.gusucode.com > 声音的处理有:LPC,FFT,共振峰,频谱源码程序 > siganlandsystemusingMatlab/SSUM/spectrum/sonogram/sonoexpofn.m
function sonoexpofn(action, datastruct) if nargin < 1 action='init'; end % Add log yscaling % Strange error when 'Explore' button is pressed for the first time. 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; movegui(f,'center'); reset(handles.winplot); reset(handles.timeplot); reset(handles.specplot); handles.exponum = 0; loadWindowPlot(handles); case 'resize' movegui(f,'onscreen'); case 'loadsound' handles.audiodata = load_audiodata; if ~isfield(handles.audiodata, 'filenamepath') return; end if (size(handles.audiodata.data,2) > 1) handles.audiodata.data = to_mono(handles.audiodata.data); end set(handles.freqzoom,'Value',1.0); contents = get(handles.fftsize,'String'); fftsize = str2double(contents{get(handles.fftsize,'Value')}); contents = get(handles.Window,'String'); shape = contents{get(handles.Window,'Value')}; [handles.spectrum, handles.bin, handles.st] = ... spectrogram(handles.audiodata, fftsize, shape); loadWindowPlot(handles); handles = makeTimePlot(handles); handles = makeSpecPlot(handles); linkedzoom([handles.timeplot, handles.specplot],'onx'); place_header(f,handles); case 'readinput' handles.audiodata = datastruct; clear datastruct; handles.audiodata.filenamepath = ''; handles.audiodata.nBits = 16; handles.audiodata.channels = size(handles.audiodata.data,2); if handles.audiodata.channels > 1 handles.audiodata.data = to_mono(handles.audiodata.data); end contents = get(handles.fftsize,'String'); fftsize = str2double(contents{get(handles.fftsize,'Value')}); contents = get(handles.Window,'String'); shape = contents{get(handles.Window,'Value')}; [handles.spectrum, handles.bin, handles.st] = ... spectrogram(handles.audiodata, fftsize, shape); loadWindowPlot(handles); handles = makeTimePlot(handles); handles = makeSpecPlot(handles); linkedzoom([handles.timeplot, handles.specplot],'onx'); place_header(f,handles); case 'playsound' if isfield(handles,'audiodata'), times = get(handles.timeplot,'XLim'); audiodata = handles.audiodata; samples = floor(times*audiodata.Fs); if (samples(1) <= 0) samples(1) = 1; end if (samples(2) > length(audiodata.data)) samples(2) = length(audiodata.data); end audiodata.data = audiodata.data(samples(1):samples(2)); if (max(abs(audiodata.data)) > 1.0) audiodata.data = normalize(audiodata.data); end play_audiodata(audiodata, handles.play); end case {'db','inverse','interpolate','colormap'} if isfield(handles,'audiodata') updateSpecPlot(handles); set(handles.specplot,'XLim',get(handles.timeplot,'XLim')); sonoexpofn 'freqzoom'; end case {'fftsize','window'} loadWindowPlot(handles); if isfield(handles,'audiodata') contents = get(handles.fftsize,'String'); fftsize = str2double(contents{get(handles.fftsize,'Value')}); contents = get(handles.Window,'String'); shape = contents{get(handles.Window,'Value')}; [handles.spectrum, handles.bin, handles.st] = ... spectrogram(handles.audiodata, fftsize, shape); updateSpecPlot(handles); set(handles.specplot,'XLim',get(handles.timeplot,'XLim')); sonoexpofn 'freqzoom'; end case 'freqzoom' if isfield(handles,'audiodata') val = get(handles.freqzoom,'Value'); if val == 0, val = 0.001; end Fs = handles.audiodata.Fs; set(handles.specplot,'YLim',[0 val*Fs/2]) end case 'normalize' if isfield(handles,'audiodata') handles.audiodata.data = normalize(handles.audiodata.data); contents = get(handles.fftsize,'String'); fftsize = str2double(contents{get(handles.fftsize,'Value')}); contents = get(handles.Window,'String'); shape = contents{get(handles.Window,'Value')}; [handles.spectrum, handles.bin, handles.st] = ... spectrogram(handles.audiodata, fftsize, shape); updateSpecPlot(handles); set(handles.specplot,'XLim',get(handles.timeplot,'XLim')); updateTimePlot(handles); set(handles.timeplot,'XLim',get(handles.specplot,'XLim')); linkedzoom([handles.timeplot, handles.specplot],'onx'); sonoexpofn 'freqzoom'; end case 'zoomreset' if isfield(handles,'audiodata') handles = makeTimePlot(handles); set(handles.specplot,'XLim',get(handles.timeplot,'XLim')); linkedzoom([handles.timeplot, handles.specplot],'onx'); end case 'explore_data' if isfield(handles,'audiodata'), explore_data(handles); end case {'fourier', 'alias', 'feature', 'firexpo', 'iirexpo', 'formantexpo', 'lpcexpo'} if isfield(handles,'audiodata'), audiodata = handles.audiodata; switch action case 'fourier' fourierexpogui(audiodata); case 'alias' aliasexpogui(audiodata); case 'feature' featurexpogui(audiodata); case 'firexpo' firexpogui(audiodata); case 'iirexpo' iirexpogui(audiodata); case 'formantexpo' formantexpogui(audiodata); case 'lpcexpo' lpcexpogui(audiodata); end end case 'print' print_figure(f); case 'close' close_figure(f,figname(1:end-4)); return; end set(f,'UserData',handles); % -------------------------------------------------------------------- function loadWindowPlot(handles); contents = get(handles.fftsize,'String'); num_samples = str2double(contents{get(handles.fftsize,'Value')}); contents = get(handles.Window,'String'); shape = contents{get(handles.Window,'Value')}; windowdata = get_windowdata(num_samples, shape); axes(handles.winplot); plot([0:num_samples+1],[0;windowdata;0]); grid; set(handles.winplot,'YTickLabel',['']); set(handles.winplot,'XLim',[-5 num_samples+5],'YLim',[0 1.1]); function handles = makeTimePlot(handles); axes(handles.timeplot); handles.t = [0:1/handles.audiodata.Fs:(length(handles.audiodata.data)-1)/... handles.audiodata.Fs]; 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; % -------------------------------------------------------------------- function updateTimePlot(handles) axes(handles.timeplot) plot(handles.t,handles.audiodata.data) set(handles.timeplot,'XLim',handles.xlim_specplot); %set(handles.timeplot,'YLim',handles.ylim_specplot); xlabel('time (s)'); grid; function updateSpecPlot(handles) axes(handles.specplot); % Check Interpolate and dB 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); 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 explore_data(handles) exponum = handles.exponum+1; axes(handles.specplot); uiwait(msgbox('Press enter when finished...','Instructions','modal')); [t,f] = ginput; bindiff = handles.bin(2) - handles.bin(1); timedif = handles.st(2) - handles.st(1); freqstring = sprintf('%s',['freqs_' num2str(exponum) ' = [']); timestring = sprintf('%s',['times_' num2str(exponum) ' = [']); ampstring = sprintf('%s',['amps_' num2str(exponum) ' = [']); for i=1:length(t), freqindex = find(handles.bin > (f(i) - bindiff/2) & ... handles.bin < (f(i) + bindiff/2)); timeindex = find(handles.st > (t(i) - timedif/2) & ... handles.st < (t(i) + timedif/2)); freqs = handles.bin(freqindex); tempstring = sprintf(' %4.2f ',freqs); freqstring = strcat(freqstring, tempstring); times = handles.st(timeindex); tempstring = sprintf(' %4.3f ',times); timestring = strcat(timestring, tempstring); amps = abs(handles.spectrum(freqindex,timeindex)); tempstring = sprintf(' %4.4f ', amps); ampstring = strcat(ampstring, tempstring); end freqstring = strcat(freqstring, '];'); timestring = strcat(timestring, '];'); ampstring = strcat(ampstring, '];'); disp(freqstring); disp(timestring); disp(ampstring); fprintf('\n');