%question5% function y = piecex2(n) F = 0.1; a = length(n); for i = 1:a if -1/(2*F) <= n(i) & n(i) <= 1/(2*F) y(i) = cos(2*pi*F*n(i)); else y(i) = 0; end end %write this in command window% F=0.1; n=-1/(2*F)*5:1/(2*F)*5; stem(n,piecex2(n)) %question6% * first define impulse function and save it: impulse function: function y=impulse(t) y=(t==0); discrete impulse function: function y = impDT(n) y = zeros(size(n)); % Initialize y as a numeric array % Define impulse function y(n == 0) = 1; % Find non-integer values of n ss = find(round(n) ~= n); % If there are non-integer values, replace corresponding elements in y with NaN if(~isempty(ss)) y(ss) = NaN; end end %write this code in command window% t=-10:0.001:10; td=-10:10; xt=impulse(t-2); yn=impDT(td-5); subplot(2,1,1) plot(t,xt) subplot(2,1,2) stem(td,yn) %question7% %write this function in m-file and save it as piecex.m% function y=piecex(t) a=length(t); for i=1:a if -1<=t(i) & t(i)<=0 y(i)=(-1*t(i))+1; elseif 0<=t(i) & t(i)<=1 y(i)=t(i)+1; else y(i)=0; end end %write this in command window% t=-8:0.1:8; subplot(5,1,1) plot(t,piecex(t),'g') subplot(5,1,2) plot(t,piecex(t-5),'m') subplot(5,1,3) plot(t,piecex(t+5),'y') subplot(5,1,4) plot(t,piecex(2*t-4),'r') subplot(5,1,5) plot(t,-2.*(piecex(-2*t+5)),'b')