www.pudn.com > audioProcessingtoolbox.rar > epdByVol.m


function [epInSampleIndex, epInFrameIndex, segment, zeroOneVec, volume] = epdByVol(y, fs, nbits, epdParam, plotOpt) 
% epdByVol: EPD based on volume only 
%	Usage: [epInSampleIndex, epInFrameIndex] = epdByVol(y, fs, nbits, epdParam, plotOpt) 
%		epInSampleIndex: two-element end-points in sample index 
%		epInFrameIndex: two-element end-points in frame index 
%		y: input audio signals 
%		fs: sampling rate 
%		epdParam: parameters for EPD 
%		plotOpt: 0 for silence operation, 1 for plotting 
% 
%	Example: 
%		waveFile='SingaporeIsAFinePlace.wav'; 
%		[y, fs, nbits]=wavReadInt(waveFile); 
%		epdParam=epdParamSet(fs); 
%		plotOpt=1; 
%		out=epdByVol(y, fs, nbits, epdParam, plotOpt); 
 
%	Roger Jang, 20040413, 20070320 
 
if nargin<1, selfdemo; return; end 
if nargin<2, fs=16000; end 
if nargin<3, nbits=16; end 
if nargin<4 | isempty(epdParam), epdParam=epdParamSet(fs); end 
if nargin<5, plotOpt=0; end 
 
if size(y, 2)~=1, error('Wave is not mono!'); end 
 
frameSize=epdParam.frameSize; 
overlap=epdParam.overlap;minSegment=round(epdParam.minSegment*fs/(frameSize-overlap)); 
maxSilBetweenWord=round(epdParam.maxSilBetweenWord*fs/(frameSize-overlap)); 
%minLastWordDuration=round(epdParam.minLastWordDuration*fs/(frameSize-overlap)); 
 
y = double(y);					% convert to double data type 
frameMat=buffer2(y, frameSize, overlap);	% frame blocking 
frameMat=frameZeroMean(frameMat, 2); 
frameNum=size(frameMat, 2);			% no. of frames 
volume=frame2volume(frameMat, 1);		% compute volume 
temp=sort(volume); 
index=round(frameNum/32); if index==0, index=1; end 
volMin=temp(index); 
volMax=temp(frameNum-index+1);			% To avoid qiYin 
volTh=(volMax-volMin)/epdParam.volRatio+volMin;	% compute volume threshold 
 
% ====== Identify voiced part that's larger than volTh2 
segment=findSegment(volume>volTh); 
 
% ====== Delete short sound clips 
index = []; 
for i=1:length(segment), 
	if segment(i).duration<=minSegment 
		index = [index, i]; 
	end 
end 
segment(index) = []; 
 
if isempty(segment) 
	epInSampleIndex=[]; 
	epInFrameIndex=[]; 
	fprintf('Warning: No segment found in %s.m.\n', mfilename); 
else 
	epInFrameIndex=[segment(1).begin, segment(end).end]; 
	epInSampleIndex=frame2sampleIndex(epInFrameIndex, frameSize, overlap);		% conversion from frame index to sample index 
end 
 
zeroOneVec=0*volume; 
for i=1:length(segment) 
	for j=segment(i).begin:segment(i).end 
		zeroOneVec(j)=1; 
	end 
end 
 
% Plotting... 
if plotOpt, 
	subplot(2,1,1); 
	time=(1:length(y))/fs; 
	frameTime=frame2sampleIndex(1:frameNum, frameSize, overlap)/fs; 
	plot(time, y); 
	for i=1:length(segment) 
		line(frameTime(segment(i).begin)*[1 1], 2^nbits/2*[-1, 1], 'color', 'm'); 
		line(frameTime(segment(i).end)*[1 1], 2^nbits/2*[-1, 1], 'color', 'g'); 
	end 
	axis([min(time) max(time) -2^nbits/2, 2^nbits/2]); 
	ylabel('Amplitude'); 
	title('Waveform'); 
 
	subplot(2,1,2); 
	plot(frameTime, volume, '.-'); 
	axis tight; 
	line([min(frameTime), max(frameTime)], volTh*[1 1], 'color', 'r'); 
	line([min(frameTime), max(frameTime)], volMin*[1 1], 'color', 'c'); 
	line([min(frameTime), max(frameTime)], volMax*[1 1], 'color', 'k'); 
	for i=1:length(segment) 
		line(frameTime(segment(i).begin)*[1 1], [0, max(volume)], 'color', 'm'); 
		line(frameTime(segment(i).end)*[1 1], [0, max(volume)], 'color', 'g'); 
	end 
	ylabel('Volume'); 
	title('Volume'); 
	 
	U.y=y; U.fs=fs; 
	if max(U.y)>1, U.y=U.y/(2^nbits/2); end 
	if ~isempty(epInSampleIndex) 
		U.voicedY=U.y(epInSampleIndex(1):epInSampleIndex(end)); 
	else 
		U.voicedY=[]; 
	end 
	set(gcf, 'userData', U); 
	uicontrol('string', 'Play all', 'callback', 'U=get(gcf, ''userData''); sound(U.y, U.fs);'); 
	uicontrol('string', 'Play voiced', 'callback', 'U=get(gcf, ''userData''); sound(U.voicedY, U.fs);', 'position', [100, 20, 60, 20]); 
end 
 
% ====== Self demo 
function selfdemo 
waveFile='SingaporeIsAFinePlace.wav'; 
waveFile='·s¦Ë¤k¤¤#21.wav'; 
[y, fs, nbits]=wavReadInt(waveFile); 
epdParam=epdParamSet(fs); 
plotOpt=1; 
out=feval(mfilename, y, fs, nbits, epdParam, plotOpt);