format compact;
clear all;
close all;
clc;

disp(' Example1: Low-pass filter:');
input('');
% input('>> deg = pi/180');
% deg=pi/180
disp(' Define a sequence:');
input('>> N = 16');
N = 16
input('>> nLength = 2*N+1');
nLength = 2*N+1

input('>> fn = zeros(nLength,1)');
input('>> f(N)= 1; f(N+1) = 2; f(N+2) = 1;');
fn = zeros(nLength,1); 
fn(N)= 1; fn(N+1) = 2; fn(N+2) = 1;
fn
input('')
disp('>> figure(1);');
disp('>> subplot(3,1,1)');
disp('>> plot(fn,''ro-'');');
disp('>> set(gca, ''xLim'', [1,nLength], ''yLim'' , [-0.2 2.2]);');
disp('>> title(''fn sequence'');');
figure(1);
subplot(3,1,1);
plot(fn,'ro-');
set(gca, 'xLim', [1,nLength], 'yLim' , [-0.2 2.2]);
title('fn sequence');
input('');

disp(' Doing DFT by FFT');
input('>> ffn = fftshift(fft(fn))');
ffn = fftshift(fft(fn))
input('');
disp('>> ffn_r = real(ffn);');
ffn_r = real(ffn);
disp('>> ffn_i = imag(ffn);');
ffn_i = imag(ffn);
input('')
disp('>> subplot(3,1,2);');
subplot(3,1,2);
disp('>> plot(ffn_r);');
plot(ffn_r);
disp('>> title(''Real part'')');
title('Real part');
disp('>> subplot(3,1,3);');
subplot(3,1,3);
disp('>> plot(ffn_i);');
plot(ffn_i);
disp('>> title(''Imaginary part'');');
title('Imaginary part');

input('');
disp(' Not the same as the calculation result. Why?');

input('');
input(' Look at X-axis');
disp(' We should have this');
subplot(3,1,1);
plot(-N:N, fn,'ro-');
set(gca, 'xLim', [-N,N], 'yLim' , [-0.2 2.2]);
title('fn sequence');
input('');
disp(' Circular Shift fn');
input('>> fn = circshift(fn,N+1);');
fn = circshift(fn,N+1);
figure(1);
subplot(3,1,1);
plot(fn,'go-');
set(gca, 'xLim', [1,nLength], 'yLim' , [-0.2 2.2])
title('fn sequence');
input(' This is the right input to FFT');
disp(' Do everything again');
ffn = fftshift(fft(fn));
ffn_r = real(ffn);
ffn_i = imag(ffn);
subplot(3,1,2);
plot(ffn_r,'bo-');
title('Real part');
set(gca, 'xLim', [1,nLength], 'yLim' , [-0.2 4.2])
subplot(3,1,3);
plot(ffn_i,'bo-');
title('Imaginary part');
set(gca, 'xLim', [1,nLength], 'yLim' , [-0.2 4.2])
