% ***** MATLAB Code Starts Here % %Z_TRANSFORM_01_MAT % T = pi/10; % Sample period, seconds ws = 2*pi/T; % Sample frequency, r/s % w1 = 2; w2 = 22; % Frequencies to be studied % % Plot the s-plane with the primary strip defined by the imaginary axis % ranging from -jws/2 to +jws/2 and the two test frequencies, w1 and w2, % identified. % fig_size = [232 84 774 624]; figure(1),clf,plot(0,w1,'bo',0,w2,'rx',[-25 25],[ws/2 ws/2],'r:',[-25 25],[-ws/2 -ws/2],'r:') axis('square'),axis([-25 25 -25 25]),plotax,xlabel('Real Axis'),ylabel('Imag Axis'),... title('s-Plane with T = pi/10 s'),set(gcf,'Position',fig_size),... text(1,w1,['w_1 = ' int2str(w1) ' r/s']),text(1,w2,['w_2 = ' int2str(w2) ' r/s']),... text(-18,11,'w_s/2'),text(-18,-9,'-w_s/2') % % Define the unit circle for use in the z-plane using the function "circle" % which is provided at the end of this file. % [xx,yy] = circle; % % Define the points in the s-plane and the z-plane corresponding to the % test frequencies w1 and w2 and the value of the sampling period T. % s1 = j*w1; s2 = j*w2; z1 = exp(s1*T); z2 = exp(s2*T); % % Plot the z-plane with the unit circle and the mappings of the frequencies % w1 and w2 into the z-plane with the period T. % figure(2),clf,... plot(xx,yy,'k',real(z1),imag(z1),'bo',real(z2),imag(z2),'rx'),grid,... xlabel('Real Axis'),ylabel('Imag Axis'),title('z-Plane with T = pi/10 s'),... axis('square'),axis([-2 2 -2 2]),... set(gcf,'Position',fig_size) text(0.9,0.6,'z_1 = z_2'),text(-0.95,1.4,'z = exp(s*T)'),... text(-0.95,1.2,'z_1 = z_2 = 0.8090 + j0.5878') % % Compute the continuous-time functions x1(t) and x2(t) corresponding to % the frequencies w1 and w2, respectively. Compute the values of those % functions at the sample points separated by T. % ts = 0:T:5; t = 0:.01:5; x1 = cos(w1*t); x2 = cos(w2*t); x1s = cos(w1*ts); x2s = cos(w2*ts); % % Plot the two time functions and their sample values. % figure(3),clf,stem(ts,x1s);grid,hold on,plot(t,x1,'k:',t,x2,'r:'),... hold off,xlabel('Time (s)'),ylabel('Amplitude'),... title('x_1(t)=cos(2*t) and x_2(t)=cos(22*t) sampled at T = pi/10 s'),... set(gcf,'Position',fig_size) % % Calculate the first several frequencies which appear when the two % signals x1(t) and x2(t) are sampled with a sampling period T. % n = [-1 0 1]; w1 = [-w1 w1]; w2 = [-w2 w2]; % wx1 = []; wx2 = []; for i = 1:length(n) wx1 = [wx1 (w1 - n(i)*ws)]; wx2 = [wx2 (w2 - n(i)*ws)]; end % wx1 = sort(wx1);wx2 = sort(wx2); clear i % % % ***** MATLAB Code Stops Here another way of z transform of matlab code % ***** MATLAB Code Starts Here % %Z_TRANSFORM_03_MAT % fig_size = [232 84 774 624]; num = 20*conv([1 -.8],[1 -.7]); % H(z) numerator den = conv([1 -.5],conv([1 -.6],[1 .8])); % H(z) denominator % h = dimpulse(num,den); % Impulse response using "dimpulse" n = [0:length(h)-1]; % discrete time vector % [res,pol,fdth] = residue(num,[den 0]); % PFE of H(z)/z % h1 = res(1)*pol(1).^n + res(2)*pol(2).^n + res(3)*pol(3).^n; h1(1) = h1(1)+res(4); % Impulse response using "residue" % figure(1),clf,stem(n,h1);grid,axis([0 25 -25 25]),... xlabel('Discrete Time Samples'),ylabel('Amplitude'),... title('Impulse Response h(n)'),... set(gcf,'Position',fig_size) % % Magnitude and Phase of H(z) using "polyval" function % wt = [0:pi/20:pi]; % Angle vector (rad) z = exp(j*wt); % Points on unit circle % mag = abs(polyval(num,z)) ./ abs(polyval(den,z)); ph = unwrap(angle(polyval(num,z)) - angle(polyval(den,z)))*180/pi; % % Figures of magnitude and phase % figure(2),clf,plot(wt,20*log10(mag),wt,20*log10(mag),'ko'),grid,... xlabel('Angle around unit circle (rad)'),... ylabel('Magnitude (db)'),title('Frequency Response Magnitude'),... set(gcf,'Position',fig_size) % figure(3),clf,plot(wt,ph,wt,ph,'ko'),grid,... xlabel('Angle around unit circle (rad)'),... ylabel('Phase (deg)'),title('Frequency Response Phase'),... set(gcf,'Position',fig_size) % % % ***** MATLAB Code Stops Here