To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / _beattracker / tempo_hmm.m @ 8:b5b38998ef3b
History | View | Annotate | Download (3.08 KB)
| 1 |
function [path,obs,prob,wv,tmat,posterior]= tempo_hmm(df,step,tempo,tightness) |
|---|---|
| 2 |
|
| 3 |
|
| 4 |
% if nargin<4 |
| 5 |
% step = 128; |
| 6 |
% tempo = 0; |
| 7 |
% tightness = 8; |
| 8 |
% end |
| 9 |
|
| 10 |
if nargin<3 |
| 11 |
step = 128; |
| 12 |
tempo = 0; |
| 13 |
end |
| 14 |
|
| 15 |
% df = df/alpha_norm(9,df); |
| 16 |
df = qnormalise(df,9,2); |
| 17 |
p = bt_parms; |
| 18 |
winlen = 512; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
df = [df(:)' zeros(1,winlen/2)]; % add half a frame... then fix last 2 values |
| 23 |
% try using matlab hmm to do tempo tracking... |
| 24 |
df = df(:)'; |
| 25 |
|
| 26 |
pin = 0; |
| 27 |
pend = length(df) - winlen; |
| 28 |
n = [1:128]; |
| 29 |
% p.rayparam = 33; |
| 30 |
|
| 31 |
if tempo |
| 32 |
mu = p.fact/tempo; |
| 33 |
sigma = mu/4; |
| 34 |
wv = exp(-0.5 * ((n - mu)./sigma).^2) ./ (sqrt(2*pi) .* sigma); |
| 35 |
else |
| 36 |
wv = (n ./ p.rayparam .^ 2) .* exp(-n .^ 2 ./ (2*p.rayparam .^ 2)); |
| 37 |
end |
| 38 |
|
| 39 |
wv = sunity(wv); |
| 40 |
lenwv = length(wv); |
| 41 |
% wv = ~~wv; |
| 42 |
|
| 43 |
ct = 0; |
| 44 |
while(pin<pend) |
| 45 |
|
| 46 |
|
| 47 |
ct = ct+1; |
| 48 |
segment = adapt_thresh(df(pin+1:pin+winlen)); |
| 49 |
% acf(:,ct) = (ftacf(segment)); |
| 50 |
acf(:,ct) = [ ftacf(segment(:))' zeros(1,512-winlen)]'; |
| 51 |
|
| 52 |
[temp,rcf] = getperiod(acf(:,ct),wv,0,128,1,p.lowest); |
| 53 |
temp2 = adapt_thresh(rcf); |
| 54 |
% st = sum(temp2); |
| 55 |
% temp3 = zeros(1,lenwv); |
| 56 |
% tempmat = zeros(lenwv); |
| 57 |
% for i=1:lenwv, |
| 58 |
% for j=1:lenwv, |
| 59 |
% tempmat(i,j) = (temp2(i)+temp2(j)); |
| 60 |
% end |
| 61 |
% tempmat(i,i) = 0; |
| 62 |
% end; |
| 63 |
% temp3 = sum(tempmat); %plot(temp3); pause; |
| 64 |
temp3=temp2; |
| 65 |
temp3 = temp3'.*(acf(1:length(temp3),ct)); |
| 66 |
% temp3 = acf(1:length(wv),ct); |
| 67 |
% temp(1:10) = 0; |
| 68 |
% temp(118:end) = 0; |
| 69 |
% temp3(1:10) = 0; |
| 70 |
|
| 71 |
% for i=1:lenwv, |
| 72 |
% temp4(i) = temp2(i)+st; |
| 73 |
% end |
| 74 |
% keyboard |
| 75 |
% obs(:,ct) = sunity(temp3(:)); %% WAS NOT COMMENTED WHEN MATTHEW GAVE IT |
| 76 |
% TO ME. |
| 77 |
obs(:,ct) = sunity(rcf) + 0.001*rand(1,length(rcf)); |
| 78 |
% obs(:,ct) = (temp3(:)); |
| 79 |
% keyboard |
| 80 |
% obs(:,ct) = sunity(adapt_thresh(rcf(:))); |
| 81 |
|
| 82 |
|
| 83 |
pin = pin+step; |
| 84 |
end |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
tmat = zeros(128); |
| 89 |
% make transition matrix |
| 90 |
for i=20:118, tmat(:,i) = sunity(normpdf(n,i,8))'; end; |
| 91 |
% % [alpha, beta, gamma] = fwdback(wv,tmat,obs); |
| 92 |
% figure; mesh(tmat); |
| 93 |
|
| 94 |
% t = textread('/home/studio/Desktop/tmat.txt');
|
| 95 |
% |
| 96 |
% tmat = reshape(t,128,128); |
| 97 |
% % figure(1); mesh(tmat); |
| 98 |
% |
| 99 |
% for i=1:size(tmat,1) |
| 100 |
% tmat(i,:) = 1/128+filtfilt(gausswin(9),1,tmat(i,:)); |
| 101 |
% end |
| 102 |
% |
| 103 |
% for i=1:size(tmat,2) |
| 104 |
% tmat(:,i) = 1/128+filtfilt(gausswin(9),1,tmat(:,i)); |
| 105 |
% end |
| 106 |
% for i=1:size(tmat,2) |
| 107 |
% tmat(:,i) = sunity(tmat(:,i)); |
| 108 |
% end |
| 109 |
% % figure; mesh(tmat); |
| 110 |
% tmat(1:20,:) = 0; |
| 111 |
% tmat(:,1:20) = 0; |
| 112 |
% tmat(120:128,:) = 0; |
| 113 |
% tmat(:,120:128) = 0; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
% figure(2); mesh(tmat); |
| 118 |
|
| 119 |
% keyboard |
| 120 |
% [path,posterior] = viterbi_path(wv,tmat,obs); |
| 121 |
path = viterbi_path(wv,tmat,obs); |
| 122 |
% try tracking backwards.. probably doesn't matter |
| 123 |
%% FIXME - forwards backwards? |
| 124 |
% [path,posterior] = viterbi_path(wv,tmat,fliplr(obs)); |
| 125 |
% path = fliplr(path); |
| 126 |
% path |
| 127 |
|
| 128 |
% obs = obs/sum(sum(obs)); |
| 129 |
|
| 130 |
% |
| 131 |
% for i=1:length(path), |
| 132 |
% % prob(i) = sum(obs(path(i)-2:path(i)+2,i)); |
| 133 |
% |
| 134 |
% prob(i) = sum(obs(path(i)-2:path(i)+2,i)); |
| 135 |
% |
| 136 |
% % range = max(min(size(obs,1),path(i)-2:path(i)+2),1); |
| 137 |
% % prob(i) = sum(obs(range,i)); |
| 138 |
% |
| 139 |
% %path(i)= refineperiod(path(i),acf(:,i),0); |
| 140 |
% end |
| 141 |
|
| 142 |
path = [path path(end)*ones(1,2)]; |
| 143 |
prob = 1; |
| 144 |
|
| 145 |
|
| 146 |
% path = interp(path,2); |
| 147 |
|
| 148 |
% for i=1:222, [r(1,i),r(2,i),r(3,i),r(4,i)]=cont_eval(anns.beats{i},beats{i},0.175);end; mean(r')
|