function varargout = A_Short_time_spectrum(varargin)
% A_SHORT_TIME_SPECTRUM MATLAB code for A_Short_time_spectrum.fig
% A_SHORT_TIME_SPECTRUM, by itself, creates a new A_SHORT_TIME_SPECTRUM or raises the existing
% singleton*.
%
% H = A_SHORT_TIME_SPECTRUM returns the handle to a new A_SHORT_TIME_SPECTRUM or the handle to
% the existing singleton*.
%
% A_SHORT_TIME_SPECTRUM('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in A_SHORT_TIME_SPECTRUM.M with the given input arguments.
%
% A_SHORT_TIME_SPECTRUM('Property','Value',...) creates a new A_SHORT_TIME_SPECTRUM or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before A_Short_time_spectrum_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to A_Short_time_spectrum_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 A_Short_time_spectrum
% Last Modified by GUIDE v2.5 30-Mar-2020 12:19:55
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @A_Short_time_spectrum_OpeningFcn, ...
'gui_OutputFcn', @A_Short_time_spectrum_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
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global h x Fs h2 width wlen_h point player flag filename;
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
wlen_h=400;
[FileName,PathName] = uigetfile('.wav','选择音频文件');
filename=[PathName FileName];
[x,Fs]=audioread(filename);
player=audioplayer(x,Fs);flag=0;
x=x(:,1);N=length(x);xmax=max(x);xmin=min(x);
t=(0:N-1)/Fs;
plot(handles.axes1,t,x);
axes(handles.axes1);
h=line([3 3],[xmin xmax],...
'color','red',...a
'linewidth',4,...
'ButtonDownFcn',{@startDragFcn,handles});
point=round(3*Fs);subinval=(point-500):(point+500);
xx=x(subinval,1);tt=subinval/Fs;
windowl=(point-wlen_h)/Fs;windowr=(point+wlen_h)/Fs;
width=windowr-windowl;
axes(handles.axes2);
plot(tt,xx);
xxrange=ylim;
hold on
h2=patch([windowl windowl windowr windowr],[xxrange(2) xxrange(1) xxrange(1) xxrange(2)],...
'red','FaceAlpha',.5,...
'ButtonDownFcn',{@startDragFcn2,handles});
hold off
% --- Executes just before A_Short_time_spectrum is made visible.
function A_Short_time_spectrum_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for A_Short_time_spectrum
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes A_Short_time_spectrum wait for user response (see UIRESUME)
% uiwait(handles.figure1);
function startDragFcn(hObject, eventdata, handles)
set(gcbf,'WindowButtonMotionFcn',{@draggingFcn,handles});
set(gcbf,'WindowButtonUpFcn','');
function draggingFcn(hObject, eventdata, handles)
global h x Fs h2 width wlen_h point N
pt=get(handles.axes1,'CurrentPoint');
set(h,'ButtonDownFcn',{@stopDragFcn,handles});
set(h,'Xdata',pt(1)*[1 1]);
point=round(pt(1)*Fs);subinval=(point-500):(point+500);xx=x(subinval,1);tt=subinval/Fs;
windowl=(point-wlen_h)/Fs;windowr=(point+wlen_h)/Fs;
width=2*wlen_h/Fs;
axes(handles.axes2);
plot(tt,xx);
xxrange=ylim;
hold on
h2=patch([windowl windowl windowr windowr],[xxrange(2) xxrange(1) xxrange(1) xxrange(2)],...
'red','FaceAlpha',.5,...
'ButtonDownFcn',{@startDragFcn2,handles});
hold off
sub=(round((windowl*Fs)):round((windowr*Fs)));N=length(sub);
fn=(0:N/2-1); xx=x(sub,1); xx1=xx.*hanning(N);
ff=fn*Fs/N; yy1=abs(fft(xx1));
plot(handles.axes3,ff,yy1(fn+1));
a=yy1(fn+1);
Peak_x=find(diff(sign(diff(a)))==-2)+1;
[~,max_po]=max(a(Peak_x));
a(Peak_x(max_po))=min(a(Peak_x));
[~,Smax_po]=max(a(Peak_x));
if max_po<=Smax_po
f1=ff(Peak_x(max_po));
f2=ff(Peak_x(Smax_po));
else
f1=ff(Peak_x(Smax_po));
f2=ff(Peak_x(max_po));
end
% y4=xcorr(xx1);
% y4=fftshift(y4);n=length(y4);
% plot(handles.axes4,0:round(n/2)-1,y4(1:round(n/2)));
lmin=fix(Fs/500); % 基音周期的最小值
lmax=fix(Fs/60); % 基音周期的最大值
y4=rceps(xx1);
n=length(y4);
for s=1:n
if y4(s)<-2
y4(s)=0;
end
end
[R,K]=max(y4(lmin:lmax));
plot(handles.axes4,1:n/2,y4(1:n/2));
set(handles.text3,'string',[num2str((lmin+K)/Fs) 's']);
set(handles.text13,'string',[num2str(Fs/(lmin+K)) 'Hz']);
function stopDragFcn(hObject, eventdata, handles)
global h
set(h,'ButtonDownFcn',{@startDragFcn,handles});
set(gcbf,'WindowButtonMotionFcn','');
function startDragFcn2(hObject, eventdata, handles)
global delt h2
click=get(handles.axes2,'CurrentPoint');
leftside=get(h2,'Xdata');
delt=click(1)-leftside(1);
set(gcbf,'WindowButtonMotionFcn',{@draggingFcn2,handles});
function draggingFcn2(hObject, eventdata, handles)
global h2 delt width x Fs xx ff fn yy N val_window
pt=get(handles.axes2,'CurrentPoint');
left=pt(1)-delt;right=left+width;
set(h2,'Xdata',[left left right right]);
set(gcbf,'WindowButtonUpFcn',{@stopDragFcn2,handles});
sub=(round((left*Fs)):round((right*Fs)));N=length(sub);
fn=(0:N/2-1); xx=x(sub,1);ff=fn*Fs/N;
switch val_window
case 1
xx1=xx.*hanning(N);
case 2
xx1=xx.*hamming(N);
case 3
xx1=xx.*boxcar(N);
end
yy=abs(fft(xx));
yy1=abs(fft(xx1));
axes(handles.axes3);
plot(handles.axes3,ff,yy(fn+1),'--b');
hold on
plot(handles.axes3,ff,yy1(fn+1),'r');
hold off
xlim([0 8000])
a=yy1(fn+1);
Peak_x=find(diff(sign(diff(a)))==-2)+1;
[~,max_po]=max(a(Peak_x));
a(Peak_x(max_po))=min(a(Peak_x));
[~,Smax_po]=max(a(Peak_x));
if max_po<=Smax_po
f1=ff(Peak_x(max_po));
f2=ff(Peak_x(Smax_po));
else
f1=ff(Peak_x(Smax_po));
f2=ff(Peak_x(max_po));
end
% y4=xcorr(xx1);n=length(y4);
% y4=fftshift(y4);y4=y4(1:round(n/2));
% plot(handles.axes4,0:round(n/2)-1,y4);
lmin=fix(Fs/500); % 基音周期的最小值
lmax=fix(Fs/60); % 基音周期的最大值
y4=rceps(xx1);
n=length(y4);
for s=1:n
if y4(s)<-2
y4(s)=0;
end
end
[~,K]=max(y4(lmin:lmax));
plot(handles.axes4,1:n/2,y4(1:n/2));
set(handles.text3,'string',[num2str((lmin+K)/Fs) 's']);
set(handles.text13,'string',[num2str(Fs/(lmin+K)) 'Hz']);
function stopDragFcn2(hObject, eventdata, handles)
global h2
set(h2,'ButtonDownFcn',{@startDragFcn2,handles});
set(gcbf,'WindowButtonMotionFcn','');
% --- Outputs from this function are returned to the command line.
function varargout = A_Short_time_spectrum_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserv