denoisefn.m
来自「非常好的数字处理教程」· M 代码 · 共 454 行 · 第 1/2 页
M
454 行
%function denoisefn(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; movegui(f,'center'); reset(handles.timeplot); reset(handles.specplot); case 'resize' movegui(f,'onscreen'); case 'loadsound' handles.audiodata = load_audiodata; handles.audiodata2 = handles.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); 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); handles.audiodata2 = 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); handles = makeTimePlot(handles); handles = makeSpecPlot(handles); linkedzoom([handles.timeplot, handles.specplot],'onx'); place_header(f,handles); case {'playsound', 'playoriginal'} if isfield(handles,'audiodata'), times = get(handles.timeplot,'XLim'); switch action case 'playsound' audiodata = handles.audiodata; button = handles.play; case 'playoriginal' audiodata = handles.audiodata2; button = handles.playoriginal; end 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, button); end case 'doFilter' if isfield(handles, 'audiodata') handles = apply_filter(handles); 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); Xlim = get(handles.timeplot,'XLim'); handles = makeTimePlot(handles); set(handles.specplot,'XLim',Xlim); set(handles.timeplot,'XLim',Xlim); denoisefn 'freqzoom'; end case {'undoFilter', 'undoNoise'} if isfield(handles,'audiodata') switch action case 'undoFilter' handles = undo(handles,'filter'); case 'undoNoise' handles = undo(handles,'noise'); end updateSpecPlot(handles); Xlim = get(handles.timeplot,'XLim'); handles = makeTimePlot(handles); set(handles.specplot,'XLim',Xlim); set(handles.timeplot,'XLim',Xlim); denoisefn 'freqzoom'; end case 'doNoise' if isfield(handles, 'audiodata') handles = apply_noise(handles); 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); Xlim = get(handles.timeplot,'XLim'); handles = makeTimePlot(handles); set(handles.specplot,'XLim',Xlim); set(handles.timeplot,'XLim',Xlim); denoisefn 'freqzoom'; end case {'db','inverse','interpolate','colormap'} if isfield(handles,'audiodata') updateSpecPlot(handles); set(handles.specplot,'XLim',get(handles.timeplot,'XLim')); denoisefn 'freqzoom'; end case {'fftsize','window'} 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')); denoisefn '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'); denoisefn '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 {'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 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
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?