% basic signal
clear all
close all
clc
set(0,'DefaultFigureWindowStyle','docked')
dt = .01; %Discrete time step
Fs = 1/dt; %Sampling frequency
m = 401; %# of points in time signal
t = [0:(m-1)]*dt; %Time vector
x = 2*cos(2*pi*15*t); %Basic signal
subplot(4,1,1)
plot(t,x)
xlabel('time(s)'),ylabel('Amp(v)')
To = max(t) - min(t); %fundamental period
fo = 1/To; %fundamental freq
wo = 2*pi*fo; %fundamental freq (radians)
%% Looking at entire signal
% assume entire signal is periodic w/ freq fo.
% Using FFT to find Ck's
N = length(x); %Aliasing will occur when this # is close to
%the same length as the entire signal. Best choice
%for the amount of Ck's to find is 1/2*length of
%signal.
% N = 2^10;
F = fft(x,N); %Take N point FFT
f = Fs*[0:(N-1)]/N; %create frequency bins
% F = F(1:(N-1)/2);
% f = f(1:(N-1)/2);
F = F(1:m); %Use FFT points up to length of signal
f = f(1:m); %Use frequency points up to length of signal
an = 2/N*real(F); %Find Fourier Coefficients
an(1) = an(1)/2;
bn = -2/N*imag(F);
D = an + 1j*bn; %Combined FC's
subplot(4,1,2)
stem(an), hold on
stem(bn,'-xr')
xlabel('Ck index'),ylabel('Ck')
subplot(4,1,3)
stem(f,abs(D))
xlabel('Freq(Hz)'),ylabel('Mag Ck')
%% Recreate the signal
% t2 = linspace(0,To,length(F));
% t2 = 0:dt:To;
% t2 = t;
t2 = 0:dt:To;
X = ones(size(t));
X = an(1)*X;
for i = 1:200 %1:(length(F)-1)/2
X = X + an(i+1)*cos((i+1)*wo*t2) + bn(i+1)*sin((i+1)*wo*t2);
end
subplot(4,1,1)
hold on, plot(t,X,'--r'),axis tight
xlabel(''),ylabel('Amp(v)')