classify_spectra.m 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858
  1. function varargout = classify_spectra(varargin)
  2. % CLASSIFY_SPECTRA M-file for classify_spectra.fig
  3. % CLASSIFY_SPECTRA, by itself, creates a new CLASSIFY_SPECTRA or raises the existing
  4. % singleton*.
  5. %
  6. % H = CLASSIFY_SPECTRA returns the handle to a new CLASSIFY_SPECTRA or
  7. % the handle to
  8. % the existing singleton*.
  9. %
  10. % CLASSIFY_SPECTRA('CALLBACK',hObject,eventData,handles,...) calls the local
  11. % function named CALLBACK in CLASSIFY_SPECTRA.M with the given input arguments.
  12. %
  13. % CLASSIFY_SPECTRA('Property','Value',...) creates a new CLASSIFY_SPECTRA or raises the
  14. % existing singleton*. Starting from the left, property value pairs are
  15. % applied to the GUI before classify_spectra_OpeningFunction gets called. An
  16. % unrecognized property name or invalid value makes property application
  17. % stop. All inputs are passed to classify_spectra_OpeningFcn via varargin.
  18. %
  19. % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
  20. % instance to run (singleton)".
  21. %
  22. % See also: GUIDE, GUIDATA, GUIHANDLES
  23. % Edit the above text to modify the response to help classify_spectra
  24. % Last Modified by GUIDE v2.5 26-Jun-2006 23:19:22
  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', @classify_spectra_OpeningFcn, ...
  30. 'gui_OutputFcn', @classify_spectra_OutputFcn, ...
  31. 'gui_LayoutFcn', [] , ...
  32. 'gui_Callback', []);
  33. if nargin & isstr(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 classify_spectra is made visible.
  43. function classify_spectra_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 classify_spectra (see VARARGIN)
  49. % Choose default command line output for classify_spectra
  50. handles.output = hObject;
  51. % Set defaults
  52. handles.recompute = logical(1); % Whether to recompute a spectra
  53. handles.cwavfile = ''; % The current wave file
  54. handles.directory = pwd;
  55. handles.Fs = 44100; % Frequency of audio sampling per second
  56. handles.movingwin=[0.01 0.002]; % Size of the moving window in seconds; the first number is the window size and the second is the step size
  57. handles.tapers=[3 5];
  58. handles.pad=1;
  59. handles.fpass=[0 20000]; % Range of frequency sampling
  60. handles.nsegments = 0; % total number of segments
  61. handles.NextIndex = 1; % the index for segments
  62. handles.maxseglength = 0; % set in seconds
  63. % ClassifyAxes handles
  64. handles.classified_height = 560 ; % the height of the image in the classified axes
  65. handles.classified_width = 450; % the width of the image in the classified axes
  66. %
  67. handles.plotmode = 'spectra'; % The main spectra plot mode can also be
  68. % see plot modes
  69. handles.plotmodes = {'spectra' 'waveform' 'spectra_dt' 'spectra_df' };
  70. handles.plotmodevalue = 1;
  71. % set up a density measurement which will allow scaling
  72. classaxpos = get(handles.ClassifiedAxes,'Position');
  73. handles.classified_height_density = handles.classified_height / classaxpos(4);
  74. handles.classified_width_density = handles.classified_width / classaxpos(3);
  75. handles.ispecheight = 100; % fixed height of the iconized spectogram
  76. handles.ismaxwidth = SmallAxes_width(handles);
  77. handles.specpad = 0.02; % pad in image sizes
  78. handles.xspacer = 5; % fixed space between the images in the horizontal direction
  79. handles.yspacer = 10; % fixed space between the row of the images
  80. handles.xpsacer_density = handles.xspacer / classaxpos(3);
  81. handles.ypsacer_density = handles.yspacer / classaxpos(4);
  82. handles.image_list = {}; % holds an array of the specicons
  83. handles.positions = []; % holds the position of images on infinitely long canvas
  84. handles.images_dim = []; % holds the size of the images
  85. handles.mapindex = []; % holds the position number for the segment
  86. handles.nimages = 0; % total number of images or spectra
  87. handles.number_rows = floor(handles.classified_height/(handles.yspacer + handles.ispecheight)); % the number of rows allowed on a page
  88. handles.cnrows = 0; % current number of rows
  89. handles.startpage = 1; % an index for the first image on the page
  90. handles.endpage = 1; % an index for the last image on the page
  91. handles.startx = 1; % for the classified axes holds the start position for the image
  92. handles.endx = handles.classified_width; % for the classified axes holds the end position for the image
  93. handles.mode = 'browse'; % A string representing the current major mode which is either
  94. % 'browse','classify','class-view','class-members'
  95. handles.submode = 'select'; %A string representing the minor mode which is either
  96. % 'select','select-class','remove-class', 'compare',
  97. % 'typify'
  98. handles.quickmode = logical(0); % Quick mode allows quick classification with minimum
  99. % work for the user. By default this is set
  100. % off
  101. handles.lastsegment = 1; % The last segment classified
  102. handles.sortclass = 'length'; % Tells how classes are to be ordered in the ClassifiedAxes
  103. % 'original' is the order the classes were created or loaded from
  104. % 'popularity' sort the classes with most popular first
  105. % 'length' sort the classes by longest
  106. % class first
  107. set(handles.SortPopupMenu,'Value',3);
  108. handles.lastclass = 0;
  109. handles.lowerfreq = 0; % Lower frequency for zooming
  110. handles.upperfreq = 7500; % Upper frequency for zooming
  111. handles.rezoom = logical(1);
  112. handles.IconListf = {};
  113. handles.baseclassname = 'mockingbird'; % This string should be set by the user
  114. % used as the base class name
  115. handles.nclasses = 0; % total number of syllable classes
  116. handles.classes = []; % structure for holding class information
  117. handles.current_class = 0; % used by compare to go through classes
  118. handles.configschanged = logical(0); % indicates whether the configs for spectra has changed
  119. handles.precomputed = logical(0);
  120. handles.configfile = 'class_spec.conf';
  121. handles.originalsize = [0 0 170 44]; % original position of the form
  122. handles.originalaxessize = [80.5 6.375 86.167 34.438];
  123. handles.blank = logical(1); % indicate that the ClassifiedAxes is blank
  124. handles.prevsize = handles.originalsize;
  125. handles.fixed = logical(1); % Whether to use fixed scaling when resizing the form
  126. % initially set to true so not to call the repositioning algorithm when the
  127. % form is blank
  128. handles.nfeatures = 10;
  129. handles.ncepestral = 10; % Number of cepestral coefficients to include
  130. set(gcf, 'ResizeFcn', {@ResizeFcn});
  131. %classify_spectra('ResizeFcn',gcbo,[],guidata(gcbo))
  132. % Update handles structure
  133. guidata(hObject, handles);
  134. % UIWAIT makes classify_spectra wait for user response (see UIRESUME)
  135. % uiwait(handles.figure1);
  136. % --- Outputs from this function are returned to the command line.
  137. function varargout = classify_spectra_OutputFcn(hObject, eventdata, handles)
  138. % varargout cell array for returning output args (see VARARGOUT);
  139. % hObject handle to figure
  140. % eventdata reserved - to be defined in a future version of MATLAB
  141. % handles structure with handles and user data (see GUIDATA)
  142. % Get default command line output from handles structure
  143. varargout{1} = handles.output;
  144. function DirectoryEditBox_Callback(hObject, eventdata, handles)
  145. handles.directory = get(hObject,'String');
  146. function DirectoryEditBox_CreateFcn(hObject, eventdata, handles)
  147. set(hObject,'string',pwd);
  148. guidata(gcbo,handles);
  149. if ispc
  150. set(hObject,'BackgroundColor','white');
  151. else
  152. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  153. end
  154. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  155. % Functions for loading segments %
  156. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  157. function segments=LoadSegmentsFromDirectory(directory)
  158. segfilelist = dir( [directory '\*' '.seg.txt'] );
  159. %segfilelist;
  160. nsegfile = length(segfilelist);
  161. segments = [];
  162. n = 1;
  163. while n <= nsegfile
  164. segfilename = segfilelist(n).name;
  165. fid=fopen( segfilename, 'rt' );
  166. if fid ~= -1 % if there is a seg file
  167. scanned=fscanf( fid, '%g %g',[2 inf] );
  168. fclose(fid);
  169. %fprintf( 'File %d of %d has %d segments: %s\n', n, nsegfile, size(scanned,2),segfilename );
  170. wavfile = segfilename(1:(length(segfilename)-8)); % can this be made more general
  171. i = 1;
  172. while i <= size(scanned, 2) % Load the start and stop of segments
  173. segment.wavfile = wavfile;
  174. segment.class = ''; % Loaded segments start out unclassified
  175. segment.features = [];
  176. segment.start = scanned(1,i);
  177. segment.end = scanned(2,i);
  178. segment.specfilename = [segment.wavfile '.' num2str(segment.start) '-' num2str(segment.end) '.spec'];
  179. segments = [ segments segment];
  180. i = i + 1;
  181. end
  182. end
  183. n = n + 1;
  184. end
  185. function handles=Load_InitialSegments(hObject,handles)
  186. %handles.directory = pwd;
  187. handles.segments = LoadSegmentsFromDirectory(handles.directory);
  188. handles.nsegments = length(handles.segments);
  189. guidata(hObject, handles);
  190. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  191. % Functions for handling syllable classes %
  192. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  193. function handles = cindex2imagelist(handles)
  194. % Takes a cindex and generates a a list of images
  195. handles.image_list = {};
  196. %load('-mat','specicons');
  197. for i = 1:length(handles.mapindex)
  198. handles.image_list{i} = handles.classes( handles.mapindex(i) ).iconS;
  199. end
  200. function mapindex = sortindexbypop(handles)
  201. % Takes a cindex and sorts by the class popularity
  202. nindexes = length(handles.mapindex);
  203. mapindex = [];
  204. for i = 1:nindexes
  205. mapindex(i,1:2) = [handles.mapindex(i) handles.classes(handles.mapindex(i)).nmembers];
  206. end
  207. mapindex = sortrows(mapindex, 2);
  208. mapindex = flipud(mapindex(:,1));
  209. function mapindex = sortindexbylength(handles)
  210. % Takes a cindex and sorts by the class popularity
  211. nindexes = length(handles.mapindex);
  212. mapindex = [];
  213. for i = 1:nindexes
  214. mapindex(i,1:2) = [handles.mapindex(i) handles.classes(handles.mapindex(i)).length];
  215. end
  216. mapindex = sortrows(mapindex, 2);
  217. mapindex = flipud(mapindex(:,1));
  218. function class_string = newclassname(handles)
  219. % Generates a new name for the class string using the baseclassname
  220. % variable. Classes are numbered sequentially from the class with the largest number.
  221. nbaseclass = length(handles.baseclassname);
  222. classnum = 0;
  223. for i = 1:handles.nclasses % make sure the largest class number is gotten
  224. classname = handles.classes(i).name;
  225. curr_classnum = str2num(classname(nbaseclass + 1:length(classname)));
  226. if curr_classnum > classnum
  227. classnum = curr_classnum;
  228. end
  229. end
  230. class_string = strcat(handles.baseclassname,num2str(classnum + 1));
  231. ;
  232. function cindex = returnclassindex(handles,classname)
  233. % Return the index to the class
  234. cindex = 0;
  235. i = 1;
  236. while (i <= handles.nclasses) && not(strcmp(classname,handles.classes(i).name))
  237. i = i + 1;
  238. end
  239. cindex = i;
  240. ;
  241. function handles = add_new_class(handles,segment)
  242. % Segment is the class that will be used to typify the class
  243. handles.nclasses = length(handles.classes);
  244. class.name = newclassname(handles);
  245. class.nmembers = 1; % the number of segments which are members of this class
  246. class.specfilename = segment.specfilename; % specfilename will be used as a unique identifier
  247. class.index = handles.NextIndex;
  248. %load('-mat',class.specfilename);
  249. class.iconS = handles.IconList{handles.NextIndex}; % this is the icon which typifies the class
  250. class.length = segment.end - segment.start; % used to hold the lengt of the length
  251. handles.classes = [handles.classes class];
  252. handles.nclasses = handles.nclasses + 1;
  253. %guidata(gcbo,handles);
  254. ;
  255. function handles=ConfigureClassSegment(handles)
  256. % Handles the gui configuration of the class information when navigating
  257. segment = handles.segments(handles.NextIndex);
  258. if strcmp(segment.class,'') % Unclassified segment
  259. set(handles.ClassifyButton,'String','Classify');
  260. set(handles.ClassifyButton,'Enable','on');
  261. else % Classified segment
  262. set(handles.ClassifyButton,'String','Declassify');
  263. set(handles.ClassifyButton,'Enable','on');
  264. end
  265. %guidata(gcbo,handles);
  266. function handles = blankaxes(handles)
  267. set(handles.NextRowButton,'Enable','off');
  268. set(handles.PreviousRowButton,'Enable','off');
  269. axes(handles.ClassifiedAxes);
  270. handles.hiclass = image(uint8(zeros(handles.classified_height,handles.classified_width)));
  271. set(handles.ClassifiedAxes,'Xtick',[]);
  272. set(handles.ClassifiedAxes,'Ytick',[]);
  273. handles.blank = logical(1);
  274. % --- Executes on button press in ClassifyButton.
  275. function ClassifyButton_Callback(hObject, eventdata, handles)
  276. % hObject handle to ClassifyButton (see GCBO)
  277. % eventdata reserved - to be defined in a future version of MATLAB
  278. % handles structure with handles and user data (see GUIDATA)
  279. status = get(handles.ClassifyButton,'String');
  280. set(handles.TypifyClassButton,'Visible', 'off');
  281. set(handles.RemoveClassButton, 'Visible', 'off');
  282. set(handles.NextClassButton,'Visible','off');
  283. % set(handles.CompareToggleButton,'Visible','off');
  284. set(handles.RenameClassButton,'Visible','off');
  285. handles.startx = 1; % if you have been scrolling reset to defaults
  286. handles.endx = handles.classified_width;
  287. set(handles.SliderClassified,'Value',0);
  288. if strcmp(status,'Classify')
  289. set_status(handles,'');
  290. set(handles.ModePopupMenu,'Enable','off');
  291. set(handles.NextSpectraButton,'Enable','off');
  292. set(handles.PreviousSpectraButton,'Enable','off');
  293. set(handles.ClassifyButton,'Enable','off');
  294. set(handles.CompareToggleButton,'Enable','off');
  295. set(handles.QuickModeButton,'Enable','off');
  296. set(handles.AutoClassifyButton,'Enable','off');
  297. set(handles.NewClassButton,'Visible','on');
  298. if handles.nclasses > 0 % Make sure there is at least one class
  299. if strcmp(handles.mode,'comparison') % if you are in comparison mode
  300. set(handles.NewClassButton,'Visible','off');
  301. handles.segments(handles.NextIndex).class = handles.classes(handles.lastclass).name;
  302. set(handles.ModePopupMenu,'Enable','on');
  303. set(handles.ClassifyButton,'String','Declassify');
  304. set(handles.ClassifyButton,'Enable','on');
  305. set(handles.CompareToggleButton,'value',0);
  306. set(handles.CompareToggleButton,'Enable','on');
  307. handles = configureclassview(handles,'select-class');
  308. set_status(handles, ['Viewing all ', num2str(handles.nclasses),' classes']);
  309. set(handles.RemoveClassButton,'Enable','on');
  310. set(handles.RemoveClassButton,'Visible','on');
  311. set(handles.QuickModeButton,'Enable','on');
  312. set(handles.AutoClassifyButton,'Enable','on');
  313. setnavigationbuttons(handles);
  314. else % regular mode
  315. set(handles.SortText,'Visible','on');
  316. set(handles.SortPopupMenu,'Visible','on');
  317. handles = configureclassview(handles,'select-class');
  318. set_status(handles, ['Select a class']);
  319. end
  320. else % draw a blank image
  321. handles = blankaxes(handles);
  322. handles.mode = 'class-view';
  323. handles.submode = 'select-class';
  324. set(handles.hiclass,'ButtonDownFcn',{@DummyClassifyAxesClickCallBack,handles});
  325. end
  326. % configure the remaining gui
  327. handles = SetModePopupMenu(handles,'class view');
  328. elseif strcmp(status,'Declassify')
  329. cindex = returnclassindex(handles,handles.segments(handles.NextIndex).class);
  330. if handles.classes(cindex).nmembers == 1 % Only one member left of that class
  331. if length(handles.classes) == 1 % Only one class remaining
  332. axes(handles.ClassifiedAxes);
  333. handles.classes = [];
  334. handles.nclasses = 0;
  335. handles = blankaxes(handles);
  336. else % remove the class
  337. handles.classes = [handles.classes(1:cindex-1) handles.classes(cindex + 1:length(handles.classes))];
  338. handles.nclasses = handles.nclasses - 1;
  339. end
  340. else
  341. if strcmp(handles.classes(cindex).specfilename,handles.segments(handles.NextIndex).specfilename)
  342. i = 1; %Test if the class you are removing the type class
  343. segs = [ handles.segments(1:(handles.NextIndex - 1)) handles.segments((handles.NextIndex + 1) : handles.nclasses)];
  344. while (i <= length(segs)) && strcmp(segs(i).class,handles.classes(cindex).name)
  345. i = i + 1;
  346. end
  347. handles.classes(cindex).specfilename = handles.segments(i).specfilename;
  348. handles.classes(cindex).length = handles.segments(i).end - handles.segments(i).start;
  349. handles.iconS = handles.IconList{i};
  350. end
  351. handles.classes(cindex).nmembers = handles.classes(cindex).nmembers - 1;
  352. end
  353. handles.segments(handles.NextIndex).class = ''; % Remove class information
  354. if handles.nclasses >= 1 % Redraw axes
  355. if strcmp(handles.mode,'class-view')
  356. handles = configureclassview(handles,'select');
  357. set_status(handles,['Viewing all ' num2str(handles.nclasses) ' classes']);
  358. elseif strcmp(handles.mode,'class-members')
  359. handles = configureclassmembers(handles,handles.classes(cindex).name);
  360. set_status(handles,['Viewing ' num2str(handles.classes(cindex).nmembers) ' members of ' num2str(handles.classes(cindex).name)]);
  361. elseif strcmp(handles.mode,'browse')
  362. ; % do nothing
  363. end
  364. end
  365. set(handles.ClassifyButton,'String','Classify');
  366. setnavigationbuttons(handles);
  367. end
  368. guidata(gcbo,handles);
  369. % --- Executes on button press in NewClassButton.
  370. function NewClassButton_Callback(hObject, eventdata, handles)
  371. % hObject handle to NewClassButton (see GCBO)
  372. % eventdata reserved - to be defined in a future version of MATLAB
  373. % handles structure with handles and user data (see GUIDATA)
  374. handles.startx = 1; % if you have been scrolling reset to defaults
  375. handles.endx = handles.classified_width;
  376. set(handles.SliderClassified,'Value',0);
  377. handles = add_new_class(handles,handles.segments(handles.NextIndex));
  378. handles.segments(handles.NextIndex).class = handles.classes(handles.nclasses).name;
  379. set(handles.SortText,'Visible','off');
  380. set(handles.SortPopupMenu,'Visible','off');
  381. set(handles.ClassifyButton,'Enable','on');
  382. set(handles.ClassifyButton,'String','Declassify');
  383. set(handles.NewClassButton,'Visible','off');
  384. set(handles.ModePopupMenu,'Enable','on');
  385. set(handles.NextSpectraButton,'Enable','on');
  386. set(handles.CompareToggleButton,'Enable','on');
  387. set(handles.QuickModeButton,'Enable','on');
  388. set(handles.AutoClassifyButton,'Enable','on');
  389. set(handles.PreviousSpectraButton,'Enable','on');
  390. handles = configureclassview(handles,'xxx');
  391. setnavigationbuttons(handles);
  392. set_status(handles,'');
  393. handles = SetModePopupMenu(handles,'class view');
  394. guidata(gcbo,handles);
  395. ;
  396. % --- Executes on button press in RemoveClassButton.
  397. function RemoveClassButton_Callback(hObject, eventdata, handles)
  398. % hObject handle to RemoveClassButton (see GCBO)
  399. % eventdata reserved - to be defined in a future version of MATLAB
  400. % handles structure with handles and user data (see GUIDATA)
  401. %set(handles.TypifyClassButton,'Enable','on');
  402. set(handles.RemoveClassButton,'Enable','off');
  403. handles.mode = 'class-view';
  404. handles.submode = 'remove-class';
  405. set_status(handles,'Select a class to remove');
  406. guidata(gcbo,handles);
  407. % --- Executes on button press in TypifyClassButton.
  408. function TypifyClassButton_Callback(hObject, eventdata, handles)
  409. % hObject handle to TypifyClassButton (see GCBO)
  410. % eventdata reserved - to be defined in a future version of MATLAB
  411. % handles structure with handles and user data (see GUIDATA)
  412. set(handles.TypifyClassButton,'Enable','off');
  413. %set(handles.RemoveClassButton,'Enable','on');
  414. handles.mode = 'class-members';
  415. handles.submode = 'typify';
  416. set_status(handles,'Select an icon to change the type');
  417. guidata(gcbo,handles);
  418. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  419. % Functions for computing the spectra %
  420. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  421. function segment=precompute_spectra(handles, segment)
  422. % This function will do the precomputing of the spectragram
  423. spec_ipad = round(handles.specpad * handles.Fs);
  424. spec_istart = round(segment.start * handles.Fs);
  425. spec_iend = round(segment.end * handles.Fs);
  426. % This is to catch an over and under run errors in the wav file because of the padding
  427. try
  428. [data] = wavread(segment.wavfile, [spec_istart - spec_ipad, spec_iend + spec_ipad]);
  429. catch
  430. errmsg = lasterr;
  431. if strfind(errmsg, 'Sample limits out of range')
  432. if (segment.start - handles.specpad) < 0 % Make sure the starting point is not negative
  433. [data] = wavread(segment.wavfile, [1 spec_iend + spec_ipad]);
  434. else % over run of the buffer
  435. [data] = wavread(segment.wavfile);
  436. [data] = data((spec_istart - spec_ipad):length(data));
  437. end
  438. end
  439. end
  440. [Sfull tfull f] = compute_spectra(data,handles.tapers,handles.Fs,handles.fpass,handles.movingwin); % precompute the portion
  441. wavlength = length(data);
  442. Ssize = size(Sfull);
  443. Slength = Ssize(2);
  444. RatioWS = Slength / (wavlength / handles.Fs); % this allows us to index by time through spec file
  445. Sstart = round(RatioWS * segment.start);
  446. Send = round(RatioWS * segment.end);
  447. Spad = round(RatioWS * handles.specpad);
  448. Spre = Sfull(:,1:Spad);
  449. S = Sfull(:,Spad+1:Spad + Send-Sstart);
  450. Spost = Sfull(:,Spad + (Send-Sstart)+1:Slength);
  451. t=[segment.start, segment.end];
  452. iconS = iconify_spec(S,handles.ispecheight);
  453. save(segment.specfilename,'S','t','f','Spre','Spost','RatioWS','tfull','iconS','-mat');
  454. fprintf('Saving %s file\n',segment.specfilename);
  455. function handles = precompute_AllSpectra(handles)
  456. % This function precomputes all the spectra in a directory
  457. hw = waitbar(0,'Precomputing spectra. . .');
  458. if handles.nsegments >= 1
  459. for i = 1:handles.nsegments
  460. precompute_spectra(handles,handles.segments(i));
  461. waitbar(i/handles.nsegments);
  462. end
  463. end
  464. close(hw);
  465. handles.IconList = get_SpecIcons(handles);
  466. ;
  467. function [S t f]=compute_spectra(data,tapers,Fs,fpass,movingwin)
  468. data = data / std(data); % normalize the variance of the spectra
  469. params.tapers=tapers; params.Fs=Fs; params.fpass=fpass;
  470. [S t f] = mtspecgramc( diff(data), movingwin, params );
  471. S = log(S)';
  472. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  473. % Functions for plotting the spectragram %
  474. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  475. function handles=get_and_plot(handles, segment)
  476. load('-mat',segment.specfilename);
  477. axes(handles.ToClassifyAxes);
  478. % RatioWS
  479. cmap = jet(256);
  480. if strcmp(handles.plotmode,'spectra')
  481. SFull = cat(2,Spre,S,Spost);
  482. elseif strcmp(handles.plotmode,'spectra_dt') || strcmp(handles.plotmode,'spectra_df') || strcmp(handles.plotmode,'waveform')
  483. wav_ipad = round(handles.specpad * handles.Fs);
  484. wav_istart = round(segment.start * handles.Fs);
  485. wav_iend = round(segment.end * handles.Fs);
  486. % This is to catch an over and under run errors in the wav file because of the padding
  487. try
  488. [data] = wavread(segment.wavfile, [wav_istart - wav_ipad, wav_iend + wav_ipad]);
  489. catch
  490. errmsg = lasterr;
  491. if strfind(errmsg, 'Sample limits out of range')
  492. if (segment.start - handles.specpad) < 0 % Make sure the starting point is not negative
  493. [data] = wavread(segment.wavfile, [1 wav_iend + wav_ipad]);
  494. else % over run of the buffer
  495. [data] = wavread(segment.wavfile);
  496. [data] = data((wav_istart - wav_ipad):length(data));
  497. end
  498. end
  499. end
  500. data = data / std(data);
  501. params.Fs=handles.Fs;
  502. params.tapers = handles.tapers;
  503. params.fpass=handles.fpass;
  504. params.pad = 1;
  505. if strcmp(handles.plotmode,'spectra_dt')
  506. cmap = gray(256);
  507. [SFull t f]= mtdspecgramc(diff(data),handles.movingwin,0,params); SFull=SFull';
  508. elseif strcmp(handles.plotmode,'spectra_df')
  509. cmap = gray(256);
  510. [SFull t f]= mtdspecgramc(diff(data),handles.movingwin,pi/2,params); SFull=SFull';
  511. end
  512. end
  513. if strcmp(handles.plotmode,'spectra') || strcmp(handles.plotmode,'spectra_dt') || strcmp(handles.plotmode,'spectra_df')
  514. cmap(1,:) = [1, 1, 1];
  515. colormap(cmap);
  516. SFmin = min(min(SFull));
  517. SFmax = max(max(SFull));
  518. SFull = uint8(1 + round(255 * (SFull-SFmin) / (SFmax-SFmin)));
  519. hi = image(tfull + segment.start - handles.specpad,f,SFull);
  520. set(hi,'ButtonDownFcn',{@PlotModeCallBack});
  521. axis xy;
  522. hline1 = line([segment.start segment.start],[f(1) max(f)],'Color',[0 0 0],'LineWidth',3);
  523. hline2 = line([segment.end segment.end],[f(1),max(f)],'Color',[0 0 0],'LineWidth',3);
  524. else
  525. % xlim([tfull(1) tfull(length(tfull))] + segment.start - handles.specpad);
  526. hp = plot(segment.start - handles.specpad + [0:length(data)-1] / handles.Fs, data);
  527. set(handles.ToClassifyAxes,'YLim',[-5 5]);
  528. set(hp,'ButtonDownFcn',{@PlotModeCallBack});
  529. axis tight;
  530. dataspan = [min(data) max(data)];
  531. hline1 = line([segment.start segment.start],dataspan,'Color',[0 0 0],'LineWidth',3);
  532. hline2 = line([segment.end segment.end],dataspan,'Color',[0 0 0],'LineWidth',3);
  533. end
  534. axes(handles.ToClassifySmallAxes);
  535. ispecFull = uint8(zeros(handles.ispecheight,handles.ismaxwidth));
  536. ispecFull = copy_into(ispecFull,handles.IconList{handles.NextIndex},1,1);
  537. if length(ispecFull(1,:)) > handles.ismaxwidth
  538. ispecFull = ispecFull(:,1:handles.ismaxwidth);
  539. end
  540. if strcmp(get(handles.ZoomButton,'String'),'Zoom out')
  541. f = [handles.lowerfreq handles.upperfreq];
  542. end
  543. tsmall = [handles.movingwin(1),handles.ismaxwidth * handles.movingwin(2) - handles.movingwin(1)];
  544. %
  545. % cmap = jet(256);
  546. % cmap(1,:) = [1, 1, 1];
  547. %
  548. % colormap(cmap);
  549. % [0 (handles.ismaxwidth * (segment.end - segment.start))/length(iconS(1,:))]
  550. ih = image(tsmall,f,flipud(ispecFull));
  551. axis xy;
  552. ;
  553. function PlotModeCallBack(src,eventdata)
  554. % A Function for handling clicks to the axes
  555. handles = guidata(gcbo);
  556. handles.plotmodevalue = handles.plotmodevalue + 1;
  557. if handles.plotmodevalue > length(handles.plotmodes)
  558. handles.plotmodevalue = 1;
  559. end
  560. handles.plotmode = handles.plotmodes{handles.plotmodevalue};
  561. handles=get_and_plot(handles, handles.segments(handles.NextIndex));
  562. guidata(gcf,handles);
  563. function handles=ConfigureSpecPlot(handles)
  564. % Handles the gui configuration of the plotting
  565. segment = handles.segments(handles.NextIndex);
  566. if not(exist(segment.specfilename)) || handles.recompute
  567. precompute_spectra(handles,segment);
  568. end
  569. set(handles.ToClassifyPanel,'Title',['Segment ' num2str(handles.NextIndex) '/' num2str(handles.nsegments)])
  570. segmentstatus = ['File: "' segment.wavfile '"; Segment length ' num2str(segment.end - segment.start,3)];
  571. set(handles.SegmentText,'String',segmentstatus);
  572. handles = get_and_plot(handles, segment);
  573. guidata(gcbo,handles);
  574. ;
  575. function NextSpectraButton_Callback(hObject, eventdata, handles)
  576. % Moves the segment viewer forward one segment
  577. handles.NextIndex = handles.NextIndex + 1;
  578. if handles.NextIndex == handles.nsegments
  579. set(handles.NextSpectraButton,'Enable','off');
  580. end
  581. if handles.NextIndex > 1
  582. set(handles.PreviousSpectraButton,'Enable','on');
  583. end
  584. handles=ConfigureClassSegment(handles);
  585. handles=ConfigureSpecPlot(handles);
  586. guidata(gcbo,handles);
  587. ;
  588. function PreviousSpectraButton_Callback(hObject, eventdata, handles)
  589. % Moves the segment viewer backwards one segment
  590. handles.NextIndex = handles.NextIndex - 1;
  591. if handles.NextIndex == 1
  592. set(handles.PreviousSpectraButton,'Enable','off');
  593. set(handles.NextSpectraButton,'Enable','on');
  594. end
  595. if handles.NextIndex < handles.nsegments
  596. set(handles.NextSpectraButton,'Enable','on');
  597. end
  598. handles=ConfigureClassSegment(handles);
  599. handles=ConfigureSpecPlot(handles);
  600. set(handles.NextSpectraButton,'Enable','off');
  601. set(handles.NextSpectraButton,'Enable','on');
  602. guidata(gcbo,handles);
  603. ;
  604. function PrecomputeButton_Callback(hObject, eventdata, handles)
  605. % Call back for the precompute button. This acts to load the file from the directory
  606. handles.directory = get(handles.DirectoryEditBox,'String');
  607. % fprintf('creating syllable list\n');
  608. %handles.segments = segments;
  609. handles.classes = [];
  610. set(handles.PrecomputeButton, 'Enable', 'off' );
  611. handles.NextIndex = 1;
  612. handles = Load_InitialSegments(hObject,handles);
  613. handles.recompute = logical(0);
  614. if handles.nsegments >= 1
  615. handles=ConfigureClassSegment(handles);
  616. handles = load_configuration(handles,handles.configfile);
  617. if exist([handles.baseclassname '.dat']) && not(handles.configschanged)
  618. load('-mat','specicons');
  619. handles.IconList = IconList;
  620. data = read_syllable_database(handles);
  621. handles = merge_syllable_database(handles,data);
  622. else
  623. handles = precompute_AllSpectra(handles);
  624. end
  625. %set(handles.ConfigureButton, 'Enable','off');
  626. handles=ConfigureSpecPlot(handles);
  627. handles=BrowseDirectory(handles);
  628. handles.precomputed = logical(1);
  629. set(handles.PlaySegmentButton, 'Enable', 'on' );
  630. set(handles.NextSpectraButton, 'Enable', 'on' );
  631. set(handles.ModePopupMenu,'Enable','on');
  632. set(handles.SaveButton,'Enable','on');
  633. set(handles.SaveItem,'Enable','on');
  634. set(handles.PrecomputeButton, 'Enable', 'off' );
  635. set(handles.CleanDirectoryItem, 'Enable', 'off' );
  636. set(handles.LoadDirectoryButton,'Enable','off');
  637. set(handles.LoadItem,'Enable','off');
  638. set(handles.ZoomButton,'Enable','on');
  639. set(handles.QuickModeButton,'Enable','on');
  640. set(handles.CompareToggleButton,'Enable','on');
  641. set(handles.ConfigureButton,'Enable','on');
  642. set(handles.ConfigureItem,'Enable','on');
  643. set(handles.AutoClassifyButton,'Enable','on');
  644. handles.fixed = logical(0); % turn off fixed scaling
  645. if not(strcmp(handles.segments(handles.NextIndex).class,'')) % Set the classification status
  646. set(handles.ClassifyButton,'String','Declassify');
  647. end
  648. else
  649. set(handles.PrecomputeButton, 'Enable', 'on' );
  650. end
  651. guidata(gcbo,handles);
  652. ;
  653. % --- Executes on button press in PlaySegmentButton.
  654. function PlaySegmentButton_Callback(hObject, eventdata, handles)
  655. % Plays the current segment in the segment viewer
  656. % hObject handle to PlaySegmentButton (see GCBO)
  657. % eventdata reserved - to be defined in a future version of MATLAB
  658. % handles structure with handles and user data (see GUIDATA)
  659. segment = handles.segments(handles.NextIndex);
  660. data = wavread(segment.wavfile,[round(handles.Fs * segment.start),round(handles.Fs * segment.end)]);
  661. wavplay(data,handles.Fs,'async');
  662. function CurrentFilenameEdit_Callback(hObject, eventdata, handles)
  663. function CurrentFilenameEdit_CreateFcn(hObject, eventdata, handles)
  664. if ispc
  665. set(hObject,'BackgroundColor','white');
  666. else
  667. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  668. end
  669. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  670. % Functions for viewing spectra icon %
  671. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  672. function newwidth = SmallAxes_width(handles)
  673. position1 = get(handles.ToClassifySmallAxes,'Position');
  674. position2 = get(handles.ClassifiedAxes,'Position');
  675. newwidth = round(position1(3) *(handles.classified_width / position2(3)));
  676. function iconS = iconify_spec(S,height)
  677. % Take a large spectra with high frequency bandwidth and reduce the height
  678. % by pixel averaging
  679. Ssize = size(S);
  680. iconS = zeros(height,Ssize(2));
  681. % averaging of values to reduce size
  682. rf = floor(Ssize(1)/height);
  683. for i = 1:(height-1)
  684. for j = 1 : Ssize(2)
  685. iconS(i,j) = sum(S(((i-1)*rf)+1:i*rf,j))/rf;
  686. end
  687. end
  688. for j = 1 : Ssize(2) % take care of the last row by also pixel averaging
  689. iconS(height,j) = mean(S(rf*(height-1) : Ssize(1),j));
  690. end
  691. iconS = flipud(iconS);
  692. % Rescaling of values
  693. maxintense = max(max(iconS));
  694. minintense = min(min(iconS));
  695. iconS=uint8(1 + round(255 * (iconS-minintense)/(maxintense-minintense)));
  696. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  697. % Functions for loading in images %
  698. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  699. function destination = copy_into(destination,source,r,c)
  700. % This copies an image into another image
  701. % Two problems: one it can be optimized by removing the nested for loops
  702. % two there is an indexing bug which returns a one pixel larger image
  703. sized = size(destination);
  704. sizes = size(source);
  705. destination(r:r+sizes(1)-1,c:c+sizes(2)-1) = source(:,:);
  706. % for i = 1:sizes(1)
  707. % for j = 1:sizes(2)
  708. % destination(r+i,c+j) = source(i,j);
  709. % end
  710. % end
  711. % ;
  712. function positions = position_images(height,width,images_dim,xspacer,yspacer)
  713. % This function returns a matrix consisting of two rows with the xy
  714. % position for images.
  715. % The function also assumes that the image's height is not restricted this
  716. % allows for easier scrolling
  717. % The function assumes that all images are of the same height
  718. % height in pixels of the original
  719. % width in pixels of the original
  720. % images
  721. % xspacer in pixels for the horizontal space between images
  722. % yspacer is the next height of the image
  723. % image height is the fixed height of the images
  724. number_images = length(images_dim(:,1));
  725. imageheight = images_dim(1);
  726. currentx = xspacer;
  727. currenty = yspacer;
  728. positions = zeros(number_images,2);
  729. for i = 1:number_images
  730. if (currentx + images_dim(i,2) + xspacer) > width % start a new row
  731. currentx = xspacer;
  732. currenty = currenty + imageheight + yspacer;
  733. positions(i,:) = [currenty currentx];
  734. currentx = currentx + images_dim(i,2) + xspacer;
  735. else
  736. positions(i,:) = [currenty currentx];
  737. currentx = currentx + images_dim(i,2) + xspacer;
  738. end
  739. %positions(i,:) = [currenty currentx];
  740. end
  741. % function image_matrix = place_images_into(image_matrix, image_list, position_list)
  742. % % Place images into a matrix
  743. % number_images = length(image_list);
  744. %
  745. % for i = 1:number_images
  746. % theimage = image_list{i};
  747. % image_matrix = copy_into(image_matrix, theimage, position_list(i,1), position_list(i,2));
  748. % end
  749. % ;
  750. function image_dim = get_image_sizes(images)
  751. % Returns an array of image sizes
  752. number_images = length(images);
  753. image_dim = zeros(number_images,2);
  754. for i = 1:number_images
  755. image_dim(i,:) = size(images{i});
  756. end
  757. ;
  758. function image_matrix = place_images_into(image_matrix, image_list, position_list)
  759. % Place images into a matrix
  760. number_images = length(image_list);
  761. for i = 1:number_images
  762. theimage = image_list{i};
  763. image_matrix = copy_into(image_matrix, theimage, position_list(i,1), position_list(i,2));
  764. end
  765. ;
  766. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  767. % Functions for manipulating and plotting spectra icons %
  768. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  769. function IconList=get_SpecIcons(handles)
  770. IconList = {};
  771. status = 0;
  772. for i = 1:handles.nsegments
  773. load('-mat',handles.segments(i).specfilename);
  774. IconList{i} = iconS;
  775. end
  776. save('specicons','IconList','-mat'); % temp code for saving
  777. function handles = plot_classified_axes(handles, image_list, position_list)
  778. % Low level drawing of the classified axes
  779. handles.blank = logical(0);
  780. handles.classmatrix = uint8(zeros(handles.classified_height,handles.classified_width));
  781. axes(handles.ClassifiedAxes);
  782. handles.classmatrix = place_images_into(handles.classmatrix,image_list,position_list);
  783. set(handles.ClassifiedAxes,'XTick',[]);
  784. set(handles.ClassifiedAxes,'YTick',[]);
  785. handles.max_width = length(handles.classmatrix(1,:));
  786. if handles.max_width > handles.classified_width
  787. set(handles.SliderClassified,'Min',0);
  788. set(handles.SliderClassified,'Max',handles.max_width - handles.classified_width);
  789. set(handles.SliderClassified,'enable','on');
  790. handles.endx = handles.classified_width;
  791. else
  792. set(handles.SliderClassified,'enable','off')
  793. handles.startx = 1;
  794. handles.endx = handles.classified_width;
  795. end
  796. classview = handles.classmatrix(:,handles.startx:handles.endx); % will cut overhang
  797. handles.hiclass = image(classview);
  798. set(handles.hiclass,'ButtonDownFcn',{@ClassifyAxesClickCallBack});
  799. set(handles.ClassifiedAxes,'XTick',[]);
  800. set(handles.ClassifiedAxes,'YTick',[]);
  801. setrowbuttons(handles);
  802. function handles = reposition_images(handles, image_list)
  803. % this is a lower level function which is called to reposition the images.
  804. % this would be called from higher level functions when images are added,
  805. % deleted, or a new list of images needs to be loaded.
  806. % initialize the handles for the images
  807. handles.nimages = length(image_list);
  808. handles.images_dim = get_image_sizes(image_list);
  809. handles.positions = position_images(handles.classified_height,handles.classified_width,handles.images_dim,handles.xspacer,handles.yspacer);
  810. handles.cnrows = length(unique(handles.positions(:,1)));
  811. % Setup the first page view
  812. handles.number_rows = floor(handles.classified_height / (handles.ispecheight + handles.yspacer));
  813. handles.startpage = 1;
  814. handles.endpage = 0;
  815. for i = 1 : handles.number_rows
  816. handles.endpage = next_row_end(handles.positions,handles.endpage);
  817. end
  818. %guidata(gcbo,handles);
  819. ;
  820. function nrow = which_row(positions,index)
  821. nrow = 1;
  822. i = 2;
  823. while i <= index
  824. if not(positions(i,1) == positions(i-1,1))
  825. nrow = nrow + 1;
  826. end
  827. i = i + 1;
  828. end
  829. function cpositions = get_curr_position(handles)
  830. % Setups the current view of the positions
  831. cpositions = handles.positions(handles.startpage:handles.endpage,:); % get the current view
  832. cpositions(:,1) = cpositions(:,1) - cpositions(1,1) + handles.yspacer;
  833. cpositions(:,2) = cpositions(:,2) - (handles.startx - 1);
  834. ;
  835. function cposition = next_row_start(positions,cposition)
  836. % Computes the position of the next row if the row based on the positions matrix
  837. % it computes the position where the row starts
  838. npos = length(positions);
  839. i = cposition;
  840. while (i <= npos) && positions(i,1) == positions(cposition,1)
  841. i = i + 1;
  842. end
  843. if i < npos % make sure the row numbers match
  844. cposition = i;
  845. end
  846. ;
  847. function cposition = next_row_end(positions,cposition)
  848. % Computes the position of the next row if the row based on the positions matrix
  849. % it computes the last position before a new row starts
  850. npos = length(positions(:,1));
  851. if cposition < npos % not at the last row
  852. i = cposition + 1;
  853. while (i < npos) && (positions(i,1) == positions(cposition+1,1))
  854. i = i + 1;
  855. end
  856. if (positions(cposition + 1) == positions(npos))
  857. cposition = npos; % handle the condition that you are now at the last row
  858. else
  859. cposition = i - 1; % make sure the row numbers match
  860. end
  861. else % handles the condition you are already at the last row
  862. cposition = npos;
  863. end
  864. ;
  865. function handles = row_forward(handles)
  866. % Moves the row forward in the classifiedaxes/browser view
  867. startpage = next_row_start(handles.positions,handles.startpage);
  868. endpage = next_row_end(handles.positions,handles.endpage);
  869. if not(endpage == handles.endpage) % indicates you are not at the last page
  870. handles.startpage = startpage;
  871. handles.endpage = endpage;
  872. end
  873. % guidata(gcbo,handles);
  874. ;
  875. function cposition = previous_row_start(positions,cposition)
  876. % Computes the position of the previous row if the row based on the positions matrix
  877. % it computes the position where the row starts
  878. npos = length(positions(:,1));
  879. if cposition > 1
  880. i = cposition - 1;
  881. while (i > 1) && (positions(i,1) == positions(cposition-1,1))
  882. i = i - 1;
  883. end
  884. if (i > 1) && (cposition ~= 2)
  885. cposition = i + 1;
  886. else
  887. cposition = 1;
  888. end
  889. else
  890. cposition = 1; % in case things get missed up and neg index
  891. end
  892. ;
  893. function cposition = previous_row_end(positions,cposition)
  894. npos = length(positions(:,1));
  895. if cposition > 1
  896. i = cposition;
  897. while (i > 1) && (positions(i,1) == positions(cposition,1))
  898. i = i - 1;
  899. end
  900. if i > 1;
  901. cposition = i;
  902. else
  903. cposition = 1;
  904. end
  905. else
  906. cposition = 1;
  907. end
  908. ;
  909. function nrows = number_of_rows(handles)
  910. % Computes the number of rows in the current view
  911. nrows = length(unique(handles.positions(handles.startpage:handles.endpage,1)));
  912. function handles = row_backward(handles)
  913. % Moves the row backwards in the classifiedaxes/browser view
  914. startpage = previous_row_start(handles.positions,handles.startpage);
  915. endpage = previous_row_end(handles.positions,handles.endpage);
  916. if number_of_rows(handles) < handles.number_rows % indicates you are not at the last page
  917. handles.startpage = startpage;
  918. handles.endpage = length(handles.positions(:,1));
  919. elseif handles.startpage == 1;
  920. handles.startpage = 1;
  921. handles.endpage = handles.endpage;
  922. else
  923. handles.startpage = startpage;
  924. handles.endpage = endpage;
  925. end
  926. % guidata(gcbo,handles);
  927. ;
  928. function handles = BrowseDirectory(handles)
  929. % This function should be called only after precompute has been called
  930. % it relies on their being a specicon file in the directory
  931. %
  932. % The function takes the current segments in the directory and loads their specicons
  933. % into memory because the segments and icons are created in their order the
  934. % order matches. This will need to be worked out better for the
  935. % classification algorithms.
  936. %load('-mat', 'specicons');
  937. set_status(handles, ['Viewing all ', num2str(handles.nsegments),' segments']);
  938. handles.mapindex = [1:handles.nsegments];
  939. handles.image_list = handles.IconList;
  940. handles = reposition_images(handles, handles.image_list);
  941. handles.cpositions = get_curr_position(handles);
  942. handles = plot_classified_axes(handles, handles.image_list(handles.startpage:handles.endpage), handles.cpositions);
  943. handles.mode='browse';
  944. %guidata(gcbo,handles);
  945. % --- Executes on button press in NextRowButton.
  946. function NextRowButton_Callback(hObject, eventdata, handles)
  947. % hObject handle to NextRowButton (see GCBO)
  948. % eventdata reserved - to be defined in a future version of MATLAB
  949. % handles structure with handles and user data (see GUIDATA)
  950. if handles.cnrows > 1
  951. handles.startx = 1; % if you have been scrolling reset to defaults
  952. handles.endx = handles.classified_width;
  953. set(handles.SliderClassified, 'Value',0);
  954. handles = row_forward(handles);
  955. handles.cpositions = get_curr_position(handles);
  956. handles = plot_classified_axes(handles, handles.image_list(handles.startpage:handles.endpage), handles.cpositions);
  957. end
  958. guidata(gcbo,handles);
  959. % --- Executes on button press in PreviousRowButton.
  960. function PreviousRowButton_Callback(hObject, eventdata, handles)
  961. % hObject handle to PreviousRowButton (see GCBO)
  962. % eventdata reserved - to be defined in a future version of MATLAB
  963. % handles structure with handles and user data (see GUIDATA)
  964. if handles.cnrows > 1
  965. handles.startx = 1;
  966. handles.endx = handles.classified_width;
  967. set(handles.SliderClassified, 'Value',0);
  968. handles = row_backward(handles);
  969. handles.cpositions = get_curr_position(handles);
  970. handles = plot_classified_axes(handles, handles.image_list(handles.startpage:handles.endpage), handles.cpositions);
  971. end
  972. guidata(gcbo,handles);
  973. % --- Executes on button press in LoadDirectoryButton.
  974. function LoadDirectoryButton_Callback(hObject, eventdata, handles)
  975. % hObject handle to LoadDirectoryButton (see GCBO)
  976. % eventdata reserved - to be defined in a future version of MATLAB
  977. % handles structure with handles and user data (see GUIDATA)
  978. %directoryname = uigetdir('./','Change directory');
  979. directoryname = uigetdir;
  980. if not(directoryname == 0)
  981. handles.directory = directoryname;
  982. set(handles.DirectoryEditBox, 'String',directoryname);
  983. cd(handles.directory); % this we will need to change
  984. end
  985. guidata(gcbo,handles);
  986. ;
  987. function im_index = coordinate2index(handles,xpos,ypos)
  988. % For the current images displayed tests if pointer position is in an image
  989. % returns the index for that image based on a left to right ordering on
  990. % that page
  991. % First check if the coordinate is totally out of the range
  992. if (xpos < 0) || (ypos < 0) || (ypos > handles.classified_height) || (xpos > handles.classified_width)
  993. im_index = 0;
  994. else
  995. im_index = 0;
  996. npos = length(handles.cpositions(:,1));
  997. i = 1;
  998. while (i <= npos) && (im_index < 1)
  999. if (xpos >= handles.cpositions(i,2)) && (xpos <= (handles.cpositions(i,2) + handles.images_dim(handles.startpage + (i-1), 2)))
  1000. if (ypos >= handles.cpositions(i,1)) && (ypos <= (handles.cpositions(i,1) + handles.images_dim(handles.startpage + (i-1),1)))
  1001. im_index = i;
  1002. end
  1003. end
  1004. i = i+1;
  1005. end
  1006. end
  1007. ;
  1008. function setnavigationbuttons(handles)
  1009. % Sets the navigation buttons based on where the pointer is
  1010. if not(handles.quickmode)
  1011. if handles.NextIndex == handles.nsegments
  1012. set(handles.PreviousSpectraButton,'Enable','off');
  1013. set(handles.NextSpectraButton,'Enable','off');
  1014. elseif handles.NextIndex == handles.nsegments
  1015. set(handles.NextSpectraButton,'Enable','off');
  1016. set(handles.PreviousSpectraButton,'Enable','on');
  1017. elseif handles.NextIndex == 1
  1018. set(handles.PreviousSpectraButton,'Enable','off');
  1019. set(handles.NextSpectraButton,'Enable','on');
  1020. elseif (handles.NextIndex > 1) && (handles.NextIndex < handles.nsegments)
  1021. set(handles.PreviousSpectraButton,'Enable','on');
  1022. set(handles.NextSpectraButton,'Enable','on');
  1023. end
  1024. end
  1025. function setrowbuttons(handles)
  1026. if handles.startpage == 1
  1027. set(handles.PreviousRowButton,'Enable','off');
  1028. elseif handles.startpage > 1
  1029. set(handles.PreviousRowButton,'Enable','on');
  1030. end
  1031. if handles.endpage == length(handles.positions) % You are the last row
  1032. set(handles.NextRowButton,'Enable','off');
  1033. elseif handles.cnrows <= handles.number_rows
  1034. set(handles.NextRowButton,'Enable','off');
  1035. elseif handles.endpage < length(handles.positions)
  1036. set(handles.NextRowButton,'Enable','on');
  1037. end
  1038. function DummyClassifyAxesClickCallBack(src,eventdata,handles)
  1039. % When the image is blank this allow you to select out of the class view
  1040. set(handles.ClassifyButton,'Enable','on');
  1041. set(handles.NewClassButton,'Visible','off');
  1042. set(handles.NextSpectraButton,'Enable','on');
  1043. set(handles.PreviousSpectraButton,'Enable','on');
  1044. set(handles.ModePopupMenu,'Enable','on');
  1045. set(handles.AutoClassifyButton,'Enable','on');
  1046. set(handles.QuickModeButton,'Enable','on');
  1047. set(handles.CompareToggleButton,'Enable','on');
  1048. handles.submode = 'select';
  1049. setnavigationbuttons(handles);
  1050. guidata(gcbo,handles);
  1051. function ClassifyAxesClickCallBack(src,eventdata)
  1052. % A Function for handling clicks to the axes
  1053. handles = guidata(gcbo);
  1054. % handles.mode
  1055. %handles.submode
  1056. %fprintf('\n');
  1057. pos = get(handles.ClassifiedAxes,'CurrentPoint');
  1058. cposition = coordinate2index(handles,pos(1,1),pos(1,2));
  1059. if handles.quickmode
  1060. if cposition == 0 % Selecting in the outside takes you out of classification mode
  1061. set_status(handles,'');
  1062. else % You have selected an icon
  1063. handles.startx = 1; % if you have been scrolling reset to defaults
  1064. handles.endx = handles.classified_width;
  1065. set(handles.SliderClassified,'Value',0);
  1066. class = handles.classes(handles.mapindex(cposition + (handles.startpage - 1)));
  1067. handles.segments(handles.NextIndex).class = class.name;
  1068. handles.classes(handles.mapindex(cposition + (handles.startpage - 1))).nmembers = handles.classes(handles.mapindex(cposition + (handles.startpage - 1))).nmembers + 1;
  1069. handles = jump_to_unclassified(handles);
  1070. handles = configureclassview(handles,'select-class');
  1071. if handles.lastsegment == handles.NextIndex % no more unclassified segments
  1072. handles = quick_mode_exit(handles);
  1073. set(handles.QuickModeButton,'Value',0);
  1074. handles.quickmode = not(handles.quickmode);
  1075. end
  1076. end
  1077. else % quick classify mode is off
  1078. if strcmp(handles.mode,'browse') && (cposition > 0)
  1079. handles.NextIndex = handles.mapindex((cposition - 1) + handles.startpage);
  1080. handles=ConfigureClassSegment(handles);
  1081. handles=ConfigureSpecPlot(handles);
  1082. else
  1083. %fprintf('%i\n', coordinate2index(handles,pos(1,1),pos(1,2)));
  1084. %fprintf('%i, %i\n\n', pos(1,1),pos(1,2));
  1085. ;
  1086. end
  1087. if strcmp('class-members',handles.mode) && strcmp('select',handles.submode) && (cposition > 0)
  1088. handles.NextIndex = handles.mapindex((cposition - 1) + handles.startpage);
  1089. handles=ConfigureClassSegment(handles);
  1090. handles=ConfigureSpecPlot(handles);
  1091. end
  1092. %Show all members of a specific class
  1093. if strcmp('class-view',handles.mode) && strcmp('select',handles.submode) && (cposition > 0)
  1094. handles.startx = 1; % if you have been scrolling reset to defaults
  1095. handles.endx = handles.classified_width;
  1096. set(handles.SliderClassified,'Value',0);
  1097. % set(handles.CompareToggleButton,'Visible','off');
  1098. cindex = handles.mapindex(cposition + (handles.startpage - 1));
  1099. class = handles.classes(cindex);
  1100. handles.lastclass = cindex;
  1101. handles = configureclassmembers(handles,class.name);
  1102. handles.mode = 'class-members';
  1103. handles.submode = 'select';
  1104. handles = SetModePopupMenu(handles,'class members');
  1105. set(handles.TypifyClassButton,'Visible','on');
  1106. set(handles.RemoveClassButton,'Visible','off');
  1107. set(handles.NextClassButton,'Visible','on');
  1108. set(handles.RenameClassButton,'Visible','on');
  1109. set_status(handles, ['Viewing ' num2str(length(handles.mapindex)),' members of ' class.name]);
  1110. end
  1111. if strcmp('class-view',handles.mode) && strcmp('select-class',handles.submode)
  1112. if cposition == 0 % Selecting in the outside takes you out of classification mode
  1113. set(handles.ClassifyButton,'Enable','on');
  1114. set(handles.NewClassButton,'Visible','off');
  1115. set_status(handles, ['Viewing all ', num2str(handles.nclasses),' classes']);
  1116. else % You have selected an icon
  1117. handles.startx = 1; % if you have been scrolling reset to defaults
  1118. handles.endx = handles.classified_width;
  1119. set(handles.SliderClassified,'Value',0);
  1120. class = handles.classes(handles.mapindex(cposition + (handles.startpage - 1)));
  1121. handles.segments(handles.NextIndex).class = class.name;
  1122. handles.classes(handles.mapindex(cposition + (handles.startpage - 1))).nmembers = handles.classes(handles.mapindex(cposition + (handles.startpage - 1))).nmembers + 1;
  1123. set(handles.ClassifyButton,'String','Declassify');
  1124. handles = SetModePopupMenu(handles,'class members');
  1125. handles = configureclassmembers(handles,class.name);
  1126. handles.mode = 'class-members';
  1127. handles.submode = 'select';
  1128. set_status(handles,['Classified segment as ' class.name]);
  1129. end
  1130. set(handles.CompareToggleButton,'Enable','on');
  1131. set(handles.QuickModeButton,'Enable','on');
  1132. set(handles.AutoClassifyButton,'Enable','on');
  1133. set(handles.ClassifyButton,'Enable','on');
  1134. set(handles.NewClassButton,'Visible','off');
  1135. set(handles.NextSpectraButton,'Enable','on');
  1136. set(handles.PreviousSpectraButton,'Enable','on');
  1137. set(handles.ModePopupMenu,'Enable','on');
  1138. set(handles.SortText,'Visible','off');
  1139. set(handles.SortPopupMenu,'Visible','off');
  1140. handles.submode = 'select';
  1141. end
  1142. if strcmp(handles.mode,'class-members') && strcmp(handles.submode,'typify')
  1143. if cposition == 0
  1144. set(handles.TypifyClassButton, 'Enable','on');
  1145. else
  1146. handles.startx = 1; % if you have been scrolling reset to defaults
  1147. handles.endx = handles.classified_width;
  1148. set(handles.SliderClassified,'Value',0);
  1149. sindex = handles.mapindex(cposition + (handles.startpage - 1));
  1150. cindex = returnclassindex(handles,handles.segments(sindex).class);
  1151. handles.classes(cindex).specfilename = handles.segments(sindex).specfilename;
  1152. handles.classes(cindex).length = handles.segments(sindex).end - handles.segments(sindex).start;
  1153. handles.classes(cindex).index = sindex;
  1154. %load('-mat','specicons');
  1155. handles.classes(cindex).iconS = handles.IconList{sindex};
  1156. handles.subclass = 'xxx';
  1157. set(handles.TypifyClassButton, 'Enable','on');
  1158. set_status(handles,'');
  1159. end
  1160. end
  1161. if strcmp('class-view', handles.mode) && strcmp('compare',handles.submode)
  1162. if cposition == 0
  1163. handles.mode = 'class-view';
  1164. handles.submode = 'select';
  1165. set(handles.CompareToggleButton,'Enable','on');
  1166. set(handles.RemoveClassButton,'Enable','on');
  1167. set(handles.NextSpectraButton,'Enable','on');
  1168. set(handles.PreviousSpectraButton,'Enable','on');
  1169. %set(handles.ModePopupMenu,'Enable','on');
  1170. else
  1171. handles.mode = 'comparison';
  1172. if strcmp(handles.segments(handles.NextIndex).class,'')
  1173. set(handles.ClassifyButton,'Enable','on');
  1174. end
  1175. handles.startx = 1; % if you have been scrolling reset to defaults
  1176. handles.endx = handles.classified_width;
  1177. set(handles.SliderClassified,'Value',0);
  1178. cindex = handles.mapindex(cposition + (handles.startpage - 1));
  1179. handles.image_list = {};
  1180. handles.lastclass = cindex;
  1181. handles.image_list{2} = handles.classes(cindex).iconS;
  1182. %load('-mat','specicons');
  1183. handles.image_list{1} = handles.IconList{handles.NextIndex};
  1184. set_status(handles,['Comparing to ' handles.classes(cindex).name]);
  1185. handles = reposition_images(handles, handles.image_list);
  1186. handles.cpositions = get_curr_position(handles);
  1187. handles = plot_classified_axes(handles, handles.image_list(handles.startpage:handles.endpage), handles.cpositions);
  1188. handles.submode = 'xxx';
  1189. end
  1190. end
  1191. if strcmp(handles.mode,'class-view') && strcmp(handles.submode,'remove-class')
  1192. if cposition == 0
  1193. handles.submode = 'select';
  1194. set(handles.RemoveClassButton, 'Enable','on');
  1195. set_status(handles, ['Viewing all ', num2str(handles.nclasses),' classes']);
  1196. else
  1197. cindex = handles.mapindex(cposition + (handles.startpage - 1));
  1198. classname = handles.classes(cindex).name;
  1199. answer = questdlg(['Remove class ' classname]);
  1200. if strcmp(answer,'Yes')
  1201. handles.startx = 1; % if you have been scrolling reset to defaults
  1202. handles.endx = handles.classified_width;
  1203. set(handles.SliderClassified,'Value',0);
  1204. for i = 1:handles.nsegments
  1205. if strcmp(classname,handles.segments(i).class)
  1206. handles.segments(i).class = '';
  1207. end
  1208. end
  1209. if handles.nclasses > 1
  1210. handles.classes = [handles.classes(1:(cindex-1)) handles.classes((cindex+1):handles.nclasses)];
  1211. handles.nclasses = handles.nclasses - 1;
  1212. handles = configureclassview(handles,'select');
  1213. set_status(handles, ['Viewing all ', num2str(handles.nclasses),' classes']);
  1214. else
  1215. handles.classes = [];
  1216. handles.nclasses = 0;
  1217. handles.image_list = {};
  1218. set_status(handles,'');
  1219. set(handles.RemoveClassButton,'Visible','off');
  1220. handles.submode = 'xxx';
  1221. handles = blankaxes(handles);
  1222. end
  1223. if strcmp(handles.segments(handles.NextIndex).class,'')
  1224. set(handles.ClassifyButton,'String','Classify');
  1225. end
  1226. set(handles.RemoveClassButton, 'Enable','on');
  1227. else
  1228. handles.submode = 'select';
  1229. set(handles.RemoveClassButton, 'Enable','on');
  1230. set_status(handles, ['Viewing all ', num2str(handles.nclasses),' classes']);
  1231. end
  1232. end
  1233. end
  1234. end
  1235. if not(strcmp(handles.mode,'comparison'))
  1236. setnavigationbuttons(handles);
  1237. end
  1238. % handles.mode
  1239. % handles.submode
  1240. guidata(gcf,handles);
  1241. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1242. % Functions for setting up the classview %
  1243. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1244. function handles = configureclassview(handles,submode)
  1245. % Configures the class-view for selecting classes
  1246. handles.mode = 'class-view';
  1247. handles.submode = submode;
  1248. % if not(handles.quickmode)
  1249. % set_status(handles, ['Select a class']);
  1250. % end
  1251. handles.mapindex = [1 : handles.nclasses];
  1252. if strcmp(handles.sortclass, 'popularity')
  1253. handles.mapindex = sortindexbypop(handles);
  1254. elseif strcmp(handles.sortclass,'length');
  1255. handles.mapindex = sortindexbylength(handles);
  1256. else
  1257. handles.mapindex = [1 : handles.nclasses];
  1258. end
  1259. handles = cindex2imagelist(handles);
  1260. handles = reposition_images(handles, handles.image_list);
  1261. if (strcmp(handles.sortclass,'length') && strcmp(handles.submode,'select-class')) || (strcmp(handles.sortclass,'length') && strcmp(handles.submode,'compare'))
  1262. % jump to the segment with the closest size match
  1263. i = 1;
  1264. while (i <= handles.nclasses) && (handles.classes(handles.mapindex(i)).length >= (handles.segments(handles.NextIndex).end - handles.segments(handles.NextIndex).start))
  1265. i = i + 1;
  1266. end
  1267. cnrow = which_row(handles.positions,i-1); % get the current row
  1268. if cnrow > handles.number_rows % The closes size segment is not in view
  1269. for i = 1:(cnrow - handles.number_rows) % matching size row is last
  1270. handles = row_forward(handles);
  1271. end
  1272. if not(handles.endpage == length(handles.positions)) % if not at the last row position so that larger and smaller rows match
  1273. nrows = floor(handles.number_rows / 2);
  1274. for i = 1:nrows
  1275. handles = row_forward(handles);
  1276. end
  1277. end
  1278. end
  1279. end
  1280. handles.cpositions = get_curr_position(handles);
  1281. handles = plot_classified_axes(handles, handles.image_list(handles.startpage:handles.endpage), handles.cpositions);
  1282. %guidata(gcbo,handles);
  1283. ;
  1284. function handles = SetModePopupMenu(handles,viewstring)
  1285. popmodes = get(handles.ModePopupMenu,'String');
  1286. find_index = 0;
  1287. i = 1;
  1288. while (i <= length(popmodes)) && not(strcmp(popmodes(i),viewstring))
  1289. i = i + 1;
  1290. end
  1291. if i <= length(popmodes) % Don't do anything if the string cannot be found
  1292. set(handles.ModePopupMenu,'Value',[i]);
  1293. end
  1294. % --- Executes on selection change in ModePopupMenu.
  1295. function ModePopupMenu_Callback(hObject, eventdata, handles)
  1296. % Configure call back
  1297. % hObject handle to ModePopupMenu (see GCBO)
  1298. % eventdata reserved - to be defined in a future version of MATLAB
  1299. % handles structure with handles and user data (see GUIDATA)
  1300. % Hints: contents = get(hObject,'String') returns ModePopupMenu contents as cell array
  1301. % contents{get(hObject,'Value')} returns selected item from ModePopupMenu
  1302. handles.startx = 1; % if you have been scrolling reset to defaults
  1303. handles.endx = handles.classified_width;
  1304. set(handles.SliderClassified,'Value',0);
  1305. %set(handles.CompareToggleButton,'Visible','off');
  1306. set(handles.RenameClassButton, 'Visible', 'off');
  1307. nmode = get(hObject,'Value');
  1308. modeview = get(hObject,'String');
  1309. if strcmp(modeview(nmode),'all')
  1310. handles.mode = 'browse';
  1311. handles.submode = 'select';
  1312. handles=BrowseDirectory(handles);
  1313. set(handles.RemoveClassButton,'Visible','off');
  1314. set(handles.TypifyClassButton, 'Visible', 'off');
  1315. set(handles.NextClassButton,'Visible','off');
  1316. elseif strcmp(modeview(nmode),'class view')
  1317. set(handles.NextClassButton,'Visible','off');
  1318. if handles.nclasses >= 1
  1319. handles = configureclassview(handles,'select');
  1320. handles.mode = 'class-view';
  1321. handles.submode = 'select';
  1322. set(handles.RemoveClassButton,'Visible','on');
  1323. set(handles.RemoveClassButton,'Enable','on');
  1324. set(handles.CompareToggleButton,'Visible','on');
  1325. set(handles.CompareToggleButton,'Enable','on');
  1326. set(handles.TypifyClassButton, 'Visible', 'off');
  1327. set_status(handles,['Viewing all ' num2str(handles.nclasses) ' classes'])
  1328. else % empty axes
  1329. handles = blankaxes(handles);
  1330. handles.mode = 'class-view';
  1331. handles.submode = 'xxx';
  1332. set_status(handles,'');
  1333. end
  1334. elseif strcmp(modeview(nmode),'unclassified') || (strcmp(handles.segments(handles.NextIndex).class,'') && strcmp(modeview(nmode),'class members'))
  1335. handles = configureclassmembers(handles,'');
  1336. set_status(handles,['A total of ' num2str(length(handles.mapindex)) ' unclassified segments ']);
  1337. handles = SetModePopupMenu(handles,'unclassified');
  1338. handles.mode = 'browse';
  1339. handles.submode = 'select';
  1340. set(handles.RemoveClassButton, 'Visible', 'off');
  1341. set(handles.TypifyClassButton,'Visible','off');
  1342. set(handles.NextClassButton,'Visible','off');
  1343. elseif strcmp(modeview(nmode),'class members')
  1344. handles = configureclassmembers(handles,handles.segments(handles.NextIndex).class);
  1345. set_status(handles,['Viewing ' num2str(length(handles.mapindex)) ' members of ' handles.segments(handles.NextIndex).class]);
  1346. handles.mode = 'class-members';
  1347. handles.submode = 'select';
  1348. handles.lastclass = get_class_index(handles,handles.segments(handles.NextIndex).class);
  1349. set(handles.TypifyClassButton,'Visible', 'on');
  1350. set(handles.RemoveClassButton, 'Visible', 'off');
  1351. set(handles.RenameClassButton,'Visible','on');
  1352. set(handles.NextClassButton,'Visible','on');
  1353. end
  1354. guidata(gcbo,handles);
  1355. % --- Executes during object creation, after setting all properties.
  1356. function ModePopupMenu_CreateFcn(hObject, eventdata, handles)
  1357. % hObject handle to ModePopupMenu (see GCBO)
  1358. % eventdata reserved - to be defined in a future version of MATLAB
  1359. % handles empty - handles not created until after all CreateFcns called
  1360. % Hint: popupmenu controls usually have a white background on Windows.
  1361. % See ISPC and COMPUTER.
  1362. if ispc
  1363. set(hObject,'BackgroundColor','white');
  1364. else
  1365. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  1366. end
  1367. function set_status(handles, statusstring)
  1368. set(handles.SegInfoText,'String',statusstring);
  1369. set(handles.SegInfoText,'Visible','on');
  1370. ;
  1371. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1372. % Functions for filtering by class type %
  1373. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1374. function handles = configureclassmembers(handles,classname)
  1375. handles.mapindex = select_class(handles,handles.segments,classname);
  1376. if length(handles.mapindex) > 0
  1377. handles.image_list = {};
  1378. %load('-mat','specicons');
  1379. for i = 1:length(handles.mapindex)
  1380. handles.image_list(i) = handles.IconList(handles.mapindex(i));
  1381. end
  1382. handles = reposition_images(handles, handles.image_list);
  1383. handles.cpositions = get_curr_position(handles);
  1384. %
  1385. %handles.mode = 'class-members';
  1386. handles = plot_classified_axes(handles, handles.image_list(handles.startpage:handles.endpage), handles.cpositions);
  1387. %handles = SetModePopupMenu(handles,'class members');
  1388. else
  1389. handles = blankaxes(handles);
  1390. %handles.mode = 'class-view';
  1391. %handles.submode = 'select';
  1392. set_status(handles,'');
  1393. end
  1394. ;
  1395. function indexfilter = select_class(handles,segments,classname)
  1396. % Returns an index array of original addresses of segments which are members of a
  1397. % specified class
  1398. indexfilter = [];
  1399. nclassmembers = 0;
  1400. for i = 1:handles.nsegments
  1401. if strcmp(segments(i).class,classname)
  1402. nclassmembers = nclassmembers + 1;
  1403. indexfilter(nclassmembers) = i;
  1404. end
  1405. end
  1406. function cindex = get_class_index(handles,classname);
  1407. cindex = 0;
  1408. i = 1;
  1409. while (i <= handles.nclasses) && not(strcmp(handles.classes(i).name,classname))
  1410. i = i + 1;
  1411. end
  1412. cindex = i;
  1413. function value = mapind(index)
  1414. % Map the index value back to its original value
  1415. value = handles.mapindex(index);
  1416. %%&& not(strcmp(segments(i).class,''));
  1417. function write_syllable_database(handles)
  1418. filename = [handles.baseclassname '.dat'];
  1419. fid = fopen(filename,'wt');
  1420. classes = handles.classes;
  1421. if fid > -1
  1422. for i = 1:handles.nsegments
  1423. segment = handles.segments(i);
  1424. wavfile = segment.wavfile;
  1425. specfilename = segment.specfilename;
  1426. seg = [ num2str(segment.start) '\t' num2str(segment.end)];
  1427. classname = [ segment.class ];
  1428. typify = '';
  1429. nclasses = length(classes);
  1430. j = 1;
  1431. while (nclasses > 0) && (j <= nclasses) && not(strcmp(segment.specfilename,classes(j).specfilename))
  1432. j = j + 1;
  1433. end
  1434. if j <= length(classes) % found a match
  1435. classes = [classes(1:j-1) classes(j+1:nclasses)]; % shorten the classes
  1436. typify = '*'; % indicates that this is typological class
  1437. end
  1438. fprintf(fid,[specfilename '\t' wavfile '\t' seg '\t' classname '\t' typify '\n']);
  1439. end
  1440. fclose(fid);
  1441. end
  1442. ;
  1443. function data = read_syllable_database(handles)
  1444. filename = [handles.baseclassname '.dat'];
  1445. fid = fopen(filename,'rt');
  1446. data = textscan(fid,'%s %s %n %n %s %s', 'delimiter','\t');
  1447. data = [data(1), data(5), data(6)]; % throw out extra stuff which will be useful for external analysis
  1448. fclose(fid);
  1449. ;
  1450. function handles = merge_syllable_database(handles,data)
  1451. % Merge the syllable list with the loaded database
  1452. %load('-mat','specicons');
  1453. specfilenames = data{1};
  1454. classnames = data{2};
  1455. typifies = data{3};
  1456. ndata = length(specfilenames);
  1457. %classnum = 1;
  1458. handles.classes = [];
  1459. handles.nclasses = 0;
  1460. for i = 1:ndata
  1461. j=1; % allow for no matches and allow for
  1462. while (j < handles.nsegments) && not(strcmp(specfilenames(i),handles.segments(j).specfilename))
  1463. j = j + 1;
  1464. end
  1465. if j <= handles.nsegments
  1466. handles.segments(j).class = classnames{i};
  1467. if strcmp(typifies(i),'*') % this is the type class
  1468. %classnum = classnum + 1;
  1469. class.specfilename = specfilenames{i};
  1470. class.name = classnames{i};
  1471. class.index = j;
  1472. class.length = handles.segments(j).end - handles.segments(j).start;
  1473. class.iconS = handles.IconList{j};
  1474. class.nmembers = 0; % will update shortly
  1475. handles.nclasses = handles.nclasses + 1;
  1476. handles.classes = [handles.classes class];
  1477. end
  1478. end
  1479. end
  1480. % Now that we have the classes defined update the number of members
  1481. for i = 1:handles.nclasses
  1482. for j = 1 : handles.nsegments
  1483. if strcmp(handles.classes(i).name,handles.segments(j).class)
  1484. handles.classes(i).nmembers = handles.classes(i).nmembers + 1;
  1485. end
  1486. end
  1487. end
  1488. % --- Executes on button press in SaveButton.
  1489. function SaveButton_Callback(hObject, eventdata, handles)
  1490. % hObject handle to SaveButton (see GCBO)
  1491. % eventdata reserved - to be defined in a future version of MATLAB
  1492. % handles structure with handles and user data (see GUIDATA)
  1493. write_syllable_database(handles); % save the database
  1494. save_configuration(handles,handles.configfile); % save the current configuration
  1495. % --- Executes on slider movement.
  1496. function SliderClassified_Callback(hObject, eventdata, handles)
  1497. % hObject handle to SliderClassified (see GCBO)
  1498. % eventdata reserved - to be defined in a future version of MATLAB
  1499. % handles structure with handles and user data (see GUIDATA)
  1500. % Hints: get(hObject,'Value') returns position of slider
  1501. % get(hObject,'Min') and get(hObject,'Max') to determine range of slider
  1502. xposition = round(get(hObject,'Value'));
  1503. %xposition
  1504. handles.startx = 1 + xposition;
  1505. handles.endx = xposition + handles.classified_width;
  1506. handles.cpositions = get_curr_position(handles);
  1507. classview = handles.classmatrix(:,handles.startx:handles.endx); % will cut overhang
  1508. axes(handles.ClassifiedAxes);
  1509. handles.hiclass = image(classview);
  1510. set(handles.hiclass,'ButtonDownFcn',{@ClassifyAxesClickCallBack});
  1511. set(handles.ClassifiedAxes,'XTick',[]);
  1512. set(handles.ClassifiedAxes,'YTick',[]);
  1513. guidata(hObject, handles);
  1514. % --- Executes during object creation, after setting all properties.
  1515. function SliderClassified_CreateFcn(hObject, eventdata, handles)
  1516. % hObject handle to SliderClassified (see GCBO)
  1517. % eventdata reserved - to be defined in a future version of MATLAB
  1518. % handles empty - handles not created until after all CreateFcns called
  1519. % Hint: slider controls usually have a light gray background, change
  1520. % 'usewhitebg' to 0 to use default. See ISPC and COMPUTER.
  1521. usewhitebg = 1;
  1522. if usewhitebg
  1523. set(hObject,'BackgroundColor',[.9 .9 .9]);
  1524. else
  1525. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  1526. end
  1527. % --- Executes on selection change in SortPopupMenu.
  1528. function SortPopupMenu_Callback(hObject, eventdata, handles)
  1529. % hObject handle to SortPopupMenu (see GCBO) eventdata reserved - to be defined in a future version of MATLAB
  1530. % handles structure with handles and user data (see GUIDATA)
  1531. % Hints: contents = get(hObject,'String') returns SortPopupMenu contents as cell array
  1532. % contents{get(hObject,'Value')} returns selected item from SortPopupMenu
  1533. sortlist = get(hObject,'String');
  1534. sortn = get(hObject,'Value');
  1535. sortby = sortlist(sortn);
  1536. if strcmp(sortby,'original')
  1537. handles.sortclass = 'original';
  1538. elseif strcmp(sortby,'by length')
  1539. handles.sortclass = 'length';
  1540. elseif strcmp(sortby,'by popularity')
  1541. handles.sortclass = 'popularity';
  1542. end
  1543. handles = configureclassview(handles,handles.submode);
  1544. guidata(gcbo,handles);
  1545. % --- Executes during object creation, after setting all properties.
  1546. function SortPopupMenu_CreateFcn(hObject, eventdata, handles)
  1547. % hObject handle to SortPopupMenu (see GCBO)
  1548. % eventdata reserved - to be defined in a future version of MATLAB
  1549. % handles empty - handles not created until after all CreateFcns called
  1550. % Hint: popupmenu controls usually have a white background on Windows.
  1551. % See ISPC and COMPUTER.
  1552. if ispc
  1553. set(hObject,'BackgroundColor','white');
  1554. else
  1555. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  1556. end
  1557. function segments = rename_segments(segments,oldname,newname)
  1558. nsegments = length(segments);
  1559. for i = 1 : nsegments
  1560. if strcmp(segments(i).class,oldname)
  1561. segments(i).class = newname;
  1562. end
  1563. end
  1564. function cln = doesclassexist(classname,classes)
  1565. % tests whether the current class name already exists if does not exist
  1566. % returns 1 if it does exist returns 0
  1567. i = 1;
  1568. nclasses = length(classes);
  1569. while (i <= nclasses) && (strcmp(classname,classes(i).name))
  1570. i = i + 1;
  1571. end
  1572. if i <= nclasses
  1573. cln = i;
  1574. else
  1575. cln = 0;
  1576. end
  1577. % --- Executes on button press in RenameClassButton.
  1578. function RenameClassButton_Callback(hObject, eventdata, handles)
  1579. % hObject handle to RenameClassButton (see GCBO)
  1580. % eventdata reserved - to be defined in a future version of MATLAB
  1581. % handles structure with handles and user data (see GUIDATA)
  1582. class = handles.classes(handles.lastclass);
  1583. answer = inputdlg({'Class name'},'Edit class name',1,{class.name});
  1584. % check the answer
  1585. sizeanswer = size(answer);
  1586. secondanswer = '';
  1587. if sizeanswer(1)
  1588. if not(strcmp(class.name,answer{1}))
  1589. classnexists = doesclassexist(answer{1},handles.classes);
  1590. if classnexists
  1591. secondanswer = questdlg('Class name already exists. Do you want to merge the two classes.')
  1592. end
  1593. if strcmp(secondanswer,'Yes') || strcmp(secondanswer,'')
  1594. segments = rename_segments(handles.segments,class.name,answer{1});
  1595. class.name = answer{1};
  1596. cindex = handles.lastclass;
  1597. handles.classes(cindex) = class;
  1598. handles.segments = segments;
  1599. if strcmp(secondanswer,'Yes') % functionality for merging two classes
  1600. handles.classes(classnexists).nmembers = handles.classes(classnexists).nmembers + handles.classes(cindex).nmembers;
  1601. handles.classes = [handles.classes(1:(cindex-1)) handles.classes((cindex+1):handles.nclasses)];
  1602. handles.nclasses = handles.nclasses - 1;
  1603. class.nmembers = handles.classes(classnexists).nmembers; % this is parasitic code
  1604. end
  1605. end
  1606. handles = configureclassmembers(handles,class.name);
  1607. set_status(handles, ['Viewing ' num2str(class.nmembers),' members of ' class.name]);
  1608. end
  1609. end
  1610. guidata(gcbo,handles);
  1611. function spectras = subsamplespectra(spectra,lowerfreq,upperfreq,freqrange)
  1612. % Returns a subsampled frequency of the spectra
  1613. freqsamples = length(spectra(:,1));
  1614. freqratio = freqsamples / (freqrange(2) - freqrange(1));
  1615. lowerfreqsamp = round(lowerfreq * freqratio) + 1;
  1616. upperfreqsamp = round(upperfreq * freqratio) + 1;
  1617. if lowerfreqsamp < 1 % make sure we are not out of range
  1618. lowerfreqsamp = 1;
  1619. end
  1620. if upperfreq > freqsamples
  1621. upperfeqsamp = freqsamples;
  1622. end
  1623. spectras = spectra(lowerfreqsamp:upperfreqsamp,:);
  1624. function handles = generate_subsamples_icons(handles)
  1625. segments = handles.segments;
  1626. IconListf = {};
  1627. hw = waitbar(0,'Zooming spectra. . .');
  1628. for i = 1:handles.nsegments
  1629. load('-mat',segments(i).specfilename);
  1630. Ssub = subsamplespectra(S,handles.lowerfreq,handles.upperfreq,handles.fpass);
  1631. IconListf{i} = iconify_spec(Ssub,handles.ispecheight);
  1632. waitbar(i/handles.nsegments);
  1633. end
  1634. close(hw);
  1635. handles.IconListf = IconListf;
  1636. function handles = ZoomSpectra(handles,status)
  1637. if strcmp(status,'Zoom in')
  1638. if (length(handles.IconListf) == 0) || (handles.rezoom)
  1639. handles = generate_subsamples_icons(handles);
  1640. handles.rezoom = logical(0);
  1641. end
  1642. handles.FullIconList = handles.IconList;
  1643. set(handles.ZoomButton,'String','Zoom out');
  1644. handles.IconList = handles.IconListf;
  1645. elseif strcmp(status,'Zoom out');
  1646. if handles.rezoom
  1647. handles = generate_subsamples_icons(handles);
  1648. handles.rezoom = logical(0);
  1649. set(handles.ZoomButton,'String','Zoom out');
  1650. handles.IconList = handles.IconListf;
  1651. else
  1652. handles.IconList = handles.FullIconList;
  1653. set(handles.ZoomButton,'String','Zoom in');
  1654. end
  1655. end
  1656. for j = 1:handles.nclasses % switch over class icons
  1657. handles.classes(j).iconS = handles.IconList{handles.classes(j).index};
  1658. end
  1659. nimages = length(handles.mapindex);
  1660. % this is kind of ugly
  1661. if not(strcmp(handles.mode,'class-view')) && not(strcmp(handles.mode,'comparison')) % update images
  1662. for i = 1:nimages
  1663. handles.image_list{i} = handles.IconList{handles.mapindex(i)};
  1664. end
  1665. elseif strcmp(handles.mode,'comparison')
  1666. handles.image_list{1} = handles.IconList{handles.NextIndex};
  1667. handles.image_list{2} = handles.IconList{handles.classes(handles.lastclass).index};
  1668. else
  1669. for i = 1:nimages
  1670. handles.image_list{i} = handles.IconList{handles.classes(handles.mapindex(i)).index};
  1671. end
  1672. end
  1673. handles=get_and_plot(handles,handles.segments(handles.NextIndex));
  1674. handles = plot_classified_axes(handles, handles.image_list(handles.startpage:handles.endpage), handles.cpositions);
  1675. % --- Executes on button press in ZoomButton.
  1676. function ZoomButton_Callback(hObject, eventdata, handles)
  1677. % hObject handle to ZoomButton (see GCBO)
  1678. % eventdata reserved - to be defined in a future version of MATLAB
  1679. % handles structure with handles and user data (see GUIDATA)
  1680. status = get(handles.ZoomButton,'String');
  1681. handles = ZoomSpectra(handles,status);
  1682. guidata(gcbo,handles);
  1683. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1684. % Functions for computing class statistics %
  1685. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1686. function [meanl sdl] = stat_lengths(handles)
  1687. lengthsarray = [];
  1688. for i = 1:length(handles.mapindex)
  1689. lengthsarray(i) = handles.segments(i).end - handles.segments(i).start;
  1690. end
  1691. meanl = mean(lengthsarray);
  1692. sdl = sd(lengthsarray);
  1693. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1694. % Functions for quick classify mode %
  1695. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1696. function handles = quick_mode_exit(handles)
  1697. set(handles.SkipButton,'Visible','off');
  1698. set(handles.UndoButton,'Visible','off');
  1699. set(handles.NewQuickButton,'Visible','off');
  1700. set(handles.ViewText,'Visible','on');
  1701. set(handles.ModePopupMenu,'Visible','on');
  1702. set(handles.SegInfoText,'Visible','on');
  1703. set(handles.ClassifyButton,'Enable','on');
  1704. set(handles.RemoveClassButton,'Enable','on');
  1705. set(handles.RemoveClassButton,'Visible','on');
  1706. set(handles.CompareToggleButton,'Enable','on');
  1707. set(handles.AutoClassifyButton,'Enable','on');
  1708. set(handles.SortPopupMenu,'Visible','off');
  1709. set(handles.ModePopupMenu,'Value',3);
  1710. handles.submode='select';
  1711. set_status(handles,['Viewing all ' num2str(handles.nclasses) ' classes']);
  1712. setnavigationbuttons(handles);
  1713. % --- Executes on button press in QuickModeButton.
  1714. function QuickModeButton_Callback(hObject, eventdata, handles)
  1715. % hObject handle to QuickModeButton (see GCBO)
  1716. % eventdata reserved - to be defined in a future version of MATLAB
  1717. % handles structure with handles and user data (see GUIDATA)
  1718. if not(handles.quickmode) % turn on quick mode
  1719. handles.quickmode = 1;
  1720. % Turn off top header
  1721. set(handles.ViewText,'Visible','off');
  1722. set(handles.ModePopupMenu,'Visible','off');
  1723. set(handles.SegInfoText,'Visible','off');
  1724. % Make visible quick mode buttons
  1725. set(handles.SkipButton,'Visible','on');
  1726. set(handles.SkipButton,'Enable','on');
  1727. set(handles.UndoButton,'Visible','on');
  1728. set(handles.UndoButton,'Enable','on');
  1729. set(handles.NewQuickButton,'Visible','on');
  1730. set(handles.NewQuickButton,'Visible','on');
  1731. set(handles.SortPopupMenu,'Visible','on');
  1732. % Disable regular mode functions
  1733. set(handles.RemoveClassButton,'Visible','off');
  1734. set(handles.NextClassButton,'Visible','off');
  1735. set(handles.NextSpectraButton,'Enable','off');
  1736. set(handles.PreviousSpectraButton,'Enable','off');
  1737. set(handles.ClassifyButton,'Enable','off');
  1738. set(handles.TypifyClassButton,'Visible','off');
  1739. set(handles.RenameClassButton,'Visible','off');
  1740. set(handles.CompareToggleButton,'Enable','off');
  1741. set(handles.AutoClassifyButton,'Enable','off');
  1742. % Will need to disable buttons underneath classified axes
  1743. if not(strcmp(handles.segments(handles.NextIndex).class,''))
  1744. handles = jump_to_unclassified(handles);
  1745. handles.lastsegment = handles.NextIndex;
  1746. end
  1747. if handles.nclasses >= 1
  1748. handles = configureclassview(handles,'select-class');
  1749. handles.mode = 'class-view';
  1750. handles.submode = 'select-class';
  1751. else % empty axes
  1752. handles = blankaxes(handles);
  1753. handles.mode = 'class-view';
  1754. handles.submode = 'xxx';
  1755. end
  1756. else
  1757. handles.quickmode = 0;
  1758. handles = quick_mode_exit(handles);
  1759. % Will need to intellegently enable buttons underneath classified axes
  1760. end
  1761. guidata(gcbo,handles);
  1762. function handles=jump_to_unclassified(handles)
  1763. % Jumps to the next unclassified segment
  1764. currindex = handles.NextIndex;
  1765. i = currindex;
  1766. while (mod(i,handles.nsegments)+1 ~= currindex) && not(strcmp(handles.segments(mod(i,handles.nsegments) + 1).class,''))
  1767. i = i + 1;
  1768. end
  1769. if not(mod(i,handles.nsegments)+1 == currindex) % there are some unclassified segments
  1770. handles.NextIndex = mod(i,handles.nsegments) + 1;
  1771. handles=ConfigureClassSegment(handles);
  1772. handles=ConfigureSpecPlot(handles);
  1773. set(handles.ClassifyButton,'Enable','off');
  1774. end
  1775. %get(handles.QuickModeButton,'Value')
  1776. handles.lastsegment = currindex;
  1777. % --- Executes on button press in SkipButton.
  1778. function SkipButton_Callback(hObject, eventdata, handles)
  1779. % hObject handle to SkipButton (see GCBO)
  1780. % eventdata reserved - to be defined in a future version of MATLAB
  1781. % handles structure with handles and user data (see GUIDATA)
  1782. handles = jump_to_unclassified(handles);
  1783. if handles.lastsegment == handles.NextIndex % no more unclassified segments
  1784. handles = quick_mode_exit(handles);
  1785. set(handles.QuickModeButton,'Value',0);
  1786. handles.quickmode = not(handles.quickmode);
  1787. end
  1788. handles = configureclassview(handles,'select-class');
  1789. guidata(gcbo,handles);
  1790. % --- Executes on button press in UndoButton.
  1791. function UndoButton_Callback(hObject, eventdata, handles)
  1792. % hObject handle to UndoButton (see GCBO)
  1793. % eventdata reserved - to be defined in a future version of MATLAB
  1794. % handles structure with handles and user data (see GUIDATA)
  1795. handles.NextIndex = handles.lastsegment;
  1796. handles=ConfigureClassSegment(handles);
  1797. handles=ConfigureSpecPlot(handles);
  1798. handles = quick_mode_exit(handles);
  1799. set(handles.QuickModeButton,'Value',0);
  1800. handles.quickmode = not(handles.quickmode);
  1801. guidata(gcbo,handles);
  1802. % --- Executes on button press in NewQuickButton.
  1803. function NewQuickButton_Callback(hObject, eventdata, handles)
  1804. % hObject handle to NewQuickButton (see GCBO)
  1805. % eventdata reserved - to be defined in a future version of MATLAB
  1806. % handles structure with handles and user data (see GUIDATA)
  1807. handles = add_new_class(handles,handles.segments(handles.NextIndex));
  1808. handles.segments(handles.NextIndex).class = handles.classes(handles.nclasses).name;
  1809. set(handles.ClassifyButton,'Enable','off');
  1810. handles = jump_to_unclassified(handles);
  1811. handles = configureclassview(handles,'select-class');
  1812. if handles.lastsegment == handles.NextIndex % no more unclassified segments
  1813. handles = quick_mode_exit(handles)
  1814. set(handles.QuickModeButton,'Value',0);
  1815. handles.quickmode = not(handles.quickmode);
  1816. end
  1817. guidata(gcbo,handles);
  1818. % --- Executes on button press in CompareToggleButton.
  1819. function CompareToggleButton_Callback(hObject, eventdata, handles)
  1820. % hObject handle to CompareToggleButton (see GCBO)
  1821. % eventdata reserved - to be defined in a future version of MATLAB
  1822. % handles structure with handles and user data (see GUIDATA)
  1823. % Hint: get(hObject,'Value') returns toggle state of CompareToggleButton
  1824. state = get(hObject,'Value');
  1825. if handles.nclasses > 0
  1826. if state % enter compare mode
  1827. handles.mode = 'class-view';
  1828. set(handles.RemoveClassButton,'Enable','off');
  1829. set(handles.RenameClassButton,'Visible','off');
  1830. set(handles.NextClassButton,'Visible','off');
  1831. set(handles.ModePopupMenu,'Enable','off');
  1832. set(handles.ClassifyButton,'Enable','off');
  1833. set(handles.QuickModeButton,'Enable','off');
  1834. set_status(handles,'Select a class to compare segment against');
  1835. set(handles.NextSpectraButton,'Enable','off');
  1836. set(handles.PreviousSpectraButton,'Enable','off');
  1837. set(handles.TypifyClassButton, 'Visible', 'off');
  1838. set(handles.AutoClassifyButton,'Enable','off');
  1839. handles = configureclassview(handles,'compare');
  1840. else
  1841. if strcmp(handles.mode,'comparison') % go back to the class view
  1842. set(hObject,'Value',1);
  1843. handles.startx = 1; % if you have been scrolling reset to defaults
  1844. handles.endx = handles.classified_width;
  1845. set(handles.SliderClassified, 'Value',0);
  1846. handles = configureclassview(handles,'compare');
  1847. handles.mode = 'class-view';
  1848. set_status(handles,'Select a class to compare segment against');
  1849. elseif strcmp(handles.mode,'class-view') % exit the compare mode entirely
  1850. set_status(handles, ['Viewing all ', num2str(handles.nclasses),' classes']);
  1851. set(handles.ModePopupMenu,'Enable','on');
  1852. set(handles.QuickModeButton,'Enable','on');
  1853. setnavigationbuttons(handles);
  1854. SetModePopupMenu(handles,'class view');
  1855. set(handles.RemoveClassButton,'Enable','on');
  1856. set(handles.RemoveClassButton,'Visible','on');
  1857. set(handles.ClassifyButton,'Enable','on');
  1858. set(handles.AutoClassifyButton,'Enable','on');
  1859. handles.submode = 'select';
  1860. end
  1861. end
  1862. else
  1863. set(hObject,'Value',0);
  1864. end
  1865. guidata(hObject, handles);
  1866. function handles = recompute_classifiedaxes(handles)
  1867. % Recomputes the width and height based on a new size for the classified
  1868. % axes
  1869. classaxpos=get(handles.ClassifiedAxes,'Position');
  1870. set(handles.SliderClassified,'Value',0);
  1871. handles.startx = 1;
  1872. handles.classified_width = round(handles.classified_width_density * classaxpos(3));
  1873. handles.classified_height = round(handles.classified_height_density * classaxpos(4));
  1874. oldpos = handles.startpage;
  1875. handles = reposition_images(handles, handles.image_list); % try to keep rows matched
  1876. cnrow = which_row(handles.positions,oldpos);
  1877. for i = 1:(cnrow-1)
  1878. handles = row_forward(handles);
  1879. end
  1880. handles.cpositions = get_curr_position(handles);
  1881. handles = plot_classified_axes(handles, handles.image_list(handles.startpage:handles.endpage), handles.cpositions);
  1882. function reposy(guielement,deltay)
  1883. oldpos = get(guielement,'Position');
  1884. set(guielement,'Position',[oldpos(1), oldpos(2) + deltay, oldpos(3), oldpos(4)]);
  1885. function reposelementsy(handles,deltay)
  1886. reposy(handles.PrecomputeButton,deltay);
  1887. reposy(handles.DirectoryEditBox,deltay);
  1888. reposy(handles.LoadDirectoryButton,deltay);
  1889. reposy(handles.SaveButton,deltay);
  1890. reposy(handles.ConfigureButton,deltay);
  1891. reposy(handles.ViewText,deltay);
  1892. reposy(handles.ModePopupMenu,deltay);
  1893. reposy(handles.SegInfoText,deltay);
  1894. reposy(handles.ToClassifyPanel,deltay);
  1895. reposy(handles.NewQuickButton,deltay);
  1896. reposy(handles.UndoButton,deltay);
  1897. reposy(handles.SkipButton,deltay);
  1898. reposy(handles.SortText,deltay);
  1899. reposy(handles.SortPopupMenu,deltay);
  1900. reposy(handles.NextClassButton,deltay);
  1901. function ResizeFcn(h, eventdata, handles, varargin)
  1902. handles = guidata(gcbo);
  1903. originalsize = handles.originalsize;
  1904. newsize = get(h,'Position');
  1905. classaxpos = get(handles.ClassifiedAxes,'Position');
  1906. if handles.originalsize(3) > newsize(3) % if form has smaller width bounce back
  1907. newsize(3) = originalsize(3);
  1908. classaxpos(3) = handles.originalaxessize(3);
  1909. sliderpos = get(handles.SliderClassified,'Position'); % Update the slider
  1910. sliderpos(3) = classaxpos(3);
  1911. set(handles.SliderClassified,'Position',sliderpos);
  1912. else % form is larger
  1913. deltax = newsize(3) - handles.prevsize(3);
  1914. classaxpos(3) = classaxpos(3) + deltax; % update width of the axes
  1915. sliderpos = get(handles.SliderClassified,'Position'); % Update the slider
  1916. sliderpos(3) = sliderpos(3) + deltax;
  1917. set(handles.SliderClassified,'Position',sliderpos);
  1918. end
  1919. if originalsize(4) > newsize(4) % if form has a smaller height bounce back
  1920. newsize(2) = newsize(2) + newsize(4) - originalsize(4);
  1921. deltay = originalsize(4) - handles.prevsize(4);
  1922. newsize(4) = originalsize(4);
  1923. classaxpos(4) = classaxpos(4) + newsize(4) - handles.prevsize(4);
  1924. reposelementsy(handles,deltay);
  1925. else % form is larger we need to reposition elements
  1926. deltay = newsize(4) - handles.prevsize(4);
  1927. reposelementsy(handles,deltay);
  1928. classaxpos(4) = classaxpos(4) + newsize(4) - handles.prevsize(4);
  1929. end
  1930. set(h,'Position',newsize);
  1931. set(handles.ClassifiedAxes,'Position',classaxpos);
  1932. if handles.fixed || handles.blank % allow fixed resizing
  1933. % do nothing except update the density measurements
  1934. handles.classified_width_density = handles.classified_width / classaxpos(3);
  1935. handles.classified_height_density = handles.classified_height / classaxpos(4);
  1936. else % reposition images
  1937. handles = recompute_classifiedaxes(handles);
  1938. end
  1939. handles.prevsize = newsize;
  1940. guidata(gcbo,handles);
  1941. function truth = truthrange(range)
  1942. truth = range(1) && range (2);
  1943. % --- Executes on button press in ConfigureButton.
  1944. function ConfigureButton_Callback(hObject, eventdata, handles)
  1945. % hObject handle to ConfigureButton (see GCBO)
  1946. % eventdata reserved - to be defined in a future version of MATLAB
  1947. % handlenb vcccs structure with handles and user data (see GUIDATA)
  1948. configs = configure_classify(handles.lowerfreq,handles.upperfreq,handles.classified_height,handles.classified_width,handles.Fs,handles.movingwin,handles.tapers,handles.fpass,handles.fixed);
  1949. if not(isempty(configs) || length(configs) == 1) % if okay is pressed
  1950. lowerfreq= configs{1};
  1951. upperfreq= configs{2};
  1952. classified_height= configs{3};
  1953. classified_width= configs{4};
  1954. Fs=configs{5};
  1955. movingwin= configs{6};
  1956. tapers= configs{7};
  1957. fpass = configs{8};
  1958. rezoom = 0;
  1959. if not(handles.lowerfreq == lowerfreq)
  1960. handles.lowerfreq = lowerfreq;
  1961. rezoom = 1;
  1962. end
  1963. if not(handles.upperfreq==upperfreq)
  1964. handles.upperfreq=upperfreq;
  1965. rezoom=1;
  1966. end
  1967. redraw = 0;
  1968. if not(handles.classified_height==classified_height)
  1969. handles.classified_height=classified_height;
  1970. redraw = 1;
  1971. end
  1972. if not(handles.classified_width==classified_width)
  1973. handles.classified_width=classified_height;
  1974. redraw = 1;
  1975. end
  1976. recompute = 0; % check to see if the spectra needs to be recomputed
  1977. if not(handles.Fs==Fs)
  1978. handles.Fs = Fs;
  1979. recompute = 1;
  1980. end
  1981. if not(truthrange(handles.movingwin == movingwin))
  1982. handles.movingwin = movingwin;
  1983. recompute = 1;
  1984. end
  1985. if not(truthrange(handles.tapers==tapers))
  1986. handles.tapers=tapers;
  1987. recompute = 1;
  1988. end
  1989. if not(truthrange(handles.fpass==fpass))
  1990. handles.fpass=fpass;
  1991. recompute = 1;
  1992. end
  1993. if recompute % The spectra need to be recomputed
  1994. status = questdlg('Recompute spectra with changed parameters');
  1995. if not(isempty(status))
  1996. if strcmp(status,'Yes')
  1997. handles = precompute_AllSpectra(handles);
  1998. save_configuration(handles,handles.configfile); % Save configuration no matter what
  1999. handles=ConfigureSpecPlot(handles); % automatically force into browse mode
  2000. set(handles.RemoveClassButton,'Visible','off');
  2001. set(handles.TypifyClassButton, 'Visible', 'off');
  2002. set(handles.NextClassButton,'Visible','off');
  2003. handles=BrowseDirectory(handles);
  2004. set(handles.ModePopupMenu,'Value',1);
  2005. if handles.nclasses >= 1 % sync changes to the classview
  2006. for j = 1:handles.nclasses
  2007. handles.classes(j).iconS = handles.IconList{handles.classes(j).index};
  2008. end
  2009. end
  2010. set(handles.RemoveClassButton,'Visible','off'); % update buttons
  2011. set(handles.TypifyClassButton, 'Visible', 'off');
  2012. set(handles.NextClassButton,'Visible','off');
  2013. redraw = logical(1);
  2014. end
  2015. end
  2016. end
  2017. if rezoom
  2018. if strcmp(get(handles.ZoomButton,'String'),'Zoom out')
  2019. handles.rezoom = logical(1);
  2020. handles = ZoomSpectra(handles,'Zoom out');
  2021. else
  2022. handles.rezoom = logical(1); % Otherwise wait to user hits the rezoom button
  2023. end
  2024. end
  2025. if not(handles.configschanged) % handles the case were the configurations were changed already
  2026. if not(recompute)
  2027. handles.configschanged = logical(1);
  2028. end
  2029. end
  2030. if redraw % Redraw the axes
  2031. classaxpos = get(handles.ClassifiedAxes,'Position');
  2032. handles.classified_width_density = handles.classified_width / classaxpos(3);
  2033. handles.classified_height_density = handles.classified_height / classaxpos(4);
  2034. handles = recompute_classifiedaxes(handles);
  2035. end
  2036. handles.fixed = configs{9};
  2037. end
  2038. guidata(gcbo,handles);
  2039. function status = save_configuration(handles,filename)
  2040. Fs = handles.Fs;
  2041. movingwin = handles.movingwin;
  2042. tapers = handles.tapers;
  2043. fpass = handles.fpass;
  2044. try
  2045. save(filename,'Fs','movingwin','tapers','fpass','-mat');
  2046. catch
  2047. status = 1;
  2048. end
  2049. function handles = load_configuration(handles,filename)
  2050. try
  2051. load('-mat',filename)
  2052. if handles.Fs == Fs || handles.movingwin == movingwin || handles.tapers == tapers || handles.fpass == fpass
  2053. handles.configshavechanged = logical(1);
  2054. end
  2055. handles.Fs = Fs;
  2056. handles.movingwin = movingwin;
  2057. handles.tapers = tapers;
  2058. handles.fpass = fpass;
  2059. catch
  2060. handles = handles;
  2061. end
  2062. % --- Executes on button press in NextClassButton.
  2063. function NextClassButton_Callback(hObject, eventdata, handles)
  2064. % hObject handle to NextClassButton (see GCBO)
  2065. % eventdata reserved - to be defined in a future version of MATLAB
  2066. % handles structure with handles and user data (see GUIDATA)
  2067. handles.lastclass = handles.lastclass + 1;
  2068. if handles.lastclass > handles.nclasses
  2069. handles.lastclass = 1;
  2070. end
  2071. class = handles.classes(handles.lastclass);
  2072. handles = configureclassmembers(handles,class.name);
  2073. set_status(handles,['Viewing ' num2str(length(handles.mapindex)) ' members of ' class.name]);
  2074. handles.mode = 'class-members';
  2075. handles.submode = 'select';
  2076. guidata(gcbo,handles);
  2077. function handles = features_segments(handles)
  2078. h=waitbar(0,'Computing Data Features. . . ');
  2079. for i = 1:handles.nsegments
  2080. startf = handles.segments(i).start;
  2081. endf = handles.segments(i).end;
  2082. cepestral = cepsfromspectra(handles.segments(i).specfilename,handles.ncepestral);
  2083. % Below shows how additional data features can be included in the
  2084. % exporting and classifying using the software
  2085. % The commented code shows how the wave file can be read in to
  2086. % calculate additional data features
  2087. % segment = handles.segments(i);
  2088. % data = wavread(segment.wavfile,round(handles.Fs * [segment.start segment.end]));
  2089. waitbar(i/handles.nsegments);
  2090. handles.segments(i).features = cepestral';
  2091. % Three additional data features can be added for example as following
  2092. %
  2093. % additional_features = compute_features(data,3);
  2094. % handles.nfeatures = handles.nfeatures + 3;
  2095. % handles.segments(i).features = [handles.segments(i).features additional_features'];
  2096. end
  2097. close(h);
  2098. function coefs = cepsfromspectra(specfilename,ncepestral)
  2099. load('-mat',specfilename);
  2100. sbase = (mean(min(exp(Spre))) + mean(min(exp(Spost)))) / 2; % might not be a good
  2101. % baseline because of
  2102. % the intensity of the
  2103. % sound around the syllable that
  2104. % is why I took the average
  2105. % minimum value
  2106. %Sdiff = S - sbase;
  2107. Sdiff = exp(S) - sbase; % arithmetic average
  2108. spectra = log(mean(Sdiff')); % average across time to generate spectra
  2109. %spectra=mean(Sdiff');
  2110. cepstrum = real(ifft(spectra));
  2111. % alternative methods for computing the cepstrum
  2112. %real(fft(hamming(length(spectra) .* spectra )));
  2113. %cepstrum = real(fft(spectra))
  2114. coefs = cepstrum(1:ncepestral); % this I understand is the
  2115. % correct way
  2116. function classifymatrix = generate_classify_mat(handles)
  2117. classifymatrix = zeros(handles.nsegments,handles.nfeatures+1);
  2118. for i = 1:handles.nsegments
  2119. seglength = handles.segments(i).end - handles.segments(i).start;
  2120. classifymatrix(i,1:1+handles.nfeatures) = [seglength handles.segments(i).features(1:handles.nfeatures)'];
  2121. end
  2122. ;
  2123. function handles = generate_classes_auto(handles,classification)
  2124. nclasses = length(unique(classification));
  2125. classesfound = zeros(nclasses,1); % used to check which classes exist
  2126. handles.classes = [];
  2127. handles.nclasses = 0;
  2128. j = 1;
  2129. for i = 1:handles.nsegments
  2130. if classification(i) == 0 % segment has not been classified
  2131. handles.segments(i).class = '';
  2132. else
  2133. if isempty(classesfound(find(classesfound == classification(i)))) % a new class is found
  2134. class.specfilename = handles.segments(i).specfilename;
  2135. class.name = newclassname(handles);
  2136. class.index = i;
  2137. class.length = handles.segments(i).end - handles.segments(i).start;
  2138. class.iconS = handles.IconList{i};
  2139. class.nmembers = 1;
  2140. handles.segments(i).class = class.name;
  2141. handles.classes = [handles.classes class];
  2142. classesfound(j) = classification(i);
  2143. j = j + 1;
  2144. handles.nclasses = handles.nclasses + 1;
  2145. else % segment belongs to a class that already exists
  2146. classindex = find(classification(i) == classesfound);
  2147. handles.segments(i).class = handles.classes(classindex).name;
  2148. handles.classes(classindex).nmembers = handles.classes(classindex).nmembers + 1;
  2149. end
  2150. end
  2151. end
  2152. ;
  2153. % --- Executes on button press in AutoClassifyButton.
  2154. function AutoClassifyButton_Callback(hObject, eventdata, handles)
  2155. % hObject handle to AutoClassifyButton (see GCBO)
  2156. % eventdata reserved - to be defined in a future version of MATLAB
  2157. % handles structure with handles and user data (see GUIDATA)
  2158. if length(handles.segments(1).features) == 0; % compute cepestral coefficients if not computed before
  2159. handles = features_segments(handles);
  2160. end
  2161. matrix2classify = generate_classify_mat(handles);
  2162. classification = auto_classify(matrix2classify,handles.nfeatures);
  2163. if length(classification) > 1 %&& not(classification == 0)
  2164. if not(isempty(handles.classes))
  2165. answer = questdlg('A classification already exists. Do you want to replace the current classification?');
  2166. if strcmp(answer,'Yes')
  2167. handles = generate_classes_auto(handles,classification);
  2168. end
  2169. else
  2170. handles = generate_classes_auto(handles,classification);
  2171. end
  2172. handles=configureclassview(handles,'select');
  2173. set(handles.ClassifyButton,'String','Declassify'); % all classes are now classified
  2174. set(handles.RemoveClassButton,'Visible','on');
  2175. set(handles.RemoveClassButton,'Enable','on');
  2176. set(handles.CompareToggleButton,'Visible','on');
  2177. set(handles.CompareToggleButton,'Enable','on');
  2178. set(handles.TypifyClassButton, 'Visible', 'off');
  2179. set_status(handles,['Viewing all ' num2str(handles.nclasses) ' classes'])
  2180. %
  2181. % set(handles.ModePopupMenu,'Value',3);
  2182. %
  2183. % ModePopupMenu_Callback(handles.ModePopupMenu,eventdata, handles);
  2184. % handles.mode = 'class-view';
  2185. % handles.submode = 'select';
  2186. end
  2187. guidata(gcbo,handles);
  2188. % --------------------------------------------------------------------
  2189. function FileMenu_Callback(hObject, eventdata, handles)
  2190. % hObject handle to FileMenu (see GCBO)
  2191. % eventdata reserved - to be defined in a future version of MATLAB
  2192. % handles structure with handles and user data (see GUIDATA)
  2193. % --------------------------------------------------------------------
  2194. function SaveItem_Callback(hObject, eventdata, handles)
  2195. % hObject handle to SaveItem (see GCBO)
  2196. % eventdata reserved - to be defined in a future version of MATLAB
  2197. % handles structure with handles and user data (see GUIDATA)
  2198. SaveButton_Callback(hObject, eventdata, handles)
  2199. % --------------------------------------------------------------------
  2200. function LoadItem_Callback(hObject, eventdata, handles)
  2201. % hObject handle to LoadItem (see GCBO)
  2202. % eventdata reserved - to be defined in a future version of MATLAB
  2203. % handles structure with handles and user data (see GUIDATA)
  2204. PrecomputeButton_Callback(hObject, eventdata, handles)
  2205. %guidata(gcbo,handles)
  2206. % --------------------------------------------------------------------
  2207. function HelpMenu_Callback(hObject, eventdata, handles)
  2208. % hObject handle to HelpMenu (see GCBO)
  2209. % eventdata reserved - to be defined in a future version of MATLAB
  2210. % handles structure with handles and user data (see GUIDATA)
  2211. % --------------------------------------------------------------------
  2212. function HelpItem_Callback(hObject, eventdata, handles)
  2213. % hObject handle to HelpItem (see GCBO)
  2214. % eventdata reserved - to be defined in a future version of MATLAB
  2215. % handles structure with handles and user data (see GUIDATA)
  2216. web('classify_spectra_help.html');
  2217. % --------------------------------------------------------------------
  2218. function AboutItem_Callback(hObject, eventdata, handles)
  2219. % hObject handle to AboutItem (see GCBO)
  2220. % eventdata reserved - to be defined in a future version of MATLAB
  2221. % handles structure with handles and user data (see GUIDATA)
  2222. msgbox('Classify_spectra (version 0.2) is being developed by the Mitra Lab at the Cold Spring Harbor Laboratory.','About classify_spectra');
  2223. % --------------------------------------------------------------------
  2224. function ConfigureItem_Callback(hObject, eventdata, handles)
  2225. % hObject handle to ConfigureItem (see GCBO)
  2226. % eventdata reserved - to be defined in a future version of MATLAB
  2227. % handles structure with handles and user data (see GUIDATA)
  2228. ConfigureButton_Callback(hObject, eventdata, handles)
  2229. function coefs = cepcoefs(data,p)
  2230. % Computes the cepstral coefficients from the data returns p coefficients
  2231. coefs = [];
  2232. if p > 0
  2233. y = fft(hamming(length(data)) .* data);
  2234. coefs = aryule(ifft(log(abs(y))),p);
  2235. end
  2236. function generate_output(handles,absolutetime,filename);
  2237. filebasetosave = filename(1:length(filename)-4);
  2238. matrixoutput = cell(handles.nsegments,1);
  2239. for i = 1:handles.nsegments
  2240. segment = handles.segments(i);
  2241. if absolutetime
  2242. dstr = regexp(segment.wavfile,'[0-9]+\-[0-9]+\-[0-9]+','match');
  2243. dstr = dstr{1}; % take first match only
  2244. tstr = regexp(segment.wavfile,'[0-9][0-9][0-9][0-9][0-9][0-9]','match');
  2245. tstr = tstr{1};
  2246. tstr = [tstr(1:2) ':' tstr(3:4) ':' tstr(5:6)];
  2247. segmentstart = datenum([dstr ' ' tstr]) + segment.start;
  2248. segmentstart = datevec(segmentstart);
  2249. else
  2250. segmentstart = segment.start;
  2251. end
  2252. matrixoutput{i} = {segment.wavfile,segment.class,segmentstart,segment.end - segment.start,segment.features};
  2253. end
  2254. save([filebasetosave '.mat'],'-mat','matrixoutput','-mat');
  2255. delimiter = '\t';
  2256. fp = fopen([filebasetosave '.txt'],'wt');
  2257. % Generate header for file
  2258. fprintf(fp,'filename');fprintf(fp,delimiter);
  2259. fprintf(fp,'class');fprintf(fp,delimiter);
  2260. if absolutetime
  2261. fprintf(fp,'year');fprintf(fp,delimiter);
  2262. fprintf(fp,'month');fprintf(fp,delimiter);
  2263. fprintf(fp,'day');fprintf(fp,delimiter);
  2264. fprintf(fp,'hour');fprintf(fp,delimiter);
  2265. fprintf(fp,'minute');fprintf(fp,delimiter);
  2266. fprintf(fp,'second');fprintf(fp,delimiter);
  2267. else
  2268. fprintf(fp,'start');fprintf(fp,delimiter);
  2269. end
  2270. fprintf(fp,'length');fprintf(fp,delimiter);
  2271. for i = 1:handles.nfeatures
  2272. fprintf(fp,['d' num2str(i)]);
  2273. if i < handles.nfeatures;
  2274. fprintf(fp,delimiter);
  2275. else
  2276. fprintf(fp,'\n');
  2277. end
  2278. end
  2279. % Generate main data
  2280. for i = 1:handles.nsegments
  2281. segment = handles.segments(i);
  2282. fprintf(fp,['"' segment.wavfile '"']);fprintf(fp,delimiter);
  2283. fprintf(fp,['"' segment.class '"']);fprintf(fp,delimiter);
  2284. if absolutetime
  2285. time = matrixoutput{i}{3};
  2286. fprintf(fp,num2str(time(1)));fprintf(fp,delimiter);
  2287. fprintf(fp,num2str(time(2)));fprintf(fp,delimiter);
  2288. fprintf(fp,num2str(time(3)));fprintf(fp,delimiter);
  2289. fprintf(fp,num2str(time(4)));fprintf(fp,delimiter);
  2290. fprintf(fp,num2str(time(5)));fprintf(fp,delimiter);
  2291. fprintf(fp,num2str(time(6)));
  2292. else
  2293. fprintf(fp,num2str(segment.start));
  2294. end
  2295. fprintf(fp,delimiter);
  2296. fprintf(fp,num2str(segment.end-segment.start));fprintf(fp,delimiter);
  2297. for j = 1:handles.nfeatures
  2298. fprintf(fp,num2str(segment.features(j)));
  2299. if j < handles.nfeatures % handle eol formatting
  2300. fprintf(fp,delimiter);
  2301. else
  2302. if i < handles.nsegments % handle eof formatting
  2303. fprintf(fp,'\n');
  2304. end
  2305. end
  2306. end
  2307. end
  2308. fclose(fp);
  2309. % --------------------------------------------------------------------
  2310. function ExportDataItem_Callback(hObject, eventdata, handles)
  2311. % hObject handle to ExportDataItem (see GCBO)
  2312. % eventdata reserved - to be defined in a future version of MATLAB
  2313. % handles structure with handles and user data (see GUIDATA)
  2314. filename = uiputfile('*.txt','File to write exported data to');
  2315. if not(filename == 0)
  2316. if length(handles.segments(1).features) == 0
  2317. handles = features_segments(handles);
  2318. end
  2319. generate_output(handles,1,filename);
  2320. end
  2321. % --------------------------------------------------------------------
  2322. function CleanDirectoryItem_Callback(hObject, eventdata, handles)
  2323. % hObject handle to CleanDirectoryItem (see GCBO)
  2324. % eventdata reserved - to be defined in a future version of MATLAB
  2325. % handles structure with handles and user data (see GUIDATA)
  2326. answer = questdlg('Clean the current directory. This will remove all files generated by classify_spectra including the file which includes the classification');
  2327. if strcmp(answer,'Yes');
  2328. specfiles = dir('*.spec');
  2329. for i = 1:length(specfiles)
  2330. delete(specfiles(i).name);
  2331. end
  2332. if exist('class_spec.conf')
  2333. delete('class_spec.conf');
  2334. end
  2335. if exist([handles.baseclassname '.dat'])
  2336. delete([handles.baseclassname '.dat']);
  2337. end
  2338. end