-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmac_GUI_addroot.m
937 lines (661 loc) · 24.5 KB
/
smac_GUI_addroot.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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
function [out,cormax,refmax]=smac_GUI_addroot(action,varargin)
% SMAC_GUI_ADDROOT Manually add a root
%
% [OUT,CORMAX,REFMAX]=SMAC_GUI_ADDROOT
%
% SMAC_GUI_ADDROOT provides a way for the user to manually add a root by
% performing frequency and damping fits around user-specified frequency and
% damping ranges. It is an iterative process where the user narrows in on
% frequency and damping ranges until a parabola curve-fit of the resulting
% correlation coefficient tightly overlays.
%
% OUT is a 1x2 vector containing the frequency, damping fraction, and
% average correlation coefficient (between the frequency and damping fits)
% for the root fit finally selected. If the user cancels the form, OUT
% will be empty. CORMAX is a vector of maximum correlation coefficients per
% reference. REFMAX is the reference giving the maximum correlation
% coefficient.
%==========================================================================
%
% ********************************************************************
% *** Copyright (C) 2008 Sandia Corporation. ***
% *** ***
% *** Under the terms of Contract DE-AC04-94AL85000 with Sandia ***
% *** Corporation, the U.S. Government retains certain rights in ***
% *** this software. ***
% ********************************************************************
%
% The contents of this file are subject to the Mozilla Public License
% Version 1.1 (the "License"); you may not use this file except in
% compliance with the License. You may obtain a copy of the License at
%
% http://www.mozilla.org/MPL/
%
% Software distributed under the License is distributed on an "AS IS"
% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
% License for the specific language governing rights and limitations under
% the License.
%
% The Original Code is SMAC.
%
% The Initial Developer of the Original Code is Randy Mayes.
% All Rights Reserved.
%
% Contributor(s): Dan Hensley / ATA Engineering.
%==========================================================================
% HISTORY
% 07-Jun-2004 / ATA Engineering / Dan Hensley
% o Initial creation
%
% 08-Jun-2004 / ATA Engineering / Dan Hensley
% o Add Help button
% o Pass out correlation coefficient as well
% o Don't allow invalid frequency and damping entries in the edit boxes
%
% 15-Jun-2004 / ATA Engineering / Dan Hensley
% o Fix Best Fit placement to better work on different resolutions
% o Show initial best fit frequency and damping as 0
%
% 06-Aug-2004 / ATA Engineering / Dan Hensley
% o Only update one fit at a time, not both
% o Add Calculate buttons to refit frequency or damping
%
% 15-Oct-2004 / ATA Engineering / Dan Hensley
% o Move damping fit buttons to just above damping plot
% o Suppress polynomial warnings
%
% 10-Nov-2004 / ATA Engineering / Dan Hensley
% o Update for changed autofit algorithms (loop over references)
% o Add output argument cormax
%
% 07-Jun-2005 / ATA Engineering / Dan Hensley
% o Use new GUI functions in uiWidgets
% o Fix bug when canceling Add root (output not assigned)
%
% 10-Sep-2005 / ATA Engineering / Dan Hensley
% o Change maximum damping value to 25%
%
% 22-Nov-2013 / ATA Engineering / Bill Fladung
% o Commented out one line that wasn't being used, but was causing an error.
%
%==========================================================================
% Check input arguments
if ~exist('action','var'),
action='';
end
% Register valid callbacks
bname='uicbAddRoot';
funcs.addroot='AddRoot';
funcs.dampingfit='DampingFit';
funcs.frequencyfit='FrequencyFit';
funcs.getdamprange='GetRange';
funcs.getfreqrange='GetRange';
funcs.calculate='Calculate';
funcs.zoomoutdamp='ZoomOutDamp';
funcs.zoomoutfreq='ZoomOutFreq';
funcs.lowdampedit='LowDampEdit';
funcs.highdampedit='HighDampEdit';
funcs.lowfreqedit='LowFreqEdit';
funcs.highfreqedit='HighFreqEdit';
% Evaluate the callback if we have one
if ~isempty(action)
if isfield(funcs,action),
fh=str2func(strcat(bname,funcs.(action)));
feval(fh,varargin{:});
return;
else
error('Unknown action ''%s''',action);
end
end
% Build the GUI
hf=uicbAddRootCreateFigure(varargin);
% Wait for the user to finish
uiwait(hf);
% Check for backup as well
if ~ishandle(hf),
out=[];
cormax=[];
refmax=[];
else
% Extract the best-fit frequency and damping and pass those out
hfreq=getappdata(hf,'HandleBestFreq');
hdamp=getappdata(hf,'HandleBestDamp');
cc=[getappdata(hdamp,'CorrCoef') getappdata(hfreq,'CorrCoef')];
out=[str2num(get(hfreq,'String')) str2num(get(hdamp,'String'))/100 mean(cc)];
cormax=getappdata(hf,'CorMax');
refmax=getappdata(hf,'RefMax');
% Delete the figure and exit
delete(hf);
end
%==========================================================================
% CALLBACKS
%==========================================================================
function hf=uicbAddRootCreateFigure(varargin)
% Create the Correlation Coefficients form
global ss;
ui=uiWidgets;
uip=ui.GetParams();
hf = figure(...
'Color',uip.Form.Color, ...
'NumberTitle','off', ...
'MenuBar','none', ...
'ToolBar','none', ...
'Resize','on', ...
'Name','SMAC Manual Add Root', ...
'BackingStore','on', ...
'DoubleBuffer','on', ...
'Tag','SMAC_AddRootForm', ...
'Visible','off');
hf=handle(hf);
setappdata(hf,'Backup',false);
%--------------------------------------------------------------------------
% Add Add Root button
hb=ui.CreateButton(hf,'Add Root',[170 40],uip.Gap*[1 1]+[50 0]);
hb.Callback='uiresume(gcbf)';
hb.TooltipString='Add the current root to the list';
%uiFitText(hb);
hb1=hb;
% Add damping axis
scr=get(0,'ScreenSize');
scr=scr(3:4);
asize=[800 300].*scr./[1600 1200]; % Resize based on 1600x1200 resolution
ha(1)=ui.CreateAxis(hf,'Damping',asize,'top+gaptop',hb);
ha(1).Position(2)=ha.Position(2)+70;
setappdata(hf,'HandleAxisDamping',ha(1));
% Add Cancel button
hb=ui.CreateButton(hf,'Cancel',[170 40],'right+gapright',hb);
hb.Callback='delete(gcbf)';
hb.TooltipString='Cancel without adding the current root';
% Add Damping Fit text
hr(1)=ui.CreateText(hf,'Damping Fit',[],'top+gaptop+gaptop',ha(1));
hr(1).TooltipString='Perform a fit of the damping value over the specified damping range';
% Add frequency axis
ha(2)=ui.CreateAxis(hf,'Frequency',asize,'top+gaptop',hr(1));
ha(2).Position(2)=ha(2).Position(2)+50;
setappdata(hf,'HandleAxisFrequency',ha(2));
%--------------------------------------------------------------------------
% Add Damping Fit and Frequency Fit rows
hr(2)=ui.CreateText(hf,'Frequency Fit',[],'top+gaptop+gaptop',ha(2));
hr(2).TooltipString='Perform a fit of the frequency value over the specified frequency range';
% Resize the width of both to the maximum
maxwidth=max(hr(1).Position(3),hr(2).Position(3));
hr(1).Position(3)=maxwidth;
hr(2).Position(3)=maxwidth;
%-------------------------------------------
% Add the rest of the damping controls
hb=ui.CreateButton(hf,'Select',[],'right+gapright',hr(1));
hb.Callback='smac_GUI_addroot(''getdamprange'',gcbf,''Damp'')';
hb.TooltipString='Select damping range from the plot';
he=ui.CreateEdit(hf,num2str(ss.fit.zeta(1)*100),[],'right+gapright',hb);
he.Callback='smac_GUI_addroot(''lowdampedit'',gcbf,gcbo)';
he.TooltipString='Enter starting damping percentage';
setappdata(hf,'HandleEditDampLow',he);
ht=ui.CreateText(hf,'-',[],'right+gapright',he);
he=ui.CreateEdit(hf,num2str(ss.fit.zeta(2)*100),[],'right+gapright',ht);
he.Callback='smac_GUI_addroot(''highdampedit'',gcbf,gcbo)';
he.TooltipString='Enter ending damping percentage';
setappdata(hf,'HandleEditDampHigh',he);
ht=ui.CreateText(hf,'%',[],'right+gapright',he);
hbz(1)=ui.CreateButton(hf,'Zoom Out',[],'right+gapright',ht);
hbz(1).Callback='smac_GUI_addroot(''zoomoutdamp'',gcbf)';
hbz(1).TooltipString='Double the current damping range';
ui.FitText(hbz(1),[0 inf]);
hbc(1)=ui.CreateButton(hf,'Calculate',[],'right+gapright',hbz(1));
hbc(1).Callback='smac_GUI_addroot(''calculate'',''Damp'',gcbf)';
hbc(1).TooltipString='Recalculate the damping using the current range';
hbc(1).Enable='off';
ui.FitText(hbc(1),[0 inf]);
% Remember the last entered values
setappdata(hf,'DampingOld',100*ss.fit.zeta(:).');
%-------------------------------------------
% Add the rest of the frequency controls
hb=ui.CreateButton(hf,'Select',[],'right+gapright',hr(2));
hb.Callback='smac_GUI_addroot(''getfreqrange'',gcbf,''Freq'')';
hb.TooltipString='Select frequency range from the plot';
he=ui.CreateEdit(hf,num2str(ss.freqrangecc(1,1)),[],'right+gapright',hb);
he.Callback='smac_GUI_addroot(''lowfreqedit'',gcbf,gcbo)';
he.TooltipString='Enter starting frequency';
setappdata(hf,'HandleEditFreqLow',he);
ht=ui.CreateText(hf,'-',[],'right+gapright',he);
he=ui.CreateEdit(hf,num2str(ss.freqrangecc(2,1)),[],'right+gapright',ht);
he.Callback='smac_GUI_addroot(''highfreqedit'',gcbf,gcbo)';
he.TooltipString='Enter ending frequency';
setappdata(hf,'HandleEditFreqHigh',he);
ht=ui.CreateText(hf,'Hz',[],'right+gapright',he);
hbz(2)=ui.CreateButton(hf,'Zoom Out',[],'right+gapright',ht);
hbz(2).Callback='smac_GUI_addroot(''zoomoutfreq'',gcbf)';
hbz(2).TooltipString='Double the current frequency range';
ui.FitText(hbz(2),[0 inf]);
hbc(2)=ui.CreateButton(hf,'Calculate',[],'right+gapright',hbz(2));
hbc(2).Callback='smac_GUI_addroot(''calculate'',''Freq'',gcbf)';
hbc(2).TooltipString='Recalculate the frequency using the current range';
hbc(2).Enable='off';
ui.FitText(hbc(2),[0 inf]);
setappdata(hf,'HandleCalculate',hbc);
% Remember the last entered values
setappdata(hf,'FrequencyOld',ss.freqrangecc(:,1).');
% Move the two zoom buttons so they are aligned
posmax=max(hbz(1).Position(1),hbz(2).Position(1));
hbz(1).Position(1)=posmax;
hbz(2).Position(1)=posmax;
% Update the Calculate button positions
hbc(1).Position(1)=posmax+hbz(1).Position(3)+uip.Gap;
hbc(2).Position(1)=posmax+hbz(1).Position(3)+uip.Gap;
%--------------------------------------------------------------------------
% Add Best Fit panel
hfr=ui.CreatePanel(hf,' Best Fit',[400 100],'right+gapright+gapright+gapright+gapright',hbc(2));
he=ui.CreateEdit(hfr,'0',[],[uip.Gap uip.Gap]);
he.Enable='inactive';
he.BackgroundColor='yellow';
setappdata(hf,'HandleBestDamp',he);
setappdata(he,'CorrCoef',0);
ht=ui.CreateText(hfr,'%',[],'right+gapright',he);
% hl(end+1)=ht; WAF131122: This is not used and was causing Randy Mayes an error.
he=ui.CreateEdit(hfr,'0',[],'top+gaptop',he);
he.Enable='inactive';
he.BackgroundColor='yellow';
setappdata(hf,'HandleBestFreq',he);
setappdata(he,'CorrCoef',0);
ht=ui.CreateText(hfr,'Hz',[],'right+gapright',he);
ui.ResizeControl(hfr);
% Shrink the axes to the Best Fit width
ha(1).Position(3)=hfr.Position(1)+hfr.Position(3)-ha(1).Position(1);
ha(2).Position(3)=hfr.Position(1)+hfr.Position(3)-ha(2).Position(1);
%--------------------------------------------------------------------------
% Store the FRF data with the figure (for performance reasons)
wr=2*pi*ss.fe(1).abscissa(ss.freqrange(1,2):ss.freqrange(2,2)).';
frf=ss.fe.ordinate(ss.freqrange(1,2):ss.freqrange(2,2),:);
frf=reshape(frf,[size(frf,1) size(ss.fe)]);
setappdata(hf,'Abscissa',wr);
setappdata(hf,'Ordinate',frf);
% Fill in the fit data with correlation coefficient information
xf=ss.fe(1).abscissa(ss.freqrangecc(1,2):ss.freqrangecc(2,2));
setappdata(hf,'FreqFitData',[xf(:) ss.corr.corr(:,1) zeros(size(ss.corr.corr(:,1)))]);
uicbAddRootUpdatePlot(hf,'Freq');
setappdata(hf,'DampFitData',[ss.fit.zeta(:) zeros(length(ss.fit.zeta(:)),2)]);
uicbAddRootUpdatePlot(hf,'Damp');
%--------------------------------------------------------------------------
% Make sure the figure encompasses all of the controls
ui.ResizeFigure(hf);
%--------------------------------------------------------------------------
% Add a Help button
hb=ui.CreateButton(hf,'HELP',[],'right',hb1);
hb.Callback='smac_helper(''mfit_hlp'')';
hb.TooltipString='Get help on the Correlation Coefficient calculations';
hb.Position(1)=ha(1).Position(1)+ha(1).Position(3)-hb.Position(3);
% Make all uicontrols normalized
ui.SetUnits(hf,'normalized');
hf.Visible='on';
%==========================================================================
function hf=uicbAddRootGetRange(varargin)
% Get the damping range and perform a damping fit
global ss;
hf=varargin{1};
type=varargin{2};
switch type
case 'Freq'
t{1}='Frequency';
t{2}='Damping';
case 'Damp'
t{1}='Damping';
t{2}='Frequency';
end
% Make the Calculate buttons active
hbc=getappdata(hf,'HandleCalculate');
set(hbc,'Enable','on');
% Make sure only the plot we want can be selected
ha=getappdata(hf,['HandleAxis' t{1}]);
set(ha,'HitTest','on','HandleVisibility','on');
set(hf,'CurrentAxes',ha);
ha=getappdata(hf,['HandleAxis' t{2}]);
set(ha,'HitTest','off','HandleVisibility','off');
% Prompt for the range to select
[x,~]=ginput(2);
x=sort(x);
% Set the edit boxes appropriately
he(1)=getappdata(hf,['HandleEdit' type 'Low']);
he(2)=getappdata(hf,['HandleEdit' type 'High']);
set(he(1),'String',num2str(x(1)));
set(he(2),'String',num2str(x(2)));
% Perform the fits
switch type
case 'Freq',
uicbAddRootFreqFit(hf);
case 'Damp',
uicbAddRootDampFit(hf);
end
%==========================================================================
function hf=uicbAddRootZoomOutDamp(varargin)
% Zoom out on the damping axis
global ss;
hf=varargin{1};
he(1)=getappdata(hf,'HandleEditDampLow');
he(2)=getappdata(hf,'HandleEditDampHigh');
% Bump the damping range by half the difference
zeta=[str2num(get(he(1),'String')) str2num(get(he(2),'String'))];
dz=diff(zeta)/2;
zeta=zeta+dz*[-1 1];
% Make sure we have sane values
zeta(zeta<=0)=.001;
zeta(zeta>25)=25;
% Put these values back into the form
set(he(1),'String',num2str(zeta(1)));
set(he(2),'String',num2str(zeta(2)));
% Perform the fits
%uicbAddRootFreqFit(hf);
uicbAddRootDampFit(hf);
% Make the Calculate buttons active
hbc=getappdata(hf,'HandleCalculate');
set(hbc,'Enable','on');
%==========================================================================
function hf=uicbAddRootZoomOutFreq(varargin)
% Zoom out on the damping axis
global ss;
hf=varargin{1};
he(1)=getappdata(hf,'HandleEditFreqLow');
he(2)=getappdata(hf,'HandleEditFreqHigh');
% Bump the frequency range by half the difference
freq=[str2num(get(he(1),'String')) str2num(get(he(2),'String'))];
df=diff(freq)/2;
freq=freq+df*[-1 1];
% Find the closest spectral line to the selected range
% FIXME: No, don't do it this way
freq(freq<ss.freqrangecc(1,1))=ss.freqrangecc(1,1);
freq(freq>ss.freqrangecc(2,1))=ss.freqrangecc(2,1);
% Put these values back into the form
set(he(1),'String',num2str(freq(1)));
set(he(2),'String',num2str(freq(2)));
% Perform the fits
uicbAddRootFreqFit(hf);
%uicbAddRootDampFit(hf);
% Make the Calculate buttons active
hbc=getappdata(hf,'HandleCalculate');
set(hbc,'Enable','on');
%==========================================================================
function hf=uicbAddRootLowDampEdit(varargin)
% Edit lower bound for damping
% FIXME: Get valid damping range from the structure
global ss;
hf=varargin{1};
he(1)=getappdata(hf,'HandleEditDampLow');
he(2)=getappdata(hf,'HandleEditDampHigh');
dampold=getappdata(hf,'DampingOld');
% Make sure the value is a valid number
if isempty(str2num(get(he(1),'String'))),
uiwait(errordlg('Invalid entry'));
set(he(1),'String',num2str(dampold(1)));
return;
end
% Get the damping values from the edit box
damp=[str2num(get(he(1),'String')) str2num(get(he(2),'String'))];
% Make sure the damping value makes sense
if damp(1)<=0 || damp(1)>25,
uiwait(errordlg('Unreasonable damping','Damping Value Error'));
damp(1)=dampold(1);
elseif damp(1)>damp(2),
uiwait(errordlg('Low value for damping must be lower than high value','Damping Value Error'));
damp(1)=dampold(1);
end
damp(damp<0)=0.001;
damp(damp>25)=25;
% Update the form
set(he(1),'String',num2str(damp(1)));
setappdata(hf,'DampingOld',damp);
% Perform the fits
%uicbAddRootFreqFit(hf);
uicbAddRootDampFit(hf);
% Make the Calculate buttons active
hbc=getappdata(hf,'HandleCalculate');
set(hbc,'Enable','on');
%==========================================================================
function hf=uicbAddRootHighDampEdit(varargin)
% Edit higher bound for damping
% FIXME: Get valid damping range from the structure
global ss;
hf=varargin{1};
he(1)=getappdata(hf,'HandleEditDampLow');
he(2)=getappdata(hf,'HandleEditDampHigh');
dampold=getappdata(hf,'DampingOld');
% Make sure the value is a valid number
if isempty(str2num(get(he(2),'String'))),
uiwait(errordlg('Invalid entry'));
set(he(2),'String',num2str(dampold(2)));
return;
end
% Get the damping values from the edit box
damp=[str2num(get(he(1),'String')) str2num(get(he(2),'String'))];
% Make sure the damping value makes sense
if damp(2)<=0 || damp(2)>25,
uiwait(errordlg('Unreasonable damping','Damping Value Error'));
damp(2)=dampold(2);
elseif damp(2)<=damp(1),
uiwait(errordlg('High value for damping must be higher than low value','Damping Value Error'));
damp(2)=dampold(2);
end
damp(damp<0)=0.001;
damp(damp>25)=25;
% Update the form
set(he(2),'String',num2str(damp(2)));
setappdata(hf,'DampingOld',damp);
% Perform the fits
%uicbAddRootFreqFit(hf);
uicbAddRootDampFit(hf);
% Make the Calculate buttons active
hbc=getappdata(hf,'HandleCalculate');
set(hbc,'Enable','on');
%==========================================================================
function hf=uicbAddRootLowFreqEdit(varargin)
% Edit lower bound for frequency
global ss;
hf=varargin{1};
he(1)=getappdata(hf,'HandleEditFreqLow');
he(2)=getappdata(hf,'HandleEditFreqHigh');
freqold=getappdata(hf,'FrequencyOld');
% Make sure the value is a valid number
if isempty(str2num(get(he(1),'String'))),
uiwait(errordlg('Invalid entry'));
set(he(1),'String',num2str(freqold(1)));
return;
end
% Get the damping values from the edit box
freq=[str2num(get(he(1),'String')) str2num(get(he(2),'String'))];
% Make sure the frequency value makes sense
if freq(1)<ss.freqrangecc(1,1),
uiwait(errordlg('Invalid frequency','Frequency Value Error'));
freq(1)=freqold(1);
elseif freq(1)>freq(2),
uiwait(errordlg('Low value for frequency must be lower than high value','Frequency Value Error'));
freq(1)=freqold(1);
end
freq(freq<ss.freqrangecc(1,1))=ss.freqrangecc(1,1);
freq(freq>ss.freqrangecc(2,1))=ss.freqrangecc(2,1);
% Update the form
set(he(1),'String',num2str(freq(1)));
setappdata(hf,'FrequencyOld',freq);
% Perform the fits
uicbAddRootFreqFit(hf);
%uicbAddRootDampFit(hf);
% Make the Calculate buttons active
hbc=getappdata(hf,'HandleCalculate');
set(hbc,'Enable','on');
%==========================================================================
function hf=uicbAddRootHighFreqEdit(varargin)
% Edit upper bound for frequency
global ss;
hf=varargin{1};
he(1)=getappdata(hf,'HandleEditFreqLow');
he(2)=getappdata(hf,'HandleEditFreqHigh');
freqold=getappdata(hf,'FrequencyOld');
% Make sure the value is a valid number
if isempty(str2num(get(he(2),'String'))),
uiwait(errordlg('Invalid entry'));
set(he(2),'String',num2str(freqold(2)));
return;
end
% Get the damping values from the edit box
freq=[str2num(get(he(1),'String')) str2num(get(he(2),'String'))];
% Make sure the frequency value makes sense
if freq(2)>ss.freqrangecc(2,1),
uiwait(errordlg('Invalid frequency','Frequency Value Error'));
freq(2)=freqold(2);
elseif freq(2)<=freq(1),
uiwait(errordlg('High value for frequency must be higher than low value','Frequency Value Error'));
freq(2)=freqold(2);
end
freq(freq<ss.freqrangecc(1,1))=ss.freqrangecc(1,1);
freq(freq>ss.freqrangecc(2,1))=ss.freqrangecc(2,1);
% Update the form
set(he(2),'String',num2str(freq(2)));
setappdata(hf,'FrequencyOld',freq);
% Perform the fits
uicbAddRootFreqFit(hf);
%uicbAddRootDampFit(hf);
% Make the Calculate buttons active
hbc=getappdata(hf,'HandleCalculate');
set(hbc,'Enable','on');
%==========================================================================
function hf=uicbAddRootCalculate(varargin)
% Recalculate the best fit given the current settings
global ss;
type=varargin{1};
hf=varargin{2};
% Perform the fit
switch type
case 'Freq',
uicbAddRootFreqFit(hf);
case 'Damp',
uicbAddRootDampFit(hf);
end
%==========================================================================
function hf=uicbAddRootFreqFit(varargin)
% Perform a frequency fit on the given range
global ss;
hf=varargin{1};
hef(1)=getappdata(hf,'HandleEditFreqLow');
hef(2)=getappdata(hf,'HandleEditFreqHigh');
hed(1)=getappdata(hf,'HandleEditDampLow');
hed(2)=getappdata(hf,'HandleEditDampHigh');
% Get the frequency range for the fit
fmm=[str2num(get(hef(1),'String')) str2num(get(hef(2),'String'))];
% Get the damping value for the fit
hbd=getappdata(hf,'HandleBestDamp');
dmm=str2num(get(hbd,'String'))/100;
if isempty(dmm),
dmm=mean([str2num(get(hed(1),'String')) str2num(get(hed(2),'String'))])/100;
end
if dmm==0, % Should only hit this the first time through
dmm=ss.corr.zeta;
end
numpts=ss.fit.freqpts;
Mcc=0; % Work around a debugger issue with mcc
nref=length(ss.ref_coords);
wr=getappdata(hf,'Abscissa');
frf=getappdata(hf,'Ordinate');
% Perform the auto fit
smac_ffit_cauto;
% Store the maximum correlation coefficients
setappdata(hf,'CorMax',cormax(1,:));
setappdata(hf,'RefMax',max_ref);
% Do a polynomial curve-fit of the data and evaluate the points
ws=warning;
warning off MATLAB:polyfit:RepeatedPointsOrRescale;
p=polyfit(fvec(:),Mcc(:),2);
z=polyval(p,fvec(:));
warning(ws);
% Get the best-fit root by finding where the slope is 0
bf=roots(polyder(p));
% Calculate the correlation coefficient at this value
cc=polyval(p,bf);
% Update the best fit frequency edit box
hbf=getappdata(hf,'HandleBestFreq');
set(hbf,'String',num2str(bf));
setappdata(hbf,'CorrCoef',cc);
% Store the curve-fit data
setappdata(hf,'FreqFitData',[fvec(:) Mcc(:) z(:)]);
% Update the frequency plot
uicbAddRootUpdatePlot(hf,'Freq');
%==========================================================================
function hf=uicbAddRootDampFit(varargin)
% Perform a damping fit on the given range
global ss;
hf=varargin{1};
hef(1)=getappdata(hf,'HandleEditFreqLow');
hef(2)=getappdata(hf,'HandleEditFreqHigh');
hed(1)=getappdata(hf,'HandleEditDampLow');
hed(2)=getappdata(hf,'HandleEditDampHigh');
% Get the frequency value for the fit
hbf=getappdata(hf,'HandleBestFreq');
fmm=str2num(get(hbf,'String'));
if isempty(fmm),
fmm=mean([str2num(get(hef(1),'String')) str2num(get(hef(2),'String'))]);
end
% Get the damping range for the fit
dmm=[str2num(get(hed(1),'String')) str2num(get(hed(2),'String'))]/100;
numpts=ss.fit.zetapts;
Mcc=0; % Work around a debugger issue with mcc
nref=length(ss.ref_coords);
wr=getappdata(hf,'Abscissa');
frf=getappdata(hf,'Ordinate');
% Perform the auto fit
smac_dfit_cauto;
% Store the maximum correlation coefficients
setappdata(hf,'CorMax',cormax(1,:));
setappdata(hf,'RefMax',max_ref);
% Do a polynomial curve-fit of the data and evaluate the points
ws=warning;
warning off MATLAB:polyfit:RepeatedPointsOrRescale;
p=polyfit(dvec(:),Mcc(:),2);
z=polyval(p,dvec(:));
warning(ws);
% Get the best-fit root by finding where the slope is 0
bd=roots(polyder(p));
% Calculate the correlation coefficient at this value
cc=polyval(p,bd);
% Update the best fit damping edit box
hbd=getappdata(hf,'HandleBestDamp');
set(hbd,'String',num2str(bd*100));
setappdata(hbd,'CorrCoef',cc);
% Store the curve-fit data
setappdata(hf,'DampFitData',[dvec(:) Mcc(:) z(:)]);
% Update the frequency plot
uicbAddRootUpdatePlot(hf,'Damp');
%==========================================================================
function hf=uicbAddRootUpdatePlot(varargin)
% Update the plot
global ss;
hf=varargin{1};
type=varargin{2};
switch type,
case 'Freq',
ha=getappdata(hf,'HandleAxisFrequency');
data=getappdata(hf,'FreqFitData');
xlab='Frequency (Hz)';
x=data(:,1);
case 'Damp',
ha=getappdata(hf,'HandleAxisDamping');
data=getappdata(hf,'DampFitData');
xlab='Damping (%)';
x=data(:,1)*100;
otherwise,
error('Internal error: Unknown plot type');
end
% Pull the data from the figure and update the plot
set(hf,'CurrentAxes',ha);
if ~isempty(data),
hp=get(ha,'Children');
if ~isempty(hp), % Figure already exists
hp=get(ha,'Children');
set(hp(1),'XData',x,'YData',data(:,2),'LineStyle','-','Color',[0 0 1]);
set(hp(2),'XData',x,'YData',data(:,3),'LineStyle','--','Color',[1 0 0]);
set(ha,'XLim',x([1 end]));
else % Draw new plots
set(hf,'CurrentAxes',ha);
hold on;
plot(x,data(:,2),'b-');
plot(x,data(:,3),'r--');
xlim(x([1 end]));
hold off;
xlabel(xlab);
ylabel('Correlation Coefficient');
end
end