声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1192|回复: 1

[共享资源] 一个有趣的教程

[复制链接]
发表于 2008-5-18 10:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
转自http://instruct1.cit.cornell.edu ... 1/Timers/index.html

呵呵,最近发现了不少很好的教程,都没时间仔细看

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

   1. The following program is a simple clock. Every second, the time and date
are updated in a small window. The program uses a very small fraction of the
cpu, and while it is running the command line prompt is active. The figure
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);


   2. This example queries a web site once every 10 minutes and sends mail if
the web site is down. It uses the 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);


   3. The next example uses a timer to acquire data from the sound port using
the 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

评分

1

查看全部评分

回复
分享到:

使用道具 举报

发表于 2008-5-21 09:12 | 显示全部楼层
比较有参考价值,虽然用其他的语言也能实现,MATLAB功能还是很强大。
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-11-18 02:57 , Processed in 0.057598 second(s), 18 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表