forked from AUMAG/matlab-plot-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nicholscurvesx.m
151 lines (131 loc) · 4.52 KB
/
nicholscurvesx.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
function nicholscurves(varargin)
%%NICHOLSCURVES
% Add M-circles and N-circles to the current figure (assumed to be a nichols chart)
%
% Parameters:
% 'phasecyc' = [-a b] corresponds to [-360a 360b] on phase axis. Default is [-1 0].
% 'phaseinc' = CL phase increment for N-circles. Default is 30.
% 'labelsize' = Text size of M- and N-circle labels. Default is 10.
% 'gainarray' = Row vector containing the values of gain for which to plot
% M-circles. An entry of 0 is ignored as this causes Inf to
% appear. Default is [12 6 3 1 0.5 -0.5 -1 -3 -6 -10 -20 -40 -60].
% 'linewidth' = Thickness of the lines on the plot. Default is 1.
% 'frag' = true or false; whether to use matlabfrab to print labels.
% 'nlabels' = true or false; whether to label the N circles
% Copyright 2011 Will Robertson
% Copyright 2011 Philipp Allgeuer
%% Process input arguments
% Parse input arguments
p=inputParser;
p.addParamValue('phasecyc',[-1 0],@(x)(isnumeric(x)&&(numel(x)==2)&&(round(x(1))<round(x(2)))));
p.addParamValue('phaseinc',30,@(x)(isnumeric(x)&&(x>0)));
p.addParamValue('labelsize',10,@(x)(isnumeric(x)&&(x>=4)));
p.addParamValue('gainarray',[12 6 3 1 0.5 -0.5 -1 -3 -6 -10 -20 -40 -60],@(x)(isnumeric(x)));
p.addParamValue('linewidth',1,@(x)(isnumeric(x)&&(x>=0.1)&&(x<=5)));
p.addParamValue('frag',false);
p.addParamValue('nlabels',true);
p.parse(varargin{:});
% Save parsed arguments
PCyc=round(p.Results.phasecyc);
PInc=p.Results.phaseinc;
LSize=p.Results.labelsize;
Gains=p.Results.gainarray;
LWidth=p.Results.linewidth;
frag_bool = p.Results.frag;
nlabels_bool = p.Results.nlabels;
% Freeze current plot
hold_bool = false;
if ishold
hold_bool = true;
else
hold on;
end
%% Draw M-circles
% Define equations that determine the M-circles
RadM=@(m) abs(m/(m^2-1));
CentreM=@(m) m^2/(1-m^2);
Ny=@(mdb,t) CentreM(10^(mdb/20))+RadM(10^(mdb/20)).*(cosd(t)+1i.*sind(t));
Ni_Ph=@(mdb,t) rad2deg(unwrap(angle(Ny(mdb,t))));
Ni_Ga=@(mdb,t) 20.*log10(abs(Ny(mdb,t)));
% Generate the colour space
CalcRgb=@(mdb) hsv2rgb([((mdb-min(Gains))/(max(Gains)-min(Gains)))^3 0.5 0.8]);
if frag_bool
user_data = @(nn) ['matlabfrag:',...
'\fboxsep=0pt\colorbox{white}{$\,',...
num2str(nn),...
'$\,dB}'];
else
user_data = @(nn) '';
end
% Apply M-circle equations and plot the result
for i=Gains
PVals=Ni_Ph(i,0:360);
GVals=Ni_Ga(i,0:360);
for j=PCyc(1):PCyc(2)-1
plot(PVals+j*360,GVals,'color',CalcRgb(i),'linewidth',LWidth);
TextX=Ni_Ph(i,210)+(j+1)*360;
TextY=Ni_Ga(i,210);
text(TextX,TextY,[num2str(i) 'dB'],...
'FontSize',LSize,..., ...
'horizontalalignment','center',...
'UserData',user_data(i));
end
end
%% Draw N-circles
% Define equations that determine the N-circles
RadN=@(phi) 1./(2.*abs(sind(phi)));
Ny_Re=@(phi,t) -0.5+RadN(phi).*cosd(t+mod(phi,180)-90);
Ny_Im=@(phi,t) 1./(2.*tand(phi))+RadN(phi).*sind(t+mod(phi,180)-90);
Ni_Ph=@(phi,t) rad2deg(unwrap(angle(Ny_Re(phi,t)+1i*Ny_Im(phi,t))))+360*floor(phi/360);
Ni_Ga=@(phi,t) 20.*log10(abs(Ny_Re(phi,t)+1i*Ny_Im(phi,t)));
Ni_La=@(phase) 0.090*10^(phase/60);
% Create input vectors
Phi=PCyc(1)*360:PInc:PCyc(2)*360;
T1=logspace(-4,log10(180),300);
T2=[T1 360-fliplr(T1)];
if frag_bool
user_data = @(nn) ['matlabfrag:',...
'\fboxsep=0pt\colorbox{white}{$\,',...
num2str(nn),...
'$\textdegree}'];
else
user_data = @(nn) '';
end
% Apply N-circle equations and plot the result
for i=Phi
if abs(sind(i))<1e-3
plot([i i],[-110,25],'color',0.75*[1 1 1],'linewidth',LWidth);
if cosd(i)>0
TextX=i;
TextY=1;
else
TextX=i;
TextY=-46.5;
end
else
plot(Ni_Ph(i,T2),Ni_Ga(i,T2),'color',0.75*[1 1 1],'linewidth',LWidth);
Offset=i-180*floor(i/180);
if(sign(sind(i))==1)
TextX=Ni_Ph(i,Ni_La(180-Offset));
TextY=Ni_Ga(i,Ni_La(180-Offset));
else
TextX=Ni_Ph(i,-Ni_La(Offset))+360;
TextY=Ni_Ga(i,-Ni_La(Offset));
end
end
if nlabels_bool
text(TextX,TextY,[num2str(i),'°'],...
'FontSize',LSize,...
'horizontalalignment','center',...
'UserData',user_data(i));
end
end
%% Finish up
% Want a box:
set(gca,'box','on')
% Modify ticks to nicely cover the required range of phases
set(gca,'xtick',PCyc(1)*360:30:PCyc(2)*360);
axis([PCyc(1)*360-15 PCyc(2)*360+15 -80 30]);
% Unfreeze current plot
if ~hold_bool, hold off; end
end