wave_browser.m 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. function varargout = wave_browser(varargin)
  2. % WAVE_BROWSER M-file for wave_browser.fig
  3. % WAVE_BROWSER, by itself, creates a new WAVE_BROWSER or raises the existing
  4. % singleton*.
  5. %
  6. % H = WAVE_BROWSER returns the handle to a new WAVE_BROWSER or the handle to
  7. % the existing singleton*.
  8. %
  9. % WAVE_BROWSER('CALLBACK',hObject,eventData,handles,...) calls the local
  10. % function named CALLBACK in WAVE_BROWSER.M with the given input arguments.
  11. %
  12. % WAVE_BROWSER('Property','Value',...) creates a new WAVE_BROWSER or raises the
  13. % existing singleton*. Starting from the left, property value pairs are
  14. % applied to the GUI before wave_browser_OpeningFunction gets called. An
  15. % unrecognized property name or invalid value makes property application
  16. % stop. All inputs are passed to wave_browser_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. % Copyright 2002-2003 The MathWorks, Inc.
  23. % Edit the above text to modify the response to help wave_browser
  24. % Last Modified by GUIDE v2.5 29-May-2007 16:30:52
  25. % Begin initialization code - DO NOT EDIT
  26. gui_Singleton = 1;
  27. gui_State = struct('gui_Name', mfilename, ...
  28. 'gui_Singleton', gui_Singleton, ...
  29. 'gui_OpeningFcn', @wave_browser_OpeningFcn, ...
  30. 'gui_OutputFcn', @wave_browser_OutputFcn, ...
  31. 'gui_LayoutFcn', [] , ...
  32. 'gui_Callback', []);
  33. if nargin && ischar(varargin{1})
  34. gui_State.gui_Callback = str2func(varargin{1});
  35. end
  36. if nargout
  37. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  38. else
  39. gui_mainfcn(gui_State, varargin{:});
  40. end
  41. % End initialization code - DO NOT EDIT
  42. % --- Executes just before wave_browser is made visible.
  43. function wave_browser_OpeningFcn(hObject, eventdata, handles, varargin)
  44. % This function has no output args, see OutputFcn.
  45. % hObject handle to figure
  46. % eventdata reserved - to be defined in a future version of MATLAB
  47. % handles structure with handles and user data (see GUIDATA)
  48. % varargin command line arguments to wave_browser (see VARARGIN)
  49. handles.firsttime = 0; % indicates the firsttime that segment has been precomputed
  50. handles.precomputed_spec = 0; % indicates that the spectra has not been precomputed
  51. handles.longfile = 0; % indicates whether the file is a long file
  52. handles.maxwavsize = 10 * 44100; % I will have to explore what number works best here
  53. handles.maxspec_t = 30; % duration of the max size of a spectra
  54. handles.Fs = 44100; % default size to start with
  55. handles.segments = []; % holds regular segments in the current chunk
  56. handles.allsegments = []; % holds segments across the maximum wave size
  57. handles.loadedsegment = 0; % indicates no segments have been loaded
  58. handles.lastmarkerstart = 1; % largest segment
  59. handles.segmentmode = 0; % by default start off with segmenting turned off
  60. handles.dontcutsegments = 0; % by default do not adapt to segments
  61. handles.automethod = 'threshold'; % use threshold or ratiof method
  62. handles.indexthresh = 10; % for ration method the threshold which to cut the curve off
  63. handles.lower_range = [10 10000]; % the numerator in the ratio
  64. handles.upper_range = [15000 20000]; % the denomitor in the ratio
  65. handles.nsmooth = 0; % moving average parameter for the thresholds curves
  66. positionP = get(handles.OptionsUiPanel,'Position');
  67. positionF = get(gcf,'Position');
  68. positionF(3) = positionF(3) - positionP(3);
  69. % set(gcf,'Position',positionF); % untested
  70. % Choose default command line output for wave_browser
  71. handles.output = hObject;
  72. % Update handles structure
  73. guidata(hObject, handles);
  74. % UIWAIT makes wave_browser wait for user response (see UIRESUME)
  75. % uiwait(handles.figure1);
  76. % --- Outputs from this function are returned to the command line.
  77. function varargout = wave_browser_OutputFcn(hObject, eventdata, handles)
  78. % Get default command line output from handles structure
  79. varargout{1} = handles.output;
  80. function Frequency_Callback(hObject, eventdata, handles)
  81. handles.Fs = eval(get(hObject,'String'));
  82. guidata(gcbo,handles);
  83. function Frequency_CreateFcn(hObject, eventdata, handles)
  84. set(hObject,'String', '44100');
  85. handles.Fs = 44100;
  86. guidata(gcbo,handles);
  87. if ispc
  88. set(hObject,'BackgroundColor','white');
  89. else
  90. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  91. end
  92. % --- Executes on button press in LoadFile.
  93. function LoadFile_Callback(hObject, eventdata, handles)
  94. [fname pname]=uigetfile({'*.wav';'*.*'},'Load Time Series');
  95. if fname == 0
  96. return
  97. end
  98. set(handles.FileNameString, 'String',fname);
  99. handles.filename = [pname fname];
  100. [path] = cell2mat(regexp( handles.filename, '^.*\', 'match' ));
  101. [extension] = cell2mat(regexp( handles.filename, '\w+$', 'match' ));
  102. set(handles.Path,'String',path);
  103. % set(handles.Extensions,'String',extension);
  104. handles.segments = [];
  105. handles.allsegments = [];
  106. handles = loadfile(hObject, eventdata, handles);
  107. guidata(hObject,handles);
  108. function [wavesize channels] = wavsizeget(filename);
  109. % Provides usable information about a file
  110. wavesize = 0;
  111. [filestatus info] = wavfinfo(filename);
  112. info = regexp(info,'[0-9]+','match');
  113. channels = str2num(info{2});
  114. wavesize = str2num(info{1});
  115. function handles = loadfile(hObject, eventdata, handles, varargin)
  116. % Function for loading a file or using the optional varargin to load a
  117. % specified position and size in the file
  118. % contents=get(handles.endian,'String');
  119. % precision=contents{get(handles.endian,'Value')};
  120. [datasize channels] = wavsizeget(handles.filename);
  121. handles.wavsize = datasize; % total number of samples in the files
  122. try
  123. handles.maxwavsize = round(handles.Fs * str2num(get(handles.MaximumWavSize,'String')));
  124. catch
  125. handles.maxwavsize = 20;
  126. set(handles.MaximumWavSize,'String',num2str(handles.maxwavsize));
  127. handles.maxwavsize = handles.maxwavsize * handles.Fs;
  128. end
  129. if isempty(varargin)
  130. handles.markerstart = 1;
  131. if datasize > handles.maxwavsize
  132. handles.markerend = handles.maxwavsize;
  133. handles.longfile = 1; % indicates that the file is long and will be loaded in chunks
  134. else
  135. handles.markerend = datasize;
  136. end
  137. else % passed in optional parameter
  138. handles.markervec = varargin{1};
  139. handles.markerstart = handles.markervec(1);
  140. handles.markerend = handles.markervec(2);
  141. end
  142. if handles.markerstart <= 1 % make sure the range is possible
  143. handles.markerstart = 1;
  144. set(handles.PreviousChunk,'Enable','off');
  145. else
  146. set(handles.PreviousChunk,'Enable','on');
  147. end
  148. if handles.markerend >= handles.wavsize
  149. handles.markerend = handles.wavsize;
  150. set(handles.NextChunk,'Enable','off');
  151. else
  152. set(handles.NextChunk,'Enable','on');
  153. end
  154. if handles.maxwavsize < handles.wavsize
  155. total_chunk = ceil(handles.wavsize / handles.maxwavsize);
  156. i = 1;
  157. while (i < total_chunk) && (handles.markerend >= (handles.maxwavsize * i))
  158. i = i + 1;
  159. end
  160. current_chunk = i-1;
  161. if handles.markerend == handles.wavsize
  162. current_chunk = total_chunk;
  163. end
  164. set(handles.ChunkText,'String',['Chunk ' num2str(current_chunk) '/' num2str(total_chunk)]);
  165. end
  166. try
  167. handles.maxseglength = round(handles.Fs * str2num(get(handles.MaxSegLength,'String')));
  168. catch
  169. handles.maxseglength = handles.Fs * 1;
  170. end
  171. set(handles.RealDuration,'String',num2str(handles.wavsize/handles.Fs,'%.1f'));
  172. if handles.segmentmode % only if in segment mode make sure segments are not cut
  173. if (handles.markerstart - handles.lastmarkerstart > 0) % only do this in terms of forward movement
  174. if handles.dontcutsegments % this code is added so segments are not cut off when segmenting
  175. if not(isempty(handles.segments)) % at least one segment has been defined previously
  176. maxsegend = handles.segments(1).end; % find last defined segment in previous view
  177. for i = 2:length(handles.segments)
  178. if handles.segments(i).end > maxsegend
  179. maxsegend = handles.segments(i).end;
  180. end
  181. end
  182. maxsegend = round(maxsegend * handles.Fs);
  183. if (handles.lastmarkerend - maxsegend) < (handles.lastmarkerend - handles.maxseglength)
  184. handles.markerstart = (handles.lastmarkerstart + maxsegend) + 1; % defined segment is closer to the end
  185. else
  186. handles.markerstart = handles.lastmarkerend - handles.maxseglength;
  187. end
  188. else
  189. handles.markerstart = handles.lastmarkerend - handles.maxseglength;
  190. end
  191. handles.markerend = handles.markerstart + handles.maxwavsize - 1;
  192. if handles.markerend > handles.wavsize
  193. handles.markerend = handles.wavsize;
  194. end
  195. end
  196. end
  197. end
  198. hw=waitbar(0,'Loading ...'); waitbar(0.5,hw); drawnow;
  199. [handles.markerstart handles.markerend]/handles.Fs
  200. [handles.ts,handles.Fs] = wavread(handles.filename, [handles.markerstart handles.markerend]);
  201. channel = str2double(get(handles.channel,'String'));
  202. handles.ts = handles.ts(:,channel);
  203. count = length(handles.ts);
  204. handles.ts = handles.ts/std(handles.ts); % variance normalisation
  205. set(handles.Frequency,'String', num2str(handles.Fs));
  206. set( handles.Duration, 'String', count/handles.Fs );
  207. Tim=eval(get(handles.DisplayWindow,'String'));
  208. display_frac = 1;%max(1,Tim*handles.Fs/count);
  209. set( handles.slider1, 'Value', 0 );
  210. set( handles.SegmentButton, 'Enable', 'on' );
  211. if handles.longfile
  212. set(handles.SeekButton,'Enable', 'on');
  213. end
  214. set(handles.LoadNext, 'Enable', 'on' );
  215. set(handles.PlayAll, 'Enable', 'on' );
  216. set(handles.PlayWindow, 'Enable', 'on' );
  217. set(handles.Plot, 'Enable', 'on' );
  218. set(handles.PlotAllButton, 'Enable', 'on');
  219. set(handles.Precompute, 'Enable','on');
  220. set(handles.Jump,'Enable','on');
  221. set(handles.JumpBack,'Enable','on');
  222. handles.segments = []; % remove the current segments
  223. handles.segments = filtersegments(handles,handles.allsegments);
  224. set(handles.Precompute,'Value',1); % Set into precompute mode
  225. Precompute_Callback(handles.Precompute, eventdata, handles);
  226. handles = guidata(gcbo);
  227. % set(handles.Precompute,'Value',1);
  228. handles.precomputed_spec = 1;
  229. handles.dontcutsegments = 1; % make sure segments are not cut off
  230. handles.lastmarkerstart = handles.markerstart;
  231. handles.lastmarkerend = handles.markerend;
  232. close(hw);
  233. % guidata(gcbo,handles);
  234. % Plot_Callback(hObject, eventdata, handles);
  235. % --- Executes on selection change in endian.
  236. function endian_Callback(hObject, eventdata, handles)
  237. % --- Executes during object creation, after setting all properties.
  238. function endian_CreateFcn(hObject, eventdata, handles)
  239. if ispc
  240. set(hObject,'BackgroundColor','white');
  241. else
  242. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  243. end
  244. function edit2_Callback(hObject, eventdata, handles)
  245. function edit2_CreateFcn(hObject, eventdata, handles)
  246. if ispc
  247. set(hObject,'BackgroundColor','white');
  248. else
  249. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  250. end
  251. function FileNameString_Callback(hObject, eventdata, handles)
  252. function FileNameString_CreateFcn(hObject, eventdata, handles)
  253. if ispc
  254. set(hObject,'BackgroundColor','white');
  255. else
  256. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  257. end
  258. function WinSize_Callback(hObject, eventdata, handles)
  259. function WinSize_CreateFcn(hObject, eventdata, handles)
  260. if ispc
  261. set(hObject,'BackgroundColor','white');
  262. else
  263. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  264. end
  265. % --- Executes on slider movement.
  266. function slider1_Callback(hObject, eventdata, handles)
  267. function slider1_CreateFcn(hObject, eventdata, handles)
  268. usewhitebg = 1;
  269. if usewhitebg
  270. set(hObject,'BackgroundColor',[.9 .9 .9]);
  271. else
  272. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  273. end
  274. function StepSize_Callback(hObject, eventdata, handles)
  275. function StepSize_CreateFcn(hObject, eventdata, handles)
  276. if ispc
  277. set(hObject,'BackgroundColor','white');
  278. else
  279. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  280. end
  281. function TW_Callback(hObject, eventdata, handles)
  282. function TW_CreateFcn(hObject, eventdata, handles)
  283. if ispc
  284. set(hObject,'BackgroundColor','white');
  285. else
  286. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  287. end
  288. function DisplayWindow_Callback(hObject, eventdata, handles)
  289. function DisplayWindow_CreateFcn(hObject, eventdata, handles)
  290. set(hObject, 'String', '4');
  291. if ispc
  292. set(hObject,'BackgroundColor','white');
  293. else
  294. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  295. end
  296. %function axes2ButtonDownCallback(hObject, eventdata, handles)
  297. %h=handles.axesS; P=get(h,'CurrentPoint');
  298. %fprintf( 'worked %f %f!\n', P(1),P(2));
  299. function indexinS = getindexpre_c(t,timestart,timeend)
  300. % A function for indexing correctly in time to a spectra stored in memory
  301. tlen = length(t);
  302. i=1;
  303. while (i <= tlen) && (t(i) < timestart)
  304. i = i + 1;
  305. end
  306. firstindex = i;
  307. while (i <= tlen) && (t(i) < timeend)
  308. i = i + 1;
  309. end
  310. secondindex = i;
  311. indexinS = [firstindex secondindex];
  312. % --- Executes on button press in Plot.
  313. function Plot_Callback(hObject, eventdata, handles)
  314. hw=waitbar(0.5,'Spectrogram calculation');drawnow
  315. params.Fs=handles.Fs;
  316. window=eval(get(handles.WinSize,'String'));
  317. winstep=eval(get(handles.StepSize,'String'));
  318. movingwin=[window winstep]*0.001;
  319. fmin=eval(get(handles.MinFreq,'String'));
  320. fmax=eval(get(handles.MaxFreq,'String'));
  321. params.fpass=[fmin fmax];
  322. p=eval(get(handles.TW,'String'));
  323. params.tapers=[p floor(2*p-1)];
  324. params.pad=1;
  325. Tslider=get(handles.slider1,'Value');
  326. Tim=eval(get(handles.DisplayWindow,'String'));
  327. NT=min(round(Tim*handles.Fs),length(handles.ts));
  328. handles.Tmin=1+floor(Tslider*length(handles.ts));
  329. handles.Tmax=min(handles.Tmin+NT,length(handles.ts));
  330. if handles.Tmax < length(handles.ts)
  331. set( handles.Jump, 'Enable', 'on' );
  332. else
  333. set( handles.Jump, 'Enable', 'off' );
  334. end
  335. if handles.Tmin > 1
  336. set( handles.JumpBack, 'Enable', 'on' );
  337. else
  338. set( handles.JumpBack, 'Enable', 'off' );
  339. end
  340. data=handles.ts(handles.Tmin:handles.Tmax);data=data(:);
  341. handles.upper_range = eval(get(handles.RatioLower,'String'));
  342. handles.lower_range = eval(get(handles.RatioUpper,'String'));
  343. handles.indexthresh = eval(get(handles.RatioThresh,'String'));
  344. handles.nsmooth = eval(get(handles.SmoothFactor,'String'));
  345. % determine spectrum type
  346. contents=get(handles.SpectrumType,'String');
  347. stype=contents{get(handles.SpectrumType,'Value')};
  348. axes(handles.axesW); plot(((handles.markerstart - 1)/handles.Fs) + [handles.Tmin:handles.Tmax]/handles.Fs,handles.ts(handles.Tmin:handles.Tmax)); axis tight;
  349. switch stype
  350. case 'Original'
  351. if not(handles.precomputed_spec) || handles.firsttime
  352. [S,t,f]=mtspecgramc(diff(data),movingwin,params);
  353. timeax=(handles.Tmin/handles.Fs)+t;
  354. else
  355. indexinS = getindexpre_c(handles.t,(handles.Tmin-1)/handles.Fs,(handles.Tmax-1)/handles.Fs);
  356. % indexinS = round(([handles.Tmin-1, handles.Tmax-1]/handles.Fs)/movingwin(2))+1;
  357. if indexinS(1) < 1
  358. indexinS(1) = 1;
  359. end
  360. SLen = length(handles.S(:,1));
  361. if indexinS(2) > SLen
  362. indexinS(2) = SLen;
  363. end
  364. f = handles.f;
  365. t = handles.t(indexinS(1):indexinS(2));
  366. S = handles.S(indexinS(1):indexinS(2),:);
  367. timeax=t;
  368. end
  369. cmap='default';
  370. th=eval(get(handles.AmpThresh,'String'));
  371. % This sets up the automatic segmenting algorithm
  372. if strcmp(handles.automethod,'threshold')
  373. [Stot boxcurve] = compute_threshold_free(S,th,handles.nsmooth);
  374. axes(handles.axesP);
  375. semilogy(timeax,Stot);
  376. axis tight;
  377. elseif strcmp(handles.automethod,'ratiof')
  378. [ratiof boxcurve] = compute_index(S,handles.lower_range,handles.upper_range,fmin,fmax,handles.indexthresh,handles.nsmooth);
  379. axes(handles.axesP);
  380. semilogy(timeax,ratiof);
  381. axis tight;
  382. end
  383. hold on; semilogy(timeax,boxcurve,'r'); hold off;
  384. axes(handles.axesS);
  385. imagesc(timeax,f,log(S)'); axis xy; colormap(cmap);
  386. %imagesc(t,f,log(S)'); axis xy; colormap(cmap);
  387. % set(h,'ButtonDownFcn',axes2ButtonDownCallback);
  388. case 'Time Derivative'
  389. if not(handles.precomputed_spec) || handles.firsttime
  390. [S,t,f]=mtdspecgramc(diff(data),movingwin,0,params);S = S';
  391. timeax=handles.Tmin/handles.Fs+t;
  392. else
  393. indexinS = getindexpre_c(handles.t,(handles.Tmin-1)/handles.Fs,(handles.Tmax-1)/handles.Fs);
  394. % indexinS = round(([handles.Tmin-1, handles.Tmax-1]/handles.Fs)/movingwin(2))+1;
  395. if indexinS(1) < 1
  396. indexinS(1) = 1;
  397. end
  398. SLen = length(handles.S(1,:));
  399. if indexinS(2) > SLen
  400. indexinS(2) = SLen;
  401. end
  402. f = handles.f;
  403. t = handles.t(indexinS(1):indexinS(2));
  404. S = handles.S(:,indexinS(1):indexinS(2));
  405. timeax = t;
  406. end
  407. cmap='gray';
  408. th=eval(get(handles.TDerThresh,'String'));
  409. if strcmp(handles.automethod,'threshold')
  410. [Stot boxcurve] = compute_threshold_free(abs(S'),th.handles.nsmooth);
  411. axes(handles.axesP);
  412. semilogy(timeax,Stot);
  413. axis tight;
  414. elseif strcmp(handles.automethod,'ratiof')
  415. [ratiof boxcurve] = compute_index(abs(S)',handles.lower_range,handles.upper_range,fmin,fmax,handles.indexthresh,handles.nsmooth);
  416. axes(handles.axesP);
  417. semilogy(timeax,ratiof);
  418. axis tight;
  419. end
  420. hold on; semilogy(timeax,boxcurve,'r'); hold off;
  421. axes(handles.axesS);
  422. imagesc(timeax,f,S); axis xy; colormap(cmap);
  423. cmin=0.02*min(min(S)); cmax=0.02*max(max(S)); caxis([cmin cmax]);
  424. case 'Frequency Derivative'
  425. if not(handles.precomputed_spec) || handles.firsttime
  426. [S,t,f]=mtdspecgramc(diff(data),movingwin,pi/2,params);S=S';
  427. timeax=handles.Tmin/handles.Fs+t;
  428. else
  429. indexinS = getindexpre_c(handles.t,(handles.Tmin-1)/handles.Fs,(handles.Tmax-1)/handles.Fs);
  430. if indexinS(1) < 1
  431. indexinS(1) = 1;
  432. end
  433. SLen = length(handles.S(1,:));
  434. if indexinS(2) > SLen
  435. indexinS(2) = SLen;
  436. end
  437. f = handles.f;
  438. t = handles.t(indexinS(1):indexinS(2));
  439. S = handles.S(:,indexinS(1):indexinS(2));
  440. timeax = t;
  441. end
  442. cmap='gray';
  443. th=eval(get(handles.TDerThresh,'String'));
  444. if strcmp(handles.automethod,'threshold')
  445. [Stot boxcurve] = compute_threshold_free(abs(S'),th,handles.nsmooth);
  446. axes(handles.axesP);
  447. semilogy(timeax,Stot);
  448. axis tight;
  449. elseif strcmp(handles.automethod,'ratiof')
  450. [ratiof boxcurve] = compute_index(abs(S)',handles.lower_range,handles.upper_range,fmin,fmax,handles.indexthresh,handles.nsmooth);
  451. axes(handles.axesP);
  452. semilogy(timeax,ratiof);
  453. axis tight;
  454. end
  455. hold on; semilogy(timeax,boxcurve,'r'); hold off;
  456. axes(handles.axesS);
  457. imagesc(timeax,f,S); axis xy; colormap(cmap);
  458. cmin=0.02*min(min(S)); cmax=0.02*max(max(S)); caxis([cmin cmax]);
  459. end;
  460. if handles.firsttime % first time precomputing the spectra
  461. handles.S = S;
  462. handles.t = t;
  463. handles.f = f;
  464. handles.precomputed_spec = 1;
  465. handles.firstime = 0;
  466. end
  467. % S = log(S)';
  468. % Smax = max(max(S));
  469. % Smin = min(min(S));
  470. % Ssmall = uint8(round(((S - Smin)/(Smax-Smin))*255));
  471. %
  472. % save('uint8_test.mat','Ssmall','-mat');
  473. % save('full_rest.mat','S','-mat');
  474. handles.times=timeax(:);
  475. handles.transition=[diff(boxcurve(:)); 0];
  476. set( handles.axesS, 'XTick', [] );
  477. set( handles.axesP, 'XTick', [] );
  478. if exist('handles.datacursor')
  479. delete( handles.datacursor );
  480. delete( handles.segmentLineP );
  481. delete( handles.segmentLineS );
  482. delete( handles.segmentLineW );
  483. end
  484. handles.datacursor=datacursormode(handles.figure1);
  485. axes(handles.axesP);
  486. handles.segmentLineP = line('Visible','off');
  487. axes(handles.axesS);
  488. handles.segmentLineS = line('Visible','off');
  489. axes(handles.axesW);
  490. handles.segmentLineW = line('Visible','off');
  491. if get( handles.SegmentButton, 'Value' )
  492. set(handles.datacursor,'Enable','on','DisplayStyle','datatip','SnapToDataVertex','off','UpdateFcn',@datacursorfunc);
  493. end
  494. guidata(gcbo,handles);
  495. close(hw);
  496. handles = draw_segments(handles);
  497. function [Stot boxcurve] = compute_threshold_free(S,th,n)
  498. % Computes the threshold based on a floating percentage of the maximum
  499. % summed intensity
  500. Stot=sum(S,2);
  501. boxcurve=Stot;
  502. smax=max(Stot);
  503. Stot = smooth_curve(Stot',n); % for removing extremes
  504. boxcurve(find(Stot<th*smax))= smax*th;
  505. boxcurve(find(Stot>th*smax))= smax;
  506. function [ratiof boxcurve] = compute_index(S,lower_range,upper_range,lowerfreq,upperfreq,indexthresh,n)
  507. % This algorithm is based on the method described in Aylin's
  508. % dissertation.
  509. S = S';
  510. nfreqs = length(S(:,1));
  511. freqspern = (upperfreq - lowerfreq) / nfreqs;
  512. indexinlower = fliplr(nfreqs - round((lower_range - lowerfreq)/freqspern));
  513. indexinupper = fliplr(nfreqs - round((upper_range - lowerfreq)/freqspern)) + 1;
  514. nrangelower = indexinlower(2)-indexinlower(1);
  515. nrangeupper = indexinupper(2)-indexinupper(1);
  516. ratiof = ( sum(S(indexinupper(1) : indexinupper(2),:)) / nrangeupper )...
  517. ./ ( sum(S( indexinlower(1) : indexinlower(2),:)) / nrangelower );
  518. ratiof = smooth_curve(ratiof,n); % for smoothing the curve
  519. maxrf = max(ratiof);
  520. boxcurve = ratiof;
  521. boxcurve(find(ratiof<indexthresh))= indexthresh;
  522. boxcurve(find(ratiof>=indexthresh))= maxrf;
  523. function smoothedcurve = smooth_curve(curve2smooth,n);
  524. % Computes the moving average of the curve where n is an integer
  525. % for example n = 1 averages the current point with the point before and afterwards
  526. m = length(curve2smooth);
  527. if m > 0
  528. curve2smooth = [repmat(curve2smooth(1),1,n) curve2smooth repmat(curve2smooth(m),1,n)];
  529. smoothedcurve = zeros(m,1);
  530. for i = 1:m
  531. smoothedcurve(i) = sum(curve2smooth(i:i + 2 * n)) / (2 * n + 1);
  532. end
  533. else % just to save computation time
  534. smoothed_curve = curve2smooth;
  535. end
  536. function MinFreq_Callback(hObject, eventdata, handles)
  537. function MinFreq_CreateFcn(hObject, eventdata, handles)
  538. if ispc
  539. set(hObject,'BackgroundColor','white');
  540. else
  541. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  542. end
  543. function MaxFreq_Callback(hObject, eventdata, handles)
  544. function MaxFreq_CreateFcn(hObject, eventdata, handles)
  545. if ispc
  546. set(hObject,'BackgroundColor','white');
  547. else
  548. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  549. end
  550. % --- Executes on selection change in SpectrumType.
  551. function SpectrumType_Callback(hObject, eventdata, handles)
  552. function SpectrumType_CreateFcn(hObject, eventdata, handles)
  553. if ispc
  554. set(hObject,'BackgroundColor','white');
  555. else
  556. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  557. end
  558. function AmpThresh_Callback(hObject, eventdata, handles)
  559. function AmpThresh_CreateFcn(hObject, eventdata, handles)
  560. if ispc
  561. set(hObject,'BackgroundColor','white');
  562. else
  563. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  564. end
  565. function TDerThresh_Callback(hObject, eventdata, handles)
  566. % --- Executes during object creation, after setting all properties.
  567. function TDerThresh_CreateFcn(hObject, eventdata, handles)
  568. if ispc
  569. set(hObject,'BackgroundColor','white');
  570. else
  571. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  572. end
  573. % --- Executes on button press in PlayAll.
  574. function PlayAll_Callback(hObject, eventdata, handles)
  575. wavplay(handles.ts,handles.Fs);
  576. % --- Executes on button press in PlayWindow.
  577. function PlayWindow_Callback(hObject, eventdata, handles)
  578. wavplay(handles.ts(handles.Tmin:handles.Tmax),handles.Fs,'async');
  579. %h=handles.axesP; axes(h); semilogy(timeax,Stot); axis tight;
  580. function txt = datacursorfunc(empt,event_obj)
  581. pos = get(event_obj,'Position');
  582. handles=guidata(get(event_obj,'Target'));
  583. set(handles.segmentLineP,'Xdata',[pos(1) pos(1)],'Ydata',[0.00000000000001*pos(2) 1000000000000*pos(2)],'Visible','on' );
  584. set(handles.segmentLineS,'Xdata',[pos(1) pos(1)],'Ydata',[0.00000000000001*pos(2) 1000000000000*pos(2)],'Visible','on' );
  585. set(handles.segmentLineW,'Xdata',[pos(1) pos(1)],'Ydata',[-100000000000*pos(2) 1000000000000*pos(2)],'Visible','on' );
  586. if handles.start_stop_enable == 1
  587. set( handles.SegStartButton, 'Enable', 'on' );
  588. else
  589. set( handles.SegEndButton, 'Enable', 'on' );
  590. end
  591. txt = {[num2str(pos(1))]};
  592. guidata(gcbo,handles);
  593. function handles = draw_segments( handles )
  594. n = 1;
  595. while n <= length( handles.segments )
  596. handles.segments(n).lines=[];
  597. handles.segments(n) = draw_all_x( handles, handles.segments(n) );
  598. n = n + 1;
  599. end
  600. guidata(gcbo,handles);
  601. % --- Executes on button press in SegmentButton.
  602. function SegmentButton_Callback(hObject, eventdata, handles)
  603. toggled = get( handles.SegmentButton, 'Value' );
  604. if toggled
  605. handles.segments = [];
  606. handles.segmentmode = 1;
  607. set( handles.SegmentButton, 'String', 'Segment On' );
  608. set( handles.SegmentButton, 'Enable', 'off' );
  609. if not(exist([handles.filename '.seg.txt']));
  610. set( handles.LoadSegments, 'Enable', 'off' );
  611. else
  612. set( handles.LoadSegments, 'Enable', 'on' );
  613. end
  614. set( handles.AutoSegmentFile, 'Enable','on');
  615. set( handles.AutoSegButton, 'Enable', 'on' );
  616. set( handles.SegmentLengthEdit, 'Enable', 'on' );
  617. set( handles.SegmentLengthText, 'Enable', 'on ' );
  618. set( handles.SaveSegments, 'Enable', 'on' );
  619. set( handles.DeleteSegment, 'Enable', 'on' );
  620. set( handles.DeleteAllButton, 'Enable', 'on' );
  621. set( handles.SegCancel, 'Enable', 'on' );
  622. set( handles.PlotSegments, 'Enable', 'on' );
  623. set( handles.LoadFile, 'Enable', 'off' );
  624. set( handles.LoadNext, 'Enable', 'off' );
  625. handles.start_stop_enable = 1;
  626. set(handles.datacursor,'Enable','on','DisplayStyle','datatip','SnapToDataVertex','off','UpdateFcn',@datacursorfunc);
  627. fprintf( 'Segment mode on!\n' );
  628. else
  629. handles.segmentmode = 0;
  630. set( handles.SegmentButton, 'String', 'Segment Off' );
  631. set( handles.AutoSegButton, 'Enable', 'off' );
  632. set( handles.AutoSegmentFile, 'Enable','off');
  633. set( handles.SegmentLengthEdit, 'Enable', 'off' );
  634. set( handles.SegmentLengthText, 'Enable', 'off' );
  635. set( handles.LoadSegments, 'Enable', 'off' );
  636. set( handles.SaveSegments, 'Enable', 'off' );
  637. set( handles.SegStartButton, 'Enable', 'off' );
  638. set( handles.SegEndButton, 'Enable', 'off' );
  639. set( handles.DeleteSegment, 'Enable', 'off' );
  640. set( handles.DeleteAllButton, 'Enable', 'off' );
  641. set( handles.SegCancel, 'Enable', 'off' );
  642. set( handles.PlotSegments, 'Enable', 'off' );
  643. set( handles.LoadFile, 'Enable', 'on' );
  644. set( handles.LoadNext, 'Enable', 'on' );
  645. set(handles.datacursor,'Enable','off')
  646. fprintf( 'Segment mode off!\n' );
  647. end
  648. guidata(gcbo,handles);
  649. % --- Executes on button press in SegStartButton.
  650. function SegStartButton_Callback(hObject, eventdata, handles)
  651. set( handles.LoadSegments, 'Enable', 'off' );
  652. set( handles.SegStartButton, 'Enable', 'off' );
  653. handles.start_stop_enable = 0;
  654. xy=get(handles.segmentLineP,'Xdata');
  655. handles.segment.start=xy(1);
  656. handles.segment.lines=[];
  657. axes(handles.axesP);
  658. set(handles.segmentLineP,'LineWidth',3);
  659. handles.segment.lines(1) = handles.segmentLineP;
  660. handles.segmentLineP = line('Visible','off');
  661. axes(handles.axesS);
  662. set(handles.segmentLineS,'LineWidth',3);
  663. handles.segment.lines(2) = handles.segmentLineS;
  664. handles.segmentLineS = line('Visible','off');
  665. axes(handles.axesW);
  666. set(handles.segmentLineW,'LineWidth',3);
  667. handles.segment.lines(3) = handles.segmentLineW;
  668. handles.segmentLineW = line('Visible','off');
  669. guidata(gcbo,handles);
  670. % --- Executes on button press in SegEndButton.
  671. function SegEndButton_Callback(hObject, eventdata, handles)
  672. set( handles.SegEndButton, 'Enable', 'off' );
  673. handles.start_stop_enable = 1;
  674. xy=get(handles.segmentLineP,'Xdata');
  675. handles.segment.end=xy(1);
  676. handles.segment=draw_all_x( handles, handles.segment );
  677. handles.segments = [handles.segments handles.segment];
  678. guidata(gcbo,handles);
  679. function out=draw_all_x( handles, segment )
  680. segment=draw_x( handles.axesP, segment );
  681. segment=draw_x( handles.axesS, segment );
  682. segment=draw_x( handles.axesW, segment );
  683. out=segment;
  684. function out=draw_x( theaxes, segment )
  685. axes(theaxes);
  686. ylim = get(theaxes,'YLim');
  687. segment.lines = [segment.lines line('Xdata',[segment.start segment.start],'Ydata',ylim,'LineWidth',3)];
  688. segment.lines = [segment.lines line('Xdata',[segment.end segment.end],'Ydata',ylim,'LineWidth',3)];
  689. segment.lines = [segment.lines line('Xdata',[segment.start segment.end],'Ydata',ylim,'LineWidth',3)];
  690. segment.lines = [segment.lines line('Xdata',[segment.start segment.end],'Ydata',[ylim(2) ylim(1)],'LineWidth',3)];
  691. out=segment;
  692. % --- Executes on button press in JumpBack.
  693. function JumpBack_Callback(hObject, eventdata, handles)
  694. Jump_shared(hObject, eventdata, handles, -1 )
  695. % --- Executes on button press in Jump.
  696. function Jump_Callback(hObject, eventdata, handles)
  697. Jump_shared(hObject, eventdata, handles, 1 )
  698. function Jump_shared(hObject, eventdata, handles, jump_dir )
  699. Tim=eval(get(handles.DisplayWindow,'String'));
  700. tDuration = str2num(get(handles.Duration,'String'));
  701. maxTslider = (tDuration - Tim)/tDuration;
  702. NT=min(round(Tim*handles.Fs),length(handles.ts));
  703. Tslider=get(handles.slider1,'Value');
  704. Tslider = Tslider + jump_dir * Tim * handles.Fs / length(handles.ts);
  705. if Tim > tDuration
  706. set(handles.DisplayWindow,'String',num2str(tDuration));
  707. Tslider = 0;
  708. end
  709. if jump_dir == 1 % jumping forward
  710. if Tslider > maxTslider
  711. Tslider = maxTslider;
  712. end
  713. end
  714. if jump_dir == -1 % jumping backwards
  715. if Tslider < 0
  716. Tslider = 0
  717. end
  718. end
  719. % if Tslider > 1
  720. % Tslider = ( length(handles.ts) - NT ) / length(handles.ts);
  721. % end
  722. % if Tslider < 0
  723. % Tslider = 0
  724. % end
  725. set(handles.slider1,'Value',Tslider);
  726. guidata(gcbo,handles);
  727. Plot_Callback(hObject, eventdata, handles)
  728. function LoadNext_Callback(hObject, eventdata, handles)
  729. % Get filename, extension. Look for next file with same extension, no seg
  730. % file associated
  731. exclude_name = [handles.filename, get(handles.ExcludeExt,'String')];
  732. if not(exist(exclude_name))
  733. fid=fopen( exclude_name, 'w' );
  734. fclose( fid);
  735. end
  736. [path] = cell2mat(regexp( handles.filename, '^.*\', 'match' ));
  737. [extension] = cell2mat(regexp( handles.filename, '\w+$', 'match' ));
  738. dirlist = dir( [path '*' extension] );
  739. ndir = length(dirlist);
  740. n = 1;
  741. while n <= ndir
  742. file = dirlist(n).name;
  743. if not(exist([path file get(handles.ExcludeExt,'String')]))
  744. break;
  745. end
  746. n = n + 1;
  747. end
  748. if n <= ndir
  749. set( handles.FileNameString, 'String',file);
  750. handles.filename = [path file];
  751. guidata(gcbo,handles);
  752. handles = loadfile(hObject, eventdata, handles);
  753. else
  754. error('No more files found matching desired pattern');
  755. end
  756. % --- Executes on button press in Precompute.
  757. function Precompute_Callback(hObject, eventdata, handles)
  758. % handles = guidata(gcbo);
  759. toggled = get( hObject, 'Value' );
  760. if toggled
  761. % Disable spectra configuration parameters
  762. % set(handles.DisplayWindow, 'Enable', 'off');
  763. set(handles.WinSize, 'Enable', 'off');
  764. set(handles.StepSize, 'Enable', 'off');
  765. set(handles.TW, 'Enable', 'off');
  766. set(handles.MinFreq, 'Enable', 'off');
  767. set(handles.MaxFreq, 'Enable', 'off');
  768. set(handles.SpectrumType, 'Enable', 'off');
  769. % set(handles.AmpThresh, 'Enable', 'off');
  770. % set(handles.TDerThresh, 'Enable', 'off');
  771. set(handles.LoadNext, 'Enable','off');
  772. % set(handles.LoadFile, 'Enable','off');
  773. valueTslider = get(handles.slider1,'Value');
  774. set(handles.slider1,'Value',0);
  775. strDuration = get(handles.Duration,'String');
  776. strWindow = get(handles.DisplayWindow,'String');
  777. handles.firsttime = 1; % indicates that the spectra need to be calculated
  778. if str2num(strDuration) > handles.maxspec_t
  779. strDuration = num2str(handles.maxspec_t);
  780. end
  781. set(handles.DisplayWindow,'String',strDuration);
  782. Plot_Callback(handles.Plot, eventdata, handles);
  783. handles = guidata(hObject);
  784. handles.firsttime = 0;
  785. handles.precomputed_spec = 1;
  786. set(handles.DisplayWindow,'String',strWindow);
  787. set(handles.slider1,'Value',valueTslider);
  788. Plot_Callback(handles.Plot, eventdata, handles);
  789. handles = guidata(hObject);
  790. handles.precomputed_spec = 1;
  791. else
  792. handles.precomputed_spec = 0;
  793. % Enable spectra configuration parameters
  794. handles.S = []; % release memory
  795. handles.t = [];
  796. handles.f = [];
  797. set(handles.WinSize, 'Enable', 'on');
  798. set(handles.StepSize, 'Enable', 'on');
  799. set(handles.TW, 'Enable', 'on');
  800. set(handles.MinFreq, 'Enable', 'on');
  801. set(handles.MaxFreq, 'Enable', 'on');
  802. set(handles.SpectrumType, 'Enable', 'on');
  803. % set(handles.AmpThresh, 'Enable', 'on');
  804. set(handles.TDerThresh, 'Enable', 'on');
  805. set(handles.LoadNext, 'Enable','on');
  806. set(handles.LoadFile, 'Enable','on');
  807. end
  808. guidata(hObject,handles);
  809. function Precompute_CreateFcn(hObject, eventdata, handles)
  810. function Path_Callback(hObject, eventdata, handles)
  811. path=get(hObject,'String')
  812. function Path_CreateFcn(hObject, eventdata, handles)
  813. set(hObject,'String',pwd);
  814. if ispc
  815. set(hObject,'BackgroundColor','white');
  816. else
  817. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  818. end
  819. function Extensions_Callback(hObject, eventdata, handles)
  820. function Extensions_CreateFcn(hObject, eventdata, handles)
  821. set(hObject,'String','wav');
  822. if ispc
  823. set(hObject,'BackgroundColor','white');
  824. else
  825. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  826. end
  827. function Duration_CreateFcn(hObject, eventdata, handles)
  828. if ispc
  829. set(hObject,'BackgroundColor','white');
  830. else
  831. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  832. end
  833. function LoadSegments_Callback(hObject, eventdata, handles)
  834. handles = load_segment(handles, [handles.filename '.seg.txt'] );
  835. set( handles.LoadSegments, 'Enable', 'off' );
  836. handles = draw_segments( handles );
  837. guidata(gcbo,handles);
  838. function handles=load_segment(handles,filename)
  839. fid=fopen( filename, 'r' );
  840. segments = [];
  841. scanned=fscanf( fid, '%g %g',[2 inf] );
  842. n = 1;
  843. while n <= size(scanned, 2)
  844. segment.start = scanned(1,n);
  845. segment.end = scanned(2,n);
  846. segment.lines = [];
  847. segments = [ segments segment ];
  848. n = n + 1;
  849. end
  850. handles.allsegments = segments; % all segments holds all segments for the file
  851. handles.segments = filtersegments(handles,handles.allsegments); % get segments for the current chunk
  852. handles.loadedsegment = 1; % indicates segments have been filtered
  853. guidata(gcf,handles);
  854. function filteredsegments = filtersegments(handles,segments)
  855. % Returns segments which are in the current defined view. Returns segments
  856. % which are not cut off.
  857. realstart = handles.markerstart / handles.Fs;
  858. realend = handles.markerend / handles.Fs;
  859. filteredsegments = [];
  860. for i = 1:length(segments) % no garuantee segments are in the same order
  861. if (segments(i).start >= realstart) && (segments(i).end <= realend)
  862. filteredsegments = [filteredsegments segments(i)];
  863. end
  864. end
  865. for i=1:length(filteredsegments)
  866. filteredsegments(i).start = filteredsegments(i).start - realstart;
  867. filteredsegments(i).end = filteredsegments(i).end - realstart;
  868. end
  869. function ExcludeExt_Callback(hObject, eventdata, handles)
  870. % Hints: get(hObject,'String') returns contents of ExcludeExt as text
  871. % str2double(get(hObject,'String')) returns contents of ExcludeExt as a double
  872. function ExcludeExt_CreateFcn(hObject, eventdata, handles)
  873. if ispc
  874. set(hObject,'BackgroundColor','white');
  875. else
  876. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  877. end
  878. function DeleteSegment_Callback(hObject, eventdata, handles)
  879. pos=get(handles.segmentLineP,'Xdata');
  880. n = 1;
  881. while n <= length( handles.segments )
  882. if pos(1) >= handles.segments(n).start && pos(1) <= handles.segments(n).end
  883. handles=delete_segment( handles, n );
  884. else
  885. n = n + 1;
  886. end
  887. end
  888. drawnow;
  889. guidata(gcbo,handles);
  890. function handles=delete_segment( handles, n )
  891. nl = 1;
  892. while nl <= length( handles.segments(n).lines )
  893. set( handles.segments(n).lines(nl), 'Visible', 'off');
  894. nl = nl + 1;
  895. end
  896. handles.segments(n) = [];
  897. fprintf('deleted!\n');
  898. function SaveSegments_Callback(hObject, eventdata, handles)
  899. % For the currently defined segments append to the segment list
  900. handles = savesegments2mem(handles);
  901. segment_file = fopen( [handles.filename '.seg.txt'], 'wt' );
  902. n = 1;
  903. while n <= size(handles.allsegments, 2)
  904. fprintf( segment_file, '%f %f\n', handles.allsegments(n).start, handles.allsegments(n).end );
  905. n = n + 1;
  906. end
  907. fclose(segment_file);
  908. set( handles.SegmentButton, 'Enable', 'on' );
  909. guidata(gcbo,handles);
  910. function handles=savesegments2mem(handles)
  911. % Updates the handles allsegments in memory
  912. % first remove in all segments all segments which are in the current chunk
  913. oldsegments = [];
  914. realstart = handles.markerstart / handles.Fs; % readjust time
  915. realend = handles.markerend / handles.Fs; % readjust time
  916. for i = 1:length(handles.allsegments)
  917. if not((handles.allsegments(i).start) >= realstart && (handles.allsegments(i).end <= realend))
  918. oldsegments = [oldsegments handles.allsegments(i)];
  919. end
  920. end
  921. % now put in the new segments
  922. newsegments = [];
  923. for i = 1:length(handles.segments)
  924. segment = handles.segments(i);
  925. segment.start = segment.start + realstart;
  926. segment.end = segment.end + realstart;
  927. newsegments = [newsegments segment];
  928. end
  929. handles.allsegments = [oldsegments newsegments];
  930. function SegCancel_Callback(hObject, eventdata, handles)
  931. set( handles.SegmentButton, 'Enable', 'on' );
  932. guidata(gcbo,handles);
  933. function PlotSegments_Callback(hObject, eventdata, handles)
  934. % Load Segments in directory
  935. [path] = cell2mat(regexp( handles.filename, '^.*\', 'match' ));
  936. [extension] = cell2mat(regexp( handles.filename, '\w+$', 'match' ));
  937. path=get(handles.Path,'String');
  938. extension=get(handles.Extensions,'String');
  939. dirlist = dir( [path '\*' extension '.seg.txt'] );
  940. ndir = length(dirlist);
  941. n = 1;
  942. all_segments = [];
  943. while n <= ndir
  944. file = dirlist(n).name;
  945. segments = load_segment([path '\' file]);
  946. all_segments = [all_segments segments];
  947. n = n + 1;
  948. end
  949. % Plot info
  950. if length(all_segments) > 2
  951. figure();
  952. axes();
  953. nbin= max(length([all_segments.end])/5,10);
  954. syllable_lengths=[all_segments.end]-[all_segments.start];
  955. hi=hist( syllable_lengths ,nbin);
  956. tl=min( syllable_lengths );
  957. th=max( syllable_lengths );
  958. times=tl:((th-tl)/(nbin-1)):th;
  959. plot(times,hi);
  960. xlabel('Segment Length (s)');
  961. ylabel('N');
  962. title(['All segments in ' path]);
  963. else
  964. error('too few segments to plot');
  965. end
  966. guidata(gcbo,handles);
  967. function AutoSegButton_Callback(hObject, eventdata, handles)
  968. n = 1;
  969. segments = [];
  970. segment.start = 0;
  971. segment.end = 0;
  972. segment.lines = [];
  973. minlen = eval(get( handles.SegmentLengthEdit, 'String' ));
  974. while n < length( handles.times )
  975. if ( handles.transition(n) > 0 )
  976. segment.start = handles.times(n);
  977. end
  978. if ( handles.transition(n) < 0 )
  979. segment.end = handles.times(n);
  980. end
  981. if (segment.start > 0) && (segment.end) > 0 && (segment.end - segment.start) > minlen
  982. segments = [ segments segment ];
  983. segment.start = 0;
  984. segment.end = 0;
  985. end
  986. n = n + 1;
  987. end
  988. handles.segments = [handles.segments segments];
  989. handles = draw_segments( handles );
  990. guidata(gcbo,handles);
  991. function DeleteAllButton_Callback(hObject, eventdata, handles)
  992. while length( handles.segments )
  993. handles = delete_segment( handles, 1 );
  994. end
  995. guidata(gcf,handles);
  996. % --- Executes on button press in PlotAllButton.
  997. function PlotAllButton_Callback(hObject, eventdata, handles)
  998. % hObject handle to PlotAllButton (see GCBO)
  999. % eventdata reserved - to be defined in a future version of MATLAB
  1000. % handles structure with handles and user data (see GUIDATA)
  1001. set(handles.slider1,'Value',0);
  1002. strDuration = get(handles.Duration,'String');
  1003. if str2num(strDuration) > handles.maxspec_t
  1004. strDuration = num2str(handles.maxspec_t);
  1005. end
  1006. set(handles.DisplayWindow,'String',strDuration);
  1007. Plot_Callback(hObject, eventdata, handles);
  1008. % --- Executes on button press in PreviousChunk.
  1009. function PreviousChunk_Callback(hObject, eventdata, handles)
  1010. % hObject handle to PreviousChunk (see GCBO)
  1011. % eventdata reserved - to be defined in a future version of MATLAB
  1012. % handles structure with handles and user data (see GUIDATA)
  1013. % [handles.markerstart handles.markerend]
  1014. handles = savesegments2mem(handles);
  1015. handles = loadfile(hObject, eventdata, handles,[handles.markerstart-handles.maxwavsize-1,handles.markerstart-1]);
  1016. % [handles.markerstart handles.markerend]
  1017. ;
  1018. guidata(gcf,handles);
  1019. % --- Executes on button press in NextChunk.
  1020. function NextChunk_Callback(hObject, eventdata, handles)
  1021. % hObject handle to NextChunk (see GCBO)
  1022. % eventdata reserved - to be defined in a future version of MATLAB
  1023. % handles structure with handles and user data (see GUIDATA)
  1024. handles = savesegments2mem(handles);
  1025. handles = loadfile(hObject, eventdata, handles, [handles.markerend+1,handles.markerend+1+handles.maxwavsize]);
  1026. guidata(gcf,handles);
  1027. % --- Executes on button press in AutoSegmentFile.
  1028. function AutoSegmentFile_Callback(hObject, eventdata, handles)
  1029. % hObject handle to AutoSegmentFile (see GCBO)
  1030. % eventdata reserved - to be defined in a future version of MATLAB
  1031. % handles structure with handles and user data (see GUIDATA)
  1032. while (handles.markerend < handles.wavsize)
  1033. PlotAllButton_Callback(hObject, eventdata, handles);
  1034. handles = guidata(gcbo);
  1035. AutoSegButton_Callback(hObject, eventdata, handles);
  1036. handles = guidata(gcbo);
  1037. NextChunk_Callback(hObject, eventdata, handles);
  1038. handles = guidata(gcbo);
  1039. end
  1040. PlotAllButton_Callback(hObject, eventdata, handles);
  1041. handles = guidata(gcbo);
  1042. AutoSegButton_Callback(hObject, eventdata, handles);
  1043. handles = guidata(gcbo);
  1044. guidata(gcbo,handles);
  1045. function MaxSegLength_Callback(hObject, eventdata, handles)
  1046. % hObject handle to MaxSegLength (see GCBO)
  1047. % eventdata reserved - to be defined in a future version of MATLAB
  1048. % handles structure with handles and user data (see GUIDATA)
  1049. % Hints: get(hObject,'String') returns contents of MaxSegLength as text
  1050. % str2double(get(hObject,'String')) returns contents of MaxSegLength as a double
  1051. % --- Executes during object creation, after setting all properties.
  1052. function MaxSegLength_CreateFcn(hObject, eventdata, handles)
  1053. % hObject handle to MaxSegLength (see GCBO)
  1054. % eventdata reserved - to be defined in a future version of MATLAB
  1055. % handles empty - handles not created until after all CreateFcns called
  1056. % Hint: edit controls usually have a white background on Windows.
  1057. % See ISPC and COMPUTER.
  1058. if ispc
  1059. set(hObject,'BackgroundColor','white');
  1060. else
  1061. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  1062. end
  1063. function MaximumWavSize_Callback(hObject, eventdata, handles)
  1064. % hObject handle to MaximumWavSize (see GCBO)
  1065. % eventdata reserved - to be defined in a future version of MATLAB
  1066. % handles structure with handles and user data (see GUIDATA)
  1067. % Hints: get(hObject,'String') returns contents of MaximumWavSize as text
  1068. % str2double(get(hObject,'String')) returns contents of MaximumWavSize as a double
  1069. % --- Executes during object creation, after setting all properties.
  1070. function MaximumWavSize_CreateFcn(hObject, eventdata, handles)
  1071. % hObject handle to MaximumWavSize (see GCBO)
  1072. % eventdata reserved - to be defined in a future version of MATLAB
  1073. % handles empty - handles not created until after all CreateFcns called
  1074. % Hint: edit controls usually have a white background on Windows.
  1075. % See ISPC and COMPUTER.
  1076. if ispc
  1077. set(hObject,'BackgroundColor','white');
  1078. else
  1079. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  1080. end
  1081. % --- Executes on button press in SeekButton.
  1082. function SeekButton_Callback(hObject, eventdata, handles)
  1083. % hObject handle to SeekButton (see GCBO)
  1084. % eventdata reserved - to be defined in a future version of MATLAB
  1085. % handles structure with handles and user data (see GUIDATA)
  1086. % Seek anywhere in a long file
  1087. handles = savesegments2mem(handles);
  1088. try
  1089. timetoseek = str2num(get(handles.SeektoEdit,'String'));
  1090. catch
  1091. timetoseek = 0;
  1092. set(handles.SeektoEdit,'String','0');
  1093. end
  1094. if timetoseek < 0
  1095. timetoseek = 0;
  1096. set(handles.SeektoEdit,'String','0');
  1097. end
  1098. timetoseek = round(timetoseek * handles.Fs);
  1099. if timetoseek >= handles.wavsize
  1100. timetoseek = timetoseek - handles.maxwavsize;
  1101. end
  1102. timetoseek = timetoseek + 1;
  1103. timetoseekend = timetoseek + handles.maxwavsize;
  1104. if timetoseekend > handles.wavsize
  1105. timetoseekend = handles.wavsize;
  1106. end
  1107. oldstate = handles.dontcutsegments;
  1108. handles.dontcutsegments = 0;
  1109. handles = loadfile(hObject,eventdata,handles,[timetoseek timetoseekend]);
  1110. handles.dontcutsegments = oldstate;
  1111. guidata(gcbo,handles);
  1112. function SeektoEdit_Callback(hObject, eventdata, handles)
  1113. % hObject handle to SeektoEdit (see GCBO)
  1114. % eventdata reserved - to be defined in a future version of MATLAB
  1115. % handles structure with handles and user data (see GUIDATA)
  1116. % Hints: get(hObject,'String') returns contents of SeektoEdit as text
  1117. % str2double(get(hObject,'String')) returns contents of SeektoEdit as a double
  1118. % --- Executes during object creation, after setting all properties.
  1119. function SeektoEdit_CreateFcn(hObject, eventdata, handles)
  1120. % hObject handle to SeektoEdit (see GCBO)
  1121. % eventdata reserved - to be defined in a future version of MATLAB
  1122. % handles empty - handles not created until after all CreateFcns called
  1123. % Hint: edit controls usually have a white background on Windows.
  1124. % See ISPC and COMPUTER.
  1125. if ispc
  1126. set(hObject,'BackgroundColor','white');
  1127. else
  1128. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  1129. end
  1130. function RealDuration_Callback(hObject, eventdata, handles)
  1131. % hObject handle to RealDuration (see GCBO)
  1132. % eventdata reserved - to be defined in a future version of MATLAB
  1133. % handles structure with handles and user data (see GUIDATA)
  1134. % Hints: get(hObject,'String') returns contents of RealDuration as text
  1135. % str2double(get(hObject,'String')) returns contents of RealDuration as a double
  1136. % --- Executes during object creation, after setting all properties.
  1137. function RealDuration_CreateFcn(hObject, eventdata, handles)
  1138. % hObject handle to RealDuration (see GCBO)
  1139. % eventdata reserved - to be defined in a future version of MATLAB
  1140. % handles empty - handles not created until after all CreateFcns called
  1141. % Hint: edit controls usually have a white background on Windows.
  1142. % See ISPC and COMPUTER.
  1143. if ispc
  1144. set(hObject,'BackgroundColor','white');
  1145. else
  1146. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  1147. end
  1148. % --- Executes on selection change in AutoMethodPopupMenu.
  1149. function AutoMethodPopupMenu_Callback(hObject, eventdata, handles)
  1150. % hObject handle to AutoMethodPopupMenu (see GCBO)
  1151. % eventdata reserved - to be defined in a future version of MATLAB
  1152. % handles structure with handles and user data (see GUIDATA)
  1153. % Hints: contents = get(hObject,'String') returns AutoMethodPopupMenu contents as cell array
  1154. % contents{get(hObject,'Value')} returns selected item from AutoMethodPopupMenu
  1155. contents = get(hObject,'String');
  1156. method = contents{get(hObject,'Value')}
  1157. if strcmp(method,'Summed intensity')
  1158. handles.automethod = 'threshold';
  1159. set(handles.AmpThresh,'Visible','on');
  1160. set(handles.RatioThresh,'Visible','off');
  1161. elseif strcmp(method,'Ratio')
  1162. handles.automethod = 'ratiof';
  1163. set(handles.AmpThresh,'Visible','off');
  1164. set(handles.RatioThresh,'Visible','on');
  1165. end
  1166. guidata(gcbo,handles);
  1167. % --- Executes during object creation, after setting all properties.
  1168. function AutoMethodPopupMenu_CreateFcn(hObject, eventdata, handles)
  1169. % hObject handle to AutoMethodPopupMenu (see GCBO)
  1170. % eventdata reserved - to be defined in a future version of MATLAB
  1171. % handles empty - handles not created until after all CreateFcns called
  1172. % Hint: popupmenu controls usually have a white background on Windows.
  1173. % See ISPC and COMPUTER.
  1174. if ispc
  1175. set(hObject,'BackgroundColor','white');
  1176. else
  1177. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  1178. end
  1179. function RatioThresh_Callback(hObject, eventdata, handles)
  1180. % hObject handle to RatioThresh (see GCBO)
  1181. % eventdata reserved - to be defined in a future version of MATLAB
  1182. % handles structure with handles and user data (see GUIDATA)
  1183. % Hints: get(hObject,'String') returns contents of RatioThresh as text
  1184. % str2double(get(hObject,'String')) returns contents of RatioThresh as a double
  1185. % --- Executes during object creation, after setting all properties.
  1186. function RatioThresh_CreateFcn(hObject, eventdata, handles)
  1187. % hObject handle to RatioThresh (see GCBO)
  1188. % eventdata reserved - to be defined in a future version of MATLAB
  1189. % handles empty - handles not created until after all CreateFcns called
  1190. % Hint: edit controls usually have a white background on Windows.
  1191. % See ISPC and COMPUTER.
  1192. if ispc
  1193. set(hObject,'BackgroundColor','white');
  1194. else
  1195. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  1196. end
  1197. function RatioLower_Callback(hObject, eventdata, handles)
  1198. % hObject handle to RatioLower (see GCBO)
  1199. % eventdata reserved - to be defined in a future version of MATLAB
  1200. % handles structure with handles and user data (see GUIDATA)
  1201. % Hints: get(hObject,'String') returns contents of RatioLower as text
  1202. % str2double(get(hObject,'String')) returns contents of RatioLower as a double
  1203. % --- Executes during object creation, after setting all properties.
  1204. function RatioLower_CreateFcn(hObject, eventdata, handles)
  1205. % hObject handle to RatioLower (see GCBO)
  1206. % eventdata reserved - to be defined in a future version of MATLAB
  1207. % handles empty - handles not created until after all CreateFcns called
  1208. % Hint: edit controls usually have a white background on Windows.
  1209. % See ISPC and COMPUTER.
  1210. if ispc
  1211. set(hObject,'BackgroundColor','white');
  1212. else
  1213. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  1214. end
  1215. function RatioUpper_Callback(hObject, eventdata, handles)
  1216. % hObject handle to RatioUpper (see GCBO)
  1217. % eventdata reserved - to be defined in a future version of MATLAB
  1218. % handles structure with handles and user data (see GUIDATA)
  1219. % Hints: get(hObject,'String') returns contents of RatioUpper as text
  1220. % str2double(get(hObject,'String')) returns contents of RatioUpper as a double
  1221. % --- Executes during object creation, after setting all properties.
  1222. function RatioUpper_CreateFcn(hObject, eventdata, handles)
  1223. % hObject handle to RatioUpper (see GCBO)
  1224. % eventdata reserved - to be defined in a future version of MATLAB
  1225. % handles empty - handles not created until after all CreateFcns called
  1226. % Hint: edit controls usually have a white background on Windows.
  1227. % See ISPC and COMPUTER.
  1228. if ispc
  1229. set(hObject,'BackgroundColor','white');
  1230. else
  1231. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  1232. end
  1233. % --- Executes on button press in OptionsDisplay.
  1234. function OptionsDisplay_Callback(hObject, eventdata, handles)
  1235. % hObject handle to OptionsDisplay (see GCBO)
  1236. % eventdata reserved - to be defined in a future version of MATLAB
  1237. % handles structure with handles and user data (see GUIDATA)
  1238. % Hint: get(hObject,'Value') returns toggle state of OptionsDisplay
  1239. % positionP = get(handles.OptionsUiPanel,'Position');
  1240. % positionF = get(gcf,'Position');
  1241. state = get(hObject,'Value');
  1242. if state
  1243. set(handles.OptionsUiPanel,'Visible','on');
  1244. % positionF(3) = positionF(3) + positionP(3);
  1245. else
  1246. set(handles.OptionsUiPanel,'Visible','off');
  1247. % positionF(3) = positionF(3) - positionP(3);
  1248. end
  1249. % set(gcf,'Position',positionF); % untested
  1250. guidata(gcbo,handles)
  1251. function Duration_Callback(hObject, eventdata, handles)
  1252. % hObject handle to Duration (see GCBO)
  1253. % eventdata reserved - to be defined in a future version of MATLAB
  1254. % handles structure with handles and user data (see GUIDATA)
  1255. % Hints: get(hObject,'String') returns contents of Duration as text
  1256. % str2double(get(hObject,'String')) returns contents of Duration as a double
  1257. function SmoothFactor_Callback(hObject, eventdata, handles)
  1258. % hObject handle to SmoothFactor (see GCBO)
  1259. % eventdata reserved - to be defined in a future version of MATLAB
  1260. % handles structure with handles and user data (see GUIDATA)
  1261. % Hints: get(hObject,'String') returns contents of SmoothFactor as text
  1262. % str2double(get(hObject,'String')) returns contents of SmoothFactor as a double
  1263. % --- Executes during object creation, after setting all properties.
  1264. function SmoothFactor_CreateFcn(hObject, eventdata, handles)
  1265. % hObject handle to SmoothFactor (see GCBO)
  1266. % eventdata reserved - to be defined in a future version of MATLAB
  1267. % handles empty - handles not created until after all CreateFcns called
  1268. % Hint: edit controls usually have a white background on Windows.
  1269. % See ISPC and COMPUTER.
  1270. if ispc
  1271. set(hObject,'BackgroundColor','white');
  1272. else
  1273. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  1274. end
  1275. function channel_Callback(hObject, eventdata, handles)
  1276. % hObject handle to channel (see GCBO)
  1277. % eventdata reserved - to be defined in a future version of MATLAB
  1278. % handles structure with handles and user data (see GUIDATA)
  1279. % Hints: get(hObject,'String') returns contents of channel as text
  1280. % str2double(get(hObject,'String')) returns contents of channel as a double
  1281. % --- Executes during object creation, after setting all properties.
  1282. function channel_CreateFcn(hObject, eventdata, handles)
  1283. % hObject handle to channel (see GCBO)
  1284. % eventdata reserved - to be defined in a future version of MATLAB
  1285. % handles empty - handles not created until after all CreateFcns called
  1286. % Hint: edit controls usually have a white background on Windows.
  1287. % See ISPC and COMPUTER.
  1288. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
  1289. set(hObject,'BackgroundColor','white');
  1290. end