0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045 clear all
0046
0047 data_mode = 1;
0048 plot_mode = 1;
0049
0050
0051
0052
0053
0054
0055
0056 tlim = [415 420];
0057
0058
0059
0060 switch data_mode
0061 case 1
0062 root_dir = [getenv('MATHOME') '/Finger_move/RH070222'];
0063 data_file = 'RH070222a.meg.mat';
0064 status_ch = '226';
0065
0066 loadspec = [];
0067 loadspec.ChannelName = {status_ch};
0068 status = vb_load_meg_data([root_dir '/' data_file], loadspec);
0069
0070 info = vb_load_meg_info([root_dir '/' data_file]);
0071 parm.fsamp = info.SampleFreq;
0072
0073
0074 Ntrial = 10;
0075 t_step = 200;
0076 Tpre = 100;
0077 Tpost = 500;
0078 Tend = 5000;
0079 case 2
0080 root_dir = './test';
0081 data_file = 'emg.mat';
0082 load( [root_dir '/' data_file] ,'status');
0083 parm.fsamp = 1000;
0084 Ntrial = 25;
0085 t_step = 500;
0086 Tpre = 500;
0087 Tpost = 800;
0088 Tend = 5000;
0089 end
0090
0091
0092 parm.p_val = [0.0001 0.0005];
0093
0094
0095
0096 switch data_mode
0097 case 1
0098 parm.t_event = 150;
0099 case 2
0100 parm.t_event = 300;
0101 end
0102
0103
0104 parm.hist_mode = 1;
0105
0106
0107
0108
0109 parm.t_smooth = 25 ;
0110 parm.t_slope = 25 ;
0111 parm.t_peak = 100;
0112
0113
0114 y = status(:);
0115
0116 dt = 1/parm.fsamp;
0117 T = length(y);
0118 t = (1:T)*dt;
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130 [ix ,zd, z, ix_y, yd, ix_z, ix_s] = vb_get_emg_onset(y,parm);
0131
0132
0133 jz = ix * dt;
0134 j_z = ix_z * dt;
0135
0136 ymax = max(abs(y));
0137
0138 xtick = 0:(t_step/1000):ceil(t(end));
0139
0140 t1 = jz - Tpre/1000;
0141 t2 = jz + Tpost/1000;
0142
0143 Nz = length(ix);
0144 nlist = 1:Nz;
0145
0146
0147 switch plot_mode
0148 case 0
0149
0150
0151
0152 yth = vb_get_peak_threshold(abs(y),yd);
0153 yd2 = (yd + mean(yth))/2;
0154
0155
0156 figure;
0157 subplot 211
0158
0159 plot(t,abs(y),'--')
0160 hold on
0161
0162 plot([jz; jz], [zeros(1,Nz); repmat(1.1*ymax, 1,Nz)], 'r--');
0163
0164 plot([0; t(end)], [yd; yd], 'r-')
0165 plot([0; t(end)], [yd2; yd2], 'b--')
0166 title('EMG threshold and onset')
0167
0168 subplot 212
0169
0170 plot(t,z,'c-')
0171 hold on
0172
0173
0174 plot([jz; jz], [zeros(1,Nz); repmat(ymax, 1,Nz)], 'r-');
0175
0176 plot([0; t(end)], [zd; zd], 'r-')
0177 title('Smoothed EMG threshold and onset')
0178
0179 figure;
0180
0181
0182 Nbin = 1000;
0183 [hy ,ylist] = hist(abs(y),Nbin);
0184 [hz ,zlist] = hist(z,Nbin);
0185
0186 subplot 221
0187
0188 hist(abs(y),Nbin);
0189 hold on
0190 plot([yd; yd], [0; max(hy)], 'r-')
0191 plot([yd2; yd2], [0; max(hy)], 'b--')
0192
0193 title('EMG histogram')
0194
0195 subplot 222
0196
0197 hist(z,Nbin);
0198 hold on
0199 plot([zd; zd], [0; max(hz)], 'r-')
0200 title('Smoothed EMG histogram')
0201
0202 subplot 223
0203
0204 jzd = diff(jz);
0205 plot(jzd,'o')
0206 title('Onset interval')
0207 case 1
0208
0209
0210 yth = vb_get_peak_threshold(abs(y),yd);
0211 yd2 = (yd + mean(yth))/2;
0212
0213 Nfig = ceil(Nz/Ntrial);
0214 t_wd = Tend/1000;
0215
0216 for n=1:Nfig
0217 n1 = 1+Ntrial*(n-1);
0218 n2 = min(Nz,n1+Ntrial-1);
0219 figure;
0220 subplot 211
0221
0222 plot(t,abs(y),'--')
0223 hold on
0224
0225
0226 plot([jz; jz], [zeros(1,Nz); repmat(1.1*ymax, 1,Nz)], 'r--');
0227
0228 plot([0; t(end)], [yd; yd], 'r-')
0229 plot([0; t(end)], [yd2; yd2], 'b--')
0230 title('EMG threshold and onset')
0231
0232 xlim([jz(n1)-t_wd jz(n2)+t_wd])
0233
0234 subplot 212
0235
0236 plot(t,z,'c-')
0237 hold on
0238
0239
0240 plot([jz; jz], [zeros(1,Nz); repmat(ymax, 1,Nz)], 'r--');
0241
0242 plot([0; t(end)], [zd; zd], 'r-')
0243 title('Smoothed EMG threshold and onset')
0244 xlim([jz(n1)-t_wd jz(n2)+t_wd])
0245 end
0246 case {2 , 20}
0247
0248 nfig=0; npng = 0;
0249 NX = 3; NY = 4;
0250
0251 for n=nlist
0252 nfig = nfig + 1;
0253 if nfig > NX*NY,
0254 npng = npng + 1;
0255 nstr = num2str(npng);
0256 if plot_mode==2
0257 print(gcf, '-dpng', ['temp_' nstr '.png']);
0258 close
0259 end
0260 figure; nfig=1;
0261 end
0262 subplot(NY,NX,nfig)
0263
0264 plot(t,abs(y),':')
0265 hold on
0266 plot(t,z,'c-')
0267
0268
0269 plot([0; t(end)], [yd; yd], 'b-')
0270 plot([0; t(end)], [zd; zd], 'b--')
0271
0272
0273 plot([jz; jz], [zeros(1,Nz); repmat(ymax, 1,Nz)], 'r--');
0274 plot(j_z, z(ix_z), 'ro');
0275
0276
0277 if ix_s(1,n) > 0
0278
0279 plot(t(ix_s(1,n):ix_s(2,n)) , z(ix_s(1,n):ix_s(2,n)),...
0280 'r-','LineWidth',2)
0281 end
0282
0283 xlim([t1(n) t2(n)])
0284 ylim([0 ymax])
0285 set(gca,'XTick',xtick);
0286 title(sprintf('n=%d',n))
0287
0288
0289 end
0290 case 3
0291
0292 jzd = diff(jz);
0293 plot(jzd,'o')
0294 title('Onset interval')
0295
0296 case 4
0297 Nbin = 1000;
0298 [hy ,ylist] = hist(abs(y),Nbin);
0299 [hz ,zlist] = hist(z,Nbin);
0300 yth = vb_get_peak_threshold(abs(y),yd);
0301 yd2 = (yd + mean(yth))/2;
0302
0303
0304 subplot 221
0305 hist(abs(y),Nbin);
0306 hold on
0307 plot([yd; yd], [0; max(hy)], 'r-')
0308
0309 title('EMG histogram')
0310
0311 subplot 222
0312 hist(z,Nbin);
0313 hold on
0314 plot([zd; zd], [0; max(hz)], 'r-')
0315 title('smoothed EMG histogram')
0316
0317 subplot 223
0318 plot(t,abs(y))
0319 hold on
0320 plot([t(1) t(end)], [yd; yd],'r-')
0321 plot([t(1) t(end)], [yd2; yd2],'r--')
0322 title('EMG signal')
0323
0324 subplot 224
0325 plot(t,z)
0326 hold on
0327 plot([t(1) t(end)], [zd; zd],'r-')
0328 title('smoothed EMG signal')
0329 case 5
0330 status_level = 1/4;
0331 Nbin = min(5000,fix(T/10));
0332 yd2 = max(y) * status_level;
0333
0334 y = abs(y);
0335 [hy ,ylist] = hist(y,Nbin);
0336 [hz ,zlist] = hist(z,Nbin);
0337
0338 [yth, A ,ym, yy] = vb_gamma_dist_param(y, parm.p_val(1));
0339 py = vb_gamma_dist_prob(ylist,hy,A);
0340 yd3 = vb_get_peak_threshold(y,yth)
0341
0342 [zth, A ,zm, zz] = vb_gamma_dist_param(z, parm.p_val(2));
0343 pz = vb_gamma_dist_prob(zlist,hz,A);
0344
0345 subplot 211
0346 hist(abs(y),Nbin);
0347 hold on
0348 plot([yd; yd], [0; max(hy)], 'r-')
0349 plot([yth; yth], [0; max(hy)], 'b-')
0350
0351 plot([yd2; yd2], [0; max(hy)], 'r:')
0352 plot([yd3(1); yd3(1)], [0; max(hy)], 'b:')
0353 plot([yd3(2); yd3(2)], [0; max(hy)], 'b:')
0354
0355 plot([ym; ym], [0; max(hy)], 'r--')
0356 plot([ym+yy; ym+yy], [0; max(hy)], 'r--')
0357 plot([ym-yy; ym-yy], [0; max(hy)], 'r--')
0358 plot(ylist,py,'-r')
0359
0360 xlim([0 1.1*max([yd,yth,yd2,yd3])])
0361
0362 title('EMG histogram')
0363
0364 subplot 212
0365 hist(z,Nbin);
0366 hold on
0367 plot([zd; zd], [0; max(hz)], 'r-')
0368 plot([zth; zth], [0; max(hz)], 'b-')
0369 plot([zm; zm], [0; max(hz)], 'r--')
0370 plot([zm+zz; zm+zz], [0; max(hz)], 'r--')
0371 plot([zm-zz; zm-zz], [0; max(hz)], 'r--')
0372 plot(zlist,pz,'-r')
0373
0374 xlim([0 1.1*max(zd,zth)])
0375 title('smoothed EMG histogram')
0376 case 10
0377
0378
0379 yth = vb_get_peak_threshold(abs(y),yd);
0380 yd2 = (yd + mean(yth))/2;
0381 figure;
0382 subplot 211
0383
0384 plot(t,abs(y),'--')
0385 hold on
0386
0387
0388 plot([jz; jz], [zeros(1,Nz); repmat(1.1*ymax, 1,Nz)], 'r--');
0389
0390 plot([0; t(end)], [yd; yd], 'r-')
0391 plot([0; t(end)], [yd2; yd2], 'b--')
0392 title('EMG threshold and onset')
0393
0394 xlim(tlim)
0395
0396 subplot 212
0397
0398 plot(t,z,'c-')
0399 hold on
0400
0401
0402 plot([jz; jz], [zeros(1,Nz); repmat(ymax, 1,Nz)], 'r--');
0403
0404 plot([0; t(end)], [zd; zd], 'r-')
0405 title('Smoothed EMG threshold and onset')
0406 xlim(tlim)
0407 end