function dftm=dftcolor
clear all
close all
clc
set(0,'defaultaxesfontsize',20);
N=input('N=?\n');
dftm=zeros(N,N);
repit=zeros(N,N);
tabcolor=zeros(N,3);

%building the DFT matrix (before I found the dftmtx command) as well as the
%repit matrix, which contains a different integer for each distinct entry
%in the DFT matrix
for lcv1=1:N
    for lcv2=1:N
        dftm(lcv1,lcv2)=exp((-2*pi*1i*(lcv1-1)*(lcv2-1))/N);
        dftm(lcv1,lcv2)=zerosmall(dftm(lcv1,lcv2),10);
        repit(lcv1,lcv2)=(lcv1-1)*(lcv2-1)-N*floor((lcv1-1)*(lcv2-1)/N)+1;
    end
end

%tabcolor is a vector of color vectors. In the hsv color scheme, [0 1 1] is
%red and [2/3 0 0] is blue, with a 1-to-1 mapping between 0-->2/3 and
%red-->blue in the visible spectrum. I then use hsv2rgb to get the rgb
%color vector that the TEXT command wants as an argument.
for lcv=1:N
    tabcolor(lcv,:)=hsv2rgb([-2/3*lcv/(N-1)+2/3*N/(N-1) 1 1]);
end

%This plots the actual values of the DFT matrix with color. Because the
%numbers ran into each other, the plot looks bad and I commented it out.
% figure
% axis([0 N+1 0 N+1])
% for lcv1=1:N
%     for lcv2=1:N
%         eval(['text(1*' num2str(lcv1) ',1*' num2str((N+1)-lcv2) ',[''{\color[rgb]{' num2str(tabcolor((repit(lcv1,lcv2)),1)) ',' num2str(tabcolor((repit(lcv1,lcv2)),2)) ',' num2str(tabcolor((repit(lcv1,lcv2)),3)) '}' num2str(dftm(lcv1,lcv2)) '}''])'])
%     end 
% end
set(0,'defaultaxesfontsize',20);
figure
axis([0 N+1 0 N+1])
for lcv1=1:N
    for lcv2=1:N
        eval(['text(1*' num2str(lcv1) ',1*' num2str((N+1)-lcv2) ',[''{\color[rgb]{' num2str(tabcolor((repit(lcv1,lcv2)),1)) ',' num2str(tabcolor((repit(lcv1,lcv2)),2)) ',' num2str(tabcolor((repit(lcv1,lcv2)),3)) '}' num2str(repit(lcv1,lcv2)) '}''])'])
    end 
end

%zerosmall was used to set very small values of the input to zero
%e.g. 1 + 3.2363E-16*i  --->  1 + 0*i
function output = zerosmall(input_complex,maxzero)
input_real=abs(real(input_complex));
input_imag=abs(imag(input_complex));

if input_real~=0
    z_chk=1;
    z_cnt=0;
    while z_chk==1
        z_cnt=z_cnt+1;
        input_real=input_real*10;
        if floor(input_real)~=0
            z_chk=0;
        end
    end

    if z_cnt>maxzero
        input_real=0;
    else
        input_real=real(input_complex);
    end
end

if input_imag~=0
    z_cnt=0;
    z_chk=1;
    while z_chk==1
        z_cnt=z_cnt+1;
        input_imag=input_imag*10;
        if floor(input_imag)~=0
            z_chk=0;
        end
    end

    if z_cnt>maxzero
        input_imag=0;
    else
        input_imag=imag(input_complex);
    end
end

output=input_real+input_imag*1i;