Home > chronux_1_50 > wave_browser > wave_browser.m

wave_browser

PURPOSE ^

WAVE_BROWSER M-file for wave_browser.fig

SYNOPSIS ^

function varargout = wave_browser(varargin)

DESCRIPTION ^

 WAVE_BROWSER M-file for wave_browser.fig
      WAVE_BROWSER, by itself, creates a new WAVE_BROWSER or raises the existing
      singleton*.

      H = WAVE_BROWSER returns the handle to a new WAVE_BROWSER or the handle to
      the existing singleton*.

      WAVE_BROWSER('CALLBACK',hObject,eventData,handles,...) calls the local
      function named CALLBACK in WAVE_BROWSER.M with the given input arguments.

      WAVE_BROWSER('Property','Value',...) creates a new WAVE_BROWSER or raises the
      existing singleton*.  Starting from the left, property value pairs are
      applied to the GUI before wave_browser_OpeningFunction gets called.  An
      unrecognized property name or invalid value makes property application
      stop.  All inputs are passed to wave_browser_OpeningFcn via varargin.

      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
      instance to run (singleton)".

 See also: GUIDE, GUIDATA, GUIHANDLES

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function varargout = wave_browser(varargin)
0002 % WAVE_BROWSER M-file for wave_browser.fig
0003 %      WAVE_BROWSER, by itself, creates a new WAVE_BROWSER or raises the existing
0004 %      singleton*.
0005 %
0006 %      H = WAVE_BROWSER returns the handle to a new WAVE_BROWSER or the handle to
0007 %      the existing singleton*.
0008 %
0009 %      WAVE_BROWSER('CALLBACK',hObject,eventData,handles,...) calls the local
0010 %      function named CALLBACK in WAVE_BROWSER.M with the given input arguments.
0011 %
0012 %      WAVE_BROWSER('Property','Value',...) creates a new WAVE_BROWSER or raises the
0013 %      existing singleton*.  Starting from the left, property value pairs are
0014 %      applied to the GUI before wave_browser_OpeningFunction gets called.  An
0015 %      unrecognized property name or invalid value makes property application
0016 %      stop.  All inputs are passed to wave_browser_OpeningFcn via varargin.
0017 %
0018 %      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
0019 %      instance to run (singleton)".
0020 %
0021 % See also: GUIDE, GUIDATA, GUIHANDLES
0022 
0023 % Copyright 2002-2003 The MathWorks, Inc.
0024 
0025 % Edit the above text to modify the response to help wave_browser
0026 
0027 % Last Modified by GUIDE v2.5 26-Jun-2006 14:38:02
0028 
0029 % Begin initialization code - DO NOT EDIT
0030 gui_Singleton = 1;
0031 gui_State = struct('gui_Name',       mfilename, ...
0032                    'gui_Singleton',  gui_Singleton, ...
0033                    'gui_OpeningFcn', @wave_browser_OpeningFcn, ...
0034                    'gui_OutputFcn',  @wave_browser_OutputFcn, ...
0035                    'gui_LayoutFcn',  [] , ...
0036                    'gui_Callback',   []);
0037 if nargin && ischar(varargin{1})
0038     gui_State.gui_Callback = str2func(varargin{1});
0039 end
0040 
0041 if nargout
0042     [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
0043 else
0044     gui_mainfcn(gui_State, varargin{:});
0045 end
0046 % End initialization code - DO NOT EDIT
0047 
0048 
0049 % --- Executes just before wave_browser is made visible.
0050 function wave_browser_OpeningFcn(hObject, eventdata, handles, varargin)
0051 % This function has no output args, see OutputFcn.
0052 % hObject    handle to figure
0053 % eventdata  reserved - to be defined in a future version of MATLAB
0054 % handles    structure with handles and user data (see GUIDATA)
0055 % varargin   command line arguments to wave_browser (see VARARGIN)
0056 
0057 handles.firsttime = 0; % indicates the firsttime that segment has been precomputed
0058 handles.precomputed_spec = 0; % indicates that the spectra has not been precomputed
0059 
0060 handles.longfile = 0;  % indicates whether the file is a long file
0061 handles.maxwavsize = 10 * 44100; % I will have to explore what number works best here
0062 handles.maxspec_t = 30; % duration of the max size of a spectra
0063 
0064 handles.Fs = 44100; % default size to start with
0065 
0066 handles.segments = []; % holds regular segments in the current chunk
0067 handles.allsegments = []; % holds segments across the maximum wave size
0068 handles.loadedsegment = 0; % indicates no segments have been loaded
0069 
0070 handles.lastmarkerstart = 1; % largest segment
0071 
0072 handles.segmentmode = 0; % by default start off with segmenting turned off
0073 handles.dontcutsegments = 0; % by default do not adapt to segments
0074 
0075 handles.automethod = 'threshold'; % use threshold or ratiof method
0076 
0077 handles.indexthresh = 10; % for ration method the threshold which to cut the curve off
0078 handles.lower_range = [10 10000];  % the numerator in the ratio
0079 handles.upper_range = [15000 20000]; % the denomitor in the ratio
0080 
0081 handles.nsmooth = 0; % moving average parameter for the thresholds curves
0082 
0083 positionP = get(handles.OptionsUiPanel,'Position');
0084 positionF = get(gcf,'Position');
0085 
0086 positionF(3) = positionF(3) - positionP(3);
0087 
0088 % set(gcf,'Position',positionF); % untested
0089 
0090 % Choose default command line output for wave_browser
0091 handles.output = hObject;
0092 
0093 
0094 % Update handles structure
0095 guidata(hObject, handles);
0096 
0097 % UIWAIT makes wave_browser wait for user response (see UIRESUME)
0098 % uiwait(handles.figure1);
0099 
0100 
0101 % --- Outputs from this function are returned to the command line.
0102 function varargout = wave_browser_OutputFcn(hObject, eventdata, handles) 
0103 
0104 % Get default command line output from handles structure
0105 varargout{1} = handles.output;
0106 
0107 function Frequency_Callback(hObject, eventdata, handles)
0108 handles.Fs = eval(get(hObject,'String'));
0109 guidata(gcbo,handles); 
0110 
0111 function Frequency_CreateFcn(hObject, eventdata, handles)
0112 set(hObject,'String', '44100');
0113 handles.Fs = 44100;
0114 guidata(gcbo,handles);
0115 if ispc
0116     set(hObject,'BackgroundColor','white');
0117 else
0118     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0119 end
0120 
0121 % --- Executes on button press in LoadFile.
0122 function LoadFile_Callback(hObject, eventdata, handles)
0123 
0124 [fname pname]=uigetfile({'*.wav';'*.*'},'Load Time Series');
0125 
0126 if fname == 0
0127     return
0128 end
0129 
0130 
0131 
0132 set(handles.FileNameString, 'String',fname);
0133 handles.filename = [pname fname];
0134 [path] = cell2mat(regexp( handles.filename, '^.*\', 'match' ));
0135 [extension] = cell2mat(regexp( handles.filename, '\w+$', 'match' ));
0136 set(handles.Path,'String',path);
0137 % set(handles.Extensions,'String',extension);
0138 
0139 handles.segments = [];
0140 handles.allsegments = [];
0141 
0142 handles = loadfile(hObject, eventdata, handles);
0143 guidata(hObject,handles);
0144 
0145 function [wavesize channels] = wavsizeget(filename);
0146 % Provides usable information about a file
0147     wavesize = 0;
0148     [filestatus info] = wavfinfo(filename);
0149     info = regexp(info,'[0-9]+','match');
0150     channels = str2num(info{2});
0151     wavesize = str2num(info{1});
0152         
0153 function handles = loadfile(hObject, eventdata, handles, varargin)
0154 % Function for loading a file or using the optional varargin to load a
0155 % specified position and size in the file
0156 
0157 % contents=get(handles.endian,'String');
0158 % precision=contents{get(handles.endian,'Value')};
0159 
0160 [datasize channels] = wavsizeget(handles.filename);
0161 handles.wavsize = datasize; % total number of samples in the files
0162 
0163 if channels > 1 % code could be added here to put the channels together
0164     msgbox('Only one audio channel is supported.');
0165     return;
0166 end
0167 
0168 try
0169     handles.maxwavsize = round(handles.Fs * str2num(get(handles.MaximumWavSize,'String')));
0170 catch
0171     handles.maxwavsize = 20;
0172     set(handles.MaximumWavSize,'String',num2str(handles.maxwavsize));
0173     handles.maxwavsize = handles.maxwavsize * handles.Fs;
0174 end
0175 
0176 if isempty(varargin)
0177     handles.markerstart = 1;
0178     if datasize > handles.maxwavsize
0179         handles.markerend = handles.maxwavsize;
0180         handles.longfile = 1; % indicates that the file is long and will be loaded in chunks
0181     else
0182         handles.markerend = datasize;
0183     end
0184 else % passed in optional parameter
0185     handles.markervec = varargin{1};
0186     handles.markerstart = handles.markervec(1);
0187     handles.markerend = handles.markervec(2);
0188 end
0189 
0190 if handles.markerstart <= 1 % make sure the range is possible
0191     handles.markerstart = 1;   
0192     set(handles.PreviousChunk,'Enable','off');
0193 else
0194     set(handles.PreviousChunk,'Enable','on');
0195 end
0196 
0197 if handles.markerend >= handles.wavsize
0198     handles.markerend = handles.wavsize;   
0199     set(handles.NextChunk,'Enable','off');
0200 else
0201     set(handles.NextChunk,'Enable','on');
0202 end
0203 
0204 
0205 if handles.maxwavsize < handles.wavsize
0206    total_chunk = ceil(handles.wavsize / handles.maxwavsize);
0207    i = 1;
0208    while (i < total_chunk) &&  (handles.markerend >= (handles.maxwavsize * i))
0209        i = i + 1;
0210    end
0211    
0212    current_chunk = i-1;  
0213    
0214    if handles.markerend == handles.wavsize
0215        current_chunk = total_chunk;
0216    end
0217    
0218    set(handles.ChunkText,'String',['Chunk ' num2str(current_chunk) '/' num2str(total_chunk)]);
0219 end
0220 
0221 try
0222     handles.maxseglength = round(handles.Fs * str2num(get(handles.MaxSegLength,'String')));
0223 catch
0224     handles.maxseglength = handles.Fs * 1;
0225 end
0226 
0227 set(handles.RealDuration,'String',num2str(handles.wavsize/handles.Fs,'%.1f'));
0228 
0229 if handles.segmentmode % only if in segment mode make sure segments are not cut
0230     if (handles.markerstart - handles.lastmarkerstart > 0) % only do this in terms of forward movement
0231         if handles.dontcutsegments % this code is added so segments are not cut off when segmenting
0232             if not(isempty(handles.segments)) % at least one segment has been defined previously
0233                 maxsegend = handles.segments(1).end; % find last defined segment in previous view
0234                 for i = 2:length(handles.segments)
0235                     if handles.segments(i).end > maxsegend
0236                         maxsegend = handles.segments(i).end;
0237                     end
0238                 end
0239         
0240                 maxsegend = round(maxsegend * handles.Fs);
0241         
0242                 if (handles.lastmarkerend - maxsegend) < (handles.lastmarkerend - handles.maxseglength)
0243                     handles.markerstart = (handles.lastmarkerstart + maxsegend) + 1; % defined segment is closer to the end
0244                 else
0245                     handles.markerstart = handles.lastmarkerend - handles.maxseglength;
0246                 end
0247             else
0248                 handles.markerstart = handles.lastmarkerend - handles.maxseglength;
0249             end
0250     
0251             handles.markerend = handles.markerstart + handles.maxwavsize - 1;
0252             if handles.markerend > handles.wavsize 
0253                 handles.markerend = handles.wavsize;
0254             end
0255         end
0256     end
0257 end
0258 
0259 hw=waitbar(0,'Loading ...'); waitbar(0.5,hw); drawnow;
0260 
0261 [handles.markerstart handles.markerend]/handles.Fs
0262 [handles.ts,handles.Fs] = wavread(handles.filename, [handles.markerstart handles.markerend]);
0263 
0264 count = length(handles.ts);
0265 handles.ts = handles.ts/std(handles.ts); % variance normalisation
0266 set(handles.Frequency,'String', num2str(handles.Fs));
0267 set( handles.Duration, 'String', count/handles.Fs );
0268 Tim=eval(get(handles.DisplayWindow,'String'));
0269 display_frac = 1;%max(1,Tim*handles.Fs/count);
0270 
0271 set( handles.slider1, 'Value', 0 );
0272 set( handles.SegmentButton, 'Enable', 'on' );
0273 
0274 if handles.longfile
0275     set(handles.SeekButton,'Enable', 'on');
0276 end
0277 
0278 set(handles.LoadNext, 'Enable', 'on' );
0279 set(handles.PlayAll, 'Enable', 'on' );
0280 set(handles.PlayWindow, 'Enable', 'on' );
0281 set(handles.Plot, 'Enable', 'on' );
0282 set(handles.PlotAllButton, 'Enable', 'on');
0283 set(handles.Precompute, 'Enable','on');
0284 set(handles.Jump,'Enable','on');
0285 set(handles.JumpBack,'Enable','on');
0286 
0287 handles.segments = []; % remove the current segments
0288 handles.segments = filtersegments(handles,handles.allsegments);
0289 
0290 set(handles.Precompute,'Value',1); % Set into precompute mode
0291 
0292 Precompute_Callback(handles.Precompute, eventdata, handles);
0293 handles = guidata(gcbo);
0294 
0295 % set(handles.Precompute,'Value',1);
0296 
0297 handles.precomputed_spec = 1;
0298 handles.dontcutsegments = 1; % make sure segments are not cut off
0299 handles.lastmarkerstart = handles.markerstart;
0300 handles.lastmarkerend = handles.markerend;
0301 
0302 close(hw);
0303 % guidata(gcbo,handles);
0304 
0305 
0306 % Plot_Callback(hObject, eventdata, handles);
0307 
0308 % --- Executes on selection change in endian.
0309 function endian_Callback(hObject, eventdata, handles)
0310 
0311 % --- Executes during object creation, after setting all properties.
0312 function endian_CreateFcn(hObject, eventdata, handles)
0313 if ispc
0314     set(hObject,'BackgroundColor','white');
0315 else
0316     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0317 end
0318 
0319 
0320 function edit2_Callback(hObject, eventdata, handles)
0321 function edit2_CreateFcn(hObject, eventdata, handles)
0322 if ispc
0323     set(hObject,'BackgroundColor','white');
0324 else
0325     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0326 end
0327 
0328 function FileNameString_Callback(hObject, eventdata, handles)
0329 
0330 function FileNameString_CreateFcn(hObject, eventdata, handles)
0331 if ispc
0332     set(hObject,'BackgroundColor','white');
0333 else
0334     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0335 end
0336 
0337 function WinSize_Callback(hObject, eventdata, handles)
0338 
0339 function WinSize_CreateFcn(hObject, eventdata, handles)
0340 if ispc
0341     set(hObject,'BackgroundColor','white');
0342 else
0343     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0344 end
0345 
0346 
0347 % --- Executes on slider movement.
0348 function slider1_Callback(hObject, eventdata, handles)
0349 
0350 function slider1_CreateFcn(hObject, eventdata, handles)
0351 usewhitebg = 1;
0352 if usewhitebg
0353     set(hObject,'BackgroundColor',[.9 .9 .9]);
0354 else
0355     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0356 end
0357 
0358 function StepSize_Callback(hObject, eventdata, handles)
0359 
0360 function StepSize_CreateFcn(hObject, eventdata, handles)
0361 if ispc
0362     set(hObject,'BackgroundColor','white');
0363 else
0364     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0365 end
0366 
0367 function TW_Callback(hObject, eventdata, handles)
0368 
0369 function TW_CreateFcn(hObject, eventdata, handles)
0370 if ispc
0371     set(hObject,'BackgroundColor','white');
0372 else
0373     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0374 end
0375 
0376 function DisplayWindow_Callback(hObject, eventdata, handles)
0377 
0378 function DisplayWindow_CreateFcn(hObject, eventdata, handles)
0379 set(hObject, 'String', '4');
0380 if ispc
0381     set(hObject,'BackgroundColor','white');
0382 else
0383     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0384 end
0385 
0386 %function axes2ButtonDownCallback(hObject, eventdata, handles)
0387 %h=handles.axesS; P=get(h,'CurrentPoint');
0388 %fprintf( 'worked %f %f!\n', P(1),P(2));
0389 
0390 function indexinS = getindexpre_c(t,timestart,timeend)
0391 % A function for indexing correctly in time to a spectra stored in memory
0392 tlen = length(t);
0393 
0394 i=1;
0395 
0396 while (i <= tlen) && (t(i) < timestart)
0397     i = i + 1;
0398 end
0399 
0400 firstindex = i;
0401 
0402 while (i <= tlen) && (t(i) < timeend)
0403     i = i + 1;
0404 end
0405 
0406 secondindex = i;
0407 
0408 indexinS = [firstindex secondindex];
0409 % --- Executes on button press in Plot.
0410 function Plot_Callback(hObject, eventdata, handles)
0411 
0412 hw=waitbar(0.5,'Spectrogram calculation');drawnow
0413 
0414 params.Fs=handles.Fs;
0415 
0416 window=eval(get(handles.WinSize,'String'));
0417 winstep=eval(get(handles.StepSize,'String'));
0418 movingwin=[window winstep]*0.001;
0419 
0420 fmin=eval(get(handles.MinFreq,'String'));
0421 fmax=eval(get(handles.MaxFreq,'String'));
0422 params.fpass=[fmin fmax];
0423 
0424 p=eval(get(handles.TW,'String'));
0425 params.tapers=[p floor(2*p-1)];
0426 
0427 params.pad=1;
0428 
0429 Tslider=get(handles.slider1,'Value');
0430 Tim=eval(get(handles.DisplayWindow,'String'));
0431 NT=min(round(Tim*handles.Fs),length(handles.ts));
0432 handles.Tmin=1+floor(Tslider*length(handles.ts));
0433 handles.Tmax=min(handles.Tmin+NT,length(handles.ts));
0434 
0435 if handles.Tmax < length(handles.ts)
0436     set( handles.Jump, 'Enable', 'on' );
0437 else 
0438     set( handles.Jump, 'Enable', 'off' );
0439 end
0440 if handles.Tmin > 1
0441     set( handles.JumpBack, 'Enable', 'on' );
0442 else
0443     set( handles.JumpBack, 'Enable', 'off' );    
0444 end
0445 
0446 data=handles.ts(handles.Tmin:handles.Tmax);data=data(:);
0447 
0448 handles.upper_range =  eval(get(handles.RatioLower,'String'));
0449 handles.lower_range  = eval(get(handles.RatioUpper,'String'));
0450 handles.indexthresh =  eval(get(handles.RatioThresh,'String'));
0451 handles.nsmooth = eval(get(handles.SmoothFactor,'String'));
0452 
0453 % determine spectrum type
0454 
0455 contents=get(handles.SpectrumType,'String');
0456 stype=contents{get(handles.SpectrumType,'Value')};
0457 
0458 axes(handles.axesW); plot(((handles.markerstart - 1)/handles.Fs) + [handles.Tmin:handles.Tmax]/handles.Fs,handles.ts(handles.Tmin:handles.Tmax)); axis tight;
0459 
0460 switch stype
0461     case 'Original'
0462         
0463      if not(handles.precomputed_spec) || handles.firsttime
0464         [S,t,f]=mtspecgramc(diff(data),movingwin,params);
0465         timeax=(handles.Tmin/handles.Fs)+t;
0466      else
0467          indexinS = getindexpre_c(handles.t,(handles.Tmin-1)/handles.Fs,(handles.Tmax-1)/handles.Fs);
0468 %          indexinS = round(([handles.Tmin-1, handles.Tmax-1]/handles.Fs)/movingwin(2))+1;
0469          
0470          if indexinS(1) < 1
0471              indexinS(1) = 1;
0472          end
0473          
0474          SLen = length(handles.S(:,1));
0475          
0476          if indexinS(2) > SLen
0477              indexinS(2) = SLen;
0478          end
0479          
0480          f = handles.f;
0481          t = handles.t(indexinS(1):indexinS(2));
0482          S = handles.S(indexinS(1):indexinS(2),:);
0483          
0484          timeax=t;
0485      end
0486     
0487     cmap='default'; 
0488     
0489     th=eval(get(handles.AmpThresh,'String'));
0490     
0491     % This sets up the automatic segmenting algorithm
0492     if strcmp(handles.automethod,'threshold')
0493         [Stot boxcurve] = compute_threshold_free(S,th,handles.nsmooth);
0494         axes(handles.axesP); 
0495         semilogy(timeax,Stot); 
0496         axis tight;
0497     elseif strcmp(handles.automethod,'ratiof')
0498         [ratiof boxcurve] = compute_index(S,handles.lower_range,handles.upper_range,fmin,fmax,handles.indexthresh,handles.nsmooth);
0499         axes(handles.axesP); 
0500         semilogy(timeax,ratiof); 
0501         axis tight;
0502     end
0503     
0504      
0505     hold on; semilogy(timeax,boxcurve,'r'); hold off;
0506     axes(handles.axesS);
0507     imagesc(timeax,f,log(S)'); axis xy; colormap(cmap);
0508     %imagesc(t,f,log(S)'); axis xy; colormap(cmap);
0509     
0510 %    set(h,'ButtonDownFcn',axes2ButtonDownCallback);
0511     
0512     case 'Time Derivative'
0513     
0514     if not(handles.precomputed_spec) || handles.firsttime   
0515         [S,t,f]=mtdspecgramc(diff(data),movingwin,0,params);S = S';
0516         timeax=handles.Tmin/handles.Fs+t;
0517     else
0518         indexinS = getindexpre_c(handles.t,(handles.Tmin-1)/handles.Fs,(handles.Tmax-1)/handles.Fs);
0519 %          indexinS = round(([handles.Tmin-1, handles.Tmax-1]/handles.Fs)/movingwin(2))+1;
0520          
0521          if indexinS(1) < 1
0522              indexinS(1) = 1;
0523          end
0524          
0525          SLen = length(handles.S(1,:));
0526          
0527          if indexinS(2) > SLen
0528              indexinS(2) = SLen;
0529          end
0530          
0531          f = handles.f;
0532          t = handles.t(indexinS(1):indexinS(2));
0533          S = handles.S(:,indexinS(1):indexinS(2));
0534          timeax = t;
0535     end
0536     
0537     cmap='gray';
0538     th=eval(get(handles.TDerThresh,'String'));
0539     
0540     if strcmp(handles.automethod,'threshold')
0541         [Stot boxcurve] = compute_threshold_free(abs(S'),th.handles.nsmooth);
0542         axes(handles.axesP); 
0543         semilogy(timeax,Stot); 
0544         axis tight;
0545     elseif strcmp(handles.automethod,'ratiof')
0546         [ratiof boxcurve] = compute_index(abs(S)',handles.lower_range,handles.upper_range,fmin,fmax,handles.indexthresh,handles.nsmooth);
0547         axes(handles.axesP); 
0548         semilogy(timeax,ratiof); 
0549         axis tight;
0550     end
0551     
0552     hold on; semilogy(timeax,boxcurve,'r'); hold off;
0553     axes(handles.axesS);
0554     imagesc(timeax,f,S); axis xy; colormap(cmap);
0555     cmin=0.02*min(min(S)); cmax=0.02*max(max(S)); caxis([cmin cmax]);
0556     
0557     case 'Frequency Derivative'
0558 
0559     if not(handles.precomputed_spec) || handles.firsttime
0560         [S,t,f]=mtdspecgramc(diff(data),movingwin,pi/2,params);S=S';
0561         timeax=handles.Tmin/handles.Fs+t;
0562     else
0563         indexinS = getindexpre_c(handles.t,(handles.Tmin-1)/handles.Fs,(handles.Tmax-1)/handles.Fs);
0564          
0565          if indexinS(1) < 1
0566              indexinS(1) = 1;
0567          end
0568          
0569          SLen = length(handles.S(1,:));
0570          
0571          if indexinS(2) > SLen
0572              indexinS(2) = SLen;
0573          end
0574          
0575          f = handles.f;
0576          t = handles.t(indexinS(1):indexinS(2));
0577          S = handles.S(:,indexinS(1):indexinS(2));
0578          timeax = t;
0579     end
0580     
0581     cmap='gray';
0582     th=eval(get(handles.TDerThresh,'String'));
0583     
0584     if strcmp(handles.automethod,'threshold')
0585         [Stot boxcurve] = compute_threshold_free(abs(S'),th,handles.nsmooth);
0586         axes(handles.axesP); 
0587         semilogy(timeax,Stot); 
0588         axis tight;
0589     elseif strcmp(handles.automethod,'ratiof')
0590         [ratiof boxcurve] = compute_index(abs(S)',handles.lower_range,handles.upper_range,fmin,fmax,handles.indexthresh,handles.nsmooth);
0591         axes(handles.axesP); 
0592         semilogy(timeax,ratiof); 
0593         axis tight;
0594     end
0595     
0596     hold on; semilogy(timeax,boxcurve,'r'); hold off;
0597     axes(handles.axesS);
0598     imagesc(timeax,f,S); axis xy; colormap(cmap);
0599     cmin=0.02*min(min(S)); cmax=0.02*max(max(S)); caxis([cmin cmax]);
0600 
0601 end;
0602 
0603 if handles.firsttime % first time precomputing the spectra
0604     handles.S = S;
0605     handles.t = t;
0606     handles.f = f;
0607     handles.precomputed_spec = 1;
0608     handles.firstime = 0;
0609 end
0610 
0611 % S = log(S)';
0612 % Smax = max(max(S));
0613 % Smin = min(min(S));
0614 % Ssmall = uint8(round(((S - Smin)/(Smax-Smin))*255));
0615 %
0616 %  save('uint8_test.mat','Ssmall','-mat');
0617 %  save('full_rest.mat','S','-mat');
0618 
0619 handles.times=timeax(:);
0620 handles.transition=[diff(boxcurve(:)); 0];
0621 
0622 set( handles.axesS, 'XTick', [] );
0623 set( handles.axesP, 'XTick', [] );
0624  
0625 if exist('handles.datacursor')
0626     delete( handles.datacursor );
0627     delete( handles.segmentLineP );
0628     delete( handles.segmentLineS );
0629     delete( handles.segmentLineW );
0630 end 
0631 
0632 handles.datacursor=datacursormode(handles.figure1);  
0633 axes(handles.axesP);
0634 handles.segmentLineP = line('Visible','off');
0635 axes(handles.axesS);
0636 handles.segmentLineS = line('Visible','off');
0637 axes(handles.axesW);
0638 handles.segmentLineW = line('Visible','off');
0639 
0640 if get( handles.SegmentButton, 'Value' )
0641     set(handles.datacursor,'Enable','on','DisplayStyle','datatip','SnapToDataVertex','off','UpdateFcn',@datacursorfunc);
0642 end
0643 
0644 guidata(gcbo,handles); 
0645 close(hw);
0646 handles = draw_segments(handles);
0647 
0648 function  [Stot boxcurve] = compute_threshold_free(S,th,n)
0649 % Computes the threshold based on a floating percentage of the maximum
0650 % summed intensity
0651     Stot=sum(S,2); 
0652     boxcurve=Stot; 
0653     smax=max(Stot); 
0654     
0655     Stot = smooth_curve(Stot',n); % for removing extremes
0656     
0657     boxcurve(find(Stot<th*smax))= smax*th; 
0658     boxcurve(find(Stot>th*smax))= smax;
0659     
0660 function [ratiof boxcurve] = compute_index(S,lower_range,upper_range,lowerfreq,upperfreq,indexthresh,n)
0661     % This algorithm is based on the method described in Aylin's
0662     % dissertation.
0663     
0664     S = S';
0665     nfreqs = length(S(:,1)); 
0666     freqspern = (upperfreq - lowerfreq) / nfreqs;
0667     
0668     indexinlower = fliplr(nfreqs - round((lower_range - lowerfreq)/freqspern));
0669     indexinupper = fliplr(nfreqs - round((upper_range - lowerfreq)/freqspern)) + 1;
0670     
0671     nrangelower = indexinlower(2)-indexinlower(1);
0672     nrangeupper = indexinupper(2)-indexinupper(1);
0673     
0674     ratiof = ( sum(S(indexinupper(1) : indexinupper(2),:)) / nrangeupper )... 
0675     ./ ( sum(S( indexinlower(1) : indexinlower(2),:)) / nrangelower );
0676      
0677     
0678     ratiof = smooth_curve(ratiof,n); % for smoothing the curve
0679     
0680     maxrf = max(ratiof);
0681     
0682     boxcurve = ratiof;
0683     
0684     boxcurve(find(ratiof<indexthresh))= indexthresh; 
0685     boxcurve(find(ratiof>=indexthresh))= maxrf;    
0686     
0687 function smoothedcurve = smooth_curve(curve2smooth,n);
0688 % Computes the moving average of the curve where n is an integer
0689 % for example n = 1 averages the current point with the point before and afterwards
0690 
0691     m = length(curve2smooth);
0692     if m > 0
0693         curve2smooth = [repmat(curve2smooth(1),1,n) curve2smooth repmat(curve2smooth(m),1,n)];
0694         smoothedcurve = zeros(m,1);
0695     
0696         for i = 1:m
0697             smoothedcurve(i) = sum(curve2smooth(i:i + 2 * n)) / (2 * n + 1);
0698         end
0699     else % just to save computation time
0700         smoothed_curve = curve2smooth;
0701     end
0702     
0703 function MinFreq_Callback(hObject, eventdata, handles)
0704 
0705 function MinFreq_CreateFcn(hObject, eventdata, handles)
0706 if ispc
0707     set(hObject,'BackgroundColor','white');
0708 else
0709     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0710 end
0711 
0712 function MaxFreq_Callback(hObject, eventdata, handles)
0713 function MaxFreq_CreateFcn(hObject, eventdata, handles)
0714 if ispc
0715     set(hObject,'BackgroundColor','white');
0716 else
0717     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0718 end
0719 
0720 % --- Executes on selection change in SpectrumType.
0721 function SpectrumType_Callback(hObject, eventdata, handles)
0722 function SpectrumType_CreateFcn(hObject, eventdata, handles)
0723 if ispc
0724     set(hObject,'BackgroundColor','white');
0725 else
0726     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0727 end
0728 
0729 function AmpThresh_Callback(hObject, eventdata, handles)
0730 function AmpThresh_CreateFcn(hObject, eventdata, handles)
0731 if ispc
0732     set(hObject,'BackgroundColor','white');
0733 else
0734     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0735 end
0736 
0737 function TDerThresh_Callback(hObject, eventdata, handles)
0738 
0739 % --- Executes during object creation, after setting all properties.
0740 function TDerThresh_CreateFcn(hObject, eventdata, handles)
0741 if ispc
0742     set(hObject,'BackgroundColor','white');
0743 else
0744     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
0745 end
0746 
0747 % --- Executes on button press in PlayAll.
0748 function PlayAll_Callback(hObject, eventdata, handles)
0749 wavplay(handles.ts,handles.Fs);
0750 
0751 
0752 % --- Executes on button press in PlayWindow.
0753 function PlayWindow_Callback(hObject, eventdata, handles)
0754 wavplay(handles.ts(handles.Tmin:handles.Tmax),handles.Fs,'async');
0755 
0756 %h=handles.axesP; axes(h); semilogy(timeax,Stot); axis tight;
0757 
0758 function txt = datacursorfunc(empt,event_obj)
0759 pos = get(event_obj,'Position');
0760 handles=guidata(get(event_obj,'Target'));
0761 
0762   set(handles.segmentLineP,'Xdata',[pos(1) pos(1)],'Ydata',[0.00000000000001*pos(2) 1000000000000*pos(2)],'Visible','on' );
0763   set(handles.segmentLineS,'Xdata',[pos(1) pos(1)],'Ydata',[0.00000000000001*pos(2) 1000000000000*pos(2)],'Visible','on' );
0764   set(handles.segmentLineW,'Xdata',[pos(1) pos(1)],'Ydata',[-100000000000*pos(2) 1000000000000*pos(2)],'Visible','on' );
0765 
0766   if handles.start_stop_enable == 1
0767       set( handles.SegStartButton, 'Enable', 'on' );
0768   else
0769       set( handles.SegEndButton, 'Enable', 'on' );
0770   end
0771 
0772 txt = {[num2str(pos(1))]};
0773 guidata(gcbo,handles); 
0774 
0775 function handles = draw_segments( handles )
0776 n = 1;
0777 while n <= length( handles.segments )
0778     handles.segments(n).lines=[];
0779     handles.segments(n) = draw_all_x( handles, handles.segments(n) );
0780     n = n + 1;
0781 end
0782 guidata(gcbo,handles); 
0783 
0784 % --- Executes on button press in SegmentButton.
0785 function SegmentButton_Callback(hObject, eventdata, handles)
0786 toggled = get( handles.SegmentButton, 'Value' );
0787 
0788 if toggled
0789     handles.segments = [];
0790     handles.segmentmode = 1;
0791     set( handles.SegmentButton, 'String', 'Segment On' );
0792     set( handles.SegmentButton, 'Enable', 'off' );
0793     if not(exist([handles.filename '.seg.txt']));
0794         set( handles.LoadSegments, 'Enable', 'off' );
0795     else
0796         set( handles.LoadSegments, 'Enable', 'on' );
0797     end
0798     set( handles.AutoSegmentFile, 'Enable','on');
0799     set( handles.AutoSegButton, 'Enable', 'on' );  
0800     set( handles.SegmentLengthEdit, 'Enable', 'on' );  
0801     set( handles.SegmentLengthText, 'Enable', 'on ' );  
0802     set( handles.SaveSegments, 'Enable', 'on' );  
0803     set( handles.DeleteSegment, 'Enable', 'on' );
0804     set( handles.DeleteAllButton, 'Enable', 'on' );
0805     set( handles.SegCancel, 'Enable', 'on' );  
0806     set( handles.PlotSegments, 'Enable', 'on' );
0807     set( handles.LoadFile, 'Enable', 'off' );
0808     set( handles.LoadNext, 'Enable', 'off' );
0809     handles.start_stop_enable = 1;
0810     set(handles.datacursor,'Enable','on','DisplayStyle','datatip','SnapToDataVertex','off','UpdateFcn',@datacursorfunc);
0811     fprintf( 'Segment mode on!\n' );
0812 else
0813     handles.segmentmode = 0;
0814     set( handles.SegmentButton, 'String', 'Segment Off' );
0815     set( handles.AutoSegButton, 'Enable', 'off' );  
0816     set( handles.AutoSegmentFile, 'Enable','off');
0817     set( handles.SegmentLengthEdit, 'Enable', 'off' );  
0818     set( handles.SegmentLengthText, 'Enable', 'off' );  
0819     set( handles.LoadSegments, 'Enable', 'off' );
0820     set( handles.SaveSegments, 'Enable', 'off' );  
0821     set( handles.SegStartButton, 'Enable', 'off' );
0822     set( handles.SegEndButton, 'Enable', 'off' );
0823     set( handles.DeleteSegment, 'Enable', 'off' );
0824     set( handles.DeleteAllButton, 'Enable', 'off' );
0825     set( handles.SegCancel, 'Enable', 'off' );  
0826     set( handles.PlotSegments, 'Enable', 'off' );
0827     set( handles.LoadFile, 'Enable', 'on' );
0828     set( handles.LoadNext, 'Enable', 'on' );
0829     set(handles.datacursor,'Enable','off')
0830     fprintf( 'Segment mode off!\n' );
0831 end
0832 guidata(gcbo,handles); 
0833 
0834 
0835 % --- Executes on button press in SegStartButton.
0836 function SegStartButton_Callback(hObject, eventdata, handles)
0837 set( handles.LoadSegments, 'Enable', 'off' );
0838 set( handles.SegStartButton, 'Enable', 'off' );
0839 handles.start_stop_enable = 0;
0840 xy=get(handles.segmentLineP,'Xdata');
0841 handles.segment.start=xy(1);
0842 handles.segment.lines=[];
0843 axes(handles.axesP);
0844 set(handles.segmentLineP,'LineWidth',3);
0845 handles.segment.lines(1) = handles.segmentLineP;
0846 handles.segmentLineP = line('Visible','off');
0847 axes(handles.axesS);
0848 set(handles.segmentLineS,'LineWidth',3);
0849 handles.segment.lines(2) = handles.segmentLineS;
0850 handles.segmentLineS = line('Visible','off');
0851 axes(handles.axesW);
0852 set(handles.segmentLineW,'LineWidth',3);
0853 handles.segment.lines(3) = handles.segmentLineW;
0854 handles.segmentLineW = line('Visible','off');
0855 
0856 guidata(gcbo,handles); 
0857 
0858 % --- Executes on button press in SegEndButton.
0859 function SegEndButton_Callback(hObject, eventdata, handles)
0860 set( handles.SegEndButton, 'Enable', 'off' );
0861 handles.start_stop_enable = 1;
0862 xy=get(handles.segmentLineP,'Xdata');
0863 handles.segment.end=xy(1);
0864 handles.segment=draw_all_x( handles, handles.segment );
0865 handles.segments = [handles.segments handles.segment];
0866 guidata(gcbo,handles); 
0867 
0868 function out=draw_all_x( handles, segment )
0869 segment=draw_x( handles.axesP, segment );
0870 segment=draw_x( handles.axesS, segment );
0871 segment=draw_x( handles.axesW, segment );
0872 out=segment;
0873 
0874 function out=draw_x( theaxes, segment )
0875 axes(theaxes);
0876 ylim = get(theaxes,'YLim');
0877 segment.lines = [segment.lines line('Xdata',[segment.start segment.start],'Ydata',ylim,'LineWidth',3)];
0878 segment.lines = [segment.lines line('Xdata',[segment.end segment.end],'Ydata',ylim,'LineWidth',3)];
0879 segment.lines = [segment.lines line('Xdata',[segment.start segment.end],'Ydata',ylim,'LineWidth',3)];
0880 segment.lines = [segment.lines line('Xdata',[segment.start segment.end],'Ydata',[ylim(2) ylim(1)],'LineWidth',3)];
0881 out=segment;
0882 
0883 % --- Executes on button press in JumpBack.
0884 function JumpBack_Callback(hObject, eventdata, handles)
0885 Jump_shared(hObject, eventdata, handles, -1 )
0886 
0887 % --- Executes on button press in Jump.
0888 function Jump_Callback(hObject, eventdata, handles)
0889 Jump_shared(hObject, eventdata, handles, 1 )
0890 
0891 function Jump_shared(hObject, eventdata, handles, jump_dir )
0892 Tim=eval(get(handles.DisplayWindow,'String'));
0893 tDuration = str2num(get(handles.Duration,'String'));
0894 maxTslider = (tDuration - Tim)/tDuration;
0895 NT=min(round(Tim*handles.Fs),length(handles.ts));
0896 Tslider=get(handles.slider1,'Value');
0897 Tslider = Tslider + jump_dir * Tim * handles.Fs / length(handles.ts);
0898 
0899 if Tim > tDuration
0900     set(handles.DisplayWindow,'String',num2str(tDuration));
0901     Tslider = 0;
0902 end
0903 
0904 
0905 if jump_dir == 1 % jumping forward
0906     if Tslider > maxTslider
0907         Tslider = maxTslider;
0908     end
0909 end
0910 
0911 if jump_dir == -1 % jumping backwards
0912     if Tslider < 0
0913         Tslider = 0
0914     end
0915 end
0916 
0917     
0918 % if Tslider > 1
0919 %     Tslider = ( length(handles.ts) - NT ) / length(handles.ts);
0920 % end
0921 % if Tslider < 0
0922 %     Tslider = 0
0923 % end
0924 set(handles.slider1,'Value',Tslider);
0925 guidata(gcbo,handles); 
0926 Plot_Callback(hObject, eventdata, handles)
0927 
0928 
0929 function LoadNext_Callback(hObject, eventdata, handles)
0930 
0931 % Get filename, extension.  Look for next file with same extension, no seg
0932 % file associated
0933 
0934 exclude_name = [handles.filename, get(handles.ExcludeExt,'String')];
0935 if not(exist(exclude_name))
0936      fid=fopen( exclude_name, 'w' );
0937      fclose( fid);
0938 end
0939 
0940 [path] = cell2mat(regexp( handles.filename, '^.*\', 'match' ));
0941 [extension] = cell2mat(regexp( handles.filename, '\w+$', 'match' ));
0942 dirlist = dir( [path '*' extension] );
0943 ndir = length(dirlist);
0944 n = 1;
0945 while n <= ndir
0946     file = dirlist(n).name;
0947     if not(exist([path file get(handles.ExcludeExt,'String')]))
0948         break;
0949     end
0950        n = n + 1;
0951 end
0952 if n <= ndir
0953     set( handles.FileNameString, 'String',file);
0954     handles.filename = [path file];
0955     guidata(gcbo,handles); 
0956     handles = loadfile(hObject, eventdata, handles);
0957 else
0958     error('No more files found matching desired pattern');
0959 end
0960 
0961 % --- Executes on button press in Precompute.
0962 function Precompute_Callback(hObject, eventdata, handles)
0963 % handles = guidata(gcbo);
0964 toggled = get( hObject, 'Value' );
0965 if toggled
0966     
0967     % Disable spectra configuration parameters
0968     
0969 %     set(handles.DisplayWindow, 'Enable', 'off');
0970     set(handles.WinSize, 'Enable', 'off');
0971     set(handles.StepSize, 'Enable', 'off');
0972     set(handles.TW, 'Enable', 'off');
0973     set(handles.MinFreq, 'Enable', 'off');
0974     set(handles.MaxFreq, 'Enable', 'off');
0975     set(handles.SpectrumType, 'Enable', 'off');
0976 %     set(handles.AmpThresh, 'Enable', 'off');
0977 %     set(handles.TDerThresh, 'Enable', 'off');
0978     set(handles.LoadNext, 'Enable','off');
0979     set(handles.LoadFile, 'Enable','off');
0980     
0981     valueTslider = get(handles.slider1,'Value');
0982     set(handles.slider1,'Value',0);
0983     strDuration = get(handles.Duration,'String');
0984     strWindow = get(handles.DisplayWindow,'String');
0985     
0986     handles.firsttime = 1; % indicates that the spectra need to be calculated
0987     
0988     if str2num(strDuration) > handles.maxspec_t
0989         strDuration = num2str(handles.maxspec_t);
0990     end
0991     
0992     set(handles.DisplayWindow,'String',strDuration);
0993     
0994     Plot_Callback(handles.Plot, eventdata, handles);
0995     
0996     handles = guidata(hObject);
0997     
0998     handles.firsttime = 0;
0999     handles.precomputed_spec = 1;
1000     set(handles.DisplayWindow,'String',strWindow);
1001     set(handles.slider1,'Value',valueTslider);
1002     Plot_Callback(handles.Plot, eventdata, handles);
1003     handles = guidata(hObject);
1004     handles.precomputed_spec = 1;
1005 else
1006   handles.precomputed_spec = 0;
1007   
1008   
1009   % Enable spectra configuration parameters
1010   
1011   handles.S = []; % release memory
1012   handles.t = [];
1013   handles.f = [];
1014   
1015   set(handles.WinSize, 'Enable', 'on');
1016     set(handles.StepSize, 'Enable', 'on');
1017     set(handles.TW, 'Enable', 'on');
1018     set(handles.MinFreq, 'Enable', 'on');
1019     set(handles.MaxFreq, 'Enable', 'on');
1020     set(handles.SpectrumType, 'Enable', 'on');
1021 %     set(handles.AmpThresh, 'Enable', 'on');
1022     set(handles.TDerThresh, 'Enable', 'on');
1023     set(handles.LoadNext, 'Enable','on');
1024     set(handles.LoadFile, 'Enable','on');
1025   
1026 end
1027 
1028 guidata(hObject,handles);
1029 
1030 function Precompute_CreateFcn(hObject, eventdata, handles)
1031    
1032 function Path_Callback(hObject, eventdata, handles)
1033 path=get(hObject,'String')
1034 
1035 
1036 function Path_CreateFcn(hObject, eventdata, handles)
1037 set(hObject,'String',pwd);
1038 if ispc
1039     set(hObject,'BackgroundColor','white');
1040 else
1041     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
1042 end
1043 
1044 function Extensions_Callback(hObject, eventdata, handles)
1045 
1046 function Extensions_CreateFcn(hObject, eventdata, handles)
1047 set(hObject,'String','wav');
1048 if ispc
1049     set(hObject,'BackgroundColor','white');
1050 else
1051     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
1052 end
1053 
1054 function Duration_CreateFcn(hObject, eventdata, handles)
1055 if ispc
1056     set(hObject,'BackgroundColor','white');
1057 else
1058     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
1059 end
1060 
1061 function LoadSegments_Callback(hObject, eventdata, handles)
1062 
1063 handles = load_segment(handles, [handles.filename '.seg.txt'] );
1064 set( handles.LoadSegments, 'Enable', 'off' );
1065 handles = draw_segments( handles );
1066 guidata(gcbo,handles);
1067 
1068 function handles=load_segment(handles,filename)
1069 fid=fopen( filename, 'r' );
1070 segments = [];
1071 scanned=fscanf( fid, '%g %g',[2 inf] );
1072 n = 1;
1073 
1074 while n <= size(scanned, 2)
1075     segment.start = scanned(1,n);
1076     segment.end = scanned(2,n);
1077     segment.lines = [];
1078     segments = [ segments segment ];
1079     n = n + 1;
1080 end
1081 
1082 handles.allsegments = segments; % all segments holds all segments for the file
1083 handles.segments = filtersegments(handles,handles.allsegments); % get segments for the current chunk
1084 handles.loadedsegment = 1; % indicates segments have been filtered
1085 
1086 guidata(gcf,handles); 
1087 
1088 
1089 function filteredsegments = filtersegments(handles,segments)
1090 % Returns segments which are in the current defined view. Returns segments
1091 % which are not cut off.
1092 
1093 realstart = handles.markerstart / handles.Fs;
1094 realend = handles.markerend / handles.Fs;
1095 
1096 filteredsegments = [];
1097 
1098 for i = 1:length(segments) % no garuantee segments are in the same order
1099     if (segments(i).start >= realstart) && (segments(i).end <= realend)
1100         filteredsegments = [filteredsegments segments(i)];
1101     end
1102 end
1103     
1104 for i=1:length(filteredsegments)
1105     filteredsegments(i).start =  filteredsegments(i).start - realstart;
1106     filteredsegments(i).end =  filteredsegments(i).end - realstart;
1107 end
1108 
1109 function ExcludeExt_Callback(hObject, eventdata, handles)
1110 
1111 % Hints: get(hObject,'String') returns contents of ExcludeExt as text
1112 %        str2double(get(hObject,'String')) returns contents of ExcludeExt as a double
1113 
1114 function ExcludeExt_CreateFcn(hObject, eventdata, handles)
1115 
1116 if ispc
1117     set(hObject,'BackgroundColor','white');
1118 else
1119     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
1120 end
1121 
1122 
1123 function DeleteSegment_Callback(hObject, eventdata, handles)
1124 
1125 
1126    pos=get(handles.segmentLineP,'Xdata');    
1127     n = 1;
1128     while n <= length( handles.segments )
1129         if pos(1) >= handles.segments(n).start && pos(1) <= handles.segments(n).end
1130             handles=delete_segment( handles, n );
1131         else
1132            n = n + 1;
1133         end 
1134     end
1135     drawnow;
1136 guidata(gcbo,handles); 
1137 
1138 function handles=delete_segment( handles, n )
1139             nl = 1;
1140             while nl <= length( handles.segments(n).lines )
1141                 set( handles.segments(n).lines(nl), 'Visible', 'off'); 
1142                 nl = nl + 1;
1143             end
1144             handles.segments(n) = [];
1145             fprintf('deleted!\n');
1146 
1147 function SaveSegments_Callback(hObject, eventdata, handles)
1148 
1149 % For the currently defined segments append to the segment list
1150 
1151 handles = savesegments2mem(handles);
1152 
1153 segment_file = fopen( [handles.filename '.seg.txt'], 'wt' );
1154 n = 1;
1155 while n <= size(handles.allsegments, 2)
1156     fprintf( segment_file, '%f %f\n', handles.allsegments(n).start, handles.allsegments(n).end );
1157     n = n + 1;
1158 end
1159 fclose(segment_file);
1160 set( handles.SegmentButton, 'Enable', 'on' );
1161 guidata(gcbo,handles); 
1162 
1163 function handles=savesegments2mem(handles)
1164 % Updates the handles allsegments in memory
1165 
1166 % first remove in all segments all segments which are in the current chunk
1167 
1168 oldsegments = [];
1169 
1170 realstart = handles.markerstart / handles.Fs; % readjust time
1171 realend = handles.markerend / handles.Fs; % readjust time
1172 
1173 for i = 1:length(handles.allsegments)
1174     if not((handles.allsegments(i).start) >= realstart && (handles.allsegments(i).end <= realend))
1175         oldsegments = [oldsegments handles.allsegments(i)];
1176     end
1177 end
1178 
1179 % now put in the new segments
1180 
1181 newsegments = [];
1182 
1183 for i = 1:length(handles.segments)
1184     segment = handles.segments(i);
1185     segment.start = segment.start + realstart;
1186     segment.end = segment.end + realstart;
1187     newsegments = [newsegments segment];
1188 end
1189 
1190 handles.allsegments = [oldsegments newsegments];
1191 
1192 function SegCancel_Callback(hObject, eventdata, handles)
1193 set( handles.SegmentButton, 'Enable', 'on' );
1194 guidata(gcbo,handles); 
1195 
1196 
1197 function PlotSegments_Callback(hObject, eventdata, handles)
1198 
1199 % Load Segments in directory
1200 
1201 [path] = cell2mat(regexp( handles.filename, '^.*\', 'match' ));
1202 [extension] = cell2mat(regexp( handles.filename, '\w+$', 'match' ));
1203 
1204 path=get(handles.Path,'String');
1205 extension=get(handles.Extensions,'String');
1206 dirlist = dir( [path '\*' extension '.seg.txt'] );
1207 ndir = length(dirlist);
1208 n = 1;
1209 all_segments = [];
1210 while n <= ndir
1211     file = dirlist(n).name;
1212     segments = load_segment([path '\' file]);
1213     all_segments = [all_segments segments];
1214     n = n + 1;
1215 end
1216 
1217 % Plot info
1218 if length(all_segments) > 2
1219     
1220     figure();
1221     axes();
1222     nbin= max(length([all_segments.end])/5,10);
1223     syllable_lengths=[all_segments.end]-[all_segments.start];
1224     hi=hist( syllable_lengths ,nbin);
1225     tl=min( syllable_lengths );
1226     th=max( syllable_lengths );
1227     times=tl:((th-tl)/(nbin-1)):th;
1228     plot(times,hi);
1229     xlabel('Segment Length (s)');
1230     ylabel('N');
1231     title(['All segments in ' path]);
1232 else
1233     error('too few segments to plot');
1234 end
1235 guidata(gcbo,handles); 
1236   
1237 
1238 
1239 function AutoSegButton_Callback(hObject, eventdata, handles)
1240     
1241     n = 1;
1242     segments = [];
1243     segment.start = 0;
1244     segment.end = 0;
1245     segment.lines = [];
1246     minlen = eval(get( handles.SegmentLengthEdit, 'String' ));
1247     while n < length( handles.times )
1248         
1249        if ( handles.transition(n) > 0 )
1250            segment.start = handles.times(n);
1251        end
1252        if ( handles.transition(n) < 0 )
1253            segment.end = handles.times(n);
1254        end
1255        if (segment.start > 0) && (segment.end) > 0 && (segment.end - segment.start) > minlen
1256            segments = [ segments segment ];
1257            segment.start = 0;
1258             segment.end = 0;
1259        end
1260        n = n + 1; 
1261     end
1262     
1263     handles.segments = [handles.segments segments]; 
1264     handles = draw_segments( handles );
1265     guidata(gcbo,handles);
1266     
1267 function DeleteAllButton_Callback(hObject, eventdata, handles)
1268 
1269 while length( handles.segments )
1270     handles = delete_segment( handles, 1 );
1271 end
1272 guidata(gcf,handles); 
1273 
1274 
1275 % --- Executes on button press in PlotAllButton.
1276 function PlotAllButton_Callback(hObject, eventdata, handles)
1277 % hObject    handle to PlotAllButton (see GCBO)
1278 % eventdata  reserved - to be defined in a future version of MATLAB
1279 % handles    structure with handles and user data (see GUIDATA)
1280 
1281 set(handles.slider1,'Value',0);
1282 
1283 strDuration = get(handles.Duration,'String');
1284 
1285 if str2num(strDuration) > handles.maxspec_t
1286     strDuration = num2str(handles.maxspec_t);
1287 end
1288 
1289 
1290 set(handles.DisplayWindow,'String',strDuration);
1291 Plot_Callback(hObject, eventdata, handles);
1292 
1293 
1294 % --- Executes on button press in PreviousChunk.
1295 function PreviousChunk_Callback(hObject, eventdata, handles)
1296 % hObject    handle to PreviousChunk (see GCBO)
1297 % eventdata  reserved - to be defined in a future version of MATLAB
1298 % handles    structure with handles and user data (see GUIDATA)
1299 
1300 % [handles.markerstart handles.markerend]
1301 handles = savesegments2mem(handles);
1302 handles = loadfile(hObject, eventdata, handles,[handles.markerstart-handles.maxwavsize-1,handles.markerstart-1]);
1303 % [handles.markerstart handles.markerend]
1304 ;
1305 guidata(gcf,handles);
1306 
1307 % --- Executes on button press in NextChunk.
1308 function NextChunk_Callback(hObject, eventdata, handles)
1309 % hObject    handle to NextChunk (see GCBO)
1310 % eventdata  reserved - to be defined in a future version of MATLAB
1311 % handles    structure with handles and user data (see GUIDATA)
1312 
1313 handles = savesegments2mem(handles);
1314 handles = loadfile(hObject, eventdata, handles, [handles.markerend+1,handles.markerend+1+handles.maxwavsize]);
1315 guidata(gcf,handles);
1316 
1317 % --- Executes on button press in AutoSegmentFile.
1318 function AutoSegmentFile_Callback(hObject, eventdata, handles)
1319 % hObject    handle to AutoSegmentFile (see GCBO)
1320 % eventdata  reserved - to be defined in a future version of MATLAB
1321 % handles    structure with handles and user data (see GUIDATA)
1322 
1323 while (handles.markerend < handles.wavsize)
1324 
1325 PlotAllButton_Callback(hObject, eventdata, handles);
1326 handles = guidata(gcbo);
1327 AutoSegButton_Callback(hObject, eventdata, handles);
1328 handles = guidata(gcbo);
1329 NextChunk_Callback(hObject, eventdata, handles);
1330 handles = guidata(gcbo);
1331 
1332 end
1333 
1334 
1335 PlotAllButton_Callback(hObject, eventdata, handles);
1336 handles = guidata(gcbo);
1337 AutoSegButton_Callback(hObject, eventdata, handles);
1338 handles = guidata(gcbo);
1339 guidata(gcbo,handles);
1340 
1341 
1342 function MaxSegLength_Callback(hObject, eventdata, handles)
1343 % hObject    handle to MaxSegLength (see GCBO)
1344 % eventdata  reserved - to be defined in a future version of MATLAB
1345 % handles    structure with handles and user data (see GUIDATA)
1346 
1347 % Hints: get(hObject,'String') returns contents of MaxSegLength as text
1348 %        str2double(get(hObject,'String')) returns contents of MaxSegLength as a double
1349 
1350 
1351 % --- Executes during object creation, after setting all properties.
1352 function MaxSegLength_CreateFcn(hObject, eventdata, handles)
1353 % hObject    handle to MaxSegLength (see GCBO)
1354 % eventdata  reserved - to be defined in a future version of MATLAB
1355 % handles    empty - handles not created until after all CreateFcns called
1356 
1357 % Hint: edit controls usually have a white background on Windows.
1358 %       See ISPC and COMPUTER.
1359 if ispc
1360     set(hObject,'BackgroundColor','white');
1361 else
1362     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
1363 end
1364 
1365 function MaximumWavSize_Callback(hObject, eventdata, handles)
1366 % hObject    handle to MaximumWavSize (see GCBO)
1367 % eventdata  reserved - to be defined in a future version of MATLAB
1368 % handles    structure with handles and user data (see GUIDATA)
1369 
1370 % Hints: get(hObject,'String') returns contents of MaximumWavSize as text
1371 %        str2double(get(hObject,'String')) returns contents of MaximumWavSize as a double
1372 
1373 
1374 % --- Executes during object creation, after setting all properties.
1375 function MaximumWavSize_CreateFcn(hObject, eventdata, handles)
1376 % hObject    handle to MaximumWavSize (see GCBO)
1377 % eventdata  reserved - to be defined in a future version of MATLAB
1378 % handles    empty - handles not created until after all CreateFcns called
1379 
1380 % Hint: edit controls usually have a white background on Windows.
1381 %       See ISPC and COMPUTER.
1382 if ispc
1383     set(hObject,'BackgroundColor','white');
1384 else
1385     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
1386 end
1387 
1388 % --- Executes on button press in SeekButton.
1389 function SeekButton_Callback(hObject, eventdata, handles)
1390 % hObject    handle to SeekButton (see GCBO)
1391 % eventdata  reserved - to be defined in a future version of MATLAB
1392 % handles    structure with handles and user data (see GUIDATA)
1393 
1394 % Seek anywhere in a long file
1395 
1396 handles = savesegments2mem(handles);
1397 
1398 try 
1399     timetoseek = str2num(get(handles.SeektoEdit,'String'));
1400 catch
1401     timetoseek = 0;
1402     set(handles.SeektoEdit,'String','0');
1403 end
1404 
1405 if timetoseek < 0
1406     timetoseek = 0;
1407     set(handles.SeektoEdit,'String','0');
1408 end
1409 
1410 timetoseek = round(timetoseek * handles.Fs);
1411 
1412 if timetoseek >= handles.wavsize
1413     timetoseek = timetoseek - handles.maxwavsize;
1414 end
1415 
1416 timetoseek = timetoseek  + 1;
1417 timetoseekend = timetoseek + handles.maxwavsize;
1418 
1419 if timetoseekend > handles.wavsize
1420     timetoseekend = handles.wavsize;
1421 end
1422 
1423 oldstate = handles.dontcutsegments;
1424 handles.dontcutsegments = 0;
1425 handles = loadfile(hObject,eventdata,handles,[timetoseek timetoseekend]);
1426 handles.dontcutsegments = oldstate;
1427 
1428 guidata(gcbo,handles);
1429 
1430 function SeektoEdit_Callback(hObject, eventdata, handles)
1431 % hObject    handle to SeektoEdit (see GCBO)
1432 % eventdata  reserved - to be defined in a future version of MATLAB
1433 % handles    structure with handles and user data (see GUIDATA)
1434 
1435 % Hints: get(hObject,'String') returns contents of SeektoEdit as text
1436 %        str2double(get(hObject,'String')) returns contents of SeektoEdit as a double
1437 
1438 
1439 % --- Executes during object creation, after setting all properties.
1440 function SeektoEdit_CreateFcn(hObject, eventdata, handles)
1441 % hObject    handle to SeektoEdit (see GCBO)
1442 % eventdata  reserved - to be defined in a future version of MATLAB
1443 % handles    empty - handles not created until after all CreateFcns called
1444 
1445 % Hint: edit controls usually have a white background on Windows.
1446 %       See ISPC and COMPUTER.
1447 if ispc
1448     set(hObject,'BackgroundColor','white');
1449 else
1450     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
1451 end
1452 
1453 
1454 
1455 
1456 
1457 function RealDuration_Callback(hObject, eventdata, handles)
1458 % hObject    handle to RealDuration (see GCBO)
1459 % eventdata  reserved - to be defined in a future version of MATLAB
1460 % handles    structure with handles and user data (see GUIDATA)
1461 
1462 % Hints: get(hObject,'String') returns contents of RealDuration as text
1463 %        str2double(get(hObject,'String')) returns contents of RealDuration as a double
1464 
1465 
1466 % --- Executes during object creation, after setting all properties.
1467 function RealDuration_CreateFcn(hObject, eventdata, handles)
1468 % hObject    handle to RealDuration (see GCBO)
1469 % eventdata  reserved - to be defined in a future version of MATLAB
1470 % handles    empty - handles not created until after all CreateFcns called
1471 
1472 % Hint: edit controls usually have a white background on Windows.
1473 %       See ISPC and COMPUTER.
1474 if ispc
1475     set(hObject,'BackgroundColor','white');
1476 else
1477     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
1478 end
1479 
1480 
1481 
1482 
1483 % --- Executes on selection change in AutoMethodPopupMenu.
1484 function AutoMethodPopupMenu_Callback(hObject, eventdata, handles)
1485 % hObject    handle to AutoMethodPopupMenu (see GCBO)
1486 % eventdata  reserved - to be defined in a future version of MATLAB
1487 % handles    structure with handles and user data (see GUIDATA)
1488 
1489 % Hints: contents = get(hObject,'String') returns AutoMethodPopupMenu contents as cell array
1490 %        contents{get(hObject,'Value')} returns selected item from AutoMethodPopupMenu
1491 contents = get(hObject,'String');
1492 method = contents{get(hObject,'Value')}
1493 
1494 if strcmp(method,'Summed intensity')
1495     handles.automethod = 'threshold';
1496     set(handles.AmpThresh,'Visible','on');
1497     set(handles.RatioThresh,'Visible','off');
1498 elseif strcmp(method,'Ratio')
1499     handles.automethod = 'ratiof';
1500     set(handles.AmpThresh,'Visible','off');
1501     set(handles.RatioThresh,'Visible','on');
1502 end
1503 
1504 guidata(gcbo,handles);
1505 
1506 % --- Executes during object creation, after setting all properties.
1507 function AutoMethodPopupMenu_CreateFcn(hObject, eventdata, handles)
1508 % hObject    handle to AutoMethodPopupMenu (see GCBO)
1509 % eventdata  reserved - to be defined in a future version of MATLAB
1510 % handles    empty - handles not created until after all CreateFcns called
1511 
1512 % Hint: popupmenu controls usually have a white background on Windows.
1513 %       See ISPC and COMPUTER.
1514 if ispc
1515     set(hObject,'BackgroundColor','white');
1516 else
1517     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
1518 end
1519 
1520 
1521 
1522 function RatioThresh_Callback(hObject, eventdata, handles)
1523 % hObject    handle to RatioThresh (see GCBO)
1524 % eventdata  reserved - to be defined in a future version of MATLAB
1525 % handles    structure with handles and user data (see GUIDATA)
1526 
1527 % Hints: get(hObject,'String') returns contents of RatioThresh as text
1528 %        str2double(get(hObject,'String')) returns contents of RatioThresh as a double
1529 
1530 
1531 % --- Executes during object creation, after setting all properties.
1532 function RatioThresh_CreateFcn(hObject, eventdata, handles)
1533 % hObject    handle to RatioThresh (see GCBO)
1534 % eventdata  reserved - to be defined in a future version of MATLAB
1535 % handles    empty - handles not created until after all CreateFcns called
1536 
1537 % Hint: edit controls usually have a white background on Windows.
1538 %       See ISPC and COMPUTER.
1539 if ispc
1540     set(hObject,'BackgroundColor','white');
1541 else
1542     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
1543 end
1544 
1545 
1546 
1547 function RatioLower_Callback(hObject, eventdata, handles)
1548 % hObject    handle to RatioLower (see GCBO)
1549 % eventdata  reserved - to be defined in a future version of MATLAB
1550 % handles    structure with handles and user data (see GUIDATA)
1551 
1552 % Hints: get(hObject,'String') returns contents of RatioLower as text
1553 %        str2double(get(hObject,'String')) returns contents of RatioLower as a double
1554 
1555 
1556 % --- Executes during object creation, after setting all properties.
1557 function RatioLower_CreateFcn(hObject, eventdata, handles)
1558 % hObject    handle to RatioLower (see GCBO)
1559 % eventdata  reserved - to be defined in a future version of MATLAB
1560 % handles    empty - handles not created until after all CreateFcns called
1561 
1562 % Hint: edit controls usually have a white background on Windows.
1563 %       See ISPC and COMPUTER.
1564 if ispc
1565     set(hObject,'BackgroundColor','white');
1566 else
1567     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
1568 end
1569 
1570 
1571 
1572 function RatioUpper_Callback(hObject, eventdata, handles)
1573 % hObject    handle to RatioUpper (see GCBO)
1574 % eventdata  reserved - to be defined in a future version of MATLAB
1575 % handles    structure with handles and user data (see GUIDATA)
1576 
1577 % Hints: get(hObject,'String') returns contents of RatioUpper as text
1578 %        str2double(get(hObject,'String')) returns contents of RatioUpper as a double
1579 
1580 
1581 % --- Executes during object creation, after setting all properties.
1582 function RatioUpper_CreateFcn(hObject, eventdata, handles)
1583 % hObject    handle to RatioUpper (see GCBO)
1584 % eventdata  reserved - to be defined in a future version of MATLAB
1585 % handles    empty - handles not created until after all CreateFcns called
1586 
1587 % Hint: edit controls usually have a white background on Windows.
1588 %       See ISPC and COMPUTER.
1589 if ispc
1590     set(hObject,'BackgroundColor','white');
1591 else
1592     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
1593 end
1594 
1595 
1596 % --- Executes on button press in OptionsDisplay.
1597 function OptionsDisplay_Callback(hObject, eventdata, handles)
1598 % hObject    handle to OptionsDisplay (see GCBO)
1599 % eventdata  reserved - to be defined in a future version of MATLAB
1600 % handles    structure with handles and user data (see GUIDATA)
1601 
1602 % Hint: get(hObject,'Value') returns toggle state of OptionsDisplay
1603 
1604 % positionP = get(handles.OptionsUiPanel,'Position');
1605 % positionF = get(gcf,'Position');
1606 
1607 state = get(hObject,'Value');
1608 
1609 if state
1610     set(handles.OptionsUiPanel,'Visible','on');
1611 %     positionF(3) = positionF(3) + positionP(3);
1612 else
1613     set(handles.OptionsUiPanel,'Visible','off');
1614 %     positionF(3) = positionF(3) - positionP(3);
1615 end
1616 
1617 %  set(gcf,'Position',positionF); % untested
1618 
1619 guidata(gcbo,handles)
1620 
1621 
1622 
1623 function Duration_Callback(hObject, eventdata, handles)
1624 % hObject    handle to Duration (see GCBO)
1625 % eventdata  reserved - to be defined in a future version of MATLAB
1626 % handles    structure with handles and user data (see GUIDATA)
1627 
1628 % Hints: get(hObject,'String') returns contents of Duration as text
1629 %        str2double(get(hObject,'String')) returns contents of Duration as a double
1630 
1631 
1632 
1633 
1634 
1635 function SmoothFactor_Callback(hObject, eventdata, handles)
1636 % hObject    handle to SmoothFactor (see GCBO)
1637 % eventdata  reserved - to be defined in a future version of MATLAB
1638 % handles    structure with handles and user data (see GUIDATA)
1639 
1640 % Hints: get(hObject,'String') returns contents of SmoothFactor as text
1641 %        str2double(get(hObject,'String')) returns contents of SmoothFactor as a double
1642 
1643 
1644 % --- Executes during object creation, after setting all properties.
1645 function SmoothFactor_CreateFcn(hObject, eventdata, handles)
1646 % hObject    handle to SmoothFactor (see GCBO)
1647 % eventdata  reserved - to be defined in a future version of MATLAB
1648 % handles    empty - handles not created until after all CreateFcns called
1649 
1650 % Hint: edit controls usually have a white background on Windows.
1651 %       See ISPC and COMPUTER.
1652 if ispc
1653     set(hObject,'BackgroundColor','white');
1654 else
1655     set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
1656 end
1657 
1658

Generated on Mon 09-Oct-2006 00:54:52 by m2html © 2003