📄 tof.m
字号:
function varargout = tof(varargin)% TOF demonstrates sound ranging (sonar) between PC% speaker and microphone (up to 6m away).%% This demo requires:% - Data Acquisition Toolbox,% - Signal Processing Toolbox% - Windows sound card% - files: sonar_anal.m tof.fig tof.m (this file)%% How does it work?% A pulse wave (chirp) is generated from the speaker,% which travels through air at the speed of sound. % At the same time the microphone records the received% wave (plus possible reflections depending on room% acoustics). The propagation time delay (sec) is % measured and converted to distance (m).% (c) 2001 R.A.Bemis, The MathWorks, Inc.% TOF Application M-file for tof.fig% FIG = TOF launch tof GUI.% TOF('callback_name', ...) invoke the named callback.% Last Modified by GUIDE v2.0 18-Oct-2001 17:27:18if nargin == 0 % LAUNCH GUI fig = openfig(mfilename,'reuse'); % Use system color scheme for figure: set(fig,'Color',get(0,'defaultUicontrolBackgroundColor')); % Generate a structure of handles to pass to callbacks, and store it. handles = guihandles(fig); guidata(fig, handles); sonar_setup(handles,0.0087) % set up audio I/O if nargout > 0 varargout{1} = fig; endelseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK try if (nargout) [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard else feval(varargin{:}); % FEVAL switchyard end catch disp(lasterr); endend%| ABOUT CALLBACKS:%| GUIDE automatically appends subfunction prototypes to this file, and %| sets objects' callback properties to call them through the FEVAL %| switchyard above. This comment describes that mechanism.%|%| Each callback subfunction declaration has the following form:%| <SUBFUNCTION_NAME>(H, EVENTDATA, HANDLES, VARARGIN)%|%| The subfunction name is composed using the object's Tag and the %| callback type separated by '_', e.g. 'slider2_Callback',%| 'figure1_CloseRequestFcn', 'axis1_ButtondownFcn'.%|%| H is the callback object's handle (obtained using GCBO).%|%| EVENTDATA is empty, but reserved for future use.%|%| HANDLES is a structure containing handles of components in GUI using%| tags as fieldnames, e.g. handles.figure1, handles.slider2. This%| structure is created at GUI startup using GUIHANDLES and stored in%| the figure's application data using GUIDATA. A copy of the structure%| is passed to each callback. You can store additional information in%| this structure at GUI startup, and you can change the structure%| during callbacks. Call guidata(h, handles) after changing your%| copy to replace the stored original so that subsequent callbacks see%| the updates. Type "help guihandles" and "help guidata" for more%| information.%|%| VARARGIN contains any extra arguments you have passed to the%| callback. Specify the extra arguments by editing the callback%| property in the inspector. By default, GUIDE sets the property to:%| <MFILENAME>('<SUBFUNCTION_NAME>', gcbo, [], guidata(gcbo))%| Add any extra arguments after the last argument, before the final%| closing parenthesis.% --------------------------------------------------------------------function varargout = Single_Callback(h, eventdata, handles, varargin)set(handles.Repeat,'enable','off') % disable Repeat buttonsonar_once(handles.figure1) % take one measurement% --------------------------------------------------------------------function varargout = Repeat_Callback(h, eventdata, handles, varargin)set(h,'enable','off') % disable Repeat buttonset(handles.Single,'enable','off') % disbale Single buttonset(handles.Stop,'enable','on') % enable Stop buttonsonar_run(handles.figure1) % measure repeatedly% --------------------------------------------------------------------function varargout = Stop_Callback(h, eventdata, handles, varargin)set(h,'enable','off') % disable Stop buttonset(handles.Repeat,'enable','on') % enable Repeat buttonset(handles.Single,'enable','on') % enable Single buttonsonar_stop(handles.figure1) % stop repeats% --------------------------------------------------------------------function sonar_setup(handles,cal_lag)fig=handles.figure1; % figure handleif nargin<2, cal_lag=0; end% physicsc=330; % speed of sound (m/sec)d=50; % max distance (m)Td=d/c; % max return time (sec)pw=0.01; % pulse width (sec)Tmax=Td+pw; % total response time (sec)% DAQ parametersai=analoginput('winsound'); % create AI objectaddchannel(ai,1); % single channel (mono)Fs=setverify(ai,'samplerate',44100); % sample rate (Hz)N=2^ceil(log2(Tmax*Fs)); % buffer size (samples)ao=analogoutput('winsound'); % create AO objectaddchannel(ao,1); % single channel (mono)ao.SampleRate=Fs; % output Fs same as input% excitation chirpf0=8e3; f1=12e3; % freq range (Hz)t=[0:(1/Fs):pw]'; % time index (sec)n=length(t); % pulse length (samples)pulse=chirp(t,f0,pw,f1); % chrip waveform (short)pulse=pulse.*hanning(n); % pulse envelopepad=zeros(N-n,1); % silence trailing chirpx=[pulse; pad]; % excitation (zero padded)% response waveformai.SamplesPerTrigger=N; % total samples to acquireai.TriggerDelayUnits='sec'; % trigger delay=0 secai.TriggerDelay=cal_lag; % calibration lag factor (sec)ai.SamplesAcquiredFcnCount=N;ai.SamplesAcquiredFcn=['sonar_anal(' num2str(fig) ')'];% user dataud.ai=ai; % audio inputud.ao=ao; % audio outputud.c=c; % wave speed in airud.x=x; % excitation waveformud.h=handles; % guide figure handlesset(fig,'userdata',ud) % store bundle with figure% --------------------------------------------------------------------function sonar_once(fig)% user dataud=get(fig,'userdata'); % user data fieldsai=ud.ai; % audio inputao=ud.ao; % audio outputx=ud.x; % excitation chirp pulseai.samplesacquiredfcn=''; % don't repeat measurementputdata(ao,x) % arm audio outputstart([ai ao]) % start measurementsonar_anal(fig) % manually analyze data% --------------------------------------------------------------------function sonar_run(fig)% user dataud=get(fig,'userdata'); % user data fieldsai=ud.ai; % audio inputao=ud.ao; % audio outputx=ud.x; % excitation chirp pulse% callback function to analyze when done acquiring dataai.samplesacquiredfcn=['sonar_anal(' num2str(fig) ')'];% NOTE: sonar_anal will rearm output and repeat measurementputdata(ao,x) % arm audio outputstart([ai ao]) % start measuring (will repeat)% --------------------------------------------------------------------function sonar_stop(fig)% user dataud=get(fig,'userdata'); % user data fieldsai=ud.ai; % audio inputai.samplesacquiredfcn=''; % stop repeating% --------------------------------------------------------------------function sonar_cleanup(fig)% user dataud=get(fig,'userdata'); % user data fieldsai=ud.ai; delete(ai) % remove audio inputao=ud.ao; delete(ao) % remove audio output% --------------------------------------------------------------------function varargout = figure1_CloseRequestFcn(h, eventdata, handles, varargin)sonar_stop(handles.figure1) % stops if still repeatingsonar_cleanup(handles.figure1) % remove audio objectsdelete(handles.figure1) % remove figure window
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -