function au2=asdFeaExtractFromFile(au, asdOpt, showPlot) % asdFeaExtractFromFile: Feature extraction from an audio file % % Usage: % au2=asdFeaExtractFromFile(au); % au2=asdFeaExtractFromFile(au, asdOpt); % au2=asdFeaExtractFromFile(au, asdOpt, showPlot); % [au2, frameClass]=asdFeaExtractFromFile(au...); % % Example: % auFile='dataSet/abnormal-cue.wav'; % asdOpt=asdOptSet; % asdOpt.feaType='mfcc'; % fea=asdFeaExtractFromFile(auFile, asdOpt, 1); % Category: Feature extraction for anomaly sound detection % Roger Jang, 20130106, 20200118 if nargin<1, selfdemo; return; end if nargin<2, asdOpt=asdOptSet; end if nargin<3, showPlot=0; end if ischar(au), au=myAudioRead(au); end [sampleCount, channel]=size(au.signal); au.signal=mean(au.signal, 2); % Take the average if it's stereo % === Feature computation [frameSize, overlap]=frameDuration2size(asdOpt.frameDuration, asdOpt.overlapDuration, au.fs); frameMat=enframe(au.signal, frameSize, overlap); [~, frameCount]=size(frameMat); frameTime=frame2sampleIndex(1:frameCount, frameSize, overlap)/au.fs; % === Get the groundtruth (if it exists) cueTime=wavCueRead(au.file); interval=rangeSearchBin(frameTime, cueTime); frameClass=2-mod(interval, 2); % 1: nothing, 2: pounding other.cueTime=cueTime; other.frameTime=frameTime; switch(lower(asdOpt.feaType)) case 'spectrum' % === Feature extraction based on spectrogram opt=auSpectrogram('defaultOpt'); opt.frameDuration=asdOpt.frameDuration; opt.overlapDuration=asdOpt.overlapDuration; opt.zeroPaddingFactor=1; opt.type='log' specObj=auSpectrogram(au, opt, showPlot); feaMat=specObj.signal; case 'mfcc' mfccOpt=auFeaMfcc('defaultOpt'); mfccOpt.frameDuration=asdOpt.frameDuration; mfccOpt.overlapDuration=asdOpt.overlapDuration; mfccOpt.useDelta=2; feaMat=auFeaMfcc(au, mfccOpt); otherwise error('Unknown feature type!'); end % === Update frameClass based on file name if strfind(au.file, 'abnormal') frameClass(frameClass==2)=3; end % === Put everything together for output au2=au; au2.feature=feaMat; au2.tOutput=frameClass; au2.other=other; if showPlot asdFeaPlot(au2); end function startEndLinePlot(cueTime) axisLimit=axis; for j=1:length(cueTime) lineColor='g'; if mod(j, 2)==0, lineColor='r'; end line(cueTime(j)*[1, 1], [axisLimit(3), axisLimit(4)], 'color', lineColor); end % ====== Self demo function selfdemo mObj=mFileParse(which(mfilename)); strEval(mObj.example);