To convert a number to a string --> num2str(numval, "val=%0.2f") % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Plot Magnitude and Phase of a DTFT - 4b % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % create the x-axis spanning -4*pi to 4*pi (256 divisions = 257 points) wval = linspace(-4*pi, 4*pi, 257); % remove the x-axis points that land on integer multiples of 2*pi wcval = nonzeros(wval .* (abs(wval - (2*pi)*round(wval / (2*pi))) > 1.0e-8))'; % define the function in problem 4a X = @(w) (2 ./ (1 - 0.75 * e.^(-j*w))); % plot the magnitude and phase subplot(2, 1, 1) plot(wcval, abs(X(wcval))) title("Problem 4b - Magnitude") xlabel("Normalized Frequency") ylabel("Magnitude") xlim([-4*pi, 4*pi]) xticks(linspace(-4*pi, 4*pi, 5)); grid("on") subplot(2, 1, 2) plot(wcval, (180/pi)*angle(X(wcval))) title("Problem 4b - Phase") xlabel("Normalized Frequency") ylabel("Phase [deg]") xlim([-4*pi, 4*pi]) ylim([-180, 180]) xticks(linspace(-4*pi, 4*pi, 5)); yticks(linspace(-180, 180, 5)); grid("on") % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% D = @(m) ((-2*j/7)*(3*sin(6*pi*m/7) + 2*sin(4*pi*m/7) + sin(2*pi*m/7))); D0 = D(0); D1 = D(1); D2 = D(2); D3 = D(3); D4 = D(-3); D5 = D(-2); D6 = D(-1); Wo = 2*pi/7; F = @(n) (D0 + D1*e^(j*Wo*n) + D2*e^(j*2*Wo*n) + D3*e^(j*3*Wo*n) + D4*e^(j*4*Wo*n) + D5*e^(j*5*Wo*n) + D6*e^(j*6*Wo*n)); % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Plot Magnitude and Phase of a DTFT % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % create the x-axis spanning -4*pi to 4*pi (256 divisions = 257 points) wval = linspace(-4*pi, 4*pi, 257); % remove the x-axis points that land on integer multiples of 2*pi wcval = nonzeros(wval .* (abs(wval - (2*pi)*round(wval / (2*pi))) > 1.0e-8))'; % define the function in problem 4a X = @(w) ((1/4) .* (1 - e.^(-j.*w.*4)) ./ (1 - e.^(-j.*w))); % plot the magnitude and phase subplot(2, 1, 1) plot(wcval, abs(X(wcval))) title("Problem 4a - Magnitude") xlabel("Normalized Frequency") ylabel("Magnitude") xlim([-4*pi, 4*pi]) xticks(linspace(-4*pi, 4*pi, 5)); grid("on") subplot(2, 1, 2) plot(wcval, (180/pi)*angle(X(wcval))) title("Problem 4a - Phase") xlabel("Normalized Frequency") ylabel("Phase [deg]") xlim([-4*pi, 4*pi]) ylim([-180, 180]) xticks(linspace(-4*pi, 4*pi, 5)); yticks(linspace(-180, 180, 5)); grid("on") % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % help % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% help help history help sinc doc doc sinc % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % plotting syntax / keys % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% plot(x, y) plot(x1, y1, lspec1, x2, y2, lspec2, ....) plot(x, y, 'Name', Value) color --> 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'black', 'white' --> 'r', 'g', 'b', 'c', 'm', 'y', 'k', 'w' --> '#FF0000" .... linestyle --> '-' (solid) '--' (dashed) ':' (dotted) '-.' (dash-dot) 'none' (no line) linewidth --> 0.5, 2.0, 8.0 linejoin --> 'round', 'miter', 'chamfer' marker --> 'o', '+', '*', '.', 'x', 's', 'd', '^', 'v', '>', '<', 'p', 'h', 'none' markermode --> 'auto', 'manual' markerindices --> 5 (every 5th point) markersize --> 6 displayname --> 'measured', 'simulated' % --- xlim("auto") xlim([-20 20]) xlabel("Time [ns]") ylabel("Magnitude [V]") title("Phase") legend() % --- ':' plots a dotted line. 'g:' plots a green, dotted line. 'g:*' plots a green, dotted line with star markers. '*' plots star markers with no line. xlabel("Greg") subplot(2,1,1) xlabel("Shan") x = linspace(0,2*pi,50); y = sin(x); plot(x,y,':') hold on y2 = cos(x); plot(x,y2,'--ro') hold off % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % plot magnitude and phase of a frequency domain function % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% F = @(w) (1 ./ (1 .+ i .* w)); wval = -10:0.01:10; subplot(2,1,1) plot(wval, abs(F(wval))) subplot(2,1,2) plot(wval, 180/pi*angle(F(wval))) hold on cval = -10:1.0:10; plot(cval, 180/pi*angle(F(cval)), 'ro') % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % define a function and plot it % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% u = @(t) ((t>=0)*1.0); w = @(t, a, b) (u(t - a) - u(t - b)) x = @(t) (w(t,-1,0).*(-1-t) + w(t,0,1).*t + w(t,1,2).*1.0 + w(t,2,3).*(3-t)) figure tvec = -3:0.025:5; plot(tvec, x(tvec), 'b', tvec, x(-2*tvec-1), 'r'); % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % niceplot.m % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% gfig = figure; set(gca, 'linewidth', 2) set(gca, 'fontsize', 14) x = linspace(0,16*pi,500); y = sin(x); z = cos(x) .* e.^(-x/(4*pi)); hold on gp = plot(x, y, '-', 'displayname', 'Jack') hp = plot(x, z, '--', 'displayname', 'Andre') xlabel('x', 'fontsize', 36) ylabel('Sin(x)', 'fontsize', 36) title('Sin(x) Graph', 'fontsize', 36) % xlim auto % ylim auto % xlim([0 45]) % ylim([-1.2 1.2]) grid on % axis equal legend() legend('boxoff') set(gp, 'linewidth', 2) set(hp, 'linewidth', 3) % set(hp, 'linestyle', '--') hold off print(gfig, 'plot.pdf', '-dpdf'); print(gfig, 'plot.svg', '-dsvg'); print(gfig, 'plot.png', '-dpng', '-r300'); % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% * ************************************************** plot(x, y) plot(x1, y1, lspec1, x2, y2, lspec2, ....) plot(x, y, 'Name', Value) color --> 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'black', 'white' --> 'r', 'g', 'b', 'c', 'm', 'y', 'k', 'w' --> '#FF0000" .... linestyle --> '-' (solid) '--' (dashed) ':' (dotted) '-.' (dash-dot) 'none' (no line) linewidth --> 0.5, 2.0, 8.0 linejoin --> 'round', 'miter', 'chamfer' marker --> 'o', '+', '*', '.', 'x', 's', 'd', '^', 'v', '>', '<', 'p', 'h', 'none' markermode --> 'auto', 'manual' markerindices --> 5 (every 5th point) markersize --> 6 displayname --> 'measured', 'simulated' * ************************************************** % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % nicerplot.m % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% gfig = figure; x = linspace(0,16*pi,500); y = sin(x); z = cos(x) .* e.^(-x/(4*pi)); hold on gp = plot(x, y, '-', 'displayname', 'Jack') hp = plot(x, z, '--', 'displayname', 'Andre') xlabel('x', 'fontsize', 16) ylabel('Sin(x)', 'fontsize', 16) title('Sin(x) Graph', 'fontsize', 16) grid on % axis equal legend() legend('boxoff') set(gp, 'linewidth', 2) set(hp, 'linewidth', 3) % set(hp, 'linestyle', '--') hold off print(gfig, 'plot.pdf', '-dpdf'); print(gfig, 'plot.svg', '-dsvg'); print(gfig, 'plot.png', '-dpng', '-r300'); % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% * To run a script -- put it in the file fname.m -- simply type fname at the command line Use Octave * Number format format hex format compact format longEng format long format shortEng * Very C-Like syntax Note opening / writing / closing a file just like C >> function y = gavg(x, y, z) y = (x + y + z) / 3; end >> gavg(1, 5, 4) ans = 3.3333 >> fp = fopen('crapX.txt', 'w'); >> fprintf(fp, 'hello %f = gavg(%f, %f, %f)\n', gavg(1, 5, 7), 1, 5, 7); >> fclose(fp); >> source /hwnet/common_r2/env/toolsetup.mathworks_r2017b https://matlabacademy.mathworks.com/ https://www.mathworks.com/help/matlab/getting-started-with-matlab.html * ans is the last calculation answer * a = [1 2 3 4] a = [1 2 3; 4 5 6; 7 8 9] z = zeros(5, 1) a` --> transpose a * inv(a) --> inverse of a det(a) --> determinite of a amazing :det(a) from above does not give 0 * format long format short * A = [a,a] --> sideways concatenation A = [a;a] --> vertical concatenation * A(4,2) --> element in row 4, col 2 A(8) for a for a 4x4 matrix is last element in 2nd column * A(1:3,2) A(3,:) --> row 3 of matrix A * B = 0:10:100