School of Computer Science

# Computer Science COMP 199 (Winter term)Excursions in Computing Science

```%MATLABpak09: cosine, makeDFT, makeDiag, unmakeDiag, binarize, gaussian

%cosine.m THM 060802
%function Cn = cosine(n,f) generates n-point cosine vector of frequency f
function Cn = cosine(n,f)
for j=1:n, Cn(j) = cos(2*pi*(j-1)*f/n); end;

%makeDFT.m THM 060704
%function Fn = makeDFT(n) generates n*n Fourier transform matrix
function Fn = makeDFT(n)
for j=1:n, for k=1:n, En(j,k)=mod((j-1)*(k-1),n); end; end;
Fn = exp((i*2*pi/n).*En)./sqrt(n);

%makeDiag.m THM 060704
%function Dn = makeDiag(n,Mn)
%converts n*n matrix Mn into 1*n diagonal Dn 00,01,10,02,11,20, ..
%e.g.	j\k	0  1  2  3
%	  	0	0  1  3  5
%		1	2  4  7 10
%		2	5  8 11 13
%		3	9 12 14 15
function Dn = makeDiag(n,Mn)
j = 0; startNextLev = 1; levlen = 1; lev = 0;
for pos = 0:n^2 - 1
if pos==startNextLev		% levels defined by j+k = 0, 1, 2, 3, ..
lev = lev + 1;
levlen = (lev=n)*(2*n-1-lev);
% if lev=n		% this will happen when row 0 is full; starts are now in col n-1
j = k+1-n;
k = n-1;
end
Dn(pos+1) = Mn(j+1,k+1);	% reset origins from 0 to 1
j = j + 1;
end

%unmakeDiag.m THM 060704
%function Mn = unmakeDiag(n,Dn)
%converts 1*n diagonal Dn into n*n matrix Mn 00,01,10,02,11,20, ..
function Mn = unmakeDiag(n,Dn)		% <-- this line differs from makeDiag
j = 0; startNextLev = 1; levlen = 1; lev = 0;
for pos = 0:n^2 - 1
if pos==startNextLev		% levels defined by j+k = 0, 1, 2, 3, ..
lev = lev + 1;
levlen = (lev=n)*(2*n-1-lev);
% if lev=n		% this will happen when row 0 is full; starts are now in col n-1
j = k+1-n;
k = n-1;
end
Mn(j+1,k+1) = Dn(pos+1);	% <-- this line differs from makeDiag
j = j + 1;
end

%binarize.m THM 060704
%function B = binarize(A) converts A elements to 0 if abs < .5 or 1 otherwise
function B = binarize(A)
s = size(A);		% assume 2D array
for j = 1:s(1)
for k = 1:s(2)
B(j,k) = abs(A(j,k))>=0.5;
end
end

%gaussian.m THM 060803
%function Gn = gaussian(n,s) generates n-point gaussian vector of std dev s
function Gn = gaussian(n,s)
%c = ceil(n/2);	% symmetrize about zero: 1,2,3->-1,0,1; 1,2,3,4->-1,0,1,2
%for j=1:n, Gn(j) = exp(-(j-c)^2/2/s^2)/s/sqrt(2*pi); end; %mean is mid-range
for j=1:n, Gn(j) = exp(-(j-1)^2/2/s^2)/s/sqrt(2*pi); end; %mean is zero

```