%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%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 isnot required.
%go to www.blinkdagger.com for more matlab tutorials!
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

clear all
fo = 4;   %frequency of the cos 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 cosine curve

%plot the cosine curve in the time domain
cosPlot = figure;
plot(t,y)
xlabel('time (seconds)')
ylabel('y(t)')
title('Sample Sine Wave')
set(cosPlot,'Position',[500,500,500,300])
grid

%plot the frequency spectrum of the cosine curve in the frequency domain
stemPlot = figure;
stem([-4,4],[1 1])
axis([-6,6,0,1.5])
xlabel('Frequency (Hz)')
ylabel('Amplitude')
title('Frequency Spectrum of Sine Wave')
set(stemPlot,'Position',[500,500,500,300])
grid

%plot the frequency spectrum using the matlab FFT command
matlabFFT = figure;  %create a new figure
YfreqDomain = fft(y); %take the fft of our sin wave, y(t)

stem(abs(YfreqDomain));  %use abs command to get the magnitude
%similary, we would use angle command to get the phase plot! 
%we'll discuss phase in another post though!

xlabel('Sample Number')
ylabel('Amplitude')
title('Using the FFT command')
grid
set(matlabFFT,'Position',[500,500,500,300])
axis([0,100,0,120])


[YfreqDomain,frequencyRange] = centeredFFT(y,Fs);
centeredFFT = figure;
stem(frequencyRange,abs(YfreqDomain));
set(centeredFFT,'Position',[500,500,500,300])
xlabel('Freq (Hz)')
ylabel('Amplitude')
title('Using the centeredFFT function')
grid
axis([-6,6,0,1.5])

[YfreqDomain,frequencyRange] = positiveFFT(y,Fs);
positiveFFT = figure;
stem(frequencyRange,abs(YfreqDomain));
set(positiveFFT,'Position',[500,500,500,300])
xlabel('Freq (Hz)')
ylabel('Amplitude')
title('Using the positiveFFT function')
grid
axis([0,20,0,1.5])


% 
% 
% plot(unwrap(angle(YfreqDomain)));  %use abs to get the magnitude
% xlabel('Sample Number')
% ylabel('Amplitude')
% 
% grid