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

Generated on Fri 28-Sep-2012 12:34:30 by m2html © 2005