-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstandard_test_2bd_embedFun_submit.m
executable file
·904 lines (847 loc) · 34.5 KB
/
standard_test_2bd_embedFun_submit.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
% Adaptive multiple narrow-band disturbance rejection applied to an active
% suspension.
% 2 narrow band disturbance
% Benchmark project by Ioan Landau.
% ============================================================
% Copyright (c) 2008-, Xu Chen
% Author(s): Xu Chen
% University of Washington
% Seattle, WA, 98115
% ============================================================
% 2012-09-09
% profile on
clear all
close all
%% Define Constants
FLAG_CONST_DIST_FREQ = 1;
SW_EXPERIMENT = 0;
FLAG_STEP_CHANGE_DIST_FREQ = 0;
FLAG_CHIRP_DIST = 2;
SW_SAMPLE_PLOT = 0;
SW_ONE_SIMU_TEST = 1;
SW_DIST_ON = 1; % default turn on const or step change disturbance frequency
SW_CHIRP_DIST = 0; % default turn off chirp disturbance
SW_TUNE = 0;
SW_INVERSE_TABLE = 0;
SW_BASELINE_CONTROL_SYS = 0; % check the baseline system
SW_ADDITIONAL_PLOT = 0;
SW_STEADY_STATE_CONTROL_SYS = 0;
Fs=800; Ts=1/Fs; Te=Ts;
bode_opt = bodeoptions;
bode_opt.FreqUnits = 'Hz';
bode_opt.FreqScale = 'Linear';
bode_opt.xlim = [0 400];
bode_opt.PhaseWrapping = 'On';
distID.THRESHOLD = 5e-3;
%%
% SELECT TEST OPTIONS HERE each time the test is run:
% FLAG_DIST_FREQ = 0 ------ step changing disturbance frequency
% FLAG_DIST_FREQ = 1 ------ constant disturbance frequency
% FLAG_DIST_FREQ = 2 ------ chirp disturbance
disp('=============Multiple Narrow Band Disturbance Rejection============')
disp('===================================================================')
disp('SELECT TEST OPTIONS:')
disp('0 (default) ---- step changing disturbance frequency')
disp('1 ---- constant disturbance frequency')
disp('2 ---- chirp disturbance')
disp(' ')
disp('Press ENTER for default selection.')
while 1
FLAG_DIST_FREQ = input(':');
if isempty(FLAG_DIST_FREQ)
FLAG_DIST_FREQ = 0;
end
if FLAG_DIST_FREQ ~= 0 && FLAG_DIST_FREQ ~= 1 && FLAG_DIST_FREQ ~= 2
disp('Wrong selection. Please re-select.')
else
break;
end
end % while 1
% in case nothing selected
if ~exist('FLAG_DIST_FREQ','var')
FLAG_DIST_FREQ = FLAG_STEP_CHANGE_DIST_FREQ;
end
disp('===================================================================')
disp('CHOOSE THE TEST LENGTH:')
disp('1 (default) ---- a quick sample test')
disp('0 ---- the entire frequency profile specified by the benchmark')
disp(' ')
disp('Press ENTER for default selection.')
while 1
SW_ONE_SIMU_TEST = input(':');
if isempty(SW_ONE_SIMU_TEST)
SW_ONE_SIMU_TEST = 1;
end
if SW_ONE_SIMU_TEST ~= 0 && SW_ONE_SIMU_TEST ~= 1
disp('Wrong selection. Please re-select.')
else
break;
end
end
disp('===================================================================')
disp('CHOOSE WHETHER OR NOT TO SAVE THE TEST DATA.')
while 1
SW_SAVE_DATA = input('Save the test result?\n 1(default, press ENTER): yes\n 0: no\n:');
if isempty(SW_SAVE_DATA)
SW_SAVE_DATA = 1;
end
if SW_SAVE_DATA ~= 0 && SW_SAVE_DATA ~= 1
disp('Wrong selection. Please re-select.')
else
break;
end
end
disp('===================================================================')
disp('ADAPTATION SCHEME.')
while 1
SW_UNIFORM_ADAP = input('Uniform adaptation gain? (more conservative performance)\n 1(default, press ENTER): yes\n 0: no\n:');
if isempty(SW_UNIFORM_ADAP)
SW_UNIFORM_ADAP = 1;
end
if SW_UNIFORM_ADAP ~= 0 && SW_UNIFORM_ADAP ~= 1
disp('Wrong selection. Please re-select.')
else
break;
end
end
FLAG_PERFORMANCE_EVAL = 0;
%%
NBn = 2; % number of narrow bands
% chirp distrubance parameters
chirp_dist.level = 2;
chirp_dist.freq1_seq = [50, 70];
chirp_dist.freq2_seq = [75, 95];
chirp_dist.para1 = [...
chirp_dist.freq1_seq(1);...
chirp_dist.freq1_seq(2);...
80];
chirp_dist.para2 = [...
chirp_dist.freq2_seq(1);...
chirp_dist.freq2_seq(2);...
95];
chirp_dist.freq1_init_time = 5;
chirp_dist.chirp_init_time = 10;
chirp_dist.chirp_dur_time = 4;
chirp_dist.freq2_dur_time = 5;
load band_pass_filter_50To95 % 2010-09-26
denoise_filter = tf(BP_ss_simulink)*tf(BP_ss_simulink);%2012-08-03
%% Adaptation parameters
% forgetting factor
adap_method = 2;
theta1_init = -2*cos(72*2*pi*Ts) - 2*cos(72*2*pi*Ts);
theta2_init = 2+2*cos(72*2*pi*Ts) * 2*cos(72*2*pi*Ts);
F1 = 1000;
F2 = 1000;
F_init = [F1, 0; 0, F2];
theta_init = [theta1_init; theta2_init];
% for exponentially increasing forgetting factor
lambda_init = 0.92;
lambda_end = 0.99;
% Band-pass Q filter parameter
alpha_init = 0.99; % 0.95
alpha_end = 0.865;%470;
alpha = alpha_end;
adap_init.alpha_init = alpha_init;
adap_init.alpha_end = alpha_end;
adap_init.F = F_init;
adap_init.theta = theta_init;
adap_init.lambda_init = lambda_init;
adap_init.lambda_end = lambda_end;
adap_init.SW_lambda = adap_method;
adap_init.SW_2Stage = 0;
%% define_plant_controllers
%/////////// primary path sys tf
load model_prim.mat Bp Ap %numerator and denominator of the primary path
%/////////// closed loop R/S controller
load RS_contr_sec R S
%/////////// secondary path sys tf
load model_sec.mat B A %numerator and denominator of the secondary path
SW_newSSModel = 0;
if SW_newSSModel
% load hinf_inv_landau_201211new36
% load hinf_inv_landau_201211new34
% load hinf_inv_landau_201211new33
load hinf_inv_landau_201211new32
[numINVP,denINVP] = tfdata(invP,'v');
% save hinf_inv_landau_coef_new numINVP denINVP
% load hinf_inv_landau_201211new19
else
load hinf_inv_landau
end
P_inv = tf(invP);
%/////////// loading noise values
load bruitbench
if SW_BASELINE_CONTROL_SYS
figure;
bodeplot(tf(R,S,Ts),bode_opt)
grid on,zoom on
title('Frequency response of the feedback controller')
% figure,bodeplot(tf(B,A,Ts),bode_opt),xlim([0,400])
end
if SW_BASELINE_CONTROL_SYS
L = length(bruitbench);
NFFT = 2^nextpow2(L);
[spec_bruitbench.f,spec_bruitbench.amp] =...
spectre_psd_rms(bruitbench,Fs,NFFT);
figure;
plot(spec_bruitbench.f,spec_bruitbench.amp)
xlabel('Frequency [Hz]')
ylabel('dB [Vrms]')
title('Spectral density of the measurement noise')
end
if SW_newSSModel
load NF98;
load NF46;
else
numNF46 = 1;
denNF46 = 1;
numNF98 = 1;
denNF98 = 1;
numNF98_2 = 1;
denNF98_2 = 1;
end
simuName = 'simulator_bench_2bd_simuSubmit';
%% Narrow band disturbances define
% Frequencies to be tested
if SW_ONE_SIMU_TEST
% freq_test1 = 60;
% freq_test2 = 90;
freq_test1 = 50;
freq_test2 = 75;
else
freq_test1 = 50:5:75;
freq_test2 = 70:5:95;
end % SW_ONE_SIMU_TEST
% Define the figure numbers
FIG_NUMBER_STEP_CHANGE_DIST = [120;121;122];
FIG_NUMBER2_STEP_CHANGE_DIST = [125;126;127];
FIG_NUMBER_CONST_DIST_FREQ = 100:100-1+length(freq_test1);
FIG_NUMBER2_CONST_DIST_FREQ = 150:150-1+length(freq_test1);
FIG_NUMBER3_CONST_DIST_FREQ = 250:250-1+length(freq_test1);
FIG_NUMBER_CHIRP_DIST = 200:202;
FIG_NUMBER2_CHIRP_DIST = 300:302;
%% Run the test
if SW_UNIFORM_ADAP
adap_init.SW_2Stage = 1;
adap_init.SW_lambda = 0;
lambda_end = 1;%0.999;
adap_init.lambda_end = lambda_end;
adap_init.alpha_pre = 0.98;
alpha_end = 0.88;
adap_init.alpha_end = alpha_end;
end
adap_init.lambda_gain = 280;
%% CONSTANT UNKONWN DISTURBANCE FREQUENCY
if FLAG_DIST_FREQ == FLAG_CONST_DIST_FREQ
if ~SW_UNIFORM_ADAP
lambda_end = 1;%0.992;
adap_init.lambda_end = lambda_end;
adap_init.SW_2Stage = 1;
adap_init.SW_lambda = 0;
end
data_cont_freq.readme = 'stores data in the case of constant disturbance frequencies';
data_cont_freq.y{1,1} = 'openLoop';
data_cont_freq.y{1,2} = 'closedLoop';
for ii = 1:length(freq_test1)
NBw = [freq_test1(ii)*2*pi;freq_test2(ii)*2*pi];
% true parameters (for result comparision later)
lb1true = 2*cos(NBw(1)*Ts);
lb2true = 2*cos(NBw(2)*Ts);
theta1_true = -(lb1true+lb2true);
theta2_true = 2+lb1true*lb2true;
% simulink parameter definition
% Narrow band disturbance injection time
t_NBon = 5;
% compensation turn on time
t_Qon = t_NBon;
t_AdapOn = t_NBon;
% narrow band disturbance duration
t_NBdur = 15/5;
t_AdapOff = t_AdapOn+t_NBdur*5;
% 2010-09-26; For disturbance generator v2
t_dur_lastDist = t_NBdur;
% freq in Hz
NBf = NBw/2/pi;
% simulink time
t_sim = 30;
f = NBf;
dist_seq1 = [NBf(1); NBf(2); 0];
dist_seq2 = [NBf(1); NBf(2); 0];
dist_seq3 = [NBf(1); NBf(2); 0];
for jj = 1:2
if jj == 1 % open loop
% run the test without compensation
SW_COMP_ON = 0; % compensation off
SW_CLOSE_LOOP = 0;
% define figure names for later use
fig_name_spec_residule = ...
['level2_spec_residule_',...
num2str(freq_test1(ii)),'_',...
num2str(freq_test2(ii)),...
'Hz_openLoop'];
fig_name_residule_time_trace = ...
['level2_time_trace_residule_',...
num2str(freq_test1(ii)),'_',...
num2str(freq_test2(ii)),...
'Hz_openLoop'];
else
% run the test with compensation
SW_COMP_ON = 1; % compensation on
SW_CLOSE_LOOP = 1;
fig_name_spec_residule = ...
['level2_spec_residule_',...
num2str(freq_test1(ii)),'_',...
num2str(freq_test2(ii)),...
'Hz_closedLoop'];
fig_name_residule_time_trace = ...
['level2_time_trace_residule_',...
num2str(freq_test1(ii)),'_',...
num2str(freq_test2(ii)),...
'Hz_closedLoop'];
end
%% simulation start
sim(simuName)
data_cont_freq.narrow_band_freq(ii,:) = NBf';
data_cont_freq.y{ii+1,jj} = y;
if ii == 1 && jj == 1
if SW_SAVE_DATA
if ~exist(['level2_test_result_',date],'dir')
mkdir(['level2_test_result_',date]);
end
end
end
level2_test_data_analysis_submit;
if SW_SAVE_DATA
try
movefile('*.fig',['level2_test_result_',date])
catch
end
end
end
pause(10); % let the CPU take a 10-sec rest
end
disp ('===================================================================')
disp ('test results saved to: level2_time_dom_result_const_freq')
disp (' level2_freq_dom_result_const_freq')
disp ('raw data saved to: data_cont_freq')
try
if SW_SAVE_DATA
save (['level2_test_result_',date,'\level2_time_dom_result_const_freq'],...
'level2_time_dom_result_const_freq');
save (['level2_test_result_',date,'\level2_freq_dom_result_const_freq'],...
'level2_freq_dom_result_const_freq');
save (['level2_test_result_',date,'\data_cont_freq'],...
'data_cont_freq');
end
catch
end
%% STEP CHANGE DISTURBANCE FREQUENCY
elseif FLAG_DIST_FREQ == FLAG_STEP_CHANGE_DIST_FREQ
if ~SW_UNIFORM_ADAP
adap_init.SW_2Stage = 1;
adap_init.SW_lambda = 2;
lambda_end = 0.999;
adap_init.lambda_end = lambda_end;
adap_init.alpha_pre = 0.98;
alpha_end = 0.88;
adap_init.alpha_end = alpha_end;
end
t_NBon = 5; % NB Dist injection time
t_Qon = t_NBon; % bandpass Q filter on time
t_AdapOn = t_NBon;
t_NBdur = 3;
t_AdapOff = t_AdapOn+5*t_NBdur;
t_sim = 40;
t_dur_lastDist = 3;
% the center frequencies of the three step changing disturbance
% frequencies
center_freq = [55, 75; % each row defines the one experiment group
70,90];
% the three step changing disturbance frquencies (in Hz)
% experiment 1
freq_seq1 = [55,60,55,50,55]; % data added in the example paper from Landau
freq_seq2 = [75,80,75,70,75];
% experiment 2
freq_seq3 = [70,75,70,65,70]; % data in the initial benchmark file
freq_seq4 = [90,95,90,85,90];
freq_table = [freq_seq1; freq_seq2; freq_seq3; freq_seq4;];
data_step_freq.readme = 'stores the data for the case of step changing disturbance frequencies';
data_step_freq.y{1,1} = 'openLoop';
data_step_freq.y{1,2} = 'closedLoop';
if SW_ONE_SIMU_TEST % perform just one test
ITER_STEP = 1;
% the center frequencies of the three step changing disturbance
% frequencies
center_freq = [55,75];
% the three step changing disturbance frquencies (in Hz)
freq_seq1 = [55,60,55,50,55]; % data added in the example paper from Landau
freq_seq2 = [75,80,75,70,75];
freq_table = [freq_seq1; freq_seq2];
else
ITER_STEP = 2;
end
for ii = 1:ITER_STEP
% define the three sets of test sequences
dist_seq1 = [center_freq(ii,1); center_freq(ii,2); 0];
dist_seq2 = [center_freq(ii,1)+5; center_freq(ii,2)+5; 0];
dist_seq3 = [center_freq(ii,1)-5; center_freq(ii,2)-5; 0];
for jj = 1:2
if jj == 1 % open loop
SW_COMP_ON = 0; % compensation off
SW_CLOSE_LOOP = 0;
fig_name_residule_time_trace = ...
['level2_time_trace_residule_center_freq_',...
num2str(center_freq(ii,1)),'_',num2str(center_freq(ii,2)),...
'Hz_openLoop'];
else
SW_COMP_ON = 1; % compensation on
SW_CLOSE_LOOP = 1;
fig_name_residule_time_trace = ...
['level2_time_trace_residule_center_freq_',...
num2str(center_freq(ii,1)),'_',num2str(center_freq(ii,2)),...
'Hz_closedLoop'];
end
%% simulation start
sim(simuName)
data_step_freq.initial_freq(ii,:) = dist_seq1';
data_step_freq.y{ii+1,jj} = y;
if ii == 1 && jj == 1
if SW_SAVE_DATA
if ~exist(['level2_test_result_',date],'dir')
mkdir(['level2_test_result_',date]);
end
end
end
%% time domain result
if jj == 2
level2_time_dom_result_step_change_freq.readme =...
{'row: each row represents one step-changing disturbance sequence',...
'col: from column 1 to column 5: transient for the 1st to 5th dist '};
for kk = 1:5
level2_time_dom_result_step_change_freq.transi_norm_square_3sec(ii,kk) = ...
sum(...
y.signals.values(...
(t_NBon+(kk-1)*t_NBdur)/Ts + 1 :...
(t_NBon+(kk-1)*t_NBdur+3)/Ts ).^2);
level2_time_dom_result_step_change_freq.max_residule(ii,kk) =...
max(...
y.signals.values(...
(t_NBon+(kk-1)*t_NBdur)/Ts :...
(t_NBon+(kk-1)*t_NBdur+1)/Ts )...
);
[temp_TD, temp_maximum, temp_transient_norm_square] =...
transient_duration_step_changes_additional_output(...
y.signals.values(1:t_AdapOff*Fs),...
Fs,t_AdapOff,t_NBon,t_NBdur,t_NBdur,'PlotOff',kk);
level2_time_dom_result_step_change_freq.t_transient(ii,kk) =...
temp_TD;
level2_time_dom_result_step_change_freq.transient_norm_square(ii,kk) =...
temp_transient_norm_square;
level2_time_dom_result_step_change_freq.maximum_transient(ii,kk) =...
temp_maximum;
end
h = figure;
plot(y.time,y.signals.values);grid;
xlabel('Time [sec]');ylabel('Residual force [V]');
figure_specific
if SW_SAVE_DATA
hgsave(h,fig_name_residule_time_trace,'-v6')
end
end
h = figure(FIG_NUMBER_STEP_CHANGE_DIST(ii));
grid on;hold on;
if jj == 1
plot(y.time,y.signals.values,'r');
else
plot(y.time,y.signals.values,'k:');
legend('open loop','closed loop')
xlabel('Time [sec]');ylabel('Residual force [V]');
figure_specific
if SW_SAVE_DATA
hgsave(h,['level2_time_trace_residule_center_freq_',...
num2str(center_freq(ii,1)),'_',num2str(center_freq(ii,2)),...
'Hz_compare'],'-v6')
end
end
h = figure(FIG_NUMBER2_STEP_CHANGE_DIST(ii));
hold on;
if jj == 1
subplot(211)
plot(y.time,y.signals.values,'r');
legend 'Open loop';
ylabel('Residual force [V]');
figure_specific
else
subplot(212)
plot(y.time,y.signals.values,'k');
legend 'Closed loop';
xlabel('Time [sec]');ylabel('Residual force [V]');
figure_specific
if SW_SAVE_DATA
hgsave(h,['level2_time_trace_residule_center_freq_',...
num2str(center_freq(ii,1)),'_',num2str(center_freq(ii,2)),...
'Hz_subplot_compare'],'-v6')
end
end
%% parameter convergence
if jj == 2
eta1vector = theta_hat.signals.values(:,1);%eta1 = -lambda1-lambda2
eta2vector = theta_hat.signals.values(:,2);%eta2 = 2+lambda1*lambda2
eta1 = eta1vector(end);
eta2 = eta2vector(end);
etaroot = sqrt(eta1vector.^2-4*(eta2vector-2));
lb1vector = (-eta1vector - etaroot)/2;% lambda = 2*cos(w*Ts)
lb2vector = (-eta1vector + etaroot)/2;
w1hat = abs(acos(lb1vector/2)/Ts);% Frequency in rad/s
w2hat = abs(acos(lb2vector/2)/Ts);
plottime = ones(length(theta_hat.time),1);
figure;
try
if SW_SAMPLE_PLOT
plot(theta_hat.signals.values);
xlabel('sample');
else
plot(theta_hat.time,theta_hat.signals.values);
xlabel('time [sec]');
end
catch
plot(theta_hat.time,theta_hat.signals.values);
xlabel('time [sec]');
end
ylabel('Estimated parameters')
grid on;
if SW_SAVE_DATA
hgsave(['level2_para_converge_step_change_center_',...
num2str(center_freq(ii,1)),'_',num2str(center_freq(ii,2)),...
'Hz'],'-v6')
end
% parameter convergence (frequency perspective)
figure;
try
if SW_SAMPLE_PLOT
plot(w1hat/2/pi,'r')
hold on
plot(w2hat/2/pi,'k')
xlabel('sample');
else
plot(theta_hat.time,w1hat/2/pi,'r',...
theta_hat.time,w2hat/2/pi,'k')
xlabel('time (sec)');
end
catch
plot(theta_hat.time,w1hat/2/pi,'r',...
theta_hat.time,w2hat/2/pi,'k')
xlabel('time (sec)');
end
ylabel('Estimated frequency (Hz)');
grid on;
if SW_SAVE_DATA
hgsave(['level2_freq_converge_step_change_center_',...
num2str(center_freq(ii,1)),'_',num2str(center_freq(ii,2)),...
'Hz'],'-v6')
end
if SW_ADDITIONAL_PLOT
figure;
plot(Ffactor.time,Ffactor.signals.values);
ylabel('Forgetting factor');
xlabel('sec');
end
end
end
pause(10); % let the CPU take a 10-sec rest
end
level2_time_dom_result_step_change_freq.freq_table =...
freq_table;
disp ('===================================================================')
disp ('test results saved to: level2_time_dom_result_step_change_freq')
disp ('raw data saved to: data_step_freq')
if SW_SAVE_DATA
try
movefile('*.fig',['level2_test_result_',date])
catch
end
save(['level2_test_result_',date,'\level2_time_dom_result_step_change_freq'],...
'level2_time_dom_result_step_change_freq');
save (['level2_test_result_',date,'\data_step_freq'],...
'data_step_freq');
end
%% TEST FOR CHIRP DISTURBANCE
elseif FLAG_DIST_FREQ == FLAG_CHIRP_DIST
SW_CHIRP_DIST = 1;
SW_DIST_ON = 0;
if ~SW_UNIFORM_ADAP
adap_init.alpha_pre = 0.98;
alpha_end = 0.88;
adap_init.alpha_end = alpha_end;
lambda_end = 0.999;
adap_init.lambda_end = lambda_end;
adap_init.SW_lambda = 3;
adap_init.SW_2Stage = 1;
if 0
alpha_init = 0.93;
alpha_end = 0.945; % 0.78;
else
alpha_init = 0.92;
alpha_end = 0.92; % 0.78;
end
adap_init.alpha_init = alpha_init;
adap_init.alpha_end = alpha_end;
alpha = adap_init.alpha_end;
end
t_sim = chirp_dist.chirp_init_time+chirp_dist.chirp_dur_time*2+chirp_dist.freq2_dur_time*2;
t_NBon = 5;
t_Qon = t_NBon;
t_AdapOn = t_NBon;
t_AdapOff = t_sim;
t_dur_lastDist = 5;
% for consistency
t_NBdur = 0;
dist_seq1 = [50; 0; 0];
dist_seq2 = [65; 0; 0];
dist_seq3 = [80; 0; 0];
adap_init.F = 4e4*[1, 0; 0, 1];
data_chirp_freq.readme = 'stores the data for the case of chirp changing disturbance frequencies';
data_chirp_freq.y{1,1} = 'openLoop';
data_chirp_freq.y{1,2} = 'closedLoop';
if SW_ONE_SIMU_TEST % perform just one test
ITER_STEP = 1;
else
ITER_STEP = length(chirp_dist.freq1_seq)/2;
end
for ii = 1:ITER_STEP
chirp_dist.para1 = [chirp_dist.freq1_seq((ii-1)*2+1);...
chirp_dist.freq1_seq((ii-1)*2+2);...
0];
chirp_dist.para2 = [chirp_dist.freq2_seq((ii-1)*2+1);...
chirp_dist.freq2_seq((ii-1)*2+2);...
0];
for jj = 1:2
if jj == 1
SW_COMP_ON = 0; % compensation off
SW_CLOSE_LOOP = 0;
fig_name_residule_time_trace = ...
['level2_time_trace_residule_chirp_dist_',...
num2str(chirp_dist.freq1_seq(ii)),'To',...
num2str(chirp_dist.freq2_seq(ii)),...
'&',...
num2str(chirp_dist.freq1_seq(ii+1)),'To',...
num2str(chirp_dist.freq2_seq(ii+1)),...
'Hz_openLoop'];
else
SW_COMP_ON = 1; % compensation on
SW_CLOSE_LOOP = 1;
fig_name_residule_time_trace = ...
['level2_time_trace_residule_chirp_dist_',...
num2str(chirp_dist.freq1_seq(ii)),'To',...
num2str(chirp_dist.freq2_seq(ii)),...
'&',...
num2str(chirp_dist.freq1_seq(ii+1)),'To',...
num2str(chirp_dist.freq2_seq(ii+1)),...
'Hz_closedLoop'];
end
%% open simulinnk
sim(simuName)
data_chirp_freq.initial_freq(ii,:) = chirp_dist.para1';
data_chirp_freq.y{ii+1,jj} = y;
if ii == 1 && jj == 1
if SW_SAVE_DATA
if ~exist(['level2_test_result_',date],'dir')
mkdir(['level2_test_result_',date]);
end
end
end
%% time domain result
if jj == 2
level2_time_dom_result_chirp_freq.readme =...
{'col 1: init chirp freq 1;',...
'col 2: end chirp freq 1;',...
'col 3: chirp increase freq',...
'col 4: chirp decrease freq',...
'col 5: init chirp freq 2;',...
'col 6: end chirp freq 2;',...
};
level2_time_dom_result_chirp_freq.transient_norm(ii,1) = ...
chirp_dist.freq1_seq(ii);
level2_time_dom_result_chirp_freq.transient_norm(ii,2) = ...
chirp_dist.freq2_seq(ii);
level2_time_dom_result_chirp_freq.transient_norm(ii,3) = ...
sqrt(...
sum(...
y.signals.values(...
chirp_dist.chirp_init_time/Ts :...
(chirp_dist.chirp_init_time + chirp_dist.chirp_dur_time)/Ts ).^2)...
);
level2_time_dom_result_chirp_freq.transient_norm(ii,4) = ...
sqrt(...
sum(...
y.signals.values(...
(chirp_dist.chirp_init_time + chirp_dist.chirp_dur_time + 5)/Ts :...
(chirp_dist.chirp_init_time + chirp_dist.chirp_dur_time + 10)/Ts ).^2)...
);
level2_time_dom_result_chirp_freq.transient_norm(ii,5) = ...
chirp_dist.freq1_seq(ii+1);
level2_time_dom_result_chirp_freq.transient_norm(ii,6) = ...
chirp_dist.freq2_seq(ii+1);
level2_time_dom_result_chirp_freq.transient_norm_square(ii,1) = ...
chirp_dist.freq1_seq(ii);
level2_time_dom_result_chirp_freq.transient_norm_square(ii,2) = ...
chirp_dist.freq2_seq(ii);
level2_time_dom_result_chirp_freq.transient_norm_square(ii,3) = ...
level2_time_dom_result_chirp_freq.transient_norm(ii,3)^2;
level2_time_dom_result_chirp_freq.transient_norm_square(ii,4) = ...
level2_time_dom_result_chirp_freq.transient_norm(ii,4)^2;
level2_time_dom_result_chirp_freq.transient_norm_square(ii,5) = ...
chirp_dist.freq1_seq(ii+1);
level2_time_dom_result_chirp_freq.transient_norm_square(ii,6) = ...
chirp_dist.freq2_seq(ii+1);
level2_time_dom_result_chirp_freq.max_residule(ii,1) = ...
chirp_dist.freq1_seq(ii);
level2_time_dom_result_chirp_freq.max_residule(ii,2) = ...
chirp_dist.freq2_seq(ii);
level2_time_dom_result_chirp_freq.max_residule(ii,3) =...
max(y.signals.values(...
chirp_dist.chirp_init_time/Ts :...
(chirp_dist.chirp_init_time + chirp_dist.chirp_dur_time)/Ts ));
level2_time_dom_result_chirp_freq.max_residule(ii,4) =...
max(y.signals.values(...
(chirp_dist.chirp_init_time + chirp_dist.chirp_dur_time + 5)/Ts :...
(chirp_dist.chirp_init_time + chirp_dist.chirp_dur_time + 10)/Ts ));
level2_time_dom_result_chirp_freq.max_residule(ii,5) = ...
chirp_dist.freq1_seq(ii+1);
level2_time_dom_result_chirp_freq.max_residule(ii,6) = ...
chirp_dist.freq2_seq(ii+1);
end
h = figure;
plot(y.time,y.signals.values);grid;
xlabel('Time [sec]');ylabel('Residual force [V]');
figure_specific
if SW_SAVE_DATA
hgsave(h,fig_name_residule_time_trace,'-v6')
end
h = figure(FIG_NUMBER_CHIRP_DIST(ii));
grid on;hold on;
if jj == 1
plot(y.time,y.signals.values,'r');
else
plot(y.time,y.signals.values,'k:');
legend('open loop','closed loop')
xlabel('Time [sec]');ylabel('Residual force [V]');
figure_specific
if SW_SAVE_DATA
hgsave(h,['level2_time_trace_residule_chirp_dist_',...
num2str(chirp_dist.freq1_seq(ii)),'To',...
num2str(chirp_dist.freq2_seq(ii)),...
'&',...
num2str(chirp_dist.freq1_seq(ii+1)),'To',...
num2str(chirp_dist.freq2_seq(ii+1)),...
'Hz_compare'],'-v6')
end
end
h = figure(FIG_NUMBER2_CHIRP_DIST(ii));
hold on;
if jj == 1
subplot(211)
plot(y.time,y.signals.values,'r');
legend 'Open loop';
ylabel('Residual force [V]');
figure_specific
else
subplot(212)
plot(y.time,y.signals.values,'k');
legend 'Closed loop';
xlabel('Time [sec]');ylabel('Residual force [V]');
figure_specific
if SW_SAVE_DATA
hgsave(h,['level2_time_trace_residule_chirp_dist_',...
num2str(chirp_dist.freq1_seq(ii)),'To',...
num2str(chirp_dist.freq2_seq(ii)),...
'&',...
num2str(chirp_dist.freq1_seq(ii+1)),'To',...
num2str(chirp_dist.freq2_seq(ii+1)),...
'Hz_subplot_compare'],'-v6')
end
end
%% parameter convergence
if jj == 2
eta1vector = theta_hat.signals.values(:,1);%eta1 = -lambda1-lambda2
eta2vector = theta_hat.signals.values(:,2);%eta2 = 2+lambda1*lambda2
eta1 = eta1vector(end);
eta2 = eta2vector(end);
etaroot = sqrt(eta1vector.^2-4*(eta2vector-2));
lb1vector = (-eta1vector - etaroot)/2;% lambda = 2*cos(w*Ts)
lb2vector = (-eta1vector + etaroot)/2;
w1hat = abs(acos(lb1vector/2)/Ts);% Frequency in rad/s
w2hat = abs(acos(lb2vector/2)/Ts);
plottime = ones(length(theta_hat.time),1);
figure;
try
if SW_SAMPLE_PLOT
plot(theta_hat.signals.values);
xlabel('time [sec]');
else
plot(theta_hat.time,theta_hat.signals.values);
xlabel('time [sec]');
end
catch
plot(theta_hat.time,theta_hat.signals.values);
xlabel('time [sec]');
end
ylabel('Estimated parameters')
grid on;
xlim([0,t_sim])
if SW_SAVE_DATA
hgsave(['level2_para_converge_chirp_dist_',...
num2str(chirp_dist.freq1_seq(ii)),'To',...
num2str(chirp_dist.freq2_seq(ii)),...
'&',...
num2str(chirp_dist.freq1_seq(ii+1)),'To',...
num2str(chirp_dist.freq2_seq(ii+1)),...
'Hz'],'-v6')
end
% para convergence (frequency perspective)
figure;
try
if SW_SAMPLE_PLOT
plot(w1hat/2/pi,'r')
hold on;
plot(w2hat/2/pi,'k');
xlabel('sample');
else
plot(theta_hat.time,w1hat/2/pi,'r',...
theta_hat.time,w2hat/2/pi,'k');
xlabel('time (sec)');
end
catch
plot(theta_hat.time,w1hat/2/pi,'r',...
theta_hat.time,w2hat/2/pi,'k');
xlabel('time (sec)');
end
ylabel('Estimated frequency (Hz)');
grid on;
if SW_SAVE_DATA
hgsave(['level2_freq_converge_chirp_dist_',...
num2str(chirp_dist.freq1_seq(ii)),'To',...
num2str(chirp_dist.freq2_seq(ii)),...
'&',...
num2str(chirp_dist.freq1_seq(ii+1)),'To',...
num2str(chirp_dist.freq2_seq(ii+1)),...
'Hz'],'-v6')
end
end
end
pause(10); % let the CPU take a 10-sec rest
end
disp ('===================================================================')
disp ('test results saved to: level2_time_dom_result_chirp_freq')
disp ('raw data saved to: data_chirp_freq')
if SW_SAVE_DATA
try
movefile('*.fig',['level2_test_result_',date])
catch
end
save (['level2_test_result_',date,'\level2_time_dom_result_chirp_freq'],...
'level2_time_dom_result_chirp_freq');
save (['level2_test_result_',date,'\data_chirp_freq'],...
'data_chirp_freq');
end
end