function varargout = readsoundgui(varargin) % READSOUNDGUI M-file for readsoundgui.fig % READSOUNDGUI, by itself, creates a new READSOUNDGUI or raises the existing % singleton*. % % H = READSOUNDGUI returns the handle to a new READSOUNDGUI or the handle to % the existing singleton*. % % READSOUNDGUI('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in READSOUNDGUI.M with the given input arguments. % % READSOUNDGUI('Property','Value',...) creates a new READSOUNDGUI or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before readsoundgui_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to readsoundgui_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 readsoundgui % Last Modified by GUIDE v2.5 08-Jan-2010 13:43:50 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @readsoundgui_OpeningFcn, ... 'gui_OutputFcn', @readsoundgui_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 readsoundgui is made visible. function readsoundgui_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 readsoundgui (see VARARGIN) % Choose default command line output for readsoundgui handles.output = hObject; % Indsat af Teis % (http://blinkdagger.com/matlab/matlab-gui-tutorial-buttons-button-group/) clc set(handles.Fs_buttongroup,'SelectionChangeFcn',@Fs_buttongroup_SelectionChangeFcn); set(handles.T_buttongroup,'SelectionChangeFcn',@T_buttongroup_SelectionChangeFcn); handles.T = 1; handles.Fs = 44100; % Update handles structure guidata(hObject, handles); % UIWAIT makes readsoundgui wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = readsoundgui_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; function f_Callback(hObject, eventdata, handles) % hObject handle to f (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 f as text % str2double(get(hObject,'String')) returns contents of f as a double % --- Executes during object creation, after setting all properties. function f_CreateFcn(hObject, eventdata, handles) % hObject handle to f (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 function T_Callback(hObject, eventdata, handles) % hObject handle to T_buttongroup (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 T_buttongroup as text % str2double(get(hObject,'String')) returns contents of T_buttongroup as a double %get(handles.T) guidata(hObject, handles); % --- Executes during object creation, after setting all properties. function T_buttongroup_CreateFcn(hObject, eventdata, handles) % hObject handle to T_buttongroup (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 function Fs_Callback(hObject, eventdata, handles) % hObject handle to Fs_buttongroup (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 Fs_buttongroup as text % str2double(get(hObject,'String')) returns contents of Fs_buttongroup as a double guidata(hObject, handles); % --- Executes during object creation, after setting all properties. function Fs_buttongroup_CreateFcn(hObject, eventdata, handles) % hObject handle to Fs_buttongroup (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 toggleFFT. function toggleFFT_Callback(hObject, eventdata, handles) % hObject handle to toggleFFT (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hint: get(hObject,'Value') returns toggle state of toggleFFT Ch = 1; % mono (1) or stereo (2) flag = get(handles.toggleFFT,'Value'); while flag flag = get(handles.toggleFFT,'Value'); T = handles.T; Fs = handles.Fs; N = T*Fs; % signal length t = (1:N)/Fs; step = round(Fs/1000); % jump inputs when plotting % Fs = str2num(get(handles.Fs,'string')); % sample frequency % Fs = str2num(get(handles.Fs_buttongroup,'UserData')); % sample frequency % T = str2num(get(handles.T,'string')); % how many seconds shall I sample over? % T = str2num(get(handles.T_buttongroup,'UserData')); % how many seconds shall I sample over? if step<1; step=1; end if N<(N/2+100*step); Nstep=1; else Nstep=100; end Nstart = ceil((str2num(get(handles.Pmin,'String')))*Fs); Nend = ceil((str2num(get(handles.Pmax,'String')))*Fs); Nstep = round((Nend-Nstart)/200); tplot = t(Nstart:Nstep:Nend); if flag % yraw = wavrecord(N,Fs,Ch); % load signal yraw = audiorecorder(Fs, 16, Ch); % load signal record(yraw); pause(T); stop(yraw); y= getaudiodata(yraw, 'double'); yplot = y(Nstart:Nstep:Nend); axes(handles.psignal), cla plot(tplot,yplot,'-','linewidth',1), grid on % axis([tplot(1) tplot(end) -1.1*max(yplot) 1.1*max(yplot)]) xlabel('Time [s]'), ylabel('Signal') drawnow [fmax, Ymax, Yplot, Fplot] = analysewav(y,T,Fs); axes(handles.pspectrum), cla bar(Fplot,Yplot,'linewidth',1), % plot(Fplot,Yplot,'-','linewidth',1), axis([str2num(get(handles.Fminplot,'String'))... str2num(get(handles.Fmaxplot,'String')) 0 1.1*max(Yplot)]), grid on, hold on plot(fmax,Ymax,'ro','markersize',10,'linewidth',2); % plot(.5*fmax,Ymax,'go','markersize',10,'linewidth',2); xlabel('Frequency (Hz)'), ylabel('Y(f)') drawnow fmax = round(fmax*100)/100; fmax2 = round(fmax*100/2)/100; set(handles.fdom,'string',num2str(fmax)); set(handles.fdom2,'string',num2str(fmax2)); end end function Fs_buttongroup_SelectionChangeFcn(hObject, eventdata) %retrieve GUI data, i.e. the handles structure handles = guidata(hObject); get(eventdata.NewValue,'Tag'); switch get(eventdata.NewValue,'Tag') % Get Tag of selected object case 'Fs8' handles.Fs = 8000; case 'Fs44' handles.Fs = 44100; case 'Fs128' handles.Fs = 128800; otherwise % Code for when there is no match. end %updates the handles structure guidata(hObject, handles); function T_buttongroup_SelectionChangeFcn(hObject, eventdata) %retrieve GUI data, i.e. the handles structure handles = guidata(hObject); switch get(eventdata.NewValue,'Tag') % Get Tag of selected object case 'T0c1' handles.T = 0.1; case 'T0c5' handles.T = 0.5; case 'T1' handles.T = 1; case 'T2' handles.T = 2; case 'T5' handles.T = 5; case 'T10' handles.T = 10; otherwise % Code for when there is no match. end %updates the handles structure guidata(hObject, handles); function [fmax, Ymax, Yplot, Fplot] = analysewav(y,T,Fs); % 16. november 2009 % % script to estimate dominant frequency of signal. % % y ~ signal to be analysed % Fs ~ sample frequency of signal % T ~ length of signal (lab time) N = T*Fs; t = (1:N)/Fs; y = y-mean(y); % -------------------- FFT SECTION -------------------- % NFFT = 2^nextpow2(N); % Next power of 2 from length of y Y = fft(y,NFFT)/N; Fplot = Fs/2*linspace(0,1,NFFT/2+1); Yplot = 2*abs(Y(1:NFFT/2+1)); % Find most dominant frequency i = find(~(Yplot-max(Yplot))); % estimate frequency based on neighbour points delta = (Fplot(2)-Fplot(1))*(Yplot(i+1)-Yplot(i-1))/(Yplot(i-1)+Yplot(i)+Yplot(i+1)); fmax = Fplot(i)+delta; Ymax = Yplot(i); function Fminplot_Callback(hObject, eventdata, handles) % hObject handle to Fminplot (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 Fminplot as text % str2double(get(hObject,'String')) returns contents of Fminplot as a double input = str2num(get(hObject,'String')); if (isempty(input)) set(hObject,'String','0') end guidata(hObject, handles); % --- Executes during object creation, after setting all properties. function Fminplot_CreateFcn(hObject, eventdata, handles) % hObject handle to Fminplot (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 function Fmaxplot_Callback(hObject, eventdata, handles) % hObject handle to Fmaxplot (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 Fmaxplot as text % str2double(get(hObject,'String')) returns contents of Fmaxplot as a double input = str2num(get(hObject,'String')); if (isempty(input)) set(hObject,'String','200') end guidata(hObject, handles); % --- Executes during object creation, after setting all properties. function Fmaxplot_CreateFcn(hObject, eventdata, handles) % hObject handle to Fmaxplot (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 function Pmin_Callback(hObject, eventdata, handles) % hObject handle to Pmin (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 Pmin as text % str2double(get(hObject,'String')) returns contents of Pmin as a double input = str2num(get(hObject,'String')); if (isempty(input)) set(hObject,'String','0') end guidata(hObject, handles); % --- Executes during object creation, after setting all properties. function Pmin_CreateFcn(hObject, eventdata, handles) % hObject handle to Pmin (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 function Pmax_Callback(hObject, eventdata, handles) % hObject handle to Pmax (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 Pmax as text % str2double(get(hObject,'String')) returns contents of Pmax as a double input = str2num(get(hObject,'String')); if (isempty(input)) set(hObject,'String','0.05') end guidata(hObject, handles); % --- Executes during object creation, after setting all properties. function Pmax_CreateFcn(hObject, eventdata, handles) % hObject handle to Pmax (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