0001 function [] = perturbRecovery(animals)
0002
0003 if nargin < 1
0004 animals = 1:9;
0005 end
0006
0007
0008 tmts = {'control','mass','inertia'};
0009
0010 ylims = {20*[-1,1],30*[-1,1],40*[-1,1],[-5,20],[-4,4],[],[-70,-10],40*[-1,1],[],[],1000*[-1,1],1000*[-1,1],1000*[-1,1]};
0011
0012 states = {'pitch','roll','yaw','x','y','z','vx','vy','ax','ay','dpitch','droll','dyaw'};
0013
0014 plot_state = [1,1,1,0,0,0,1,1,0,0,1,1,1];
0015
0016
0017
0018 units = {'deg','deg','deg','cm','cm','cm','cm/sec','cm/sec','g','g','deg/sec','deg/sec','deg/sec'};
0019
0020 sig_plot = 1;
0021 save_pre = 0;
0022 save_post = 0;
0023 save_postc = 1;
0024
0025 cols = hsv(length(tmts));
0026
0027 for t = 1:length(tmts)
0028 load(['data/',tmts{t},'_lp.mat'],'lp');
0029 eval(['lp',tmts{t},' = lp;']);
0030 load(['data/',tmts{t},'_rec.mat'],'rec');
0031 eval(['rec',tmts{t},' = rec;']);
0032 end
0033
0034
0035 np = size(rec(1).Ppre,2);
0036 pinc = (2*pi/np);
0037 p = 0:pinc:(2*pi-pinc);
0038
0039 ani = struct('c',[],'m',[],'i',[]);
0040
0041
0042 for a = animals
0043
0044 n = [];
0045 ti = '';
0046 leg = {};
0047
0048 if length(animals) == 1
0049 ti = [ti,', a = ',num2str(animals)];
0050 end
0051
0052
0053 for t = 1:length(tmts)
0054
0055
0056 eval(['lp = lp',tmts{t},';']);
0057 eval(['rec = rec',tmts{t},';']);
0058
0059 Bpr = [];
0060
0061
0062 for s = 1:length(states)
0063
0064 if ~plot_state(s)
0065 continue;
0066 end
0067
0068 if save_post
0069 postfig = figure(1);
0070 clf
0071 if sig_plot
0072 s1 = subplot(3,2,[1,2,3,4]);
0073 hold on;
0074 s2 = subplot(3,2,[5,6]);
0075 hold on;
0076 else
0077 s1 = gca;
0078 hold on;
0079 end
0080 end
0081
0082 Ppre = [];
0083 Bpre = [];
0084
0085
0086 for k = find(lp.animal == a)'
0087
0088 if ~isempty(rec(k).Ppre)
0089
0090 Ppre = [Ppre, rec(k).Ppre'];
0091 Bpre = [Bpre, rec(k).Bpre(:,:,s)'];
0092 end
0093
0094 end
0095
0096 Bpr(1:length(Bpre(:)),s) = Bpre(:);
0097
0098 ord = 5;
0099
0100 Bo(s) = ftFit( 5, mod(Ppre(:),2*pi), Bpre(:) );
0101 Bm = [];
0102 for k = 1:size(Ppre,2)
0103 Bm(:,k) = ftVal( Bo(s), Ppre(:,k) );
0104 end
0105 Be = Bpre - Bm;
0106 L20 = sqrt(sum(Be.^2,1));
0107
0108 m0 = mean(L20);
0109 s0 = std(L20);
0110
0111 z0 = 1.65*s0+m0;
0112
0113 L2 = [];
0114
0115
0116
0117 cols = hsv(length(find(lp.animal == a)));
0118
0119 K = find(lp.animal == a)';
0120
0121 for k = 1:length(K);
0122
0123 if ~isempty(rec(K(k)).P) && length(rec(K(k)).P)-rec(K(k)).post >= np
0124
0125 Bp = ftVal( Bo(s), rec(K(k)).P(rec(K(k)).post+(0:np-1)) )';
0126
0127 for n = 0:length(rec(K(k)).P)-np-rec(K(k)).post-1
0128
0129 L2(k,n+1) = sqrt(sum((rec(K(k)).B(1,rec(K(k)).post+n+(0:np-1),s) - Bp(mod((0:np-1)+n,np)+1)).^2));
0130
0131 end
0132
0133 L2(k,find(L2(k,:)==0)) = nan;
0134
0135 rs = find(L2(k,:) <= z0,1,'first');
0136 if isempty(rs) || rs == 1
0137 rp = nan;
0138 r = nan;
0139 else
0140
0141 rp = rs*2*pi/np;
0142
0143 Bp = ftVal( Bo(s), rec(K(k)).P(rec(K(k)).post + (0:rs)) )';
0144 r = sum(abs(rec(K(k)).B(1,rec(K(k)).post+(0:rs),s)-Bp));
0145 end
0146
0147 eval(['ani(a).',tmts{t}(1),'(s,k) = r;']);
0148
0149 if save_post
0150
0151
0152 plot(s1,rec(K(k)).P,rec(K(k)).B(:,:,s),'color',cols(k,:),'LineWidth',1);
0153
0154
0155 plot(s2,rec(K(k)).P(rec(K(k)).post+(1:sum(~isnan(L2(k,:))))),L2(k,1:sum(~isnan(L2(k,:)))),'color',cols(k,:),'LineWidth',1);
0156
0157 if ~isempty(ylims{s})
0158 ylim1 = ylims{s};
0159 else
0160 ylim1 = get(s1,'YLim');
0161 end
0162
0163 ylim2 = get(s2,'YLim');
0164
0165
0166 plot(s1,rec(K(k)).P(rec(K(k)).post)*[1,1],ylim1,'--','color',cols(k,:));
0167 plot(s2,rec(K(k)).P(rec(K(k)).post)*[1,1],ylim2,'--','color',cols(k,:));
0168 end
0169
0170 end
0171 end
0172
0173
0174
0175
0176 if save_post
0177
0178 figure(postfig);
0179
0180 if ~isempty(ylims{s})
0181 ylim = ylims{s};
0182 else
0183 ylim = get(s1,'YLim');
0184 end
0185
0186 xlim = [-4*pi,8*pi];
0187
0188 Bf = ftVal( Bo(s), xlim(1):pinc:xlim(2) );
0189 plot(s1,xlim(1):pinc:xlim(2),Bf,'k','LineWidth',2);
0190
0191 plot(s2,xlim,z0*[1,1],'k','LineWidth',2);
0192
0193 xlabel(s2,'Phase (rad)','FontName','Times','FontSize',18);
0194 ylabel(s1,[states{s},' (',units{s},')'],'FontName','Times','FontSize',18);
0195 ylabel(s2,'{L_2}','FontName','Times','FontSize',18);
0196 title(s1,[tmts{t},': a#',num2str(a),', ',states{s},' (',units{s},')'],'FontName','Times','FontSize',22);
0197 xtick = [fliplr(0:-6.28:xlim(1)),6.28:6.28:xlim(2)];
0198 set(s1,'XLim',xlim,'XTick',xtick,'YLim',ylim,'FontName','Times','FontSize',14);
0199 set(s2,'XLim',xlim,'XTick',xtick,'FontName','Times','FontSize',14);
0200
0201
0202
0203 figprefix = [tmts{t}(1),'_a',num2str(a),'_'];
0204
0205 dirs = {['fig/rec/'],['fig/rec/fig'],['fig/rec/eps']};
0206 for d = 1:length(dirs)
0207 if ~exist(dirs{d},'dir')
0208 mkdir(dirs{d});
0209 end
0210 end
0211 saveas(gca,['fig/rec/',figprefix,states{s},'.png'],'png');
0212
0213
0214 end
0215
0216
0217
0218 if save_pre
0219
0220 figure(2);
0221 plot(mod(Ppre,2*pi),Bpre,'.');
0222
0223
0224 hold on;
0225 plot(p,Bp,'k','LineWidth',6);
0226
0227 if ~isempty(ylims{s})
0228 ylim = ylims{s};
0229 else
0230 ylim = get(gca,'YLim');
0231 end
0232
0233 xlabel('Phase (rad)','FontName','Times','FontSize',18);
0234 ylabel([states{s},' (',units{s},')'],'FontName','Times','FontSize',18);
0235 title([tmts{t},': a#',num2str(a),', ',states{s},' (',units{s},')'],'FontName','Times','FontSize',22);
0236 set(gca,'XLim',[0,2*pi],'YLim',ylim,'FontName','Times','FontSize',14);
0237
0238 figprefix = [tmts{t}(1),'_a',num2str(a),'_'];
0239
0240 dirs = {['fig/pre/'],['fig/pre/fig'],['fig/pre/eps']};
0241 for d = 1:length(dirs)
0242 if ~exist(dirs{d},'dir')
0243 mkdir(dirs{d});
0244 end
0245 end
0246 saveas(gca,['fig/',figprefix,states{s},'.png'],'png');
0247
0248
0249 end
0250 end
0251
0252 Ppr = Ppre(:);
0253
0254 ord = 5;
0255
0256
0257 st = 10+[1,2,3];
0258
0259 Bo = ftFit( 5, mod(Ppre(:),2*pi), Bpr );
0260 Bm = ftVal( Bo, mod(Ppre(:),2*pi) );
0261 Be = Bpr - Bm;
0262 L20 = sqrt(sum(reshape(sum(Be(:,st).^2,2),np,size(Be,1)/np),1));
0263
0264 m0 = mean(L20);
0265 s0 = std(L20);
0266
0267 z0 = 1.65*s0+m0;
0268
0269
0270 if save_postc
0271
0272 figure(137);
0273 clf;
0274 hold on;
0275 end
0276
0277
0278 K = find(lp.animal == a)';
0279
0280 for k = 1:length(K);
0281
0282 if ~isempty(rec(K(k)).P) && length(rec(K(k)).P)-rec(K(k)).post >= np
0283
0284 Bp = ftVal( Bo, rec(K(k)).P(rec(K(k)).post+(0:np-1)) );
0285
0286 for n = 0:length(rec(K(k)).P)-np-rec(K(k)).post-1
0287
0288 L2(k,n+1) = sqrt(sum(sum((squeeze(rec(K(k)).B(1,rec(K(k)).post+n+(0:np-1),st)) - Bp(mod((0:np-1)+n,np)+1,st)).^2,2),1));
0289
0290 end
0291
0292 L2(k,find(L2(k,:)==0)) = nan;
0293
0294 rs = find(L2(k,:) <= z0,1,'first');
0295 if isempty(rs) || rs == 1
0296 rp = nan;
0297
0298 else
0299
0300 rp = rs*2*pi/np;
0301
0302
0303
0304 end
0305 eval(['ani(a).',tmts{t}(1),'(14,k) = rp;']);
0306
0307
0308 if save_postc
0309
0310 plot(rec(K(k)).P(rec(K(k)).post+(1:sum(~isnan(L2(k,:))))),L2(k,1:sum(~isnan(L2(k,:)))),'color',cols(k,:),'LineWidth',1);
0311
0312 end
0313
0314 end
0315 end
0316
0317 if save_postc
0318
0319 xlabel('Phase (rad)','FontName','Times','FontSize',18);
0320 ylabel('{L_2}','FontName','Times','FontSize',18);
0321 title([tmts{t},': a#',num2str(a)],'FontName','Times','FontSize',22);
0322 set(gca,'FontName','Times','FontSize',14);
0323
0324
0325
0326 figprefix = [tmts{t}(1),'_a',num2str(a),'_c'];
0327
0328 dirs = {['fig/recc/'],['fig/recc/fig'],['fig/recc/eps']};
0329 for d = 1:length(dirs)
0330 if ~exist(dirs{d},'dir')
0331 mkdir(dirs{d});
0332 end
0333 end
0334 saveas(gca,['fig/rec/',figprefix,'.png'],'png');
0335 end
0336
0337 keyboard
0338
0339 end
0340
0341
0342
0343
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384
0385
0386
0387
0388
0389
0390
0391
0392
0393
0394
0395
0396
0397
0398
0399
0400
0401
0402
0403
0404
0405
0406 end
0407
0408 save(['data/ani.mat'],'ani');