声音监控,有东西发出声音就进行报警:纯m文件编程实现……
以下是程序:
function sounddetection
%参数设置
secondsToRecord = 10;
soundThreshold = 0.05; % 0.0 ~ 1.0.
ai = analoginput('winsound');
addchannel(ai,[1 2]);
set(ai, 'LogToDiskMode', 'overwrite');
set(ai, 'TriggerType', 'manual');
set(ai, 'TriggerRepeat', Inf);
samplesToRecord = ceil(ai.SampleRate * secondsToRecord);
set(ai, 'SamplesPerTrigger', samplesToRecord );
set(ai,'StartFcn',@soundStartFcn);
set(ai, 'TimerPeriod', 0.1);
set(ai, 'TimerFcn', @soundTimerFcn);
%创建一个figure
fig = figure('DoubleBuffer','on', ...
'Name', 'Intruder Detection', ...
'NumberTitle', 'off', ...
'WindowStyle', 'docked', ...
'Toolbar', 'none', ...
'MenuBar', 'none', ...
'Color',[.5 .5 .5], ...
'CloseRequestFcn', @figureCloseFcn, ...
'DeleteFcn', @figureDeleteFcn);
timePrevious = [];
start(ai);
function soundStartFcn(vid, event)
timePrevious = now;
end
function soundTimerFcn(vid, event)
try
timeCurrent = now;
catch
return;
end
samplesRequested = ceil((timeCurrent - timePrevious) * (60*60*24) *...
ai.SampleRate);
warning('off','daq:peekdata:requestedSamplesNotAvailable');
try
sound = peekdata(ai, samplesRequested);
catch
sound = zeros(samplesRequested, length(ai.Channel));
end
warning('on','daq:peekdata:requestedSamplesNotAvailable');
sound = sound - mean(sound(:,1)); % Center about the mean.
soundMax = max(max(abs(sound))); % Calculate max deviation from mean.
timePrevious = timeCurrent;
% Make our figure current.
figOld = get(0,'CurrentFigure');
if fig ~= figOld
set(0, 'CurrentFigure', fig);
end
plot(sound);
% axis([0 size(sound,1) -1 1]);
ylim([-1 1]);
set(gca,'XTick',[]);
% Look for noise.
if soundMax > soundThreshold
noise = true;
else
noise = false;
end
if ~islogging(ai)
if noise
set(gcf, 'Color', [1 0 0]);
% trigger(ai);
% 发现高音量后的处理
customIntruderAction();
else
set(gcf, 'Color', [.5 .5 .5]);
end;
end
if fig ~= figOld
set(0, 'CurrentFigure', figOld);
end
end
% 发现高音量后的处理
function customIntruderAction()
% tts('please turn your volumn down'); % 语音报警
end
function figureCloseFcn(obj, event)
try
stop(ai);
catch
end
closereq;
end
function figureDeleteFcn(obj, event)
delete(ai);
end
end
哈哈……
程序的前提:不是有摄像头了,二是有采集装置。
运行程序后,周围不要发出声音,可以看到波动很小,
然后你可以咳嗽一声或者什么的……然后图像波动变大,
并且gui的背景变为红色……
调节相应的参数,可以调整监控的灵敏度……
顺便在此介绍一下matalb中利用声音采集装置进行采集的一些方法:
类似于matlab调用视频时的videoinput函数,采集声音时,也后相应的函数:
analoginput。查看自己电脑中支持采集声音的适配器方法:利用函数:daqhwinfo
返回值中有一个InstalledAdaptors的变量,查看此变量就可以发现自己电脑所带的相应的适配器,
如我的电脑:
>> out = daqhwinfo
out =
ToolboxName: 'Data Acquisition Toolbox'
ToolboxVersion: '2.17 (R2010b)'
MATLABVersion: '7.11 (R2010b)'
InstalledAdaptors: {2x1 cell}
>> out.InstalledAdaptors
ans =
'parallel'
'winsound'
显示有两个:'parallel'和'winsound'
查看后,就可以使用函数analoginput了……
利用函数analoginput返回的Object就可以进行相应的设置了:
如:'StartFcn'、 'TimerPeriod'和'TimerFcn'等函数了……
(和调用u***摄像头原理类似……)
然后就可以在相应的函数中进行自己的编程了……
无声音:
有声音:
|