# rtf

## PURPOSE

## SYNOPSIS

function rtf(plot_frq,flag_save)

## DESCRIPTION

## CROSS-REFERENCE INFORMATION

This function calls:

- close VR=CLOSE(VR)
- close VW=CLOSE(VW)
- close VR=CLOSE(VR)
- close VW=CLOSE(VW)
- close VR=CLOSE(VR)
- close VW=CLOSE(VW)
- start_display

This function is called by:

## SUBFUNCTIONS

- function keypress(src, e)
- function flag(obj,event)
- function stop_dev(obj,event)
- function disply(obj,event)

## SOURCE CODE

    0001 function rtf(plot_frq,flag_save)
    0002
    0003
    0004 close all
    0005 evalin('base','stop=0;');
    0006
    0007
    0008
    0009 %=========SET THE BASIC FIGURE=================
    0010 fig = figure('Position',[500,500,800,600],...
    0011         'NumberTitle','off',...
    0012         'Name','Scope',...
    0013         'doublebuffer','on',...
    0014         'HandleVisibility','on',...
    0015         'KeyPressFcn', @keypress, ...
    0016         'Renderer', 'openGL');
    0017 %=============================================
    0018
    0019
    0020 %=============OPEN THE DEVICE FOR RECORD======
    0021 sample_frequency = 44100;
    0022 samples_per_frame = 1024;
    0023 %plot_frq=10;
    0024 record_time=600;
    0025 samples_to_acquire = record_time * sample_frequency;
    0026
    0027
    0028 %PREPARE    THE    DEVICE
    0029 ai = analoginput('winsound');
    0030 chan = addchannel( ai, 1 );
    0031 set( ai, 'SampleRate', sample_frequency )
    0032 set( ai, 'SamplesPerTrigger', samples_to_acquire )
    0033 set(ai, 'StopFcn', @stop_dev)
    0034
    0035 sample_frequency = get( ai, 'SampleRate' );
    0036
    0037
    0038 %SETTING CALL BACK FUNCTIONS:
    0039 %The first for capture   the
    0040 %second     for      display
    0041 set(ai, 'SamplesAcquiredFcnCount',samples_per_frame);
    0042 set(ai, 'SamplesAcquiredFcn',@flag);
    0043 set(ai, 'TimerPeriod',(1/plot_frq));
    0044 set(ai, 'TimerFcn',@disply);
    0045
    0046
    0047 %=============SAVE THE CONFIGURATION======
    0048 plot_ref=plot(zeros(10,1));
    0049 fid=-1;
    0050
    0051
    0052 %SAVE THE CURRENT PARAMETERS:
    0053 name_of_file=sprintf('%s-%d','real-anal',(round(sample_frequency/samples_per_frame)));
    0054 remark={1,...
    0055         zeros(samples_per_frame*20,1)',...
    0056         0,...
    0057         plot_ref,...
    0058         plot_frq,...
    0059         cputime,...
    0060         flag_save,...
    0061         -1,...
    0062         name_of_file
    0063         };
    0064
    0065 set(ai, 'UserData',remark)
    0066
    0067
    0068
    0069
    0070 %=============START TO RECORD================
    0071 fprintf ('To stop the program set <stop=1> or press q in the figure window\n');
    0072 start (ai)
    0073
    0074
    0075
    0076
    0077
    0078 %=============================================
    0079 %=========THE MAIN PROGRAM====================
    0080 %=============================================
    0081 %                   *
    0082 %                  ***
    0083 %                 *****
    0084 %                  ***
    0085 %                  ***
    0086 %                  ***
    0087 %                  ***
    0088 %                  ***
    0089 %                 *****
    0090 %                  ***
    0091 %                   *
    0092 %=============================================
    0093 %==========CALLBACK FUNCTIONS=================
    0094 %=============================================
    0095
    0096
    0097
    0098 %=========Keypress callback===========
    0099 function keypress(src, e)
    0100   keypressed=get(gcf,'CurrentCharacter');
    0101
    0102   % ignore raw control, shift, alt keys
    0103   if keypressed
    0104     % Quit
    0105     if strcmp( keypressed, 'q')
    0106         evalin('base','stop=1;');
    0107     end
    0108   end
    0109 return
    0110
    0111
    0112 %============FLAG FUNCTION===================
    0113 %This function activated when we capture
    0114 %certain    amount  of          samples
    0115 function flag(obj,event)
    0116
    0117   % CHECK FOR STOP SIGNAL
    0118   if  evalin('base','stop')
    0119       stop(obj)
    0120   end
    0121
    0122 % GET THE OLD DATA
    0123  remark=get(obj,'UserData');
    0124  flag_write=remark{1};  %Do I have to
    0125  buffer=remark{3};      %What is the old picture
    0126  flag_save=remark{7};   %Are we in saving mode?
    0127  fid=remark{8};         %What file descriptor to save
    0128
    0129  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0130  %IN CASE - DELETE/SAVE THE OLD DATA
    0131
    0132  if flag_write>20
    0133
    0134
    0135      % IN CASE WE HAVE TO SAVE - CLOSE THE OLD FILE AND MAKE A NEW
    0136      if flag_save>0
    0137         fclose(fid);
    0138         name_of_data=sprintf('%s-%d.dat','dat',(round(cputime*1000)));
    0139         fid=fopen(name_of_data,'w');
    0140     end
    0141
    0142       %DELETE OLD DATA
    0143      flag_write=1;
    0144      buffer=[];
    0145      remark{1}=flag_write; % SET THE POSITION OF THE READING SHIFT
    0146  end
    0147
    0148
    0149
    0150  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0151  % TAKE THE NEW DATA
    0152
    0153  samples_per_frame=get(obj,'SamplesAcquiredFcnCount');
    0154  data=(getdata(obj,samples_per_frame))';
    0155
    0156  % IN CASE - WRITE THE DATA
    0157  if flag_save>0
    0158      if fid==-1
    0159          name_of_data=sprintf('%s-%d.dat','dat',(round(cputime*1000)));
    0160          fid=fopen(name_of_data,'w');
    0161      end
    0162      fwrite(fid,(data*10000),'short');
    0163      remark{8}=fid;
    0164  end
    0165
    0166
    0167  % Add to buffer
    0168  buffer=[buffer data];
    0169  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    0170
    0171
    0172  remark{3}=buffer;
    0173  set(obj,'UserData',remark);
    0174
    0175
    0176 return
    0177
    0178
    0179 function stop_dev(obj,event)
    0180
    0181      remark=get(obj,'UserData');
    0182      if (remark{8}>0)                 %FID>0 == There is open file
    0183          fclose (remark{8});
    0184      end
    0185      close all
    0186
    0187      fprintf('\n\nThanks for using Erlich Real-Time scope\n');
    0188      save (remark{9},'remark');
    0189
    0190      delete(obj)
    0191      clear obj
    0192 return
    0193
    0194
    0195
    0196
    0197
    0198
    0199
    0200
    0201
    0202
    0203
    0204 function disply(obj,event)
    0205
    0206
    0207
    0208
    0209   sample_frequency=get(obj,'SampleRate');
    0210   remark=get(obj,'UserData');
    0211   refresh_frq=remark{5};
    0212   read_shift=remark{1};
    0213
    0214   ring=remark{2};
    0215   buffer=remark{3};
    0216
    0217
    0218   end_shift=min((read_shift+round(sample_frequency/refresh_frq)),length(buffer));
    0219   new_data=buffer(read_shift:end_shift);
    0220   ring=[ring new_data];
    0221   ring(1:length(new_data))=[];
    0222   remark{1}=end_shift;
    0223   remark{2}=ring;
    0224
    0225
    0226   start_display(ring,remark{4})
    0227
    0228   set(obj,'UserData',remark);
    0229 return
    0230
    0231