0% found this document useful (0 votes)
104 views4 pages

MATLAB Basic Exercises 2

This document provides examples and explanations for plotting functions, analyzing linear time-invariant systems, performing fast Fourier transforms, using hggroup and hgtransform objects to manipulate graphics, and processing MATLAB data files. It includes examples of plotting multiple lines, customizing axis labels and titles, analyzing step and impulse responses of transfer functions, using bode plots and nyquist plots, creating feedback loops, and performing periodogram analysis of spectral data. It also demonstrates how to group and manipulate graphics objects, and how to load, check contents, save, and modify MATLAB data files.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
104 views4 pages

MATLAB Basic Exercises 2

This document provides examples and explanations for plotting functions, analyzing linear time-invariant systems, performing fast Fourier transforms, using hggroup and hgtransform objects to manipulate graphics, and processing MATLAB data files. It includes examples of plotting multiple lines, customizing axis labels and titles, analyzing step and impulse responses of transfer functions, using bode plots and nyquist plots, creating feedback loops, and performing periodogram analysis of spectral data. It also demonstrates how to group and manipulate graphics objects, and how to load, check contents, save, and modify MATLAB data files.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 4

CTP146N Signal Processing ANNEX 7.7.3.- Basic Exercises 2__ pub.

________________________________________________________________________________________________________________________________
John Bofarull Guix 1 / 8
ANNEX 7.7.3 BASIC EXERCISES 2

Contents

plot
LTI system responses
fft
hggroup, hgtransform
processing files

________________________

PLOT

note: MSWord autocorrection replaces 3 dots with a character that looks like 3 dots but when cut and pasted to MATLAB
command window, MATLAB returns error, and it doesn't point to the 3 dots, it points some characters left.

set(0,'DefaultAxesColorOrder',[0 0 0],'DefaultAxesLineStyleOrder','-|-.|--|:')
plot(rand(12,2));hold all;plot(randn(12,2))

x = -pi:pi/10:pi;
y = tan(sin(x)) - sin(tan(x));
plot(x,y,'--rs','LineWidth',2,'MarkerEdgeColor','k','MarkerFaceColor','g','MarkerSize',10)

x=-pi:pi/10:pi
y=sin(x)
plot(x,y)
set(gca,'XTick',-pi:pi/2:pi)
set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'})
xlabel('-\pi \leq \Theta \leq \pi')
ylabel('sin(\Theta)')
title('Plot of sin(\Theta)')
text(-pi/4,sin(-pi/4),'\leftarrow sin(-\pi/4)','HorizontalAlignment','left')

set(findobj(gca,'Type','line','Color',[0 0 1]),'Color','r','LineWidth',2)

x = -pi:.1:pi;
y = sin(x);
plot(x,y)
set(gca,'XTick',-pi:pi/2:pi)
set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'})
xlabel('-\pi \leq \Theta \leq \pi')
ylabel('sin(\Theta)')
title('Plot of sin(\Theta)')
text(-pi/4,sin(-pi/4),'\leftarrow sin(-\pi/4)','HorizontalAlignment','left')
set(findobj(gca,'Type','line','Color',[0 0 1]),'Color','red','LineWidth',2)


LTI system responses

sys=tf([8 18 32],[1 6 14 24])
subplot(211);step(sys);subplot(212); impulse(sys)
clf;t=0:.01:4;u=sin(10*t);lsim(sys,u,t)

A=[-.8 3.6 -2.1;3 -1.2 4.8;3 -4.3 -1.1]
B=[0;-1.1;-.2]
C=[1.2 0 .6]
D=-.6
G=ss(A,B,C,D)
x0=[-1;0;2] % initial state
initial(G,x0);grid

sys=tf([8 18 32],[1 6 14 24]);bode(sys);grid
nyquist(sys);grid
CTP146N Signal Processing ANNEX 7.7.3.- Basic Exercises 2__ pub.

________________________________________________________________________________________________________________________________
John Bofarull Guix 2 / 8

% feedback loop
s=tf('s');
G=-(2*s+1)/(s^2+3*s+2);
k=.7
T=feedback(G*k,1)
pzmap(T);grid on;axis([-2 0 -1 1])
clf;step(T)

% for k=1.5 system becomes unstable

rlocus(G);grid on;step(T)

sys=rss(3,2,2);
sys.a=[-.5 -.3 -.2;0 -1.3 -1.7;.4 1.7 -1.3];step(T);
sigma(sys);grid on

% comparing systems
k1 = 0.4; T1 = feedback(G*k1,1);
k2 = 1; T2 = feedback(G*k2,1);
step(T,'b',T1,'r',T2,'g')
legend('k = 0.7','k = 0.4','k = 1')

x=-2.9:.2:2.9;bar(x,exp(-x.*x))
x=0:0.25:10;stairs(x,sin(x));
x=-2:0.1:2;
y=erf(x);
e = rand(size(x))/10;
errorbar(x,y,e);

t=0:0.01:2*pi;
polar(t,abs(sin(2*t).*cos(2*t)));
x = 0:0.1:4;
y = sin(x.^2).*exp(-x);
stem(x,y)

load count.dat
scatter(count(:,1),count(:,2),'r*')
xlabel('Number of Cars on Street A');
ylabel('Number of Cars on Street B');

FFT

load sunspot.dat
year=sunspot(:,1);
relNums=sunspot(:,2);
plot(year,relNums)
title('Sunspot Data')
plot(year(1:50),relNums(1:50),'b.-')

Y=fft(relNums);Y(1)=[]
plot(Y,'ro')
title('Fourier Coefficients in complex plane');xlabel('Real part');ylabel('Imaginary part')

n=length(Y);
power = abs(Y(1:floor(n/2))).^2;
nyquist = 1/2;
freq = (1:n/2)/(n/2)*nyquist;
plot(freq,power)
xlabel('cycles/year')
title('Periodogram')

plot(freq(1:40),power(1:40))
xlabel('cycles/year')

CTP146N Signal Processing ANNEX 7.7.3.- Basic Exercises 2__ pub.

________________________________________________________________________________________________________________________________
John Bofarull Guix 3 / 8
period=1./freq;
plot(period,power);
axis([0 40 0 2e+7]);
ylabel('Power');
xlabel('Period (Years/Cycle)');

hold on;
index=find(power==max(power));
mainPeriodStr=num2str(period(index));
plot(period(index),power(index),'r.', 'MarkerSize',25);
text(period(index)+2,power(index),['Period = ',mainPeriodStr]);
hold off;



HGROUP() and HGTRANSFORM()

hggroup() groups objects but does not have axes rotation, scaling, translation methods

EXAMPLE 1 HOW TO CREATE OBJECTS: BUILD OBJECT AND MAKE A COPY

ax = axes('XLim',[-1.5 1.5],'YLim',[-1.5 1.5],... % 1. set pitch
'ZLim',[-1.5 1.5]);
view(3); grid on; axis equal

[x y z] = cylinder([.3 0]); % 2. create surface objects to group
h(1) = surface(x,y,z,'FaceColor','red');
h(2) = surface(x,y,-z,'FaceColor','green');
h(3) = surface(z,x,y,'FaceColor','blue');
h(4) = surface(-z,x,y,'FaceColor','cyan');
h(5) = surface(y,z,x,'FaceColor','magenta');
h(6) = surface(y,-z,x,'FaceColor','yellow');

t = hgtransform('Parent',ax); %3. create a void hgtransform object and (link) parent previous surface
objects to it
set(h,'Parent',t)
set(gcf,'Renderer','opengl') % 4. select renderer and show object
drawnow

Rz = eye(4); % 5. init rotation and scaling arrays
Sxy = Rz;

for r = 1:.1:2*pi % 6. rotation: Z-axis
Rz = makehgtform('zrotate',r);

Sxy = makehgtform('scale',r/4); % Scaling matrix

set(t,'Matrix',Rz*Sxy) % Concatenate the transforms and set the hgtransform property 'Matrix'
drawnow
end
pause(1)

set(t,'Matrix',eye(4)) % 7. reset original orientation and size


EXAMPLE 2:

t1 = hgtransform('Parent',ax); % 3. create 2 objects, they are overlapping!
t2 = hgtransform('Parent',ax);

set(gcf,'Renderer','openGL') % 4. choose renderer

set(h,'Parent',t1) % 5. group (parent) surfaces into respective single objects
h2 = copyobj(h,t2);

CTP146N Signal Processing ANNEX 7.7.3.- Basic Exercises 2__ pub.

________________________________________________________________________________________________________________________________
John Bofarull Guix 4 / 8
Txy = makehgtform('translate',[-1.5 -1.5 0]); % 6. translation of 2nd object away from 1st object
set(t2,'Matrix',Txy)
drawnow

for r = 1:.1:20*pi % 7. Rotation in [rad] 10*2*pi, using one rotation to rotate 2 objects
opposite spin
Rz = makehgtform('zrotate',r);
set(t1,'Matrix',Rz) % Set both hgtransform objects to rotate with 'Matrix'
set(t2,'Matrix',Txy*inv(Rz))
drawnow
end

makehgtransform() generates 'Matrix' inputs according to desired operation: Scale, Rotate, Translate


M = makehgtform identity
M = makehgtform('translate',[tx ty tz]) or M = makehgtform('translate',tx,ty,tz)
M = makehgtform('scale',s) scales uniform scaling
M = makehgtform('scale',[sx,sy,sz]) dimension pondered scaling
M = makehgtform('xrotate',t) rotates around x-axis t[rad]
M = makehgtform('yrotate',t) rotates around y-axis t[rad]
M = makehgtform('zrotate',t) rotates around z-axis t[rad]
M = makehgtform('axisrotate',[ax,ay,az],t) Rotate around axis [ax ay az] by t radians.

multiple operations can be specified in single call:
m = makehgtform('xrotate',pi/2,'yrotate',pi/2); is the same as m = makehgtform('xrotate',pi/2)
*makehgtform('yrotate',pi/2);



PROCESSING FILES


testdata.mat
check contents: whos -file testdata.mat
or selecting in current folder
load testdata.mat testDataSummary
type testdata.mat tells the
save testdata.mat -v7.3
load testdata.mat testDataSummary
type testdata.mat
matFileObj=matfile('testdata.mat')
matFileObj.testDataFull(1:2:10,1:3)
size(matFileObj,'testDataFull')
matFileObj.testDataFull(1:8,1:8)
matFileObj.Properties.Writable=true
matFileObj.testDataFull(1:5,1:5)=zeros(5,5)

You might also like