1.はじめに
隠れマルコフモデル(隠れマルコフモデル、HMM)は、最も単純な構造を持つ動的ベイジアンネットワーク生成モデルであり、よく知られている有向グラフモデルでもあります。これは、自然言語でラベリングを処理するための典型的な統計的機械学習モデルです。この記事では、この古典的な機械学習モデルに焦点を当てます。
1はじめに
3つの異なるサイコロ(6面、4面、8面)があるとします。3つのサイコロのいずれかを選択するたびに、次の図に示すように、各サイコロが選択される確率は1/3です。上記のプロセスを繰り返します。 1つの文字列値[1,6,3,5,2,7]を取得します。これらの観測可能な変数は、観測可能な状態のチェーンを形成します。同時に、隠れマルコフモデル(この場合はサイコロのシーケンス)には、隠れ変数で構成される隠れ状態連鎖があります。たとえば、このデジタルサイコロの文字列を取得するシーケンスは、[D6、D8、D8、D6、D4、D8]の場合があります。
隠れマルコフ型の概略図は次のとおりです。
図の矢印は、変数間の依存関係を示しています。図の各矢印の説明は次のとおりです。
常に、観測された変数(die)は状態変数(どの種類のdie)にのみ依存し、時間tでの状態qtは状態qt-1のみに依存します。時間t-1で。これがマルコフ連鎖です。つまり、システムの次の瞬間は、現在の状態(メモリなし)、つまり「均一マルコフ仮説」によってのみ決定されます
。2隠れマルコフモデルの定義
上記の例によると、これが隠れマルコフモデルです。Kofuの定義。隠れマルコフモデルは、時系列に関する確率モデルであり、隠れマルコフ連鎖から観測不可能な状態の観測不可能なランダムシーケンスをランダムに生成し、各状態から観測可能なランダムシーケンスを生成するプロセスを記述します。隠れマルコフ連鎖ランダムに生成された状態シーケンスは状態シーケンス(つまり、上記の例ではD6、D8など)と呼ばれます。各状態は観測を生成し、結果として得られるランダムな観測シーケンスは観測シーケンス(つまり、上記の観測シーケンス)と呼ばれます。例)1、6など)。シーケンス内の各位置は瞬間と見なすことができます。
隠れマルコフモデルは、初期確率分布、状態遷移確率分布、および観測確率分布によって決定されます。具体的な形式は次のとおりです。ここで、Qはすべての可能な状態のセットであり、Vはすべての可能な観測のセットです
。3フォワードアルゴリズムの
最初のステップでは、状態i1 = q1および観測o1です。最初の瞬間に同時確率。ステップ(2)は、順方向確率の再帰式であり、時間t + 1での部分観測シーケンスがo1、o2、...、ot、ot + 1であり、時間tで状態qiにある順方向確率を計算します。 +1。上図に示すように、at(j)は、o1、o2、...、otが時間tで観測され、時間tの状態にあるときの、qjの前方確率であるため、at(j)ajiは次のようになります。時間tでo1、o2を観察します。...、Otは、時間tでqj状態にあり、時間t +1でqi状態に到達する同時確率です。時間tでのこの積のすべての可能なN状態の合計、結果は、時間tでのo1、o2、...、otおよび時間t +1での状態qiとしての観測の同時確率です。最後に、3番目のステップはP(O | lamda)の結果を計算することです。
もちろん、これは多くのアルゴリズムの1つにすぎず、同様に後方アルゴリズムもあります(理解のために関連する本を読むことができます)。隠れマルコフモデル予測問題を解くための動的計画法で最も広く使用されているのは、ビタビアルゴリズムです。
第二に、ソースコード
function varargout = SleepStatus(varargin)
% SLEEPSTATUS MATLAB code for SleepStatus.fig
% SLEEPSTATUS, by itself, creates a new SLEEPSTATUS or raises the existing
% singleton*.
%
% H = SLEEPSTATUS returns the handle to a new SLEEPSTATUS or the handle to
% the existing singleton*.
%
% SLEEPSTATUS('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in SLEEPSTATUS.M with the given input arguments.
%
% SLEEPSTATUS('Property','Value',...) creates a new SLEEPSTATUS or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before SleepStatus_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to SleepStatus_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 SleepStatus
% Last Modified by GUIDE v2.5 21-Oct-2020 17:16:55
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @SleepStatus_OpeningFcn, ...
'gui_OutputFcn', @SleepStatus_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 SleepStatus is made visible.
function SleepStatus_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 SleepStatus (see VARARGIN)
% Choose default command line output for SleepStatus
handles.output = hObject;
set(handles.text2,'string','Sleep status detector','foregroundcolor','b','fontsize',20);
set(handles.pushbutton1,'String','打开文件');
set(handles.pushbutton2,'String','播放且判断');
set(handles.pushbutton3,'String','停止');
set(handles.edit1,'String','实时判断结果');
set(handles.edit2,'String','过往判断结果');
set(handles.edit2,'Max',10);
set(handles.edit2,'Min',0);
cla(handles.axes1);
cla(handles.axes2);
global player timer_counter accurate_time n_last n_last1 all_result num
player=[];
timer_counter =0;
accurate_time = 0;
n_last =2;
n_last1 =2;
num =1;
all_result={
};
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes SleepStatus wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = SleepStatus_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;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% 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)
global wavfilename timer_counter all_result num
[FileName,PathName] = uigetfile('*.wav','Select the wav-file');
if FileName==0
wavfilename =[];
return
end
wavfilename = [PathName,FileName];
[x,fs] = audioread(wavfilename);
x = mean(x,2);
axes(handles.axes1);
plot(x);
ylim([-1 1]);
timer_counter = 0;
accurate_time =0;
all_result={
};
set(handles.edit2,'string','');
num = 1;
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global wavfilename tAxis player myStruct start_minute start_seconds
if isempty(wavfilename)
return
end
%default
durT = 30*60; %seconds
fs = 32000;
durS = fs*durT; %samples
[x,fs] = audioread(wavfilename);
x = mean(x,2);
durT = size(x,1)/fs;
durS = size(x,1);
dt = 1/fs;
tAxis = dt:dt:durT;
frameRate = 25; %fps
frameT = 1/frameRate;
mag = 1;
axes(handles.axes1);
cla(handles.axes1);
plot(tAxis, x);
ylim([-mag mag])
xlim([0 durT])
xlabel('Time [s]')
playHeadLoc = 0;
% hold on;
ax = plot([0 playHeadLoc], [-mag mag], 'r', 'LineWidth', 2);
player = audioplayer(x, fs);
myStruct.playHeadLoc = playHeadLoc;
myStruct.frameT = frameT;
myStruct.ax1 = handles.axes1;
myStruct.ax2 = handles.axes2;
myStruct.edit1 = handles.edit1;
myStruct.edit2 = handles.edit2;
tmr1 = timer('ExecutionMode', 'FixedRate', ...
'Period', 0.5, ...
'TimerFcn', {
@timerCallback});
% tmr2 = timer('ExecutionMode', 'FixedRate', ...
% 'Period', 0.1, ...
% 'TimerFcn', {
@timerCallback2});
handles.timer1 = tmr1;
% handles.timer2 = tmr2;
guidata(hObject, handles);
start_time = clock();
start_seconds = start_time(6);
start_minute = start_time(5);
start(tmr1);
% start(tmr2);
playblocking(player);
% play(player);
if ~isplaying(player)
stop(handles.timer1);
% stop(handles.timer2);
end
% function timerCallback2(hObj, eventdata)
% global accurate_time
% accurate_time = accurate_time + 0.1;
function timerCallback(hObj, eventdata)
global wavfilename myStruct tAxis timer_counter accurate_time start_minute start_seconds n_last n_last1 all_result num
[x1,fs] = audioread(wavfilename);
x1= mean(x1,2);
length_max = length(x1);
% timer_counter = accurate_time;
start_time = clock();
seconds = start_time(6);
minute = start_time(5);
timer_counter = (minute*60+seconds)-(start_minute*60+start_seconds);
% disp(timer_counter)
axes(myStruct.ax1);
plot(tAxis,x1);
line([timer_counter timer_counter],[-1 1],'color','r');
axis([tAxis(1) tAxis(end) -1 1]);
time1 = timer_counter;
time2 = time1 + 1;%1s
idx1 = fix(fs*time1);
idx2 = fix(fs*time2);
if (idx1<1)
idx1 = 1;
end
if(idx1>=length_max)
idx1 = length_max;
end
if(idx2>=length_max)
idx2 = length_max;
end
if(idx1>=idx2)
return
end
3、実行中の結果
四、備考
QQ 1564658423を追加するために、コードを完成させるか、記述してください