rtf.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. function rtf(plot_frq,flag_save)
  2. close all
  3. evalin('base','stop=0;');
  4. %=========SET THE BASIC FIGURE=================
  5. fig = figure('Position',[500,500,800,600],...
  6. 'NumberTitle','off',...
  7. 'Name','Scope',...
  8. 'doublebuffer','on',...
  9. 'HandleVisibility','on',...
  10. 'KeyPressFcn', @keypress, ...
  11. 'Renderer', 'openGL');
  12. %=============================================
  13. %=============OPEN THE DEVICE FOR RECORD======
  14. sample_frequency = 44100;
  15. samples_per_frame = 1024;
  16. %plot_frq=10;
  17. record_time=600;
  18. samples_to_acquire = record_time * sample_frequency;
  19. %PREPARE THE DEVICE
  20. ai = analoginput('winsound');
  21. chan = addchannel( ai, 1 );
  22. set( ai, 'SampleRate', sample_frequency )
  23. set( ai, 'SamplesPerTrigger', samples_to_acquire )
  24. set(ai, 'StopFcn', @stop_dev)
  25. sample_frequency = get( ai, 'SampleRate' );
  26. %SETTING CALL BACK FUNCTIONS:
  27. %The first for capture the
  28. %second for display
  29. set(ai, 'SamplesAcquiredFcnCount',samples_per_frame);
  30. set(ai, 'SamplesAcquiredFcn',@flag);
  31. set(ai, 'TimerPeriod',(1/plot_frq));
  32. set(ai, 'TimerFcn',@disply);
  33. %=============SAVE THE CONFIGURATION======
  34. plot_ref=plot(zeros(10,1));
  35. fid=-1;
  36. %SAVE THE CURRENT PARAMETERS:
  37. name_of_file=sprintf('%s-%d','real-anal',(round(sample_frequency/samples_per_frame)));
  38. remark={1,...
  39. zeros(samples_per_frame*20,1)',...
  40. 0,...
  41. plot_ref,...
  42. plot_frq,...
  43. cputime,...
  44. flag_save,...
  45. -1,...
  46. name_of_file
  47. };
  48. set(ai, 'UserData',remark)
  49. %=============START TO RECORD================
  50. fprintf ('To stop the program set <stop=1> or press q in the figure window\n');
  51. start (ai)
  52. %=============================================
  53. %=========THE MAIN PROGRAM====================
  54. %=============================================
  55. % *
  56. % ***
  57. % *****
  58. % ***
  59. % ***
  60. % ***
  61. % ***
  62. % ***
  63. % *****
  64. % ***
  65. % *
  66. %=============================================
  67. %==========CALLBACK FUNCTIONS=================
  68. %=============================================
  69. %=========Keypress callback===========
  70. function keypress(src, e)
  71. keypressed=get(gcf,'CurrentCharacter');
  72. % ignore raw control, shift, alt keys
  73. if keypressed
  74. % Quit
  75. if strcmp( keypressed, 'q')
  76. evalin('base','stop=1;');
  77. end
  78. end
  79. return
  80. %============FLAG FUNCTION===================
  81. %This function activated when we capture
  82. %certain amount of samples
  83. function flag(obj,event)
  84. % CHECK FOR STOP SIGNAL
  85. if evalin('base','stop')
  86. stop(obj)
  87. end
  88. % GET THE OLD DATA
  89. remark=get(obj,'UserData');
  90. flag_write=remark{1}; %Do I have to
  91. buffer=remark{3}; %What is the old picture
  92. flag_save=remark{7}; %Are we in saving mode?
  93. fid=remark{8}; %What file descriptor to save
  94. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  95. %IN CASE - DELETE/SAVE THE OLD DATA
  96. if flag_write>20
  97. % IN CASE WE HAVE TO SAVE - CLOSE THE OLD FILE AND MAKE A NEW
  98. if flag_save>0
  99. fclose(fid);
  100. name_of_data=sprintf('%s-%d.dat','dat',(round(cputime*1000)));
  101. fid=fopen(name_of_data,'w');
  102. end
  103. %DELETE OLD DATA
  104. flag_write=1;
  105. buffer=[];
  106. remark{1}=flag_write; % SET THE POSITION OF THE READING SHIFT
  107. end
  108. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  109. % TAKE THE NEW DATA
  110. samples_per_frame=get(obj,'SamplesAcquiredFcnCount');
  111. data=(getdata(obj,samples_per_frame))';
  112. % IN CASE - WRITE THE DATA
  113. if flag_save>0
  114. if fid==-1
  115. name_of_data=sprintf('%s-%d.dat','dat',(round(cputime*1000)));
  116. fid=fopen(name_of_data,'w');
  117. end
  118. fwrite(fid,(data*10000),'short');
  119. remark{8}=fid;
  120. end
  121. % Add to buffer
  122. buffer=[buffer data];
  123. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  124. remark{3}=buffer;
  125. set(obj,'UserData',remark);
  126. return
  127. function stop_dev(obj,event)
  128. remark=get(obj,'UserData');
  129. if (remark{8}>0) %FID>0 == There is open file
  130. fclose (remark{8});
  131. end
  132. close all
  133. fprintf('\n\nThanks for using Erlich Real-Time scope\n');
  134. save (remark{9},'remark');
  135. delete(obj)
  136. clear obj
  137. return
  138. function disply(obj,event)
  139. sample_frequency=get(obj,'SampleRate');
  140. remark=get(obj,'UserData');
  141. refresh_frq=remark{5};
  142. read_shift=remark{1};
  143. ring=remark{2};
  144. buffer=remark{3};
  145. end_shift=min((read_shift+round(sample_frequency/refresh_frq)),length(buffer));
  146. new_data=buffer(read_shift:end_shift);
  147. ring=[ring new_data];
  148. ring(1:length(new_data))=[];
  149. remark{1}=end_shift;
  150. remark{2}=ring;
  151. start_display(ring,remark{4})
  152. set(obj,'UserData',remark);
  153. return