% EE 438 Digital Signal Processing with Applications 
% Spectral Analysis
% file: spectral_analysis.m
% Windowing a Cosine Signal
%---------------------------------------------------------------
% Initialize
clear all;
close all;
%---------------------------------------------------------------
% Parameters
fs = 20e3		% sampling frequency (cycles/sec)
fa = 7e3		% cosine frequency (cycles/sec)
%---------------------------------------------------------------
rad_per_sample = 2*pi*fa/fs		% radians per sample
number_of_cycles = 128*fa/fs
pause
nx = 1:1:128;
x = cos(rad_per_sample*nx);
figure(1)
plot(x)
title('Cosine')
pause

X = fft(x);
X = abs(X);
X=fftshift(X);
w=linspace(-fs/2,fs/2,128);
figure(2)
plot(w,X)
xlabel('freq (Hz)')
title('Cosine Spectrum')
pause

ny = 1:1:1024;
y = zeros(1,length(ny));
y(1:128) = x;
figure(3)
plot(y)
title('Truncated Cosine')
pause
Y = fft(y);
Y = abs(Y);
Y = fftshift(Y);
w = linspace(-fs/2,fs/2,1024);
figure(4)
plot(w,Y)
xlabel('freq (Hz)')
title('Spectrum of Truncated Cosine')
pause
 
X=fftshift(X);	% rotate back
Y=fftshift(Y);
Xint(1:8:1024) = X;
figure(5)
plot(ny(257:257+127), Y(257:257+127), 'b-', ny(257:8:257+127), Xint(257:8:257+127), 'ro')
legend('truncated cosine spectrum','original cosine spectrum',2)
title('Overlay of both Spectra')

