Advertisement
BORUTO-121

realtime_signalaquizition&&signalgenrationGUI

Mar 15th, 2022 (edited)
242
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function varargout = Lab2(varargin)
  2. % LAB2 MATLAB code for Lab2.fig
  3. %      LAB2, by itself, creates a new LAB2 or raises the existing
  4. %      singleton*.
  5. %
  6. %      H = LAB2 returns the handle to a new LAB2 or the handle to
  7. %      the existing singleton*.
  8. %
  9. %      LAB2('CALLBACK',hObject,eventData,handles,...) calls the local
  10. %      function named CALLBACK in LAB2.M with the given input arguments.
  11. %
  12. %      LAB2('Property','Value',...) creates a new LAB2 or raises the
  13. %      existing singleton*.  Starting from the left, property value pairs are
  14. %      applied to the GUI before Lab2_OpeningFcn gets called.  An
  15. %      unrecognized property name or invalid value makes property application
  16. %      stop.  All inputs are passed to Lab2_OpeningFcn via varargin.
  17. %
  18. %      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
  19. %      instance to run (singleton)".
  20. %
  21. % See also: GUIDE, GUIDATA, GUIHANDLES
  22.  
  23. % Edit the above text to modify the response to help Lab2
  24.  
  25. % Last Modified by GUIDE v2.5 16-Mar-2022 21:28:07
  26.  
  27. % Begin initialization code - DO NOT EDIT
  28. gui_Singleton = 1;
  29. gui_State = struct('gui_Name',       mfilename, ...
  30.                    'gui_Singleton',  gui_Singleton, ...
  31.                    'gui_OpeningFcn', @Lab2_OpeningFcn, ...
  32.                    'gui_OutputFcn',  @Lab2_OutputFcn, ...
  33.                    'gui_LayoutFcn',  [] , ...
  34.                    'gui_Callback',   []);
  35. if nargin && ischar(varargin{1})
  36.     gui_State.gui_Callback = str2func(varargin{1});
  37. end
  38.  
  39. if nargout
  40.     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  41. else
  42.     gui_mainfcn(gui_State, varargin{:});
  43. end
  44. % End initialization code - DO NOT EDIT
  45.  
  46.  
  47.  
  48. % --- Executes just before Lab2 is made visible.
  49. function Lab2_OpeningFcn(hObject, eventdata, handles, varargin)
  50. % This function has no output args, see OutputFcn.
  51. % hObject    handle to figure
  52. % eventdata  reserved - to be defined in a future version of MATLAB
  53. % handles    structure with handles and user data (see GUIDATA)
  54. % varargin   command line arguments to Lab2 (see VARARGIN)
  55.  
  56. % Choose default command line output for Lab2
  57. handles.output = hObject;
  58.  
  59. % Update handles structure
  60. guidata(hObject, handles);
  61.  
  62. % UIWAIT makes Lab2 wait for user response (see UIRESUME)
  63. % uiwait(handles.figure1);
  64.  
  65.  
  66. % --- Outputs from this function are returned to the command line.
  67. function varargout = Lab2_OutputFcn(hObject, eventdata, handles)
  68. % varargout  cell array for returning output args (see VARARGOUT);
  69. % hObject    handle to figure
  70. % eventdata  reserved - to be defined in a future version of MATLAB
  71. % handles    structure with handles and user data (see GUIDATA)
  72.  
  73. % Get default command line output from handles structure
  74. varargout{1} = handles.output;
  75.  
  76.  
  77.  
  78. function edit2_Callback(hObject, eventdata, handles)
  79. % hObject    handle to edit2 (see GCBO)
  80. % eventdata  reserved - to be defined in a future version of MATLAB
  81. % handles    structure with handles and user data (see GUIDATA)
  82.  
  83. % Hints: get(hObject,'String') returns contents of edit2 as text
  84. %        str2double(get(hObject,'String')) returns contents of edit2 as a double
  85.  
  86.  
  87. % --- Executes during object creation, after setting all properties.
  88. function edit2_CreateFcn(hObject, eventdata, handles)
  89. % hObject    handle to edit2 (see GCBO)
  90. % eventdata  reserved - to be defined in a future version of MATLAB
  91. % handles    empty - handles not created until after all CreateFcns called
  92.  
  93. % Hint: edit controls usually have a white background on Windows.
  94. %       See ISPC and COMPUTER.
  95. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  96.     set(hObject,'BackgroundColor','white');
  97. end
  98.  
  99.  
  100.  
  101. function edit3_Callback(hObject, eventdata, handles)
  102. % hObject    handle to edit3 (see GCBO)
  103. % eventdata  reserved - to be defined in a future version of MATLAB
  104. % handles    structure with handles and user data (see GUIDATA)
  105.  
  106. % Hints: get(hObject,'String') returns contents of edit3 as text
  107. %        str2double(get(hObject,'String')) returns contents of edit3 as a double
  108.  
  109.  
  110. % --- Executes during object creation, after setting all properties.
  111. function edit3_CreateFcn(hObject, eventdata, handles)
  112. % hObject    handle to edit3 (see GCBO)
  113. % eventdata  reserved - to be defined in a future version of MATLAB
  114. % handles    empty - handles not created until after all CreateFcns called
  115.  
  116. % Hint: edit controls usually have a white background on Windows.
  117. %       See ISPC and COMPUTER.
  118. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  119.     set(hObject,'BackgroundColor','white');
  120. end
  121.  
  122.  
  123.  
  124. function edit1_Callback(hObject, eventdata, handles)
  125. % hObject    handle to edit1 (see GCBO)
  126. % eventdata  reserved - to be defined in a future version of MATLAB
  127. % handles    structure with handles and user data (see GUIDATA)
  128.  
  129. % Hints: get(hObject,'String') returns contents of edit1 as text
  130. %        str2double(get(hObject,'String')) returns contents of edit1 as a double
  131.  
  132.  
  133. % --- Executes during object creation, after setting all properties.
  134. function edit1_CreateFcn(hObject, eventdata, handles)
  135. % hObject    handle to edit1 (see GCBO)
  136. % eventdata  reserved - to be defined in a future version of MATLAB
  137. % handles    empty - handles not created until after all CreateFcns called
  138.  
  139. % Hint: edit controls usually have a white background on Windows.
  140. %       See ISPC and COMPUTER.
  141. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  142.     set(hObject,'BackgroundColor','white');
  143. end
  144.  
  145.  
  146.  
  147. function edit6_Callback(hObject, eventdata, handles)
  148. % hObject    handle to edit6 (see GCBO)
  149. % eventdata  reserved - to be defined in a future version of MATLAB
  150. % handles    structure with handles and user data (see GUIDATA)
  151.  
  152. % Hints: get(hObject,'String') returns contents of edit6 as text
  153. %        str2double(get(hObject,'String')) returns contents of edit6 as a double
  154.  
  155.  
  156. % --- Executes during object creation, after setting all properties.
  157. function edit6_CreateFcn(hObject, eventdata, handles)
  158. % hObject    handle to edit6 (see GCBO)
  159. % eventdata  reserved - to be defined in a future version of MATLAB
  160. % handles    empty - handles not created until after all CreateFcns called
  161.  
  162. % Hint: edit controls usually have a white background on Windows.
  163. %       See ISPC and COMPUTER.
  164. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  165.     set(hObject,'BackgroundColor','white');
  166. end
  167.  
  168.  
  169.  
  170. function edit7_Callback(hObject, eventdata, handles)
  171. % hObject    handle to edit7 (see GCBO)
  172. % eventdata  reserved - to be defined in a future version of MATLAB
  173. % handles    structure with handles and user data (see GUIDATA)
  174.  
  175. % Hints: get(hObject,'String') returns contents of edit7 as text
  176. %        str2double(get(hObject,'String')) returns contents of edit7 as a double
  177.  
  178.  
  179. % --- Executes during object creation, after setting all properties.
  180. function edit7_CreateFcn(hObject, eventdata, handles)
  181. % hObject    handle to edit7 (see GCBO)
  182. % eventdata  reserved - to be defined in a future version of MATLAB
  183. % handles    empty - handles not created until after all CreateFcns called
  184.  
  185. % Hint: edit controls usually have a white background on Windows.
  186. %       See ISPC and COMPUTER.
  187. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  188.     set(hObject,'BackgroundColor','white');
  189. end
  190.  
  191.  
  192. % --- Executes on button press in pushbutton1.
  193. function pushbutton7_Callback(hObject, eventdata, handles)
  194.  
  195. handles.StopAkvizicija=0;
  196. guidata(hObject,handles);
  197.  
  198. Kanal = str2num(get(handles.edit1,'String'))
  199. Trajanje = str2num(get(handles.edit3,'String'))
  200. sempl = str2num(get(handles.edit2,'String'))
  201. refresh=str2double(get(handles.edit8,'String'))
  202.  
  203. AI = analoginput('mcc',0);
  204. addchannel(AI,Kanal);
  205. % set(AI,'SampleRate',sempl);
  206. % set(AI,'SamplesPerTrigger',Trajanje*sempl);
  207. AI.SampleRate=sempl;
  208. AI.SamplesPerTrigger=AI.SampleRate*Trajanje;
  209. start(AI);
  210.  
  211. axes(handles.axes1);
  212. cla reset;
  213. grid on;
  214. xlabel('Vrijeme sec.');
  215. ylabel('Amplituda');
  216. title('Akvizicija signala');
  217. ylim([0 5]);%%PodeŇ°evat u oisnosti od ocekivanog ulaza
  218. hold on;
  219. i=0;%%vrijeme
  220. %isrunning(AI)
  221. while isrunning(AI) && i<=Trajanje && handles.StopAkvizicija==0
  222.     tic
  223.     data=peekdata(AI,fix(refresh*AI.SampleRate));
  224.     [m,~]=size(data);
  225.     t=linspace(i,i+refresh,m);
  226.    
  227.     if (i>=refresh)
  228.         xlim([i-refresh i]);
  229.     end
  230.    
  231.     plot(t',data);
  232.     time=toc;
  233.     i=i+refresh;
  234.     pause(refresh-time);
  235.     handles = guidata(hObject);
  236.     drawnow
  237. end
  238.  
  239. hold off
  240. delete AI
  241.  
  242.  
  243. % --- Executes on button press in pushbutton3.
  244. function pushbutton3_Callback(hObject, eventdata, handles)
  245.  
  246. handles.StopGenSignal = 0;
  247. guidata(hObject,handles);
  248.  
  249. amp=str2double(get(handles.edit7,'String'));
  250. frek=str2double(get(handles.edit6,'String'));
  251. Trajanje=str2double(get(handles.edit9,'String'));
  252.  
  253. %A0=analogoutput('mcc',0);%%Vjezba
  254. A0=analogoutput('winsound');%%Kuca
  255. addchannel(A0,1) % Definiranje kanala koji ?e se koristiti (mono)
  256. set(A0,'SampleRate',8000) % Postavljanje brzine uzorkovanja:
  257.  
  258. t=0:1/8000:Trajanje-1/8000;
  259. y=amp*sin(2*pi*frek.*t)+amp;
  260.  
  261. cla reset;
  262. axes(handles.axes2);
  263.  
  264. plot(t,y-amp);
  265. xlabel('Vrijeme [sec]');
  266. ylabel('Amplituda [V]');
  267. title('Generisani signal');
  268. hold on;
  269. grid on;
  270. putdata(A0,y');
  271. start(A0);
  272. tic;
  273. time=0;
  274. while toc<Trajanje && handles.StopGenSignal==0
  275.     xlim([time toc]);%%Pomjeranje ose
  276.     time=toc;
  277.     handles = guidata(hObject);%%Update
  278.     pause(0.1);%%Simbolicna pauza da mozemo osvjeziti stopgensignal
  279. end
  280. stop(A0);
  281.    
  282.    
  283.  
  284. function pushbutton5_Callback(hObject, eventdata, handles)
  285.  handles.StopGenSignal = 1;
  286.  guidata(hObject,handles);
  287.  pause(0.1);
  288.  
  289. % hObject    handle to pushbutton5 (see GCBO)
  290. % eventdata  reserved - to be defined in a future version of MATLAB
  291. % handles    structure with handles and user data (see GUIDATA)
  292.  
  293.  
  294. % --- Executes on button press in pushbutton6.
  295. function pushbutton6_Callback(hObject, eventdata, handles)
  296. handles.StopAkvizicija=1;
  297. guidata(hObject,handles);
  298. % hObject    handle to pushbutton6 (see GCBO)
  299. % eventdata  reserved - to be defined in a future version of MATLAB
  300. % handles    structure with handles and user data (see GUIDATA)
  301.  
  302.  
  303.  
  304. function edit8_Callback(hObject, eventdata, handles)
  305. % hObject    handle to edit8 (see GCBO)
  306. % eventdata  reserved - to be defined in a future version of MATLAB
  307. % handles    structure with handles and user data (see GUIDATA)
  308.  
  309. % Hints: get(hObject,'String') returns contents of edit8 as text
  310. %        str2double(get(hObject,'String')) returns contents of edit8 as a double
  311.  
  312.  
  313. % --- Executes during object creation, after setting all properties.
  314. function edit8_CreateFcn(hObject, eventdata, handles)
  315. % hObject    handle to edit8 (see GCBO)
  316. % eventdata  reserved - to be defined in a future version of MATLAB
  317. % handles    empty - handles not created until after all CreateFcns called
  318.  
  319. % Hint: edit controls usually have a white background on Windows.
  320. %       See ISPC and COMPUTER.
  321. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  322.     set(hObject,'BackgroundColor','white');
  323. end
  324.  
  325.  
  326.  
  327. function edit9_Callback(hObject, eventdata, handles)
  328. % hObject    handle to edit9 (see GCBO)
  329. % eventdata  reserved - to be defined in a future version of MATLAB
  330. % handles    structure with handles and user data (see GUIDATA)
  331.  
  332. % Hints: get(hObject,'String') returns contents of edit9 as text
  333. %        str2double(get(hObject,'String')) returns contents of edit9 as a double
  334.  
  335.  
  336. % --- Executes during object creation, after setting all properties.
  337. function edit9_CreateFcn(hObject, eventdata, handles)
  338. % hObject    handle to edit9 (see GCBO)
  339. % eventdata  reserved - to be defined in a future version of MATLAB
  340. % handles    empty - handles not created until after all CreateFcns called
  341.  
  342. % Hint: edit controls usually have a white background on Windows.
  343. %       See ISPC and COMPUTER.
  344. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  345.     set(hObject,'BackgroundColor','white');
  346. end
  347.  
Advertisement
RAW Paste Data Copied
Advertisement