📄 genmarkov.m
字号:
function [trainx,testx, trainlabel, testlabel] = genmarkov(trmatrix,weights,param,dim,maxtrn,maxtst)%--------------------------------------------------------------------------------------------------%function [trainx,testx] = genmarkov(ntrain,ntest,trmatrix,weights,param,dim)% Generates training and test data based on some % hidden distribution and markovian property% using M previous states. This is useful in generating% time series data and for speech waveforms in% a controlled fashion. %% ntrain -> number of training data% ntest -> number of test data% trmatrix -> transition matrix% param -> parameter matrix for the distribution% ( {mean, variance} for each mixture)% dim -> dimension of the data to be generated% weights -> for each state the mixture probability% %--------------------------------------------------------------------------------------------------global poption;global dynamics;global startstate;global finalstate;global header;% Start with a zero state for the random % number generator[S,S] = size(trmatrix);state = rand(2,dim,S)*1e+09;[S,M] = size(weights);% Initial distribution = uniform distributionstart = ceil(rand(1,1)*(S));% Get the distribution based on the transition% probability% 0|------1|---2|------3|---------------4|--5|% % generate random numbers before so that the % distribution settles down to a steady state % value. Therefore scramble it with some initial% distribution.prevstate=start;% Generate a temporary transition matrix to% scramble the datatemptr = 1/S*ones(S,S);for i = 1:header, numberline = tril(ones(S,S))*temptr(:,prevstate); temp = (find(numberline>=rand(1,1))); prevstate = temp(1); clear temp; clear numberline; % Once u have chosen the next state now % generate a feature vector from it % assuming no correlation between parameters. % We dont have different random number generators % therefore we have mean and variance. % Or we have to store the state of the random number % generator for each state. % Now choose a mixture numberline = weights(prevstate,:)*tril(ones(M,M)); temp = (find(numberline>rand(1,1))); mixture = temp(1); clear temp; clear numberline; for d = 1:dim, % number of dimension randn('state',state(:,d,prevstate)); randn(1,1); state(:,d,prevstate) = randn('state'); end;end;% If the plot option is onif poption == 1, figure; hold on;end;% Generate training data. Now one can start % from the start state and generate data according% to the given transition matrix.prevstate = startstate;stopgen = 0;ntrain = 1;while stopgen == 0 & ntrain <= maxtrn, numberline = tril(ones(S,S))*trmatrix(:,prevstate); temp = (find(numberline>=rand(1,1))); prevstate = temp(1); if prevstate == finalstate, stopgen = 1; end; clear temp; clear numberline; % Once u have chosen the next state now % generate a feature vector from it % assuming no correlation between parameters. % We dont have different random number generators % therefore we have mean and variance. % Or we have to store the state of the random number % generator for each state. % Now choose a mixture numberline = weights(prevstate,:)*tril(ones(M,M)); temp = (find(numberline>rand(1,1))); mixture = temp(1); clear temp; clear numberline; for d = 1:dim, % number of dimension randn('state',state(:,d,prevstate)); trainx(ntrain,d) = param(prevstate,2*(mixture-1)*(dim)+ 2*(d-1) +1) + ... + param(prevstate,2*(mixture-1)*(dim)+ 2*(d-1) +2)*randn(1,1); state(:,d,prevstate) = randn('state'); end; trainlabel(ntrain) = prevstate; ntrain = ntrain + 1; if poption == 1, if trainlabel(ntrain) == 1, plot(trainx(ntrain,1),trainx(ntrain,2),'r+'); else plot(trainx(ntrain,1),trainx(ntrain,2),'b*'); end; end;end;% Now process the test dataif poption == 1, figure; hold on;end;% Generate test data using the initial state.prevstate = startstate;stopgen = 0;ntest = 1;while stopgen == 0 & ntest <= maxtst, numberline = tril(ones(S,S))*trmatrix(:,prevstate); temp = (find(numberline>=rand(1,1))); prevstate = temp(1); if prevstate == finalstate, stopgen = 1; end; clear temp; clear numberline; % Once u have chosen the next state now % generate a feature vector from it % assuming no correlation between parameters. % We dont have different random number generators % therefore we have mean and variance. % Or we have to store the state of the random number % generator for each state. % Now choose a mixture numberline = weights(prevstate,:)*tril(ones(M,M)); temp = (find(numberline>rand(1,1))); mixture = temp(1); clear temp; clear numberline; for d = 1:dim, % number of dimension randn('state',state(:,d,prevstate)); testx(ntest,d) = param(prevstate,2*(mixture-1)*(dim)+ 2*(d-1) +1) + ... + param(prevstate,2*(mixture-1)*(dim)+ 2*(d-1) +2)*randn(1,1); state(:,d,prevstate) = randn('state'); end; testlabel(ntest) = prevstate; ntest = ntest + 1; if poption == 1, if testlabel(ntest) == 1, plot(testx(ntest,1),testx(ntest,2),'r+'); else plot(testx(ntest,1),testx(ntest,2),'b*'); end; end;end;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -