Categories
Software

How to adjust figures in Matlab?

I was asked few times about possible adjustment of figures in Matlab. Many times there is simply a need to change slightly the default view in order to align with the template in our publication, book, etc. Thus I will try to show few parameters that can easily adjust the view according to our expectations.

Fortunately in Matlab there is an extended flexibility of doing that. I need to admit that it is much more convenient to edit figures in Matlab than in other engineering tools (e.g. LabVIEW, PSCAD, PowerFactory, etc.). Therefore I personally export all of my results into Matlab and later adjust.

Let me explain how to obtain the following figures. As one can see the figures present the same result but slightly in a different way. I do not need to mention that nicely presented research results can easily attract broader audience. Hence even in the conservative scientific world it is of great importance to be able selling our findings.

Notch Filter - Bode Plot
Notch Filter - 3D Phase

The figures above show the variation of notch filter depending on the quality factor. The filter is tuned for 100Hz and included in synchronous reference frame and afterwards represented in natural/stationary reference frame. Thus the resonant peaks are shifted ±50Hz. The notch filter transfer function is expressed in the following way.

Such figures can be obtained by using the following code. Please note that also Control System Toolbox is needed to obtain the frequency response of the notch filter.

% Prepare workspace
clc, clear('all'), close('all'),
% Define font parameters
fontname= 'Cambria';
set(0,'defaultaxesfontname',fontname);
set(0,'defaulttextfontname',fontname);
fontsize= 10;
set(0,'defaultaxesfontsize',fontsize);
set(0,'defaulttextfontsize',fontsize);
% Get screen resolution
scrsz= get(0,'ScreenSize');

%% Notch filter
% Define frequency parameters
f= 50;                    % Grid frequency [Hz]
omegaO= 2*pi*f;           % Angular frequency [rad/s]
omegaN= 2*omegaO;         % Resonant frequency [rad/s]
step= 0.1;                % Frequency step [Hz]
frequencySeries= 1:step:200;
omegaSeries= 2*pi*frequencySeries;
% Construct transfer function
s= tf('s');
sN= s-1i*omegaO;
sP= s+1i*omegaO;
% Allocate memory
firstIndex= 1; lastIndex= 30; k= 1;
surfTf= zeros(length(frequencySeries),lastIndex);
map= zeros(lastIndex,3);

%% Display results
% Initiate figure
f1= figure('Name','Transfer Function Plot',...
'Position',[scrsz(3)*0.2 scrsz(4)*0.2 scrsz(3)*0.35 scrsz(4)*0.45]);
hold('on'),
for index=firstIndex:k:lastIndex,
     Qn= 100/sqrt(2); Qd= index+5;           % Quality factor
     GnN= (sN^2+omegaN*sN/Qn+omegaN^2)/(sN^2+omegaN*sN/Qd+omegaN^2);
     GnP= (sP^2+omegaN*sP/Qn+omegaN^2)/(sP^2+omegaN*sP/Qd+omegaN^2);
     Gn= 1/2*(GnP+GnN);                      % From SRF to NRF
     [magGn,phaseGn]= bode(Gn,omegaSeries);  % Frequency response
     GnCplx= magGn.*exp(1i*phaseGn);
     surfTf(:,index)= GnCplx(:);
     sub1= subplot(2,1,1,'Parent',f1);box(sub1,'on'),hold(sub1,'all'),
     plot(frequencySeries,abs(GnCplx(:)),...
          'Color',[0 1-index/lastIndex index/lastIndex]),
          ylabel('|{\itG_{notch}}| [abs]'),
          xlim([min(frequencySeries) max(frequencySeries)]),
          ylim([0.5 1.02]),hold('on'),grid('off'),
     sub2= subplot(2,1,2,'Parent',f1);box(sub2,'on'),hold(sub2,'all'),
     plot(frequencySeries,180*unwrap(angle(GnCplx(:)))/pi,...
          'Color',[0 1-index/lastIndex index/lastIndex]),
          xlim([min(frequencySeries) max(frequencySeries)]),
          ylim([-1100 1300]),hold('on'),grid('off'),
          ylabel('\angle{\it{G_{notch}}} [\circ]'),xlabel('{\itf} [Hz]'),
     map(index,:)= [0 1-index/lastIndex index/lastIndex];
end
c1= colorbar('peer',sub1,'East');colormap(map),
set(c1,'YTickMode','manual','YTickLabelMode','manual',...
 'YTick',[firstIndex; floor((lastIndex-firstIndex)/2); lastIndex],...
 'YTickLabel',[firstIndex+5; floor((lastIndex-firstIndex)/2)+5; lastIndex+5]),
hold('off'),

f2= figure('Name','Impedance Angle',...
     'Position',[scrsz(3)*0.2 scrsz(4)*0.2 scrsz(3)*0.35 scrsz(4)*0.45]);
ax2= axes('Parent',f2);grid(ax2,'on'),hold(ax2,'all'),
mesh(180*unwrap(angle(surfTf))/pi),zlabel('\angle{\it{G_{notch}}} [\circ]'),
     ylabel('{\itf} [Hz]'),xlabel('{\itQ_n} ','Rotation',322),view([60 40]),
set(gca,'XTick',[firstIndex; floor((lastIndex-firstIndex)/2); lastIndex],...
 'XTickLabel',[firstIndex+5; floor((lastIndex-firstIndex)/2)+5; lastIndex+5],...
 'YTick',1:50/step:length(frequencySeries),...
 'YTickLabel',min(frequencySeries):50:max(frequencySeries)),

Feel free to use and modify included Matlab code. I am also looking forward to hear from you in case of any suggestions and comments.

Categories
Software

Change font name in Bode plot

I had been struggling with this problem a while before I did manage to find a work around. It was actually quite important for me to change the font name in Bode plots because I decided to use everywhere Cambria in my report. Normally Heveltiva is used by default in Matlab.

In general it is possible to change font in Matlab plots without any problems. But this is in case the standard package. It should be emphasized that each of Matlab toolboxes is developed by separated teams of specialists. That is why sometimes different features can be solved with a different approach and actually in Control System Toolbox it is not predicted to change font name. You can change the size or style but not the name.

Available parameters for different labels in case of Bode, Nyquist, etc. plots are the following

>> PlotHandle= bodeplot(TrunsferFunction);
>> PlotOptions= getoptions(PlotHandler);
>> PlotOptions.Title,
ans =

String: 'Dummy Title'
FontSize: 10
FontWeight: 'normal'
FontAngle: 'normal'
Color: [0 0 0]
Interpreter: 'tex'

As everyone can see there is not option defining the font name. Fortunately there is some space to deal with it due to the fact that it is possible to define the interpreter. In Tex it is actually possible to define font name locally in text. This can be done in the following way

PlotOptions.Title.String= '';
PlotOptions.XLabel.String= '\fontname{Cambria}{\itf}';
PlotOptions.XLabel.FontSize= 10;
PlotOptions.YLabel.String= {'\fontname{Cambria}|{\itG_{ol}}|',...
    '\fontname{Cambria}\angle{\itG_{ol}}'};
PlotOptions.YLabel.FontSize= 10;
PlotHandle.setoptions(PlotOptions),

And here is the final result.

Bode Plot Font NameAnother solution is to get the frequency response into arrays and display results using functions form Matlab base package. In this case it is also possible to change axes font name.

You can also change the default system font for all figures and axes by putting the following code at the beginning of your m-file. Please note that this will change your default font during your Matlab session. I do this quite often if I do not want to think so much preparing figures for printing.

fontname= 'Cambria';
set(0,'defaultaxesfontname',fontname);
set(0,'defaulttextfontname',fontname);
fontsize= 10;
set(0,'defaultaxesfontsize',fontsize);
set(0,'defaulttextfontsize',fontsize);

Hope this helps and looking forward to see some comments.

Categories
Software

Shapes in deep shadows and high lights in Matlab

Normally I work in different toolboxes of Matlab on purpose. Either it is for my research project purposes  or due to my research project purposes. That is why I came up with an idea to do something in Matlab that would not have any application. I wanted to do something what would look nice and be by itself. Later of course I found some interesting application areas of my work but let us start from the beginning.

Łukasz Kocewiak
Photograph by Magdalena Sozańska

I decided to use Image Processing Toolbox because, in my opinion, no matter of what working on images should always give interesting results. Why not to use some of my pictures from the past taken with my old Minolta Dynax 700si. I still should have it somewhere in the basement.

%% Gausian convolution matrix
G= [1  4  7  4 1;
    4 16 26 16 4;
    7 26 41 26 7;
    4 16 26 16 4;
    1  4  7  4 1];
G= G/sum(sum(G));

%% Picture analysis
ExampleUint= imread('Example.jpg');
ExampleDouble= double(ExampleUint);
[m n]= size(ExampleDouble(:,:,1));

%%  Linear Gaussian filter
ExampleFilterUint= imfilter(ExampleUint,G,'conv');
for k=1:10,
    ExampleFilterUint= imfilter(ExampleFilterUint,G,'conv');
    k= k+1; %#ok<FXSET>
end
ExampleFilterDouble= double(ExampleFilterUint);

Due to the fact that the picture was taken still in analog technology and scanned in any photo lat the quality is not so good. Even in the small picture (a) noise can be clearly seen. I decided to get rig of it by application of a digital filter. A lot of noise and sharp edges that can affect the final result. In order to make the picture more smooth we will use a low-pass filter (b). I decided to use Gaussian filter with the convolution matrix as presented above.

Łukasz Kocewiak 3DThis apprioach can be succesfully used if we would like to evaluate how objects are shaped by light. A good example is in case of body shaping by either attending gym or fitness. If we simply cannot assess if our muscles are sufficiently shaped, just take a picture and do some processing in Matlab. Probably other applications can be found. Please let me know if you have any suggestions by posting a comment.

And an exemplary code from Matlab showing how to display two-dimensional matrix of double precision numbers.

scrsz= get(0,'ScreenSize');
%% Show results
fi1= figure('Name','3D Plot',...
     'Position',[0.1*scrsz(3) 0.1*scrsz(4) 0.35*scrsz(3) 0.5*scrsz(4)]);
ax1= axes('Parent',fi1,'FontName','Verdana','FontSize',10);
grid(ax1,'on'),hold(ax1,'all'),
mesh(ExampleFilterDouble(:,:,1)),colormap('hot'),
view(ax1,[-150 70]),xlim([0 n]),ylim([0 m]),