You are on page 1of 29

Signal Analysis Lab, Dept. of ECE, KL University, A.P., India.

Student Id No:150050085

Name: k.Ganasathwik   Branch:ECM Lab Section:15


Id No:150050085
  Project Group:9
Pre-lab Session In-Lab Session Post Lab session Viva Total Marks
work (15M) work (15M) work (10M) (10M) 50M

Remarks:

Date: Signature of the Instructor Marks awarded

Signal Analysis-Lab (15 ES 2002L)


Lab 7: Frequency Analysis: The Continuous Time Fourier
Series (CTFS)
Lab Report

Introduction: A Fourier series is an infinite sum of harmonic functions (sines and


cosines) with every term in the series having a frequency which is an integral multiple of
some “principal” frequency and an amplitude that varies inversely with its frequency. The

usefulness of such series is that any periodic function x(t ) with period T can be written
as a Fourier series as illustrated below.
Basic theory: The basic formulations for Fourier series is illustrated below.
1. Trigonometrical Fourier Series Representation:
The trigonometrical Fourier series is represented by
x(t )  a 0  a1 cos  0t  a 2 cos 2 0t  a 3 cos 3 0t  . . . . .
 b1 sin  0t  b2 sin 2 0t  b3 sin 3 0t  . . . . .

 a 0    a n cos n 0t  bn sin n 0t 
n 1

where the term a 0 is called constant or dc or average value

cos  0 and sin  0t are fundamental frequency components.

cos 2 0t and sin 2 0t are 2nd harmonic components

cos n 0t and sin n 0t are nth harmonic components, and

1
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

a n and bn are Fourier Series coefficients.


Formulas for evaluation of Fourier Series coefficients:
1 T 2 T 2 T
an  
T 0
x(t ) dt , an  
T 0
x (t ) cos n  0t dt and bn 
T 0 x(t )sin n  0t dt
Fourier Spectrum:

The magnitude spectrum An  a n2  bn2 , and

 b 
Phase spectrum  n  tan 1  a 
n
 n 
2. Exponential Fourier Series:
The exponential Fourier series is represented by

x(t )   c n e jn  0t
n 
The exponential Fourier series coefficients c n can be represented in terms of magnitude

and phase as c n  | c n | e j n and c n  | c n | e  j n  c n* , where * indicate complex

conjugate.

| c n |  1 a n2  bn2  1 A n , and  n  tan 1  abn 


2 2  n 
Examples:
Ex7.1: Demo: Trigonometrical Fourier Series representation of a square wave.

clear all; close all; clc;

% Generation of original square wave signal


tr = [-3 -2 -2 -1 -1 0 0 1 1 2 2 3];
yr = [-1 -1 1 1 -1 -1 1 1 -1 -1 1 1];

figure(1);
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':b','LineWidth',3);
xlabel('time (seconds)');
ylabel('Amplitude');
title('Original square wave signal');
axis([-1 3 -1.2 1.2]);

%-----------------------------------------------------------------------

% Parameters
N = 11; % summation limit (use N odd)

2
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

wo = pi; % fundamental frequency (rad/s)


c0 = 0; % dc bias
t = -3:0.01:3; % declare time values

% Compute x, the Fourier Series in trigonometric form


N = 1;
x = c0*ones(size(t)); % initialize yt to c0

for n = 1:2:N, % loop over series index n (odd)


cn = 2/(j*n*wo); % Fourier Series Coefficient
x = x + 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation
end

figure(2);
subplot(2,2,1)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS
plot(t,x,'k','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-3 3 -1.2 1.2]);

N = 5;
x = c0*ones(size(t)); % initialize yt to c0

for n = 1:2:N, % loop over series index n (odd)


cn = 2/(j*n*wo); % Fourier Series Coefficient
x = x + 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation
end

subplot(2,2,2)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS
plot(t,x,'b','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-3 3 -1.2 1.2]);

N = 11;
x = c0*ones(size(t)); % initialize yt to c0

for n = 1:2:N, % loop over series index n (odd)


cn = 2/(j*n*wo); % Fourier Series Coefficient
x = x + 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation
end

subplot(2,2,3)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;

3
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS


plot(t,x,'r','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-3 3 -1.2 1.2]);

N = 21;
x = c0*ones(size(t)); % initialize yt to c0

for n = 1:2:N, % loop over series index n (odd)


cn = 2/(j*n*wo); % Fourier Series Coefficient
x = x + 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation
end

subplot(2,2,4)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS
plot(t,x,'m','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-3 3 -1.2 1.2]);
Original square wave signal

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1

-1 -0.5 0 0.5 1 1.5 2 2.5 3


time (seconds)

4
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

Trig. Fourier Series with N = 1 Trig. Fourier Series with N = 5


1 1

0.5 0.5
y(t)

y(t)
0 0

-0.5 -0.5

-1 -1
-2 0 2 -2 0 2
t (seconds) t (seconds)
Trig. Fourier Series with N = 11 Trig. Fourier Series with N = 21
1 1

0.5 0.5
y(t)

y(t)
0 0

-0.5 -0.5

-1 -1
-2 0 2 -2 0 2
t (seconds) t (seconds)

Ex7.2: Demo: Complex Exponential Fourier Series representation of a sawtooth wave.


Refer Matlab Script : SA7_CTFS2.m
clear all; close all; clc;

% Generation of original sawtooth signal


tr = [-1 0 0 1 1 2 2];
xr = [0 1 0 1 0 1 0];

figure();
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,xr,':','LineWidth',3);
xlabel('time (seconds)');ylabel('Amplitude');
title('Original sawtooth signal');
axis([-1 2 -0.1 1.2]);

% Plot Truncated Fourier Series Approximation (N = 1)

N = 1; % define number of terms to use (n = -N..N)


c0 = 0.5; % define dc bias coefficient
t = -1:0.001:2; % define time values for y(t)
y = c0 * ones(size(t)); % let initial y = c0 (dc bias) for all times

for n = -N:-1, % compute y for negative n


cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;
% compute y for positive n and add to y
for n = 1:N, % found using negative n
cn = exp(j*pi/2)/(2*pi*n);

5
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

y = y + real(cn * exp(j*n*2*pi*t));
end;

figure();
subplot(2,2,1); % plot approximation
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(t,y,'r','LineWidth',2); hold on;
plot(tr,xr,':','LineWidth',1);
xlabel('time (seconds)');
ylabel('y(t) approximation');
title('Truncated FS, N = 1');
axis([-1 2 -0.1 1.2]);

% Plot Truncated Fourier Series Approximation (N = 2)

N = 2; % define number of terms to use (n = -N..N)


c0 = 0.5; % define dc bias coefficient
t = -1:0.001:2; % define time values for y(t)
y = c0 * ones(size(t)); % let initial y = c0 (dc bias) for all times

for n = -N:-1, % compute y for negative n


cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;
% compute y for positive n and add to y
for n = 1:N, % found using negative n
cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;

subplot(2,2,2); % plot approximation


plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(t,y,'m','LineWidth',2);hold on;
plot(tr,xr,':','LineWidth',1);
xlabel('time (seconds)');
ylabel('y(t) approximation');
title('Truncated FS, N = 2');
axis([-1 2 -0.1 1.2]);

% Plot Truncated Fourier Series Approximation (N = 3)

N = 3; % define number of terms to use (n = -N..N)


c0 = 0.5; % define dc bias coefficient
t = -1:0.001:2; % define time values for y(t)
y = c0 * ones(size(t)); % let initial y = c0 (dc bias) for all times

for n = -N:-1, % compute y for negative n


cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));

6
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

end;
% compute y for positive n and add to y
for n = 1:N, % found using negative n
cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;

subplot(2,2,3); % plot approximation


plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(t,y,'k','LineWidth',2);hold on;
plot(tr,xr,':','LineWidth',1);
xlabel('time (seconds)');
ylabel('y(t) approximation');
title(' Truncated FS, N = 3');
axis([-1 2 -0.1 1.2]);

% Plot Truncated Fourier Series Approximation (N = 10)

N = 10; % define number of terms to use (n = -N..N)


c0 = 0.5; % define dc bias coefficient
t = -1:0.001:2; % define time values for y(t)
y = c0 * ones(size(t)); % let initial y = c0 (dc bias) for all times

for n = -N:-1, % compute y for negative n


cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;
% compute y for positive n and add to y
for n = 1:N, % found using negative n
cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;

subplot(2,2,4); % plot approximation


plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(t,y,'--r','LineWidth',2);hold on;
plot(tr,xr,':','LineWidth',1);
xlabel('time (seconds)');
ylabel('y(t) approximation');
title('Truncated FS, N = 10');
axis([-1 2 -0.1 1.2]);

7
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

Original sawtooth signal

0.8
Amplitude

0.6

0.4

0.2

-1 -0.5 0 0.5 1 1.5 2


time (seconds)

Truncated FS, N = 1 Truncated FS, N = 2

1 1
y(t) approximation

y(t) approximation

0.5 0.5

0 0
-1 0 1 2 -1 0 1 2
time (seconds) time (seconds)
Truncated FS, N = 3 Truncated FS, N = 10

1 1
y(t) approximation

y(t) approximation

0.5 0.5

0 0
-1 0 1 2 -1 0 1 2
time (seconds) time (seconds)

Ex7.3: Demo: Complex Exponential Fourier Series representation of a square wave as


well as amplitude and phase spectrum.
Refer Matlab Script : SA7_CTFS3.m
clear all; close all; clc;

% Generation of original square wave signal


tr = [-3 -2 -2 -1 -1 0 0 1 1 2 2 3];
yr = [-1 -1 1 1 -1 -1 1 1 -1 -1 1 1];

figure(1);
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':r','LineWidth',3);
xlabel('time (seconds)');
ylabel('Amplitude');

8
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

title('Original square wave signal');


axis([-1 3 -1.2 1.2]);

%-----------------------------------------------------------------------

% Parameters
N = 11; % summation limit (use N odd)
wo = pi; % fundamental frequency (rad/s)
c0 = 0; % dc bias
t = -3:0.01:3; % declare time values

figure(1) % put first two plots on figure 1

% Compute yce, the Fourier Series in complex exponential form

yce = c0*ones(size(t)); % initialize yce to c0

for n = -N:2:N, % loop over series index n (odd)


cn = 2/(j*n*wo); % Fourier Series Coefficient
yce = yce + real(cn*exp(j*n*wo*t)); % Fourier Series computation
end
figure(2);
% subplot(2,1,1)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',1);hold on;
plot(t,yce,'m','LineWidth',2); % plot truncated exponential FS

xlabel('t (seconds)'); ylabel('y(t)');


ttle=['Truncated Exponential Fourier Series with N = ',num2str(N)];
title(ttle); axis([-3 3 -1.2 1.2]);

% Draw the amplitude spectrum from exponential Fourier Series

figure(3) % put next plots on figure 3

subplot(2,1,1)

stem(0,c0,'r','fill','LineWidth',2); hold on; % plot c0 at nwo = 0

for n = -N:2:N, % loop over series index n


cn = 2/(j*n*wo); % Fourier Series Coefficient
stem(n*wo,abs(cn),'m','fill','LineWidth',2) % plot |cn| vs nwo
end
for n = -N+1:2:N-1, % loop over even series index n
cn = 0; % Fourier Series Coefficient
stem(n*wo,abs(cn),'b','fill','LineWidth',2); % plot |cn| vs nwo
end

xlabel('w (rad/s)')

9
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

ylabel('|cn|')
ttle = ['Amplitude Spectrum with N = ',num2str(N)];
title(ttle); grid on

% Draw the phase spectrum from exponential Fourier Series

subplot(2,1,2)
stem(0,angle(c0)*180/pi,'k','fill','LineWidth',2); % plot angle of c0 at nwo = 0
hold on ;

for n = -N:2:N, % loop over odd series index n


cn = 2/(j*n*wo); % Fourier Series Coefficient
stem(n*wo,angle(cn)*180/pi,'b','fill','LineWidth',2); % plot |cn| vs nwo
end
for n = -N+1:2:N-1, % loop over even series index n
cn = 0; % Fourier Series Coefficient
stem(n*wo,angle(cn)*180/pi,'r','fill','LineWidth',2); % plot |cn| vs nwo
end

xlabel('w (rad/s)')
ylabel('angle(cn) (degrees)')
ttle = ['Phase Spectrum with N = ',num2str(N)];
title(ttle);
grid;

10
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

Truncated Exponential Fourier Series with N = 11

0.8

0.6

0.4

0.2
y(t)

-0.2

-0.4

-0.6

-0.8

-1

-3 -2 -1 0 1 2 3
t (seconds)

Original square wave signal

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1

-1 -0.5 0 0.5 1 1.5 2 2.5 3


time (seconds)

11
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

Amplitude Spectrum with N = 11


0.8

0.6

|cn|
0.4

0.2

0
-40 -30 -20 -10 0 10 20 30 40
w (rad/s)
Phase Spectrum with N = 11
100
angle(cn) (degrees)

50

-50

-100
-40 -30 -20 -10 0 10 20 30 40
w (rad/s)

Pre lab work:

(a)Study and understand the basic theory required for this lab.
(b) Run the Matlab scripts given for examples and study the results.
(c)Perform the analytical solutions for the given laboratory exercises.
(d) Prepare the Matlab codes for the given laboratory exercises well before the
commencement of lab scheduled time. Discuss with your lab instructors.

Lab Exercise-7
Exercise 7.1: Consider a periodic square wave (odd signal) shown below.

Justify that the trigonometrical and exponential Fourier series are represented as below.
Trigonometrical Fourier Series Coefficients: The mathematical equations for given
signal can be written as

 A, 0t  T
 2
x(t )  
T
 A, 2
t T

The given signal is odd signal and have rotational symmetry. That is x(t )   x(t ) and

x(t  T2 )   x(t ) . Therefore the Fourier series coefficients consists only odd harmonics.

12
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

a 0  0, a n  0, bn  4 A , n  odd
n
Then the Trigonometrical Fourier Series is represented by
4A  1 1 1 
x(t )   sin  0t  sin 3 0t  sin 5 0t  sin 7 0t  . . . .
  3 5 7 
Similarly, the exponential Fourier coefficients are represented by

0, n  even
cn   j2 A
  n , n  odd
Then the Fourier synthesis is represented by
j2 A j2A j2A j2A
x(t )      5 e  j 5ω0t  3 e  j 3ω0t   e  jω0t   e jω0t
j2 A j2A
 3 e j 3ω0t  5 e j 5ω0t    

Let A  1 , T  1 . Develop Matlab code to determine and plot the magnitude and phase

spectrum for the number of coefficients N  1, N  5, N  13 , both in trigonometrical and


exponential Fourier series.

clear all; close all; clc;


% Generation of original square wave signal
tr = [-1.5 -1.5 -1 -1 -0.5 -0.5 0 0 0.5 0.5 1 1 1.5 1.5 2 2];
yr = [ 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1 1 -1 -1 1];
figure(1);
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':b','LineWidth',3);
xlabel('time (seconds)');
ylabel('Amplitude');
title('Original square wave signal');
axis([-1 2 -1.5 1.5]);

%-----------------------------------------------------------------------

% Parameters
N = 1; % summation limit (use N odd)
wo = pi; % fundamental frequency (rad/s)
c0 = 0; % dc bias
t = -1:0.001:1; % declare time values

% Compute x, the Fourier Series in trigonometric form


N = 1;
x = c0*ones(size(t)); % initialize yt to c0

for n = 1:2:N, % loop over series index n (odd)


cn = 4/(n*wo); % Fourier Series Coefficient
x = 2*abs(cn)*sin(n*wo*t+angle(cn)); % Fourier Series computation
end

13
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

figure(2);
subplot(3,1,1)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS
plot(t,x,'k','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-1.5 1.5 -2.5 2.5]);

N = 5;
x = c0*ones(size(t)); % initialize yt to c0

for n = 1:2:N, % loop over series index n (odd)


cn = 4/(n*wo); % Fourier Series Coefficient
x = 2*abs(cn)*sin(n*wo*t+angle(cn)); % Fourier Series computation
end

subplot(3,1,2)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS
plot(t,x,'b','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-1.5 1.5 -1 1]);

N = 13;
x = c0*ones(size(t)); % initialize yt to c0

for n = 1:2:N, % loop over series index n (odd)


cn = 4/(n*wo); % Fourier Series Coefficient
x = 2*abs(cn)*sin(n*wo*t+angle(cn)); % Fourier Series computation
end

subplot(3,1,3)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS
plot(t,x,'r','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-1.5 1.5 -1 1]);
% Draw the amplitude spectrum from exponential Fourier Series for n=1

figure(3) % put next plots on figure 3

subplot(2,1,1)
N=1;
stem(0,c0,'r','fill','LineWidth',2); hold on; % plot c0 at nwo = 0

for n = -N:2:N, % loop over series index n


cn = 0 ; % Fourier Series Coefficient
stem(n*wo,abs(cn),'m','fill','LineWidth',2) % plot |cn| vs nwo
end

14
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

for n = -N+1:2:N-1, % loop over even series index n


cn = 4/(n*wo); % Fourier Series Coefficient
stem(n*wo,abs(cn),'b','fill','LineWidth',2); % plot |cn| vs nwo
end

xlabel('w (rad/s)')
ylabel('|cn|')
ttle = ['Amplitude Spectrum with N = ',num2str(N)];
title(ttle); grid on

% Draw the phase spectrum from exponential Fourier Series

subplot(2,1,2)
stem(0,angle(c0)*180/pi,'k','fill','LineWidth',2); % plot angle of c0 at nwo = 0
hold on ;

for n = -N:2:N, % loop over odd series index n


cn = 0; % Fourier Series Coefficient
stem(n*wo,angle(cn)*180/pi,'b','fill','LineWidth',2); % plot |cn| vs nwo
end
for n = -N+1:2:N-1, % loop over even series index n
cn =4/(n*wo); % Fourier Series Coefficient
stem(n*wo,angle(cn)*180/pi,'r','fill','LineWidth',2); % plot |cn| vs nwo
end

xlabel('w (rad/s)')
ylabel('angle(cn) (degrees)')
ttle = ['Phase Spectrum with N = ',num2str(N)];
title(ttle);
grid;

% Draw the amplitude spectrum from exponential Fourier Series for n=5

figure(4) % put next plots on figure 3

subplot(2,1,1)
N=5;
stem(0,c0,'r','fill','LineWidth',2); hold on; % plot c0 at nwo = 0

for n = -N:2:N, % loop over series index n


cn = 0 ; % Fourier Series Coefficient
stem(n*wo,abs(cn),'m','fill','LineWidth',2) % plot |cn| vs nwo
end
for n = -N+1:2:N-1, % loop over even series index n
cn = 4/(n*wo); % Fourier Series Coefficient
stem(n*wo,abs(cn),'b','fill','LineWidth',2); % plot |cn| vs nwo
end

xlabel('w (rad/s)')
ylabel('|cn|')
ttle = ['Amplitude Spectrum with N = ',num2str(N)];
title(ttle); grid on

15
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

% Draw the phase spectrum from exponential Fourier Series

subplot(2,1,2)
stem(0,angle(c0)*180/pi,'k','fill','LineWidth',2); % plot angle of c0 at nwo = 0
hold on ;

for n = -N:2:N, % loop over odd series index n


cn = 0; % Fourier Series Coefficient
stem(n*wo,angle(cn)*180/pi,'b','fill','LineWidth',2); % plot |cn| vs nwo
end
for n = -N+1:2:N-1, % loop over even series index n
cn =4/(n*wo); % Fourier Series Coefficient
stem(n*wo,angle(cn)*180/pi,'r','fill','LineWidth',2); % plot |cn| vs nwo
end

xlabel('w (rad/s)')
ylabel('angle(cn) (degrees)')
ttle = ['Phase Spectrum with N = ',num2str(N)];
title(ttle);
grid;

% Draw the amplitude spectrum from exponential Fourier Series for n=13

figure(5) % put next plots on figure 3

subplot(2,1,1)
N=13;
stem(0,c0,'r','fill','LineWidth',2); hold on; % plot c0 at nwo = 0

for n = -N:2:N, % loop over series index n


cn = 0 ; % Fourier Series Coefficient
stem(n*wo,abs(cn),'m','fill','LineWidth',2) % plot |cn| vs nwo
end
for n = -N+1:2:N-1, % loop over even series index n
cn = 4/(n*wo); % Fourier Series Coefficient
stem(n*wo,abs(cn),'b','fill','LineWidth',2); % plot |cn| vs nwo
end

xlabel('w (rad/s)')
ylabel('|cn|')
ttle = ['Amplitude Spectrum with N = ',num2str(N)];
title(ttle); grid on

% Draw the phase spectrum from exponential Fourier Series

subplot(2,1,2)
stem(0,angle(c0)*180/pi,'k','fill','LineWidth',2); % plot angle of c0 at nwo = 0
hold on ;

for n = -N:2:N, % loop over odd series index n


cn = 0; % Fourier Series Coefficient
stem(n*wo,angle(cn)*180/pi,'b','fill','LineWidth',2); % plot |cn| vs nwo
end

16
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

for n = -N+1:2:N-1, % loop over even series index n


cn =4/(n*wo); % Fourier Series Coefficient
stem(n*wo,angle(cn)*180/pi,'r','fill','LineWidth',2); % plot |cn| vs nwo
end

xlabel('w (rad/s)')
ylabel('angle(cn) (degrees)')
ttle = ['Phase Spectrum with N = ',num2str(N)];
title(ttle);
grid;

Original square wave signal


1.5

0.5
Amplitude

-0.5

-1

-1.5
-1 -0.5 0 0.5 1 1.5 2
time (seconds)

Trig. Fourier Series with N = 1


2
y(t)

-2
-1.5 -1 -0.5 0 0.5 1 1.5
t (seconds)
Trig. Fourier Series with N = 5
1
y(t)

-1
-1.5 -1 -0.5 0 0.5 1 1.5
t (seconds)
Trig. Fourier Series with N = 13
1
y(t)

-1
-1.5 -1 -0.5 0 0.5 1 1.5
t (seconds)

17
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

Amplitude Spectrum with N = 1


1

0.5

|cn| 0

-0.5

-1
-4 -3 -2 -1 0 1 2 3 4
w (rad/s)
Phase Spectrum with N = 1
1
angle(cn) (degrees)

0.5

-0.5

-1
-4 -3 -2 -1 0 1 2 3 4
w (rad/s)

Amplitude Spectrum with N = 5


0.8

0.6
|cn|

0.4

0.2

0
-20 -15 -10 -5 0 5 10 15 20
w (rad/s)
Phase Spectrum with N = 5
200
angle(cn) (degrees)

150

100

50

0
-20 -15 -10 -5 0 5 10 15 20
w (rad/s)

18
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

Amplitude Spectrum with N = 13


0.8

0.6

|cn| 0.4

0.2

0
-50 -40 -30 -20 -10 0 10 20 30 40 50
w (rad/s)
Phase Spectrum with N = 13
200
angle(cn) (degrees)

150

100

50

0
-50 -40 -30 -20 -10 0 10 20 30 40 50
w (rad/s)

Exercise 7.2: Develop Matlab code to determine and plot the magnitude and phase
spectrum for the various number of coefficients, both in trigonometrical and exponential
Fourier series for the following continuous time periodic signals. Assume A  1 , T  1

(a)tr =[-3 -2, -1, 0, 1, 2, 3, 4 5];


yr = [-1 1, -1, 1,-1, 1,-1, 1,-1];

figure(1);
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':b','LineWidth',3);
xlabel('time (seconds)');
ylabel('Amplitude');
title('Original square wave signal');
axis([-1 3 -1.2 1.2]);

%-----------------------------------------------------------------------

% Parameters
N = 11; % summation limit (use N odd)
wo = pi; % fundamental frequency (rad/s)

19
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

c0 = 0; % dc bias
t = -3:0.01:3; % declare time values

% Compute x, the Fourier Series in trigonometric form


N = 1; % initialize yt to c0
for n = 1:2:N, % loop over series index n (odd)
cn = 2/(j*n*wo); % Fourier Series Coefficient
x = 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation
end

figure(2);
subplot(2,1,1)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS
plot(t,x,'k','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-3 3 -1.2 1.2]);

N = 5;

for n = 1:2:N, % loop over series index n (odd)


cn = 2/(j*n*wo); % Fourier Series Coefficient
x = 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation
end

subplot(2,1,2)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS
plot(t,x,'b','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-3 3 -1.2 1.2]);

N = 11; % initialize yt to c0
for n = 1:2:N, % loop over series index n (odd)
cn = 2/(j*n*wo); % Fourier Series Coefficient
x = 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation
end
figure(3)
subplot(2,1,1)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS
plot(t,x,'k','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-2 2 -1 1]);

20
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

N = 21; % initialize yt to c0
for n = 1:2:N, % loop over series index n (odd)
cn = 2/(j*n*wo); % Fourier Series Coefficient
x = 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation
end

subplot(2,1,1)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS
plot(t,x,'k','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-3 3 -1 1]);

%-------------------------------------------exponential ft
N = 1; % define number of terms to use (n = -N..N)
c0 = 0.5; % define dc bias coefficient
t = -1:0.001:2; % define time values for y(t)
y = c0 * ones(size(t)); % let initial y = c0 (dc bias) for all times

for n = -N:-1, % compute y for negative n


cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;
% compute y for positive n and add to y
for n = 1:N, % found using negative n
cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;

figure(3);
subplot(2,1,1); % plot approximation
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(t,y,'r','LineWidth',2); hold on;
plot(tr,yr,':','LineWidth',1);
xlabel('time (seconds)');
ylabel('y(t) approximation');
title('Truncated FS, N = 1');
axis([-1 2 -1 1]);
N = 2; % define number of terms to use (n = -N..N)
c0 = 0.5; % define dc bias coefficient
t = -1:0.001:2; % define time values for y(t)
y = c0 * ones(size(t)); % let initial y = c0 (dc bias) for all times

for n = -N:-1, % compute y for negative n


cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;

21
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

% compute y for positive n and add to y


for n = 1:N, % found using negative n
cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;

subplot(2,1,2); % plot approximation


plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(t,y,'m','LineWidth',2);hold on;
plot(tr,yr,':','LineWidth',1);
xlabel('time (seconds)');
ylabel('y(t) approximation');
title('Truncated FS, N = 2');
axis([-1 2 -0.1 1.2]);
%----------plot magnitude

figure(4) % put next plots on figure 3

subplot(2,1,1)

stem(0,c0,'r','fill','LineWidth',2); hold on; % plot c0 at nwo = 0

for n = -N:2:N, % loop over series index n


cn = 2/(j*n*wo); % Fourier Series Coefficient
stem(n*wo,abs(cn),'m','fill','LineWidth',2) % plot |cn| vs nwo
end
for n = -N+1:2:N-1, % loop over even series index n
cn = 0; % Fourier Series Coefficient
stem(n*wo,abs(cn),'b','fill','LineWidth',2); % plot |cn| vs nwo
end

xlabel('w (rad/s)')
ylabel('|cn|')
ttle = ['Amplitude Spectrum with N = ',num2str(N)];
title(ttle); grid on

% Draw the phase spectrum from exponential Fourier Series

subplot(2,1,2)
stem(0,angle(c0)*180/pi,'k','fill','LineWidth',2); % plot angle of c0 at nwo = 0
hold on ;

for n = -N:2:N, % loop over odd series index n


cn = 2/(j*n*wo); % Fourier Series Coefficient
stem(n*wo,angle(cn)*180/pi,'b','fill','LineWidth',2); % plot |cn| vs nwo
end
for n = -N+1:2:N-1, % loop over even series index n
cn = 0; % Fourier Series Coefficient

22
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

stem(n*wo,angle(cn)*180/pi,'r','fill','LineWidth',2); % plot |cn| vs nwo


end

xlabel('w (rad/s)')
ylabel('angle(cn) (degrees)')
ttle = ['Phase Spectrum with N = ',num2str(N)];
title(ttle);
grid on;

Original square wave signal

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1

-1 -0.5 0 0.5 1 1.5 2 2.5 3


time (seconds)

Trig. Fourier Series with N = 1

0.5
y(t)

-0.5

-1
-3 -2 -1 0 1 2 3
t (seconds)
Trig. Fourier Series with N = 5

0.5
y(t)

-0.5

-1
-3 -2 -1 0 1 2 3
t (seconds)

23
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

Truncated FS, N = 1
1

y(t) approximation
0.5

-0.5

-1
-1 -0.5 0 0.5 1 1.5 2
time (seconds)
Truncated FS, N = 2

1
y(t) approximation

0.5

0
-1 -0.5 0 0.5 1 1.5 2
time (seconds)

Amplitude Spectrum with N = 2


0.8

0.6
|cn|

0.4

0.2

0
-8 -6 -4 -2 0 2 4 6 8
w (rad/s)
Phase Spectrum with N = 2
100
angle(cn) (degrees)

50

-50

-100
-8 -6 -4 -2 0 2 4 6 8
w (rad/s)

(b)
tr = [-3,-2.5,-2,-1.5,-1,-0.5,0, 0.5,1, 1.5,2, 2.5,3];
yr = [ 0,-1, 0, 1, 0,-1, 0, 1, 0, -1, 0, 1, 0];

figure(1);
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':b','LineWidth',3);
xlabel('time (seconds)');
ylabel('Amplitude');
title('Original square wave signal');
axis([-1 3 -1.2 1.2]);

24
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

%-----------------------------------------------------------------------

% Parameters
N = 11; % summation limit (use N odd)
wo = pi; % fundamental frequency (rad/s)
c0 = 0; % dc bias
t = -3:0.01:3; % declare time values

% Compute x, the Fourier Series in trigonometric form


N = 1; % initialize yt to c0
for n = 1:2:N, % loop over series index n (odd)
cn = 2/(j*n*wo); % Fourier Series Coefficient
x = 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation
end

figure(2);
subplot(2,1,1)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS
plot(t,x,'k','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-3 3 -1.2 1.2]);

N = 5;

for n = 1:2:N, % loop over series index n (odd)


cn = 2/(j*n*wo); % Fourier Series Coefficient
x = 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation
end

subplot(2,1,2)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS
plot(t,x,'b','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-3 3 -1.2 1.2]);

N = 11; % initialize yt to c0
for n = 1:2:N, % loop over series index n (odd)
cn = 2/(j*n*wo); % Fourier Series Coefficient
x = 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation
end
figure(3)
subplot(2,1,1)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS

25
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

plot(t,x,'k','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-2 2 -1 1]);

N = 21; % initialize yt to c0
for n = 1:2:N, % loop over series index n (odd)
cn = 2/(j*n*wo); % Fourier Series Coefficient
x = 2*abs(cn)*cos(n*wo*t+angle(cn)); % Fourier Series computation
end

subplot(2,1,1)
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(tr,yr,':','LineWidth',2);hold on; % plot truncated trigonometric FS
plot(t,x,'k','LineWidth',2);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Trig. Fourier Series with N = ',num2str(N)];
title(ttle);axis([-3 3 -1 1]);

%-------------------------------------------exponential ft
N = 1; % define number of terms to use (n = -N..N)
c0 = 0.5; % define dc bias coefficient
t = -1:0.001:2; % define time values for y(t)
y = c0 * ones(size(t)); % let initial y = c0 (dc bias) for all times

for n = -N:-1, % compute y for negative n


cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;
% compute y for positive n and add to y
for n = 1:N, % found using negative n
cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;

figure(3);
subplot(2,1,1); % plot approximation
plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(t,y,'r','LineWidth',2); hold on;
plot(tr,yr,':','LineWidth',1);
xlabel('time (seconds)');
ylabel('y(t) approximation');
title('Truncated FS, N = 1');
axis([-1 2 -1 1]);
N = 2; % define number of terms to use (n = -N..N)
c0 = 0.5; % define dc bias coefficient
t = -1:0.001:2; % define time values for y(t)
y = c0 * ones(size(t)); % let initial y = c0 (dc bias) for all times

26
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

for n = -N:-1, % compute y for negative n


cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;
% compute y for positive n and add to y
for n = 1:N, % found using negative n
cn = exp(j*pi/2)/(2*pi*n);
y = y + real(cn * exp(j*n*2*pi*t));
end;

subplot(2,1,2); % plot approximation


plot(tr,zeros(size(tr)),'k','LineWidth',1); hold on;
plot(zeros(size(tr)),tr,'k','LineWidth',1); hold on;
plot(t,y,'m','LineWidth',2);hold on;
plot(tr,yr,':','LineWidth',1);
xlabel('time (seconds)');
ylabel('y(t) approximation');
title('Truncated FS, N = 2');
axis([-1 2 -0.1 1.2]);
%----------plot magnitude

figure(4) % put next plots on figure 3

subplot(2,1,1)

stem(0,c0,'r','fill','LineWidth',2); hold on; % plot c0 at nwo = 0

for n = -N:2:N, % loop over series index n


cn = 2/(j*n*wo); % Fourier Series Coefficient
stem(n*wo,abs(cn),'m','fill','LineWidth',2) % plot |cn| vs nwo
end
for n = -N+1:2:N-1, % loop over even series index n
cn = 0; % Fourier Series Coefficient
stem(n*wo,abs(cn),'b','fill','LineWidth',2); % plot |cn| vs nwo
end

xlabel('w (rad/s)')
ylabel('|cn|')
ttle = ['Amplitude Spectrum with N = ',num2str(N)];
title(ttle); grid on

% Draw the phase spectrum from exponential Fourier Series

subplot(2,1,2)
stem(0,angle(c0)*180/pi,'k','fill','LineWidth',2); % plot angle of c0 at nwo = 0
hold on ;

for n = -N:2:N, % loop over odd series index n

27
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

cn = 2/(j*n*wo); % Fourier Series Coefficient


stem(n*wo,angle(cn)*180/pi,'b','fill','LineWidth',2); % plot |cn| vs nwo
end
for n = -N+1:2:N-1, % loop over even series index n
cn = 0; % Fourier Series Coefficient
stem(n*wo,angle(cn)*180/pi,'r','fill','LineWidth',2); % plot |cn| vs nwo
end

xlabel('w (rad/s)')
ylabel('angle(cn) (degrees)')
ttle = ['Phase Spectrum with N = ',num2str(N)];
title(ttle);
grid on;
Original square wave signal

0.8

0.6

0.4

0.2
Amplitude

-0.2

-0.4

-0.6

-0.8

-1

-1 -0.5 0 0.5 1 1.5 2 2.5 3


time (seconds)

Trig. Fourier Series with N = 1

0.5
y(t)

-0.5

-1
-3 -2 -1 0 1 2 3
t (seconds)
Trig. Fourier Series with N = 5
1

0.5
y(t)

-0.5

-1
-3 -2 -1 0 1 2 3
t (seconds)

28
Signal Analysis Lab, Dept. of ECE, KL University, A.P., India. Student Id No:150050085

Truncated FS, N = 1
1

y(t) approximation
0.5

-0.5

-1
-1 -0.5 0 0.5 1 1.5 2
time (seconds)
Truncated FS, N = 2

1
y(t) approximation

0.5

0
-1 -0.5 0 0.5 1 1.5 2
time (seconds)

Amplitude Spectrum with N = 2


0.8

0.6
|cn|

0.4

0.2

0
-8 -6 -4 -2 0 2 4 6 8
w (rad/s)
Phase Spectrum with N = 2
100
angle(cn) (degrees)

50

-50

-100
-8 -6 -4 -2 0 2 4 6 8
w (rad/s)

In lab Session Lab:


(a) Execute the Matlab codes prepared for the exercises and observe the results.
(b) Compute all the tasks given in the exercises.
(c) Note the results and store the necessary figures and data for the lab report.

Post Session Lab:


(a) Complete the lab work in all aspects in the given specified lab time.
(b) Answer for the given questions.
(c) Submit the lab report to the lab in-structure and get the signature in time.
(d) Type the complete description of commands used in the lab.

29

You might also like