%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%this matlab Script created by Quan Quach on 02/22/2008
%these matlab functions/scripts are free to use.
%it would be appreciated if we are credited if this code is used
%but it is not required.
%go to www.blinkdagger.com for more matlab tutorials!
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
close all
clear all
fo = 4;   %frequency of the sine wave
Fs = 100; %sampling rate
Ts = 1/Fs; %sampling time interval
t = 0:Ts:1-Ts; %sampling period
n = length(t); %number of samples
y = 2*sin(2*pi*fo*t); %the sine curve

fftMultiPlot = figure;
set(fftMultiPlot,'Position',[500,500,600,600])

nextPower = nextpow2(length(y));
[a1,b1] = centeredFFT_zero_padding(y,Fs,2^(nextPower));
[a2,b2] = centeredFFT_zero_padding(y,Fs,2^(nextPower+1));
[a3,b3] = centeredFFT_zero_padding(y,Fs,2^(nextPower+2));

subplot(3,1,1)
stem(b1,abs(a1)),axis([-10 10 0 1.2])
ylabel('Magnitude')
title('FFT of Input Signal Using N = 128', 'FontWeight','Bold')
grid
subplot(3,1,2)
stem(b2,abs(a2)),axis([-10 10 0 1.2])
title('FFT of Input Signal Using N = 256', 'FontWeight','Bold')
ylabel('Magnitude')
grid
subplot(3,1,3)
stem(b3,abs(a3)),axis([-10 10 0 1.2])
title('FFT of Input Signal Using N = 512', 'FontWeight','Bold')
ylabel('Magnitude')
xlabel('Frequency (Hz)')
grid
inputSignalTime = figure;
set(inputSignalTime,'Position',[500,500,600,600])


Y1 = fft(y,2^(nextPower));
y1 = ifft(Y1);
Y2 = fft(y,2^(nextPower+1));
y2 = ifft(Y2);
Y3 = fft(y,2^(nextPower+2));
y3 = ifft(Y3);
subplot(3,1,1)
plot(y1),xlim([0 512])
title('Input Signal Padded with Zeros up to N = 128', 'FontWeight','Bold')
ylabel('Amplitude')
grid
subplot(3,1,2)
plot(y2),xlim([0 512])
title('Input Signal Padded with Zeros up to N = 256', 'FontWeight','Bold')
ylabel('Amplitude')
grid
subplot(3,1,3)
plot(y3),xlim([0 512])
title('Input Signal Padded with Zeros up to N = 512', 'FontWeight','Bold')
ylabel('Amplitude')
xlabel('Sample Number')
grid
% 
timeDomainSignal = figure;
f1 = 4;   %frequency of the sine wave
f2 = 4.5;
Fs = 100; %sampling rate
Ts = 1/Fs; %sampling time interval
t = 0:Ts:2-Ts; %sampling period
n = length(t); %number of samples
y = 100*sin(2*pi*f1*t) + 100*sin(2*pi*f2*t); %the sine curve
plot(t,y)
title('Sample Signal', 'FontWeight','Bold')
xlabel('Time (seconds)')
ylabel('Amplitude')
set(timeDomainSignal,'Position',[500,500,600,300])
grid

fftPlot1 = figure
set(fftPlot1,'Position',[500,500,600,300])
[a,b] = positiveFFT(y,Fs);

plot(b,abs(a))
title('FFT of Sample Signal with No Zero Padding', 'FontWeight','Bold')
xlabel('Freq (Hz)')
ylabel('Magnitude')
grid
xlim([0 10])

fftPlot2 = figure
set(fftPlot2,'Position',[500,500,600,300])
[a,b] = positiveFFT_zero_padding(y,Fs,2^(nextpow2(length(y))+3));
plot(b,abs(a))
title('FFT of Sample Signal: Zero Padding up to N = 1024', 'FontWeight','Bold')
xlabel('Freq (Hz)')
ylabel('Magnitude')
grid
xlim([0 10])
