Cornell University
BioNB 441
Using Matlab Timers
Introduction
Matlab 6.5 includes the ability to schedule m-files to run. The scheduling can be one-shot or repetitive. Scheduling allows (almost) real-time control of external instruments and recording of electical signals. For instance, it should be possible to make a software oscilloscope which acquires voltages from the sound port at regular intervals.
Examples
CloseRequestfcn
is set up to destroy the timer so that when the
window is closed, the timer stops. The timer object is assigned a period,
starting delay, and a call-back string which executes when the timer-object
times out.figure clf set(gcf,'position',[10 50 175 35]) set(gcf,'doublebuffer','on') set(gcf, 'closerequestfcn','stop(t);delete(t);delete(gcf)'); set(gcf, 'menubar','none') axes('position',[0 0 1 1 ]); %Build Timer object and turn on timer %delay for 1/2 second so rest of setup finishes t = timer('period',1.0); set(t,'ExecutionMode','fixedrate','StartDelay',0.5); set(t,'timerfcn',['cla;',... 'ct = datestr(clock,''mmm.dd,yyyy HH:MM:SS'');',.... 'text(.05,.5,ct,''fontsize'',12);']); start(t);
urlread
function to check the
web site and the sendmail
function to send the mail. Note that
you will need to set the values of your SMTP server and your mail id on that
server using the commands given in the example. You will also need to supply
the mail address of the recepient to sendmail
.figure clf set(gcf,'position',[10 50 175 35]) set(gcf,'doublebuffer','on') set(gcf, 'closerequestfcn','stop(t);delete(t);delete(gcf)'); set(gcf, 'menubar','none') setpref('Internet','SMTP_Server','yourSMTPserver.com'); setpref('Internet','E_mail','yourMailID'); axes('position',[0 0 1 1 ]); %Build Timer object and turn on timer %delay for 1/2 second so rest of setup finishes t = timer('period',600.0); set(t,'ExecutionMode','fixedrate','StartDelay',0.5); set(t,'timerfcn',['cla;',... 's=[];s=urlread(''http://www.cornell.edu'');',... 'ct=''Server Up'';',... 'if isempty(s);',... 'ct=''Cornell Server Down'';',... 'sendmail(''user@otherdomain.com'',''Server Down'','''');',... 'end;',.... 'text(.05,.5,ct,''fontsize'',12);']); start(t);
audiorecorder
object. The program is written as a function
which is called by the callbacks. The sound is filtered, and the original
spectrum and filtered spectrum are displayed in a figure. The execution of
the timer event callback takes around 0.10 to 0.25 seconds if the cursor is
in any Matlab window, but takes around 0.05 second if the mouse cursor has
been clicked outside of any Matlab window. The getaudiodata
function
does not always return the number of samples that you asked for, but it always
returns at least the desired number. The tic
and toc
commands in the timer callback function were used to determine the execution
time.%record and filter as fast as possible %using Matlab 6.5 timer and audiorecorder functions function audio1(varargin); %find out what kind of call it was %timer event passes {obj,event,param1,param2} switch nargin case 0 fcn = 'setup'; %starting up case 1 fcn = varargin{1}; %quiting case 4 fcn = varargin{3}; %timer event otherwise error('bad agrument'); end %=========================================================== switch fcn case 'setup' data.myname = mfilename ; fig=figure(1); clf; set(fig,'doublebuffer','on'); %sample rate and number of inputs per tirgger data.Nin = 1024; data.Fs = 8192; data.tperiod = data.Nin/data.Fs; Nchan = 1; Nbits = 16; %Configure the Filter filterlength=8; %frequencies a a fraction of the nyquist freq locut=400/(data.Fs/2); hicut=1200/(data.Fs/2); [data.b,data.a] = butter(filterlength,[locut,hicut]); %plot the filter freq response [fresponse, ffreq] = freqz(data.b,data.a,1000); subplot(3,1,2) plot(ffreq/pi*data.Fs/2,abs(fresponse)); xlabel('frequency') ylabel('filter response') axis([0 data.Fs/2 0 2]); % Input Object Configuration. % Create an analog input object with one channel. data.rec = audiorecorder(data.Fs, Nbits, Nchan); % Start the analog input object. %and get the first sample of music recordblocking(data.rec,data.tperiod); data.music = getaudiodata(data.rec); data.filteredmusic = filter(data.b, data.a, data.music); %Build Timer object and turn on timer %delay for 1/2 second so rest of setup finishes data.t = timer('timerFcn',{data.myname,'timer',gcf},... 'period',data.tperiod*2); set(data.t,'ExecutionMode','fixedrate','StartDelay',0.5); set(data.t,'tag','time'); start(data.t); %plot of spectrum of filtered signal subplot(3,1,3) [spect,freq]=periodogram(data.filteredmusic,[],[],data.Fs); data.fline=plot(freq,spect); set(gca,'ylim',[0 .001]); xlabel('frequency'); ylabel('filtered amp'); %plot of spectrum of raw signal subplot(3,1,1) [spect,freq]=periodogram(data.music,[],[],data.Fs); data.iline=plot(freq,spect); set(gca,'ylim',[0 .001]); xlabel('frequency'); ylabel('music amp'); %new start the next data aqusition %after this, the timer routine starts data record(data.rec,data.tperiod); %one gui element to stop the music data.quitbutton=uicontrol('Style','pushbutton',... 'Position',[0 0 45 25],... 'String','Quit',... 'callback', [data.myname,' quit']); set(fig,'userdata',data); case 'quit' data=get(gcf,'userdata'); %stop everything and delete it stop(data.rec) delete(data.rec) stop(data.t) delete(data.t) close(gcf); case 'timer' tic data=get(varargin{4},'userdata'); %get data from recording started last time while isrecording(data.rec) end data.music = getaudiodata(data.rec); data.music = data.music(1:data.Nin); % Start recording new data. record(data.rec,data.tperiod); % compute the spectrum and display it spect = periodogram(data.music,[],[],data.Fs); set(data.iline,'ydata',spect); %filter it , compute the spectrum and display data.filteredmusic = filter(data.b,data.a,data.music) ; spect=periodogram(data.filteredmusic,[],[],data.Fs); set(data.fline,'ydata',spect); set(varargin{4},'userdata',data); toc end