-
Notifications
You must be signed in to change notification settings - Fork 22
/
BodePlotGui.m
1256 lines (1118 loc) · 49.4 KB
/
BodePlotGui.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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = BodePlotGui(varargin)
% BODEPLOTGUI Application M-file for BodePlotGui.fig
% FIG = BODEPLOTGUI launch BodePlotGui GUI.
% BODEPLOTGUI('callback_name', ...) invoke the named callback.
% Last Modified by GUIDE v2.5 17-Oct-2014 11:41:15
%Written by Erik Cheever (Copyright 2002)
%Contact: erik_cheever@swarthmore.edu
% Erik Cheever
% Dept. of Engineering
% Swarthmore College
% 500 College Avenue
% Swarthmore, PA 19081 USA
%This function acts as a switchyard for several callbacks. It also intializes
%the variables used by the GUI. Note that all variables are initialized here to
%default values. A brief description of each variable is included.
if (nargin == 0) || (isa(varargin{1},'tf')) %If no arguments, or first
fig = openfig(mfilename,'new');
handles = guihandles(fig); %Get handles structure.
initBodePlotGui(handles);
handles=guidata(handles.AsymBodePlot); %Reload handles after function call.
if nargout > 0 %If output argument is used, set it to figure.
varargout{1} = fig;
end
if ((nargin~=0) && (isa(varargin{1},'tf'))),
%Transfer function chosen.
handles.Sys=varargin{1}; %The variable Sys is the transfer function.
doBodeGUI(handles);
end
elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK
try
[varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard
catch
disp(lasterr);
end
end
% ------------------End of function BodePlotGui ----------------------
function initBodePlotGui(handles)
handles.IncludeString=[]; %An array of strings representing terms to include in the plot.
handles.ExcludeString=[]; %An array of strings representing terms to exclude from plot.
handles.IncElem=[]; %An array of indices of terms corresponding to their
% location in the IncludeString array.
handles.ExcElem=[]; %An array of indices of terms corresponding to their
% location in the ExcludeString array.
handles.FirstPlot=1; %This term is 1 the first time a plot is made. This lets
% Matlab do the original autoscaling. The scales are then
% saved and reused.
handles.MagLims=[]; %The limits on the magnitude plot determined by MatLab autoscaling.
handles.PhaseLims=[]; %The limits on the phase plot determined by MatLab autoscaling.
handles.LnWdth=2;
set(handles.LineWidth,'String',num2str(handles.LnWdth));
%Set the color of lines used in gray scale. The plotting functions
%cycle through these colors (and then cycle through the linestyles).
handles.Gray=[0.75 0.75 0.75; 0.5 0.5 0.5; 0.25 0.25 0.25];
handles.GrayZero=[0.9 0.9 0.9]; %This is the color used for the zero reference.
%Set the color of lines used in color plots. The plotting functions
%cycle through these colors (and then cycle through the linestyles).
handles.Color=[0 1 1; 0 0 1; 0 1 0; 1 0 0; 1 0 1;1 0.52 0.40];
handles.ColorZero=[1 1 0]; %Yellow, this is the color used for the zero reference.
%Sets order of linestyles used.
handles.linestyle={':','--','-.'};
%This sets the default scheme to color (GUI can set them to gray scale).
handles.colors=handles.Color;
handles.zrefColor=handles.ColorZero;
handles.exactColor=[0 0 0]; %Black
handles.Sys=[];
handles.SysInc=[];
handles.Terms=[];
%The structure "Term" has three elements.
% type: this can be any of the 7 types listed below.
% 1) The multiplicative constant.
% 2) Real poles
% 3) Real zeros
% 4) Complex poles
% 5) Complex zeros
% 6) Poles at the origin
% 7) Zeros at the origin
% value: this is the location of the pole or zero (or in the case
% of the multiplicative constant, its value).
% multiplicity: this gives the multiplicity of the pole or zero. It has
% no meaning in the case of the multiplicative constant.
%The variable "Acc" is a relative accuracy used to determine whether or not
%two poles (or zeros) are the same. Because Matlab uses an approximate
%technique to find roots of an equation, it is likely to give slightly
%different locations to identical roots.
handles.Acc=1E-3;
set(handles.TransferFunctionText,'String',...
{' ','No transfer function chosen',' '});
guidata(handles.AsymBodePlot, handles); %save changes to handles.
loadSystems(handles);
handles=guidata(handles.AsymBodePlot); %Reload handles after function call.
guidata(handles.AsymBodePlot, handles); %save changes to handles.
function simpleTF = makeSimple(origTF)
simpleTF=minreal(origTF); %Get minimum realization.
% Get numerator and denominator of two realizations. If their
% lengths are unequal, it means that there were poles and zeros that
% cancelled.
[n1,d1]=tfdata(simpleTF,'v');
[n2,d2]=tfdata(origTF,'v');
if (length(n1)~=length(n2)),
disp(' ');
disp(' ');
disp(' ');
disp('************Warning******************');
disp('Original transfer function was:');
origTF
disp('Some poles and zeros were equal. After cancellation:');
simpleTF
disp('The simplified transfer function is the one that will be used.');
disp('*************************************');
disp(' ');
beep;
waitfor(warndlg('System has poles and zeros that cancel. See Command Window for caveats.'));
end
function doBodeGUI(handles)
handles.Sys=makeSimple(handles.Sys);
handles.SysInc=handles.Sys; %The variable sysInc is that part of the transfer
% function that will be plotted (with no poles or zeros excluded). Start with
% it equal to Sys. This variable is modified in BodePlotSys
%The function BodePlotTerms separates the transfer function into its consituent parts.
% The variable DoQuit will come back as non-zero if there was a problem.
DoQuit=BodePlotTerms(handles);
handles=guidata(handles.AsymBodePlot); %Reload handles after function call.
%if DoQuit is zero, there were no problems and we may continue.
if ~DoQuit,
BodePlotter(handles); %Make plot
handles=guidata(handles.AsymBodePlot); %Reload handles after function call.
%DoQuit was non-zero, so there was a problem. Quit program.
else
CloseButton_Callback(fig,'',handles,'');
end
% --------------------------------------------------------------------
function varargout = IncludedElements_Callback(~, ~, handles, varargin)
% Stub for Callback of the uicontrol handles.IncludedElements.
% If a term in the "Included Elements" box is clicked, this callback is invoked.
%Get index of element in box that is chosen.%If the index corresponds to one of the terms of the transfer function, deal with it.
i=get(handles.IncludedElements,'Value');
% The alternative is that it corresponds to another string in the box (there is a blank
% line, a line with dashes "----" and a line instructing the user to click on an element
% to include it).
if i<=length(handles.IncElem)
TermsInd=handles.IncElem(i); %Get the index of the included element.
handles.Terms(TermsInd).display=0; %Set display to 0 (to exclude it)
guidata(handles.AsymBodePlot, handles); %save changes to handles.
BodePlotter(handles); %Plot the Transfer function.
handles=guidata(handles.AsymBodePlot); %Reload handles after function call.
end
% ------------------End of function IncludedElements_Callback --------
% --------------------------------------------------------------------
function varargout = ExcludedElements_Callback(~, ~, handles, varargin)
% Callback of the uicontrol handles.ExcludedElements.
% If a term in the "Excluded Elements" box is clicked, this callback is invoked.
i=get(handles.ExcludedElements,'Value'); %Get index of element in box that is chosen.
%If the index corresponds to one of the terms of the transfer function, deal with it.
% The alternative is that it corresponds to another string in the box (there is a blank
% line, a line with dashes "----" and a line instructing the user to click on an element
% to exclude it).
if i<=length(handles.ExcElem)
TermsInd=handles.ExcElem(i); %Get the index of the excluded element.
handles.Terms(TermsInd).display=1; %Set display to 1 (to include it)
guidata(handles.AsymBodePlot, handles); %save changes to handles.
BodePlotter(handles); %Plot the Transfer function.
handles=guidata(handles.AsymBodePlot); %Reload handles after function call.
end
% ------------------End of function ExcludedElements_Callback --------
% --------------------------------------------------------------------
function varargout = CloseButton_Callback(~, ~, handles, varargin)
% Callback for the uicontrol handles.CloseButton.
%This function closes the window, and displays a message.
disp(' '); disp('Asymptotic Bode Plotter closed.'); disp(' '); disp(' ');
delete(handles.AsymBodePlot);
% ------------------End of function CloseButton_Callback -------------
% --------------------------------------------------------------------
function DoQuit=BodePlotTerms(handles)
%This function takes a system and splits it up into terms that are plotted
%individually when making a Bode plot by hand (it finds
%1) The multiplicative constant.
%2) All real poles
%3) All real zeros
%4) All complex poles
%5) All complex zeros
%6) All poles at the origin
%7) All zeros at the origin
%
%In addition to finding the poles and zeros, it determines their multiplicity.
%If two poles or zeros are very close they are determined to be the same pole
%or zero.
sys=handles.Sys;
Acc=handles.Acc; %Relative accuracy.
[z,p,k]=zpkdata(sys,'v'); %Get pole and zero data.
%Find gain term.
[n,d]=tfdata(sys,'v');
k=n(max(find(n~=0)))/d(max(find(d~=0)));
term(1).type='Constant';
term(1).value=k;
term(1).multiplicity=1;
%Get all poles.
j=length(term);
for i=1:length(p),
term(j+i).value=p(i);
term(j+i).multiplicity=1;
term(j+i).type='Pole';
end
%Get all zeros.
j=length(term);
for i=1:length(z),
term(j+i).value=z(i);
term(j+i).multiplicity=1;
term(j+i).type='Zero';
end
%Check for multiplicity
for i=2:length(term),
for k=(i+1):length(term),
%Handle pole or zero at origin as special case.
if (term(i).value==0),
%Multiple pole or zero at origin.
if (term(k).value==0),
term(i).multiplicity=term(i).multiplicity+term(k).multiplicity;
%Set multiplicity of kth term to 0 to signify that it has been
% subsumed by term(i) (by increasing the ith term's multiplicity).
term(k).multiplicity=0;
end
%We know term is not at origin, so check for (approximate) equality
% Since we know this term is not at origin, we can divide by value.
elseif (abs((term(i).value-term(k).value)/term(i).value) < Acc),
term(i).multiplicity=term(i).multiplicity+term(k).multiplicity;
%Set multiplicity of kth term to 0 to signify that it has been
% subsumed by term(i) (by increasing the ith term's multiplicity).
term(k).multiplicity=0;
end
end
end
%Check for location of poles and zeros (and remove complex conjugates).
i=2;
while (i<=length(term)),
%If root is at origin, handle it separately
if (term(i).value==0),
term(i).type=['Origin' term(i).type];
%If imaginary part is sufficiently small...
elseif (abs(imag(term(i).value)/term(i).value)<Acc),
term(i).type=['Real' term(i).type]; %...Add "Real" to type
term(i).value=real(term(i).value); %...And set imaginary part=0
%If imaginary part is *not* small...
else
term(i).type=['Complex' term(i).type]; %...Add "Complex" to type
term(i+1).multiplicity=0; %...Remove complex conjugate
i=i+1; %...And skip conjugate.
end
i=i+1; %Go to next root.
end
%Remove all terms with multiplicity 0.
j=0;
for i=1:length(term),
if (term(i).multiplicity~=0)
j=j+1;
term(j)=term(i);
term(j).display=1;
end
end
term=term(1:j);
DoQuit=0;
%Check for poles or zeros in right half plane,
% or on imaginary axis. Poles and zeros at origin are OK.
if any(real(p)>0), %Poles in RHP.
beep;
waitfor(errordlg('System has poles with positive real part, cannot make plot.'));
DoQuit=1;
return;
end
if any(real(z)>0), %Zeros in RHP.
disp(' ');
disp(' ');
disp(' ');
disp('************Warning******************');
handles.Sys
disp('is a nonminimum phase system (zeros in right half plane).');
disp('The standard rules for phase do not apply.');
disp(' ');
disp('Also - The plots produced may be different than the Matlab Bode plot');
disp(' by a factor of 360 degrees. So though the graphs don''t look');
disp(' the same, they are equivalent');
disp(' ');
disp('Location(s) of zero(s):');
disp(z);
disp('*************************************');
disp(' ');
beep;
waitfor(warndlg('System has zeros with positive real part. See Command Window for caveats.'));
end
%Check for terms near imaginary axis, or multiple poles or zeros at origin.
for i=2:length(term),
if (term(i).value~=0),
if (abs(real(term(i).value)/term(i).value)<Acc),
disp(' ');
disp(' ');
disp(' ');
disp('************Warning******************');
handles.Sys
disp('has a pole or zero near, or on, the imaginary axis.');
disp('The plots may be inaccurate near that frequency.')
disp(' ');
disp('--------');
disp('Pole(s):');
disp(p)
disp('--------');
disp('Zero(s):');
disp(z);
disp('*************************************');
disp(' ');
disp(' ');
disp(' ');
beep;
waitfor(warndlg('System has poles or zeros with real part near zero. See Command Window.'));
end
elseif (term(i).multiplicity>1),
disp(' ');
disp(' ');
disp(' ');
disp('************Warning******************');
handles.Sys
disp('has multiple poles or zeros at the origin.');
disp('Components of the phase plot may appear to disagree.');
disp('This is because the phase of a complex number is');
disp('not unique; the phase of -1 could be +180 degrees');
disp('or -180 or +/-540... Likewise the phase of 1/s^2');
disp('could be +180 degrees, or -180 degrees (or +/-540');
disp('Keep this in mind when looking at the phase plots.');
disp('*************************************');
disp(' ');
beep;
waitfor(warndlg('System has multiple poles or zeros at origin. See Command Window.'));
end
end
handles.Terms=term;
guidata(handles.AsymBodePlot, handles); %save changes to handles.
% ------------------End of function BodePlotTerms --------------------
% --------------------------------------------------------------------
function BodePlotter(handles)
%Get the constituent terms and the system itself.
Terms=handles.Terms;
sys=handles.Sys;
%Call function to get a system with only included poles and zeros.
BodePlotSys(handles);
handles=guidata(handles.AsymBodePlot); %Reload handles after function call.
%Get systems with included poles and zeros.
sysInc=handles.SysInc;
%Find min and max freq.
MinF=realmax;
MaxF=-realmax;
for i=2:length(Terms),
if Terms(i).value~=0,
MinF=min(MinF,abs(Terms(i).value));
MaxF=max(MaxF,abs(Terms(i).value));
end
end
%If there is exclusively a pole or zero at origin, MinF and MaxF will
% not have changed. So set them arbitrarily to be near unity.
if MaxF==-realmax,
MinF=0.9;
MaxF=1.1;
end
%MinFreq is a bit more than an order of magnitude below lowest break.
MinFreq=10^(floor(log10(MinF)-1.01));
%MaxFreq is a bit more than an order of magnitude above highest break.
MaxFreq=10^(ceil(log10(MaxF)+1.01));
%Calculate 500 frequency points for plotting.
w=logspace(log10(MinFreq),log10(MaxFreq),1000);
%%%%%%%%%%%%%%%%%%%% Start Magnitude Plot %%%%%%%%%%
axes(handles.MagPlot);
cla;
%Plot line at 0 dB for reference.
semilogx([MinFreq MaxFreq],[0 0],...
'Color',handles.zrefColor,...
'LineWidth',1.5);
hold on;
%For each term, plot the magnitude accordingly.
%The variable mag_a has the combined asymptotic magnitude.
mag_a=zeros(size(w));
%The variable peakinds holds the indices at which peaks in underdamped
%responses occur.
peakinds=[];
for i=1:length(Terms),
if Terms(i).display,
switch Terms(i).type,
case 'Constant',
%A constant term is unchanging from beginning to end.
f=[MinFreq MaxFreq];
m=20*log10(abs([Terms(i).value Terms(i).value]));
semilogx(f,m,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
mag_a=mag_a+interp1(log(f),m,log(w)); %Build up asymptotic approx.
case 'RealPole',
%A real pole has a single break frequency and then
%decreases at 20 dB per decade (Or more if pole is multiple).
wo=-Terms(i).value;
f=[MinFreq wo MaxFreq];
m=-20*log10([1 1 MaxFreq/wo])*Terms(i).multiplicity;
semilogx(f,m,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
mag_a=mag_a+interp1(log(f),m,log(w)); %Build up asymptotic approx.
case 'RealZero',
%Similar to real pole, but increases instead of decreasing.
wo=abs(Terms(i).value);
f=[MinFreq wo MaxFreq];
m=20*log10([1 1 MaxFreq/wo])*Terms(i).multiplicity;
semilogx(f,m,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
mag_a=mag_a+interp1(log(f),m,log(w)); %Build up asymptotic approx.
case 'ComplexPole',
%A complex pole has a single break frequency and then
%decreases at 40 dB per decade (Or more if pole is multiple).
%There is also a peak value whose height and location are
%determined by the natural frequency and damping coefficient.
%We will plot a circle ('o') at the location of the peak.
wn=abs(Terms(i).value);
theta=atan(abs(imag(Terms(i).value)/real(Terms(i).value)));
zeta=cos(theta);
if (zeta < 0.5), %Show peaking if zeta<0.5
peak=2*zeta;
f=[MinFreq wn wn wn MaxFreq];
m=-20*log10([1 1 peak 1 (MaxFreq/wn)^2])*Terms(i).multiplicity;
semilogx(f,m,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
semilogx(wn,-20*log10(peak),'o','Color',GetLineColor(i,handles));
% Set up for interpolation (w/o peak)
f=[MinFreq wn MaxFreq];
m=-20*log10([1 1 (MaxFreq/wn)^2])*Terms(i).multiplicity;
mag_a=mag_a+interp1(log(f),m,log(w)); %Build up asymptotic approx.
%Find location closest to peak, and adjust its amplitude.
index=find(w>=wn,1,'first');
mag_a(index)=mag_a(index)-20*log10(peak);
peakinds=[peakinds index]; %Save this index.
else
f=[MinFreq wn MaxFreq];
m=-20*log10([1 1 (MaxFreq/wn)^2])*Terms(i).multiplicity;
semilogx(f,m,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
% Set up for interpolation (w/o peak)
mag_a=mag_a+interp1(log(f),m,log(w)); %Build up asymptotic approx.
end
case 'ComplexZero',
%Similar to complex pole, but increases instead of decreasing.
wn=abs(Terms(i).value);
theta=atan(abs(imag(Terms(i).value)/real(Terms(i).value)));
zeta=cos(theta);
if (zeta < 0.5), %Show peaking if zeta<0.5
peak=2*zeta;
f=[MinFreq wn wn wn MaxFreq];
m=20*log10([1 1 peak 1 (MaxFreq/wn)^2])*Terms(i).multiplicity;
semilogx(f,m,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
semilogx(wn,20*log10(peak),'o','Color',GetLineColor(i,handles));
% Set up for interpolation (w/o peak)
f=[MinFreq wn MaxFreq];
m=20*log10([1 1 (MaxFreq/wn)^2])*Terms(i).multiplicity;
mag_a=mag_a+interp1(log(f),m,log(w)); %Build up asymptotic approx.
%Find location closest to peak, and adjust its amplitude.
index=find(w>=wn,1,'first');
mag_a(index)=mag_a(index)+20*log10(peak);
peakinds=[peakinds index]; %Save this index.
else
f=[MinFreq wn MaxFreq];
m=20*log10([1 1 (MaxFreq/wn)^2])*Terms(i).multiplicity;
semilogx(f,m,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
% Set up for interpolation (w/o peak)
mag_a=mag_a+interp1(log(f),m,log(w)); %Build up asymptotic approx.
end
case 'OriginPole',
%A pole at the origin is a monotonically decreasing straigh line.
f=[MinFreq MaxFreq];
m=-20*log10([MinFreq MaxFreq])*Terms(i).multiplicity;
semilogx(f,m,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
mag_a=mag_a+interp1(log(f),m,log(w)); %Build up asymptotic approx.
case 'OriginZero',
%Similar to pole at origin, but increases instead of decreasing.
f=[MinFreq MaxFreq];
m=20*log10([MinFreq MaxFreq])*Terms(i).multiplicity;
semilogx(f,m,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
mag_a=mag_a+interp1(log(f),m,log(w)); %Build up asymptotic approx.
end
end
end
%Set the x-axis limits to the minimum and maximum frequency.
set(gca,'XLim',[MinFreq MaxFreq]);
[mg,ph,w]=bode(sysInc,w); %Calculate the exact bode plot.
semilogx(w,20*log10(mg(:)),...
'Color',handles.exactColor,...
'LineWidth',handles.LnWdth/2); %Plot it.
if (get(handles.ShowAsymptoticCheckBox,'Value')~=0),
semilogx(w,mag_a,...
'Color',handles.exactColor,...
'LineStyle',':',...
'LineWidth',handles.LnWdth); %Plot asymptotic approx.
semilogx(w(peakinds),mag_a(peakinds),'o','Color',handles.exactColor);
end
if handles.FirstPlot,
%If this is the first time, let Matlab do autoscaling, but save
%the y-axis limits so that they will be unchanged as the plot changes.
ylims=get(gca,'YLim')/20;
ylims(1)=min(-20,ceil(ylims(1))*20);
ylims(2)=max(20,floor(ylims(2))*20);
handles.MagLims=ylims;
else
%If this is not the first time, retrieve the old y-axis limits.
ylims=handles.MagLims;
end
set(gca,'YLim',ylims);
set(gca,'YTick',ylims(1):20:ylims(2)); %Make ticks every 20 dB.
ylabel('Magnitude - dB');
xlabel('Frequency - \omega, rad-sec^{-1}')
title('Magnitude Plot','color','b','FontWeight','bold');
if get(handles.GridCheckBox,'Value')==1,
grid on
end
hold off;
%%%%%%%%%%%%%%%%%%%% End Magnitude Plot %%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% Start Phase Plot %%%%%%%%%%
%Much of this section mirrors the previous section and is not commented.
%One difference is that phase is calculated explicitly, rather than use
%Matlab's "bode" command. Since phase is not unique (you can add or
%subtract multiples of 360 degrees) There were sometimes discrepancies
%between Matlab's phase calculations and mine
axes(handles.PhasePlot);
cla;
%Plot line at 0 degrees for reference.
semilogx([MinFreq MaxFreq],[0 0],...
'Color',handles.zrefColor,...
'LineWidth',1.5);
hold on;
%The variable phs_a has the combined asymptotic phase.
phs_a=zeros(size(w));
for i=1:length(Terms),
if Terms(i).display,
switch Terms(i).type,
case 'Constant',
f=[MinFreq MaxFreq];
if Terms(i).value>0,
p=[0 0];
else
p=[180 180];
end
if get(handles.RadianCheckBox,'Value')==1,
p=p/180;
end
semilogx(f,p,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
case 'RealPole',
wo=-Terms(i).value;
f=[MinFreq wo/10 wo*10 MaxFreq];
p=[0 0 -90 -90]*Terms(i).multiplicity;
if get(handles.RadianCheckBox,'Value')==1,
p=p/180;
end
semilogx(f,p,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
case 'RealZero',
if Terms(i).value>0, %Non-minimum phase
wo=Terms(i).value;
%Uncomment the next section to get agreement with Matlab plots.
%if Terms(1).value>0, %Choose 0 or 360 to agree with MatLab plots
% p=[0 0 0 0]; % (based on sign of constant term).
% else
% p=[360 360 360 360];
%end
p=[0 0 -90 -90]*Terms(i).multiplicity;
else
%Minimum phase
wo=-Terms(i).value;
p=[0 0 90 90]*Terms(i).multiplicity;
end
f=[MinFreq wo/10 wo*10 MaxFreq];
if get(handles.RadianCheckBox,'Value')==1,
p=p/180;
end
semilogx(f,p,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
case 'ComplexPole',
wo=abs(Terms(i).value);
bf=0.1^zeta;
f=[MinFreq wo*bf wo/bf MaxFreq];
p=[0 0 -180 -180]*Terms(i).multiplicity;
if get(handles.RadianCheckBox,'Value')==1,
p=p/180;
end
semilogx(f,p,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
case 'ComplexZero',
wo=abs(Terms(i).value);
bf=0.1^zeta;
f=[MinFreq wo*bf wo/bf MaxFreq];
if real(Terms(i).value)>0, %Non-minimum phase
p=[0 0 -180 -180]*Terms(i).multiplicity;
else
p=[0 0 180 180]*Terms(i).multiplicity;
end
if get(handles.RadianCheckBox,'Value')==1,
p=p/180;
end
semilogx(f,p,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
case 'OriginPole',
f=[MinFreq MaxFreq];
p=[-90 -90]*Terms(i).multiplicity;
if get(handles.RadianCheckBox,'Value')==1,
p=p/180;
end
semilogx(f,p,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
case 'OriginZero',
f=[MinFreq MaxFreq];
p=[90 90]*Terms(i).multiplicity;
if get(handles.RadianCheckBox,'Value')==1,
p=p/180;
end
semilogx(f,p,...
'LineWidth',handles.LnWdth,...
'LineStyle',GetLineStyle(i,handles),...
'Color',GetLineColor(i,handles));
end
phs_a=phs_a+interp1(log(f),p,log(w)); %Build up asymptotic approx.
end
end
if get(handles.RadianCheckBox,'Value')==1,
ph=ph/180;
end
semilogx(w,ph(:),...
'Color',handles.exactColor,...
'LineWidth',handles.LnWdth/2); %Plot it.
if (get(handles.ShowAsymptoticCheckBox,'Value')~=0),
% There can be discrepancies between Matlabs calculation of phase and
% the quantity "phs_a." This is because phase is not unique (you can
% add or subtract multiples of 360 degrees). The next line shifts the
% value of phs_a so that phs_a(1)=ph(1). Ensuring they are the same
% at the beginning of the plot ensures that they align elsewhere. Note
% that if the plots are already aligned (ph(1)=phs_a(1)), that the next
% line does nothing.
phs_a=phs_a+(ph(1)-phs_a(1));
semilogx(w,phs_a,...
'Color',handles.exactColor,...
'LineStyle',':',...
'LineWidth',handles.LnWdth); %Plot asymptotic approx.
end
set(gca,'XLim',[MinFreq MaxFreq]);
if handles.FirstPlot,
ylims=get(gca,'YLim')/45;
ylims(1)=ceil(ylims(1)-1)*45;
ylims(2)=floor(ylims(2)+1)*45;
handles.DPhaseLims=ylims; %Find phase limits in degrees
handles.RPhaseLims=ylims/180; %Find phase limits in radians/pi
else
if get(handles.RadianCheckBox,'Value')==1,
ylims=handles.RPhaseLims;
else
ylims=handles.DPhaseLims;
end
end
if get(handles.RadianCheckBox,'Value')==1,
set(gca,'YLim',ylims);
set(gca,'YTick',ylims(1):0.25:ylims(2))
ylabel('Phase - radians/\pi');
else
set(gca,'YLim',ylims);
set(gca,'YTick',ylims(1):45:ylims(2))
ylabel('Phase - degrees');
end
xlabel('Frequency - \omega, rad-sec^{-1}');
title('Phase Plot','color','b','FontWeight','bold');
if get(handles.GridCheckBox,'Value')==1,
grid on
end
hold off;
%%%%%%%%%%%%%%%%%%%% End Phase Plot %%%%%%%%%%
BodePlotDispTF(handles); %Display the transfer function
BodePlotLegend(handles); %Display the legend.
handles=guidata(handles.AsymBodePlot); %Reload handles after function call.
%Set first plot to zero (so Matlab won't autoscale on subsequent calls).
handles.FirstPlot=0;
guidata(handles.AsymBodePlot, handles); %save changes to handles.
% ----------------- End of function BodePlotter ----------------------
% --------------------------------------------------------------------
function BodePlotSys(handles)
%This function makes up a transfer function of all the terms that are not in
%the "Excluded Elements" box in the GUI.
Terms=handles.Terms; %Get all terms from original transfer function.
p=[]; %Start with no poles, or zeros, and a constant of 1
z=[];
k=1;
for i=1:length(Terms), %For each term.
%If the term is not in "Excluded Elements". then we want to display it.
if Terms(i).display,
switch Terms(i).type,
case 'Constant',
k=Terms(i).value; %This is the constant.
case 'RealPole',
for j=1:Terms(i).multiplicity,
p=[p Terms(i).value]; %Add poles.
end
case 'RealZero',
for j=1:Terms(i).multiplicity,
z=[z Terms(i).value]; %Add zeros.
end
case 'ComplexPole',
for j=1:Terms(i).multiplicity,
p=[p Terms(i).value]; %Add poles.
p=[p conj(Terms(i).value)];
end
case 'ComplexZero',
for j=1:Terms(i).multiplicity,
z=[z Terms(i).value]; %Add zeros.
z=[z conj(Terms(i).value)];
end
case 'OriginPole',
for j=1:Terms(i).multiplicity,
p=[p 0]; %Add poles.
end
case 'OriginZero',
for j=1:Terms(i).multiplicity,
z=[z 0]; %Add zeros.
end
end
end
end
%Determine multiplicative constant in standard Bode Plot form.
for i=1:length(p),
if p(i)~=0
k=-k*p(i);
end
end
for i=1:length(z),
if z(i)~=0
k=-k/z(i);
end
end
%If poles and/or zeros were complex conjugate pairs, there may be
%some residual imaginary part due to finite precision. Remove it.
k=real(k);
handles.SysInc=zpk(z,p,k);
guidata(handles.AsymBodePlot, handles); %save changes to handles.
% ------------------End of function BodePlotSys ----------------------
% --------------------------------------------------------------------
function BodePlotDispTF(handles)
% This function displays a tranfer function that is a helper function for the
% BodePlotGui routine. It takes the transfer function of the numerator and
% splits it into three lines so that it can be displayed nicely. For example:
% " s + 1"
% "H(s) = ---------------"
% " s^2 + 2 s + 1"
%
% The numerator string is in the variable nStr,
% the second line is in divStr,
% and the denominator string is in dStr.
% Get numerator and denominator.
[n,d]=tfdata(handles.SysInc,'v');
% Get string representations of numerator and denominator
nStr=poly2str(n,'s');
dStr=poly2str(d,'s');
% Find length of strings.
LnStr=length(nStr);
LdStr=length(dStr);
if LnStr>LdStr,
%the numerator is longer than denominator string, so pad denominator.
n=LnStr; %n is the length of the longer string.
nStr=[' ' nStr]; %add spaces for characters at beginning of divStr.
dStr=[' ' blanks(floor((LnStr-LdStr)/2)) dStr]; %pad denominator.
else
%the demoninator is longer than numerator, pad numerator.
n=LdStr;
nStr=[' ' blanks(floor((LdStr-LnStr)/2)) nStr];
dStr=[' ' dStr];
end
divStr=[];
for i=1:n,
divStr=[divStr '-'];
end
divStr=['H(s) = ' divStr];
set(handles.TransferFunctionText,'String',strvcat(nStr,divStr,dStr));
%Change type font and size.
set(handles.TransferFunctionText,'FontName','Courier New')
%set(handles.TransferFunctionText,'FontSize',10)
guidata(handles.AsymBodePlot, handles); %save changes to handles.
% ------------------End of function BodePlotDispTF -------------------
% --------------------------------------------------------------------
function BodePlotLegend(handles)
%This function creates the legends for the Bode plot being displayed.
%It also makes four changes to the "handles" structure.
% 1) It updates the array "IncElem" that holds the indices that determine
% which elements are included in the Bode plot.
% 2) It updates the sring array "IncStr" that hold the description of
% each included elements.
% 3) Updates ExcElem that holds indices of excluded elements.
% 4) Updates ExcStr that hold descriptions of excluded elements.
%Load the terms and the plotting strings into local variables for convenience.
Terms=handles.Terms;
axes(handles.LegendPlot); %Set axes to the legend widow,
cla; % and clear it.
Xleg=[0 0.1]; %Xleg holds start and end of line segment for legend.
XlegText=0.125; %XlegText is location of text.
FntSz=8; %Font Size of text.
y=1-1/(length(Terms)+6); %Vertical location of first text item
plot(Xleg,[y y],...
'Color',handles.exactColor,...
'LineWidth',handles.LnWdth/2); %Plot line for legend.
text(XlegText,y,'Exact Bode Plot','FontSize',FntSz); %Place text
hold on;
if (get(handles.ShowAsymptoticCheckBox,'Value')~=0),
y=1-2/(length(Terms)+6); %Vertical location of second item
plot(Xleg,[y y],...
'Color',handles.exactColor,...
'LineStyle',':',...
'LineWidth',handles.LnWdth); %Plot line for legend.
text(XlegText,y,'Asymptotic Plot','FontSize',FntSz); %Place text
end
y=1-3/(length(Terms)+6); %Vertical location of third item.
plot(Xleg,[y y],... %Line.
'Color',handles.zrefColor,...
'LineWidth',2);
text(XlegText,y,'Zero Value (for reference only)','FontSize',FntSz); %Text.
IncElem=[]; %The indices of elements to be included in plot.
ExcElem=[]; %The indices of elements to be excluded from plot.
IncStr=''; %An array of strings describing included elements.
ExcStr=''; %An array of strings describing excluded elements.
%These variables are used as local counters later. Here they are initialized.
i1=1;
i2=1;
for i=1:length(Terms), %For each term,
%Tv is a local variable representing the pole location. It is used solely
% for convenience.
Tv=Terms(i).value;
%Tm is a local variable representing the pole multiplicity. It is used solely
% for convenience.
Tm=Terms(i).multiplicity;
%S2 is a blank string to be added to later in the loop.
S2='';
%The next section of code ("switch" statement) plus a few lines, creates
%a string that describes the pole or zero, its location, muliplicity...
%The variable "DescStr" hold a Descriptive String for the pole or zero. The
%string "S2" is a Second String that holds additional information (if needed)
switch Terms(i).type,
case 'Constant',
%If the term is a consant, print its value in a string.
DescStr=sprintf('Constant = %0.2g (%0.2g dB)',Tv,20*log10(abs(Tv)));
if Tv>=0,
DescStr=[DescStr ' phi=0'];
else
DescStr=[DescStr ' phi=180 (pi/2)'];
end;
case 'RealPole',
%If the term is a real pole, print its value in string.
DescStr=sprintf('Real Pole at %0.2g',Tv);
case 'RealZero',
%If the term is a real zero, print its value in string.
DescStr=sprintf('Real Zero at %0.2g',Tv);
if real(Tv)>0,
DescStr=[DescStr ' RHP (Non-min phase)'];
end;
case 'ComplexPole',
%If the term is a complex pole, print its value in string.
%However, do this in terms of natural frequency and damping, as
%well as the actual location of the pole (in S2).
wn=abs(Tv);
theta=atan(abs(imag(Tv)/real(Tv)));
zeta=cos(theta);
DescStr=sprintf('Complex Pole at wn=%0.2g, zeta=%0.2g',wn,zeta);
if (zeta < 0.5), %peaking only if zeta<0.5
S2=sprintf('(%0.2g +/- %0.2gj) Circle shows peak height.',real(Tv),imag(Tv));
else
S2=sprintf('(%0.2g +/- %0.2gj) (no peaking shown, zeta>0.5)',real(Tv),imag(Tv));
end
case 'ComplexZero',
%If the term is a complex zero, print its value in string.
%However, do this in terms of natural frequency and damping, as
%well as the actual location of the zero (in S2).