%
%  plot_magnitude_phase.m  ver 1.1  January 21, 2014
%
%  by Tom Irvine
%
disp(' This script plots the magnitude & phase of a complex frequency');
disp(' response function.  It also plots the real and imaginary components.');
clear THM;
clear ff;
clear FRF_p;
clear FRF_m;
%
close all;
fig_num=1;
%
disp(' ');
disp(' The input array must have two columns:  freq(Hz) complex amplitude ');
disp(' ');
THM=input(' Enter complex array name:  ');
%
disp(' ');
fmin=input(' Enter minimum plot frequency (Hz)  ');
fmax=input(' Enter maximum plot frequency (Hz)  ');
%
disp(' ');
t_string=input(' Enter plot title: ','s');
%
sz=size(THM);
%
n=sz(1);
ff=zeros(n,1);
FRF_m=zeros(n,1);
FRF_p=zeros(n,1);
%
for i=1:n
    ff(i)=THM(i,1);
    FRF_m(i)=abs(THM(i,2));
        aa=real(THM(i,2));
        bb=imag(THM(i,2));
%
    FRF_p(i)=(180/pi)*atan2(bb,aa);
end 
FRF=THM;
%
figure(fig_num);
fig_num=fig_num+1;
%
subplot(3,1,1);
plot(ff,FRF_p);
title(t_string);
grid on;
ylabel('Phase (deg)');
axis([fmin,fmax,-180,180]);
set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-180,-90,0,90,180]);
%
if(max(FRF_p)<=0.)
%
axis([fmin,fmax,-180,0]);
set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-180,-90,0]);
end  
%
if(min(FRF_p)>=-90. && max(FRF_p)<90.)
%
axis([fmin,fmax,-90,90]);
set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[-90,0,90]);
end 
%
if(min(FRF_p)>=0.)
%
axis([fmin,fmax,0,180]);
set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
                    'YScale','lin','ytick',[0,90,180]);
end 
%
subplot(3,1,[2 3]);
plot(ff,FRF_m);
xlim([fmin fmax])
grid on;
xlabel('Frequency(Hz)');
ylabel('Magnitude');
set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
         'YScale','log');
%
figure(fig_num);
fig_num=fig_num+1;
%
subplot(2,1,1);
plot(THM(:,1),real(THM(:,2)));
grid on;
xlim([fmin fmax]);
ylabel('Real');
title(t_string);
set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
         'YScale','lin');
%
subplot(2,1,2);
plot(THM(:,1),imag(THM(:,2)));
grid on;
xlim([fmin fmax]);
xlabel('Frequency (Hz)');
ylabel('Imaginary');
set(gca,'MinorGridLineStyle','none','GridLineStyle',':','XScale','log',...
         'YScale','lin');