function varargout = MotifCount(varargin) % MOTIFCOUNT MATLAB code for MotifCount.fig % MOTIFCOUNT, by itself, creates a new MOTIFCOUNT or raises the existing % singleton*. % % H = MOTIFCOUNT returns the handle to a new MOTIFCOUNT or the handle to % the existing singleton*. % % MOTIFCOUNT('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in MOTIFCOUNT.M with the given input arguments. % % MOTIFCOUNT('Property','Value',...) creates a new MOTIFCOUNT or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before MotifCount_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to MotifCount_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 % Edit the above text to modify the response to help MotifCount % Last Modified by GUIDE v2.5 12-Apr-2021 08:03:19 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @MotifCount_OpeningFcn, ... 'gui_OutputFcn', @MotifCount_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before MotifCount is made visible. function MotifCount_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to MotifCount (see VARARGIN) % Choose default command line output for MotifCount handles.output = hObject; %setup 2^8 element colormap axes(handles.CurrentFileAxes); colormap(rand([2^8,3]));colormap('jet'); %take care of intial contrast settings set(handles.MaxSpecValSlider,'Value',1.0); set(handles.MinSpecValSlider,'Value',0.67); handles.MainDir = 'T:\screen'; handles.motifTimes = []; % Update handles structure guidata(hObject, handles); % UIWAIT makes MotifCount wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = MotifCount_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; % --- Executes on button press in ChooseDirectory. function ChooseDirectory_Callback(hObject, eventdata, handles) % hObject handle to ChooseDirectory (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) try cd T:\screen catch handles.MainDir = 'Z:\screen'; end OriginalFilesDirectory = uigetdir; cd(OriginalFilesDirectory) OriginalFiles = dir('*.wav'); currentDate = OriginalFiles(1).date; hyp = strfind(currentDate,'-'); Day = str2double(currentDate(1:hyp(1)-1)); Month = month(currentDate); Year = str2double(currentDate(hyp(2)+(1:4))); StartTime = datenum(Year,Month,Day,7,30,00); handles.waveNames=[]; k=1; for i = 1:length(OriginalFiles); if OriginalFiles(i).datenum>=StartTime handles.waveNames{k} = OriginalFiles(i).name; k = k+1; end end set(handles.OriginalFilesList,'Value',1) set(handles.OriginalFilesList,'String',handles.waveNames) cd(handles.MainDir) handles.OriginalFilesDirectory = OriginalFilesDirectory; handles.motifTimes = []; handles.motifMark = []; guidata(hObject, handles); % --- Executes on button press in SaveSong. function SaveSong_Callback(hObject, eventdata, handles) % hObject handle to SaveSong (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) TmpName = get(handles.SaveName, 'string'); FileName = [handles.SavePath '\' TmpName '.wav']; audiowrite(FileName,handles.CurrentSong,handles.Fs) cd(handles.SavePath); SavedFiles = dir('*.wav'); for i = 1:length(SavedFiles); handles.savedWaveNames{i} = SavedFiles(i).name; end set(handles.SavedFilesList,'Value',1) set(handles.SavedFilesList,'String',handles.savedWaveNames) cd(handles.MainDir); guidata(hObject, handles); % --- Executes on button press in CutXAxis. function CutXAxis_Callback(hObject, eventdata, handles) % hObject handle to CutXAxis (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) [x y] = ginput(2); handles.Xlim = x; handles.CurrentSong = handles.OriginalSong(round(x(1)*handles.Fs):round(x(2)*handles.Fs)); axes(handles.CurrentFileAxes); DisplaySonogram2(hObject, handles,handles.CurrentFileAxes, handles.CurrentSong, handles.StimTimes, handles.flag1 ); guidata(hObject, handles); % --- Executes on slider movement. function MinSpecValSlider_Callback(hObject, eventdata, handles) % hObject handle to MinSpecValSlider (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider %vtmp = get(handles.MinSpecValSlider,'Value'); %vtmp = handles.MinSpVal+vtmp*(handles.MaxSpVal-handles.MinSpVal); %vtmp = exp(vtmp); vtmpmin = get(handles.MinSpecValSlider,'Value'); vtmpmax = get(handles.MaxSpecValSlider,'Value'); if (vtmpmin>vtmpmax) vtmpmin=vtmpmax-1e-10; set(handles.MinSpecValSlider,'Value',vtmpmax); end handles.SPECTH=vtmpmin*((2^8)-1); axes(handles.CurrentFileAxes); caxis(((2^8)-1)*[vtmpmin,vtmpmax]); %if (vtmp>=handles.MAXSPECTH) % set(handles.MinSpecValSlider,'Value',get(handles.MaxSpecValSlider,'Value')); %end %sptemp = handles.SPECGRAMVALS; %pp = find(sptemp<=handles.SPECTH);sptemp(pp)=handles.SPECTH; %pp = find(sptemp>=handles.MAXSPECTH);sptemp(pp)=handles.MAXSPECTH; %top freqs already taken out %sptemp=log(sptemp);sptemp = sptemp - min(min(sptemp)); %sptemp = uint8(2^8*(sptemp./max(max(sptemp)))); % SAVE SOME MEMORY 8X less than 64 bit double %set(handles.SPECT_HNDL,'CData',sptemp); %axes(handles.SpecGramAxes); %vv=axis;hold off; %imagesc(handles.TIMEVALS,handles.FREQVALS,log(sptemp)); %set(gca,'YDir','normal');axis(vv); %spectitle=handles.INPUTFILES(handles.NFILE).fname; %title(RemoveUnderScore(spectitle)); guidata(hObject,handles); drawnow;pause(0.05) return; % --- Executes during object creation, after setting all properties. function MinSpecValSlider_CreateFcn(hObject, eventdata, handles) % hObject handle to MinSpecValSlider (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end return; % --- Executes on slider movement. function MaxSpecValSlider_Callback(hObject, eventdata, handles) % hObject handle to MaxSpecValSlider (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider %vtmp = get(handles.MaxSpecValSlider,'Value'); %vtmp = handles.MinSpVal + vtmp*(handles.MaxSpVal-handles.MinSpVal); %vtmp = exp(vtmp); vtmpmin = get(handles.MinSpecValSlider,'Value'); vtmpmax = get(handles.MaxSpecValSlider,'Value'); if (vtmpmax=handles.MAXSPECTH);sptemp(pp)=handles.MAXSPECTH; %top freqs already taken out %sptemp=log(sptemp);sptemp = sptemp - min(min(sptemp)); %sptemp = uint8(2^8*(sptemp./max(max(sptemp)))); % SAVE SOME MEMORY 8X less than 64 bit double %set(handles.SPECT_HNDL,'CData',sptemp); guidata(hObject,handles); drawnow;pause(0.05) return; % --- Executes during object creation, after setting all properties. function MaxSpecValSlider_CreateFcn(hObject, eventdata, handles) % hObject handle to MaxSpecValSlider (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end return; % --- Executes on button press in ShowOrigin. function ShowOrigin_Callback(hObject, eventdata, handles) % hObject handle to ShowOrigin (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) axes(handles.CurrentFileAxes) zoom on handles.CurrentSong = handles.OriginalSong; DisplaySonogramWithStim(hObject, handles,handles.CurrentFileAxes, handles.CurrentSong, handles.StimTimes ); guidata(hObject, handles); % --- Executes on button press in PlaySound. function PlaySound_Callback(hObject, eventdata, handles) % hObject handle to PlaySound (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) Fs = handles.Fs; Song = handles.CurrentSong; player = audioplayer(Song, Fs); handles.player = player; play(player); axes(handles.CurrentFileAxes) ShowOrigin_Callback(hObject, [], handles); guidata(hObject, handles); function SaveName_Callback(hObject, eventdata, handles) % hObject handle to SaveName (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of SaveName as text % str2double(get(hObject,'String')) returns contents of SaveName as a double % --- Executes during object creation, after setting all properties. function SaveName_CreateFcn(hObject, eventdata, handles) % hObject handle to SaveName (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in SelectSavingPath. function SelectSavingPath_Callback(hObject, eventdata, handles) % hObject handle to SelectSavingPath (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) SavePath = uigetdir; handles.SavePath = SavePath; guidata(hObject, handles); % --- Executes on button press in StopSound. function StopSound_Callback(hObject, eventdata, handles) % hObject handle to StopSound (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) player = handles.player; stop(player); % --- Executes on button press in MarkRev. function MarkRev_Callback(hObject, eventdata, handles) % hObject handle to MarkRev (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) TmpName = get(handles.SaveName, 'string'); FileName = [handles.SavePath '\' TmpName 'REV.wav']; audiowrite(FileName,flipud(handles.CurrentSong),handles.Fs) function SetThreshold_Callback(hObject, eventdata, handles) % hObject handle to SetThreshold (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of SetThreshold as text % str2double(get(hObject,'String')) returns contents of SetThreshold as a double % --- Executes on button press in SaveJPG. function SaveJPG_Callback(hObject, eventdata, handles) % hObject handle to SaveJPG (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) F = getframe(handles.PreviousFileAxes); Image = frame2im(F); TmpName = get(handles.SaveName, 'string'); FileName = [handles.SavePath '\' TmpName '.jpg']; imwrite(Image, FileName) % --- Executes on slider movement. function slider3_Callback(hObject, eventdata, handles) % hObject handle to slider3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider % --- Executes during object creation, after setting all properties. function slider3_CreateFcn(hObject, eventdata, handles) % hObject handle to slider3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end % --- Executes on slider movement. function slider4_Callback(hObject, eventdata, handles) % hObject handle to slider4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider % --- Executes during object creation, after setting all properties. function slider4_CreateFcn(hObject, eventdata, handles) % hObject handle to slider4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end % --- Executes on selection change in OriginalFilesList. function OriginalFilesList_Callback(hObject, eventdata, handles) % hObject handle to OriginalFilesList (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: contents = cellstr(get(hObject,'String')) returns OriginalFilesList contents as cell array % contents{get(hObject,'Value')} returns selected item from OriginalFilesList soundToUse = get(handles.OriginalFilesList, 'Value'); fileType = handles.waveNames{soundToUse}(end-2:end); switch fileType case 'wav' [Song,Fs] = audioread([handles.OriginalFilesDirectory '\' handles.waveNames{soundToUse}]); StimTimes = NaN; case 'bin' tmpFileName = [handles.OriginalFilesDirectory '\' handles.waveNames{soundToUse}]; tmpRecName = [handles.OriginalFilesDirectory '\' handles.waveNames{soundToUse}(1:end-4) 'rec']; [StimTimes, flag1] = getStimTimes(tmpRecName); [Song,Fs]=ReadCbinFile(tmpFileName); handles.flag1 = flag1; end handles.StimTimes = StimTimes; Song = Song(:,1)./max(abs(Song(:,1))); [B,A]=ellip(6,.1,50,[985 10000]/(Fs*0.5)); OriginalSong=filtfilt(B,A,Song); handles.OriginalSong = OriginalSong; handles.CurrentSong = OriginalSong; handles.Fs = Fs; set(handles.FileName_StaticText, 'string',handles.waveNames{soundToUse}); axes(handles.CurrentFileAxes) ShowOrigin_Callback(hObject, [], handles); guidata(hObject, handles); % --- Executes during object creation, after setting all properties. function OriginalFilesList_CreateFcn(hObject, eventdata, handles) % hObject handle to OriginalFilesList (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: listbox controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in ChoosePreviousDirectory. function ChoosePreviousDirectory_Callback(hObject, eventdata, handles) % hObject handle to ChoosePreviousDirectory (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) curdir = cd; try likelySoundDir = handles.SavePath; handles.savedWaveDir = uigetdir(likelySoundDir); catch handles.savedWaveDir = uigetdir(curdir); end cd(handles.savedWaveDir); SavedFiles = dir('*.wav'); for i = 1:length(SavedFiles); handles.savedWaveNames{i} = SavedFiles(i).name; end set(handles.SavedFilesList,'Value',1) set(handles.SavedFilesList,'String',handles.savedWaveNames) cd('T:\Mor\songfiles\songsparrow'); guidata(hObject, handles); % --- Executes on selection change in SavedFilesList. function SavedFilesList_Callback(hObject, eventdata, handles) % hObject handle to SavedFilesList (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: contents = cellstr(get(hObject,'String')) returns SavedFilesList contents as cell array % contents{get(hObject,'Value')} returns selected item from SavedFilesList soundToUse = get(handles.SavedFilesList, 'Value'); fileType = handles.savedWaveNames{soundToUse}(end-2:end); switch fileType case 'wav' [Song,Fs] = audioread([handles.SavePath '\' handles.savedWaveNames{soundToUse}]); case 'bin' [Song,Fs] = ReadCbinFile([handles.SavePath '\' handles.savedWaveNames{soundToUse}]); end [B,A]=ellip(6,.1,50,[985 10000]/(Fs*0.5)); OriginalSong=filtfilt(B,A,Song); handles.SavedOriginalSong = OriginalSong; handles.SavedCurrentSong = OriginalSong; handles.Fs = Fs; set(handles.File2Name, 'string',handles.savedWaveNames{soundToUse}); axes(handles.PreviousFileAxes) axes(handles.PreviousFileAxes) DisplaySonogramWithStim(hObject, handles, handles.PreviousFileAxes, handles.SavedOriginalSong); guidata(hObject, handles); % --- Executes during object creation, after setting all properties. function SavedFilesList_CreateFcn(hObject, eventdata, handles) % hObject handle to SavedFilesList (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: listbox controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on button press in UpdateDir. function UpdateDir_Callback(hObject, eventdata, handles) % hObject handle to UpdateDir (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) OriginalFilesDirectory = handles.OriginalFilesDirectory; cd(OriginalFilesDirectory) OriginalFiles = dir('*.wav'); currentDate = OriginalFiles(1).date; hyp = strfind(currentDate,'-'); Day = str2double(currentDate(1:hyp(1)-1)); Month = month(currentDate); Year = str2double(currentDate(hyp(2)+(1:4))); StartTime = datenum(Year,Month,Day,8,00,00); handles.waveNames=[]; k=1; for i = 1:length(OriginalFiles); if OriginalFiles(i).datenum>=StartTime handles.waveNames{k} = OriginalFiles(i).name; k = k+1; end end set(handles.OriginalFilesList,'Value',1) set(handles.OriginalFilesList,'String',handles.waveNames) cd(handles.MainDir) handles.OriginalFilesDirectory = OriginalFilesDirectory; handles.motifTimes = []; guidata(hObject, handles); % --- Executes on button press in GetBatchFile. function GetBatchFile_Callback(hObject, eventdata, handles) % hObject handle to GetBatchFile (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) [BatchFile, OriginalFilesDirectory] = uigetfile('*batch'); cd(OriginalFilesDirectory) % [OriginalFiles, pc, tm] = textread(BatchFile, '%s %s %s', 'delimiter', ',', 'whitespace', '\n'); OriginalFiles = textread(BatchFile, '%s', 'delimiter', ',', 'whitespace', '\n');; % handles.waveNames=[]; % for i = 1:length(OriginalFiles); % handles.waveNames{i} = OriginalFiles(i).name; % end handles.waveNames = OriginalFiles; set(handles.OriginalFilesList,'Value',1) set(handles.OriginalFilesList,'String',handles.waveNames) try cd(handles.MainDir) end handles.OriginalFilesDirectory = OriginalFilesDirectory; handles.motifTimes = []; handles.motifMark = []; guidata(hObject, handles); % --- Executes on button press in CbinToWav. function CbinToWav_Callback(hObject, eventdata, handles) % hObject handle to CbinToWav (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) OriginalSong = handles.OriginalSong; Fs = handles.Fs; fileName = get(handles.FileName_StaticText, 'string'); fn = [handles.SavePath '\' fileName]; fn = fn(1:end-5); audiowrite([fn '.wav'], OriginalSong, Fs); % --- Executes on button press in MarkMotif_button. function MarkMotif_button_Callback(hObject, eventdata, handles) % hObject handle to MarkMotif_button (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) x = ginput; motifTimes = x(:,1); dashes = strfind(handles.FileName_StaticText.String, '_'); timeStamp = handles.FileName_StaticText.String((dashes(2)+1):(dashes(2)+6)); motifTimesSeconds = seconds(motifTimes); try tt = datetime(timeStamp, 'InputFormat', 'HHmmss')+motifTimesSeconds; catch timeStamp = handles.FileName_StaticText.String((dashes(4)+1):(dashes(4)+8)); motifTimesSeconds = seconds(motifTimes); tt = datetime(timeStamp, 'InputFormat', 'HH_mm_ss')+motifTimesSeconds; end handles.motifTimes = [handles.motifTimes; tt]; nn = length(handles.motifMark); handles.motifMark(nn+1).fileName = [handles.OriginalFilesDirectory '\' handles.FileName_StaticText.String]; handles.motifMark(nn+1).times = [motifTimes(1:end)]; % mark start only % handles.motifMark(nn+1).times = [motifTimes(1:2:end) % motifTimes(2:2:end)]; % mark start and end motifCount = size(handles.motifTimes,1); set(handles.MotifCount_StaticText,'string', num2str(motifCount)); zoom on guidata(hObject, handles); NextFile_button_Callback(hObject, eventdata, handles); % --- Executes on button press in NextFile_button. function NextFile_button_Callback(hObject, eventdata, handles) % hObject handle to NextFile_button (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) CurrentFile = get(handles.OriginalFilesList, 'value'); Nfiles = length(handles.OriginalFilesList.String); if CurrentFile