N = 32;
j = sqrt(-1);
disp('Rather use demo_spectrumest for demonstrations');

% Make the signal
nv=-3:N+3;
sv=zeros(size(nv));
for i=1:length(nv);
  n = nv(i);
  sv(i) = cos(pi/3*n) + 0.75*cos(2*pi/3*n);
end

ws = zeros(size(nv));
ws(4:N+4-1) = 1;

svw=sv.*ws;

% Slowly calculate FT at required points
w=-pi:0.01:pi;
mH = zeros(size(w));
for n=1:length(w)
  cw = w(n);
  cmH = 0;
  for p=0:N-1
    cmH = cmH + sv(4+p)*exp(sqrt(-1)*cw*p);
  end
  mH(n) = abs(cmH);
end

subplot(3,1,1)
ph = stem(nv,sv,'filled');
set(ph(1),'MarkerSize',2);
hold on; plot(nv,zeros(size(nv)),'k');  hold off;
xt = [0, N];
%xtl = {'0', 'N'};
set(gca,'XTick',xt);
%set(gca,'XTickLabel',xtl);
%yt = [0, 1];
%set(gca,'YTick',yt);
ylabel('x[n]');
ms = 1.1*max(abs(sv));
axis([-3 N+3 -ms ms]);
%paperify(11,7,0.9,0.8);

subplot(3,1,2)
ph = stem(nv,ws,'filled');
set(ph(1),'MarkerSize',2);
xt = [0, N];
%xtl = {'0', 'N'};
set(gca,'XTick',xt);
%set(gca,'XTickLabel',xtl);
%yt = [0, 1];
%set(gca,'YTick',yt);
ylabel('wr[n]');
axis([-3 N+3 0 1.1]);
%paperfy(11,7,0.9,0.8);

subplot(3,1,3)
ph = stem(nv,svw,'filled');
set(ph(1),'MarkerSize',2);
hold on; plot(nv,zeros(size(nv)),'k');  hold off;
xt = [0, N];
%xtl = {'0', 'N'};
set(gca,'XTick',xt);
%set(gca,'XTickLabel',xtl);
%yt = [0, 1];
%set(gca,'YTick',yt);
xlabel('n');  ylabel('xw[n]');
axis([-3 N+3 -ms ms]);
%paperify(11,7,0.9,0.8);

%print(gcf, '-depsc2', 'dft_fig17.eps');
%close(gcf);


% DFT_FIG18

nv=-3:N+3;
ws = zeros(size(nv));
ws(4:N+4) = 1;
figure;

% Slowly calculate FT of window
w=-pi:0.01:pi;
mW = zeros(size(w));
for n=1:length(w)
  cw = w(n);
  cmW = 0;
  for p=0:N-1
    cmW = cmW + ws(4+p)*exp(sqrt(-1)*cw*p);
  end
  mW(n) = abs(cmW);
end

% Make the signal
nv=-3:N+3;
sv=zeros(size(nv));
for i=1:length(nv);
  n = nv(i);
  sv(i) = cos(pi/3*n) + 0.75*cos(2*pi/3*n);
end
sv = sv.*ws;

% Slowly calculate FT of signal
w=-pi:0.01:pi;
mXw = zeros(size(w));
for n=1:length(w)
  cw = w(n);
  cmXw = 0;
  for p=0:N-1
    cmXw = cmXw + sv(4+p)*exp(sqrt(-1)*cw*p);
  end
  mXw(n) = abs(cmXw);
end

% Ideal frequency response
wideal=[-2*pi/3 -pi/3 pi/3 2*pi/3];
mX = [0.75 1 1 0.75];

subplot(3,1,1)
ph = stem(wideal,mX,'^','filled');
%set(ph, 'Marker', '^');
xt = [-pi, -2*pi/3, -pi/3, 0, pi/3, 2*pi/3, pi];
xtl = {'-pi', '-2pi/3', '-pi/3', '0', 'pi/3', '2pi/3', 'pi'};
set(gca,'XTick',xt);
set(gca,'XTickLabel',xtl);
%yt = [0, N];
%set(gca,'YTick',yt);
ylabel('|X(ejw)|');
axis([-pi pi 0 1.1]);
%paperify(11,7,0.9,0.8);

subplot(3,1,2)
ph = plot(w,mW);
xt = [-pi, -2*pi/3, -pi/3, 0, pi/3, 2*pi/3, pi];
xtl = {'-pi', '-2pi/3', '-pi/3', '0', 'pi/3', '2pi/3', 'pi'};
set(gca,'XTick',xt);
set(gca,'XTickLabel',xtl);
yt = [0, N];
set(gca,'YTick',yt);
ylabel('|Wr(ejw)|');
axis([-pi pi 0 N]);
%paperify(11,7,0.9,0.8);

subplot(3,1,3)
ph = plot(w,mXw);
xt = [-pi, -2*pi/3, -pi/3, 0, pi/3, 2*pi/3, pi];
xtl = {'-pi', '-2pi/3', '-pi/3', '0', 'pi/3', '2pi/3', 'pi'};
set(gca,'XTick',xt);
set(gca,'XTickLabel',xtl);
%yt = [0, N];
%set(gca,'YTick',yt);
xlabel('omega');  ylabel('|Xw(ejw)|');
axis([-pi pi 0 N]);
%paperify(11,7,0.9,0.8);
