Sei sulla pagina 1di 19

From the web-site: http://www.eece.unm.edu/faculty/pattichis/matlab.

html we are given the


Fourier Series expansion (in one-dimension) as:

where; To represents the period:

We are also given the exponential form of the Fourier Series expansion:

where; the Fourier coefficients, cn, are given in integral form as:

note: Co is Cn at n = 0, C-n is Cn at n < 0, Cn is Cn at n > 0, and Cn is the complex


conjugate of Cn.
The current Matlab assigment asks the student to consider the periodic square-wave
function represented as:
A,
0 < t < To/2
f(t)=
-A,
To/2 < t < To

Problem 1 asks the student to compute the Fourier Series of f(t), expressing the
answer in exponential form of the Fourier Series.
Step 1: Matlab approximation of the square-wave function f(t) over a single period, t = 0 to
To may be implemented with the following code:
>> A = 1;
>> period = 1;
>> len = 100;
>>
>> t = linspace(0, period, len+1);
>> f = A;
>> f=zeros(1,101);
>> f = [(t>0).*(t<0.5) - (t<1).*(t>0.5)];
>> plot(t,f)

EECE314, Matlab Assignment

Page 1 of 19

The corresponding Matlab curve is displayed below:


Square-Wave Representation over single period (To=1)

The Fourier Coefficients are now solved for using Riemanns approximation to the
integral:

The Riemann approximation may be summarized in words as the frequency of the signal (f
= 1/To) multiplied by the summation of the function at an incremental time-step (ma)
multiplied by a complex exponential term taken at the same incremental time-step (ma)
multiplied by the unit time-step (a).
Using the expressions for Co, the following Matlab calculation was achieved.
>> c0=(1/period)*sum((period/len)*f)
c0 =
-3.1225e-017
Unfortunately, the Cn and C-n terms were not as straight-forward. The Matlab code
developed to achieve these solutions was ugly, painful, and in general an embarrassment.
Below is the code used:

EECE314, Matlab Assignment

Page 2 of 19

Matlab Code for determining Cn terms:


>> ec1=-i*2*pi/period*t
>> ec1=[ec1*1;ec1*2;ec1*3;ec1*4;ec1*5;ec1*6;ec1*7;ec1*8;ec1*9;ec1*10]
>> e_cpp=exp(ec1)
>> cpn_1=((period/len)*f_1.*e_cpp)
(4)

(1)
(2)
(3)

Eqn (1) handles the [-j 2/To ma] component of the exponential term. Note that
the term t is one-dimensional array containing 101 element, each element
representing a time step within the range 0 to To. Eqn(1) intentionally neglects the
n term of the Riemann approximation, this term is handled within Eqn (2).
Eqn (2) generates a 10x101 element array, ec1[x,y]. The x-axis of this new array is
used to capture the harmonic n-term contribution from the Riemann approximation.
Eqn (3) generates the exponential from the ec1 array.
Eqn (4) generates the full Rieman approximation for an individual time-step and
individual n harmonic-term, this handling results in a 10x101 array that is used to
find the individual harmonic terms.

Individual equations were used to solve for the Cn harmonics 1-10. These equations and
the resulting expressions are shown below.
>> cpn1=sum(cpn_1(1,1:101))
>> cpn2=sum(cpn_1(2,1:101))
>> cpn3=sum(cpn_1(3,1:101))
>> cpn4=sum(cpn_1(4,1:101))
>> cpn5=sum(cpn_1(5,1:101))
>> cpn6=sum(cpn_1(6,1:101))
>> cpn7=sum(cpn_1(7,1:101))
>> cpn8=sum(cpn_1(8,1:101))
>> cpn9=sum(cpn_1(9,1:101))
>> cpn10=sum(cpn_1(10,1:101))

->
->
->
->
->
->
->
->
->
->

cpn1 = 0.0000 - 0.6364i


cpn2 = 3.2960e-017 +1.3097e-016i
cpn3 = 0.0000 - 0.2116i
cpn4 = -1.2143e-016 +4.3368e-017i
cpn5 = 0.0000 - 0.1263i
cpn6 = 3.8164e-017 -1.7694e-016i
cpn7 = 0.0000 - 0.0895i
cpn8 = 4.5103e-016 +2.5153e-017i
cpn9 = 0.0000 - 0.0688i
cpn10 = -1.4051e-016 +1.2577e-016i

Note: from the discussion above, the C-n terms are equivalent to the complex conjugates of
the Cn terms.
cnn1 =
cnn2 =
cnn3 =
cnn4 =
cnn5 =
cnn6 =
cnn7 =
cnn8 =
cnn9 =
cnn10 =

EECE314, Matlab Assignment

+ 0.6364i
3.2960e-017 - 1.3097e-016i
+ 0.2116i
-1.2143e-016 - 4.3368e-017i
+ 0.1263i
3.8164e-017 + 1.7694e-016i
+ 0.0895i
4.5103e-016 - 2.5153e-017i
+ 0.0688i
-1.4051e-016 - 1.2577e-016i

~0
~0
~0
~0
~0

Page 3 of 19

Quickly examining the harmonic terms it is apparent that the even-harmonics are
approximately zero, and that the odd harmonics are entirely imaginary terms. These results
are as anticipated due to the odd and quarter-wave symmetry of the square-wave. Due to
the shape of the function, it is anticipated that the fourier series expansion would be
composed entirely of harmonic sine functions, the more harmonic terms the better the
square-wave approximation.
The expression for the exponential form of the Fourier series expansion is shown below:

This equation may be used along with the coefficient terms above to express the fourier
series to to the 10th harmonic as:
f(t) = j0.0688exp(-j 2/To 9t) + j0.0895exp(-j 2/To 7t) + j0.1263exp(-j 2/To 5t) +
j0.2116exp(-j 2/To 3t) + j0.6364exp(-j 2/To t) - j0.0688exp(j 2/To 9t) - j0.0895exp(j
2/To 7t) - j0.1263exp(j 2/To 5t) - j0.2116exp(j 2/To 3t) - j0.6364exp(j 2/To t)
This equation may be plugged back into Matlab to gain an appreciation of the quality of the
Fourier series expansion (as well as a sanity check for whether the coefficient solutions).
Fourier Expansion of f(t) with 10-harmonics over single period (To=1)

EECE314, Matlab Assignment

Page 4 of 19

Problem 2
The function square_wave implemented is shown below:
function [t, f] = square_wave(A, no_of_points, period)
%
% [t, f]=square_wave(A, no_of_points, period)
%
% Given:
% no_of_points: the number of points to use fo sampling the square wave.
%
(must be a multiple of 4).
% period: the period of the function
%
% Computes:
% t: an array of the time values.
% f: an array of the function values.
%
% Examples:
% A = 1;
% period = 4;
% len = 16;
% [t, f] = square_wave(A, len, period);
% plot(t,f)l hold on; plot(t, f, '*');
%
t=linspace(0, period, no_of_points+1); % generate all the points.
t=t(1:no_of_points)
% remove the last point so it does not repeat.
f=[0 A*ones(1,no_of_points/4)]; % generates first half of square wave w/ ampl A
f=[f f(no_of_points/4:-1:1)];
f=[f -f(no_of_points/2:-1:2)]
% mirror and negate the first half.

EECE314, Matlab Assignment

Page 5 of 19

M-file code was developed for solution to problem2.


The first attempt at problem 2 was performed entirely with command line
entry; results from command line entry were difficult to validate and
trouble-shoot. The M-file code developed is given below.
% prob314_v2.m
% matlab code for 314 problems 2 and 3
% declarations % -----------no_of_coefs=16;
no_of_points=256;
period=4;
time_step=period/no_of_points;
A=1;
% call to function square wave to initialize t and f arrays
[t,f]=square_wave(A,no_of_points,period);
% background calculations for Cn solution using Riemann's Approximation
ec1_t=-i*2*pi/period*t
% solve for exp term excluding "n" contribution
ec1_c=[1:1:no_of_coefs/2]
ec1_c=ec1_c(:)
ec1=ec1_c*ec1_t
%ec1=[ec1*1;ec1*2;ec1*3;ec1*4;ec1*5;ec1*6;ec1*7;ec1*8; ...
% ec1*9;ec1*10;ec1*11;ec1*12;ec1*13;ec1*14;ec1*15;ec1*16]
e_cpp=time_step*exp(ec1)
% generates array with n-term exponentials
% plot of output square-wave from square_wave function
disp(sprintf('Square-wave, f, array and plot:'));
f
close;
plot(t,f); hold on; plot(t,f,'*');
title('Square-wave function, f (To=4, Ampl=1, #-of-pts=256)');
pause;
clf;
close;
% calculation of FFT from square-wave function f
disp(sprintf('Calculated Fast Fourier Transform from matlab fft() command:'));
f_fft=fft(f)
f=f(:);
% solution of coefficients

EECE314, Matlab Assignment

Page 6 of 19

% _________________________
% Co term solution
c0=(1/period)*sum((period/no_of_points)*f);
cpn_matrix=1/period*e_cpp*f;
cpn_matrix=[c0; cpn_matrix];
cpn_2xlast_term=cpn_matrix((no_of_coefs/2)+1,1);
cpn_matrix=cpn_matrix(1:no_of_coefs/2);
cpn_matrix=[cpn_matrix; cpn_2xlast_term];
cnn_matrix=conj(cpn_matrix(no_of_coefs/2:-1:2));
disp(sprintf('Calculated Coefficient Terms in matrix form (Co -> 2*CpN ->
Cmn)'));
c_column_vector=[cpn_matrix; cnn_matrix]
% calculation of error terms and comparison of results
%harmonics_num=(-((no_of_coefs/2)-1):no_of_coefs/2);
harmonics_num=[0:no_of_coefs/2];
harmonics_num=[harmonics_num -harmonics_num(no_of_coefs/2:-1:2)]
pause;
n_time_matrix=t(:)*harmonics_num;
harmonics_matrix=exp((i*2*pi/period)*n_time_matrix);
f_true_harms=harmonics_matrix*c_column_vector;
% remove imaginary part - introduced by numerical approximation
f_true_harms=real(f_true_harms);
% output plot of results
close;
figure(1);
clf;
plot(t,f,'-',t,f,'*',t,f_true_harms,'-',t,f_true_harms,'+');
disp(sprintf('Plot of square wave function v. fourier expansion'));
title('Square-wave function, f v. Fourier Series Expansion (*=square wave, f;
+=fourier expansion)');
pause;
% solution of maximum absolute error
f_true_harms=f_true_harms(:);
f=f(:);
[max_error, error_location] = max(abs(f_true_harms - f));
t_error=t(error_location);

EECE314, Matlab Assignment

Page 7 of 19

disp(sprintf('Maximum error = %f occurs for t = %f', ...


max_error, t_error));
pause;
% find mean-absolute error:
disp(sprintf('Mean-square error = %f for %d points.', ...
mean(abs(f_true_harms - f).^2), ...
no_of_points));
close;
% approach to problem 3
% _____________________
no_of_coefs=16;
N=no_of_coefs;
M=N-1;
A=1;
period=4;
% call square_wave to compute f
[t,f]=square_wave(A,M+1,period);
% calculate fft() of f
f_fft=fft(f)
% build harmonics matrix
m=[0:N-1]
n=[0:N/2]
nxm=n(:)*m
harms_matrix=(-i*2*pi/N)*nxm;
harms_matrix=exp(harms_matrix);
cpn=harms_matrix*f(:);
cnn=conj(cpn(N/2:-1:2));
cpn_2xlast_term=2*cpn((N/2)+1,1);
cpn=cpn(1:N/2);
c_approx=1/N*[cpn; cpn_2xlast_term; cnn]
correction_factor=1/N
c_from_fft=f_fft*correction_factor
pause;

EECE314, Matlab Assignment

Page 8 of 19

disp('FFT error is:');


disp(transpose(abs(c_approx(:) - c_column_vector(:))));
% ******** Complete ********
Problem 2 Results from Code Developed
Plot of function square_wave output (f v. t)
(A=1, period=4, no_of_points=256)

2(a) Fast-Fourier Transform of the Square-Wave:


Calculated Fast Fourier Transform from matlab fft() command:
Calculated Fast Fourier Transform from matlab fft() command:
f_fft =
1.0e+002 *
Columns 1 through 5
0
0.0000 - 1.6297i
0
0.0000 - 0.5430i
0
Columns 6 through 10
0.0000 - 0.3255i
0
-0.0000 - 0.2322i
0
0.0000 - 0.1803i
Columns 11 through 15
0
-0.0000 - 0.1473i
0
0.0000 - 0.1243i
0

EECE314, Matlab Assignment

Page 9 of 19

Columns 16 through 20
-0.0000 - 0.1074i
0
0.0000 - 0.0945i
0
-0.0000 - 0.0842i
Columns 21 through 25
0
0.0000 - 0.0759i
0
-0.0000 - 0.0690i
0
Columns 26 through 30
0.0000 - 0.0631i
0
-0.0000 - 0.0581i
0
-0.0000 - 0.0538i
Columns 31 through 35
0
-0.0000 - 0.0500i
0
0.0000 - 0.0467i
0
Columns 36 through 40
0.0000 - 0.0437i
0
-0.0000 - 0.0410i
0
-0.0000 - 0.0385i
Columns 41 through 45
0
0.0000 - 0.0363i
0
-0.0000 - 0.0343i
0
Columns 46 through 50
0.0000 - 0.0325i
0
-0.0000 - 0.0307i
0
0.0000 - 0.0292i
Columns 51 through 55
0
-0.0000 - 0.0277i
0
0.0000 - 0.0263i
0
Columns 56 through 60
-0.0000 - 0.0250i
0
-0.0000 - 0.0238i
0
-0.0000 - 0.0226i
Columns 61 through 65
0
-0.0000 - 0.0215i
0
0 - 0.0205i
0
Columns 66 through 70
0 - 0.0195i
0
-0.0000 - 0.0186i
0
-0.0000 - 0.0177i
Columns 71 through 75
0
0.0000 - 0.0168i
0
-0.0000 - 0.0160i
0
Columns 76 through 80
0.0000 - 0.0152i
0
-0.0000 - 0.0145i
0
-0.0000 - 0.0137i
Columns 81 through 85
0
0.0000 - 0.0130i
0
0.0000 - 0.0123i
0
Columns 86 through 90
-0.0000 - 0.0117i
0
-0.0000 - 0.0110i
0
0.0000 - 0.0104i
Columns 91 through 95
0
-0.0000 - 0.0098i
0
-0.0000 - 0.0092i
0
Columns 96 through 100
0.0000 - 0.0086i
0
-0.0000 - 0.0080i
0
0.0000 - 0.0074i
Columns 101 through 105
0
-0.0000 - 0.0069i
0
-0.0000 - 0.0063i
0
Columns 106 through 110
0.0000 - 0.0058i
0
-0.0000 - 0.0053i
0
0.0000 - 0.0047i
Columns 111 through 115
0
-0.0000 - 0.0042i
0
0.0000 - 0.0037i
0
Columns 116 through 120
-0.0000 - 0.0032i
0
0.0000 - 0.0027i
0
-0.0000 - 0.0022i
Columns 121 through 125
0
-0.0000 - 0.0017i
0
-0.0000 - 0.0012i
0
Columns 126 through 130
-0.0000 - 0.0007i
0
-0.0000 - 0.0002i
0
-0.0000 + 0.0002i
Columns 131 through 135
0
-0.0000 + 0.0007i
0
-0.0000 + 0.0012i
0
Columns 136 through 140
-0.0000 + 0.0017i
0
-0.0000 + 0.0022i
0
0.0000 + 0.0027i
Columns 141 through 145
0
-0.0000 + 0.0032i
0
0.0000 + 0.0037i
0
Columns 146 through 150
-0.0000 + 0.0042i
0
0.0000 + 0.0047i
0
-0.0000 + 0.0053i
Columns 151 through 155

EECE314, Matlab Assignment

Page 10 of 19

0
0.0000 + 0.0058i
Columns 156 through 160
-0.0000 + 0.0069i
0
Columns 161 through 165
0
0.0000 + 0.0086i
Columns 166 through 170
-0.0000 + 0.0098i
0
Columns 171 through 175
0
-0.0000 + 0.0117i
Columns 176 through 180
0.0000 + 0.0130i
0
Columns 181 through 185
0
0.0000 + 0.0152i
Columns 186 through 190
0.0000 + 0.0168i
0
Columns 191 through 195
0
0 + 0.0195i
Columns 196 through 200
-0.0000 + 0.0215i
0
Columns 201 through 205
0
-0.0000 + 0.0250i
Columns 206 through 210
-0.0000 + 0.0277i
0
Columns 211 through 215
0
0.0000 + 0.0325i
Columns 216 through 220
0.0000 + 0.0363i
0
Columns 221 through 225
0
0.0000 + 0.0437i
Columns 226 through 230
-0.0000 + 0.0500i
0
Columns 231 through 235
0
0.0000 + 0.0631i
Columns 236 through 240
0.0000 + 0.0759i
0
Columns 241 through 245
0
-0.0000 + 0.1074i
Columns 246 through 250
-0.0000 + 0.1473i
0
Columns 251 through 255
0
0.0000 + 0.3255i
Column 256
0.0000 + 1.6297i

-0.0000 + 0.0063i

0.0000 + 0.0074i
0

-0.0000 + 0.0226i
0

0.0000 + 0.1243i

0.0000 + 0.1803i
0

-0.0000 + 0.0690i

-0.0000 + 0.0842i
0

0.0000 + 0.0467i

-0.0000 + 0.0538i

0.0000 + 0.5430i

0
-0.0000 + 0.0186i

-0.0000 + 0.0343i

-0.0000 + 0.0385i

0
-0.0000 + 0.0145i

0.0000 + 0.0263i

0.0000 + 0.0292i

EECE314, Matlab Assignment

0 + 0.0205i

0
-0.0000 + 0.0110i

-0.0000 + 0.0160i

-0.0000 + 0.0177i
0

0.0000 + 0.0123i

-0.0000 + 0.0137i
0

-0.0000 + 0.0080i

-0.0000 + 0.0092i

0.0000 + 0.0104i
0

-0.0000 + 0.0238i
0
-0.0000 + 0.0307i
0
-0.0000 + 0.0410i
0
-0.0000 + 0.0581i
0
0.0000 + 0.0945i
0
-0.0000 + 0.2322i
0

Page 11 of 19

2(b)

Calculated Coefficient Terms in matrix form (Co -> CpN -> Cmn)

c_column_vector =
0
0.0000 - 0.6366i
0.0000 - 0.0000i
0.0000 - 0.2121i
0.0000 + 0.0000i
0.0000 - 0.1272i
0.0000 + 0.0000i
0.0000 - 0.0907i
-0.0000 + 0.0000i
0.0000 + 0.0907i
0.0000 - 0.0000i
0.0000 + 0.1272i
0.0000 - 0.0000i
0.0000 + 0.2121i
0.0000 + 0.0000i
0.0000 + 0.6366i
2(c)

->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->

C0
C1
C2
C3
C4
C5
C6
C7
2*C8
C-7
C-6
C-5
C-4
C-3
C-2
C-1

Square-Wave Function, f 16-Coefficient Harmonics Expansion


(* = square_wave, + = Harmonic Expansion)

EECE314, Matlab Assignment

Page 12 of 19

2(e)

Maximum error = 0.875395 occurs for t = 3.984375


Mean-square error = 0.042915 for 256 points, 16-Coefficients.

2(f)

Re-execution of parts (a)-(d) with no_of_coefficients = 32


Fast Fourier Transform with 32 Harmonic Terms:
Calculated Fast Fourier Transform from matlab fft() command:
f_fft =
1.0e+002 *
Columns 1 through 5
0
0.0000 - 1.6297i
0
0.0000 - 0.5430i
0
Columns 6 through 10
0.0000 - 0.3255i
0
-0.0000 - 0.2322i
0
0.0000 - 0.1803i
Columns 11 through 15
0
-0.0000 - 0.1473i
0
0.0000 - 0.1243i
0
Columns 16 through 20
-0.0000 - 0.1074i
0
0.0000 - 0.0945i
0
-0.0000 - 0.0842i
Columns 21 through 25
0
0.0000 - 0.0759i
0
-0.0000 - 0.0690i
0
Columns 26 through 30
0.0000 - 0.0631i
0
-0.0000 - 0.0581i
0
-0.0000 - 0.0538i
Columns 31 through 35
0
-0.0000 - 0.0500i
0
0.0000 - 0.0467i
0
Columns 36 through 40
0.0000 - 0.0437i
0
-0.0000 - 0.0410i
0
-0.0000 - 0.0385i
Columns 41 through 45
0
0.0000 - 0.0363i
0
-0.0000 - 0.0343i
0
Columns 46 through 50
0.0000 - 0.0325i
0
-0.0000 - 0.0307i
0
0.0000 - 0.0292i
Columns 51 through 55
0
-0.0000 - 0.0277i
0
0.0000 - 0.0263i
0
Columns 56 through 60
-0.0000 - 0.0250i
0
-0.0000 - 0.0238i
0
-0.0000 - 0.0226i
Columns 61 through 65
0
-0.0000 - 0.0215i
0
0 - 0.0205i
0
Columns 66 through 70
0 - 0.0195i
0
-0.0000 - 0.0186i
0
-0.0000 - 0.0177i
Columns 71 through 75
0
0.0000 - 0.0168i
0
-0.0000 - 0.0160i
0
Columns 76 through 80
0.0000 - 0.0152i
0
-0.0000 - 0.0145i
0
-0.0000 - 0.0137i
Columns 81 through 85
0
0.0000 - 0.0130i
0
0.0000 - 0.0123i
0
Columns 86 through 90
-0.0000 - 0.0117i
0
-0.0000 - 0.0110i
0
0.0000 - 0.0104i
Columns 91 through 95
0
-0.0000 - 0.0098i
0
-0.0000 - 0.0092i
0
Columns 96 through 100
0.0000 - 0.0086i
0
-0.0000 - 0.0080i
0
0.0000 - 0.0074i
Columns 101 through 105
0
-0.0000 - 0.0069i
0
-0.0000 - 0.0063i
0

EECE314, Matlab Assignment

Page 13 of 19

Columns 106 through 110


0.0000 - 0.0058i
0
-0.0000 - 0.0053i
0
0.0000 - 0.0047i
Columns 111 through 115
0
-0.0000 - 0.0042i
0
0.0000 - 0.0037i
0
Columns 116 through 120
-0.0000 - 0.0032i
0
0.0000 - 0.0027i
0
-0.0000 - 0.0022i
Columns 121 through 125
0
-0.0000 - 0.0017i
0
-0.0000 - 0.0012i
0
Columns 126 through 130
-0.0000 - 0.0007i
0
-0.0000 - 0.0002i
0
-0.0000 + 0.0002i
Columns 131 through 135
0
-0.0000 + 0.0007i
0
-0.0000 + 0.0012i
0
Columns 136 through 140
-0.0000 + 0.0017i
0
-0.0000 + 0.0022i
0
0.0000 + 0.0027i
Columns 141 through 145
0
-0.0000 + 0.0032i
0
0.0000 + 0.0037i
0
Columns 146 through 150
-0.0000 + 0.0042i
0
0.0000 + 0.0047i
0
-0.0000 + 0.0053i
Columns 151 through 155
0
0.0000 + 0.0058i
0
-0.0000 + 0.0063i
0
Columns 156 through 160
-0.0000 + 0.0069i
0
0.0000 + 0.0074i
0
-0.0000 + 0.0080i
Columns 161 through 165
0
0.0000 + 0.0086i
0
-0.0000 + 0.0092i
0
Columns 166 through 170
-0.0000 + 0.0098i
0
0.0000 + 0.0104i
0
-0.0000 + 0.0110i
Columns 171 through 175
0
-0.0000 + 0.0117i
0
0.0000 + 0.0123i
0
Columns 176 through 180
0.0000 + 0.0130i
0
-0.0000 + 0.0137i
0
-0.0000 + 0.0145i
Columns 181 through 185
0
0.0000 + 0.0152i
0
-0.0000 + 0.0160i
0
Columns 186 through 190
0.0000 + 0.0168i
0
-0.0000 + 0.0177i
0
-0.0000 + 0.0186i
Columns 191 through 195
0
0 + 0.0195i
0
0 + 0.0205i
0
Columns 196 through 200
-0.0000 + 0.0215i
0
-0.0000 + 0.0226i
0
-0.0000 + 0.0238i
Columns 201 through 205
0
-0.0000 + 0.0250i
0
0.0000 + 0.0263i
0
Columns 206 through 210
-0.0000 + 0.0277i
0
0.0000 + 0.0292i
0
-0.0000 + 0.0307i
Columns 211 through 215
0
0.0000 + 0.0325i
0
-0.0000 + 0.0343i
0
Columns 216 through 220
0.0000 + 0.0363i
0
-0.0000 + 0.0385i
0
-0.0000 + 0.0410i
Columns 221 through 225
0
0.0000 + 0.0437i
0
0.0000 + 0.0467i
0
Columns 226 through 230
-0.0000 + 0.0500i
0
-0.0000 + 0.0538i
0
-0.0000 + 0.0581i
Columns 231 through 235
0
0.0000 + 0.0631i
0
-0.0000 + 0.0690i
0
Columns 236 through 240
0.0000 + 0.0759i
0
-0.0000 + 0.0842i
0
0.0000 + 0.0945i
Columns 241 through 245
0
-0.0000 + 0.1074i
0
0.0000 + 0.1243i
0

EECE314, Matlab Assignment

Page 14 of 19

Columns 246 through 250


-0.0000 + 0.1473i
0
0.0000 + 0.1803i
0
-0.0000 + 0.2322i
Columns 251 through 255
0
0.0000 + 0.3255i
0
0.0000 + 0.5430i
0
Column 256
0.0000 + 1.6297i

Calculated Coefficient Terms in matrix form (Co -> CpN -> Cmn)
c_column_vector =
0
0.0000 - 0.6366i
0.0000 - 0.0000i
0.0000 - 0.2121i
0.0000 + 0.0000i
0.0000 - 0.1272i
0.0000 + 0.0000i
0.0000 - 0.0907i
-0.0000 + 0.0000i
0.0000 - 0.0704i
-0.0000 + 0.0000i
0.0000 - 0.0575i
-0.0000 + 0.0000i
-0.0000 - 0.0486i
0.0000 + 0.0000i
0.0000 - 0.0420i
-0.0000 - 0.0000i
0.0000 + 0.0420i
0.0000 - 0.0000i
-0.0000 + 0.0486i
-0.0000 - 0.0000i
0.0000 + 0.0575i
-0.0000 - 0.0000i
0.0000 + 0.0704i
-0.0000 - 0.0000i
0.0000 + 0.0907i
0.0000 - 0.0000i
0.0000 + 0.1272i
0.0000 - 0.0000i
0.0000 + 0.2121i
0.0000 + 0.0000i
0.0000 + 0.6366i

EECE314, Matlab Assignment

->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->
->

C0
C1
C2
C3
C4
C5
C6
C7
C8
C9
C10
C11
C12
C13
C14
C15
2*C16
C-15
C-14
C-13
C-12
C-11
C-10
C-9
C-8
C-7
C-6
C-5
C-4
C-3
C-2
C-1

Page 15 of 19

Square-Wave Function, f v Harmonics Expansion


(* = square_wave 256-pts, + = Harmonic Expansion 32-coefs)

Maximum error = 0.753176 occurs for t = 2.015625


Mean-square error = 0.018135 for 256 points, 32 coefficients.
3(a) Fast Fourier Transform of the time-domain sequence (16 coefficients):
f_fft =
Columns 1 through 5
0
0 -10.0547i

Columns 6 through 10
0 - 1.3364i
0

0 - 0.3978i

Columns 11 through 15
0
0 + 1.3364i

0 - 2.9932i
0
0 + 2.9932i

0
0 + 0.3978i
0

Column 16
0 +10.0547i
EECE314, Matlab Assignment

Page 16 of 19

3(b) Derive approximate relationship for the exponential form of Fourier Series
From the web-site: http://www.eece.unm.edu/faculty/pattichis/matlab.html we are given the
coefficient matrix approximation (c_approx) of:

From this expression we see that a single harmonic positive coefficient may be expressed
as the 1/N multiplied by the summation of the function (square wave in this case)
multiplied by the exponential of [ j 2 n m /N ], where; N is the number of coefficients
evaluated (in the case investigated, 16 coefficients are used), m is the incremental grid
point under evaluation (computational mesh is divided into N points with values ranging
from 0 <= m <= N-1), ma is the time step (equal to the computation grid # times time-step
per grid increment), an n is the current coefficient (0 <= n <= N/2, this is due to the
existence of both positive and negative coefficients, therefore N coefficients results in N/2
+ coefficients, concept is high-lighted in 2(b) above with coefficient column vectors).
This equation may be fairly easily translated into Matlab (performed within the code
attached as part of prob 2) by generating an [n x m] harmonics matrix and multiplying by
the matrix [f] or in Matlab, the output of the square_wave function f(:).
From the Matlab help utility, it is explained that the FFT (Discrete Fourier Transform) as
performed by Matlab may be expressed by the equation (Note: this equation was also
pulled from the web-site listed above):

where F[n] is the FFT of function f[m]. Comparing this equation to the equation above, it
may be seen how strikingly similar the two are. Note in the equation above f(ma) is the
value of the function f at time step ma, while in the equation above, f[m] is the value of the
function f at the computational grid point m. In the current implementation, the square
wave is determined at a number of computational mesh points ranging m = 0 to N-1,
therefore the value of f in the two equations is equivalent.
We may then say that:

or, c_approx = (1/N) * fft(f). The correction factor for modifying the fft(f) into the
coefficient matrix is (1/N)

EECE314, Matlab Assignment

Page 17 of 19

This result is further confirmed in the Matlab code developed. Below is the output from
Matlab calculation:
c_approx = [nxm harmonics matrix] x [f(:)]
0
0.0000 - 0.6284i
-0.0000 - 0.0000i
0.0000 - 0.1871i
0.0000 + 0.0000i
0.0000 - 0.0835i
0.0000 + 0.0000i
-0.0000 - 0.0249i
0 + 0.0000i
-0.0000 + 0.0249i
0.0000 - 0.0000i
0.0000 + 0.0835i
0.0000 - 0.0000i
0.0000 + 0.1871i
-0.0000 + 0.0000i
0.0000 + 0.6284i
correction_factor = 1/N (N=16)
0.0625
correction_factor * FFT is:
ans =
0
0 - 0.6284i
0
0 - 0.1871i
0
0 - 0.0835i
0
0 - 0.0249i
0
0 + 0.0249i
0
0 + 0.0835i
0
0 + 0.1871i
0
0 + 0.6284i

EECE314, Matlab Assignment

Page 18 of 19

Note: that the coefficients from the correction of the FFT are about dead on with C0-C8 and
C-n is simply the complex conjugate of C+n.
3(c) Evaluate the error between the actual and estimated harmonic coefficients:
From the equation provided the FFT error or difference between the calculated coefficients
from Probs 1 & 2 and that calculated from the FFT is
FFT error is:
Columns 1 through 9
0 0.0082 0.0000 0.0250 0.0000 0.0436 0.0000 0.0659 0.0000
Columns 10 through 16
0.0659 0.0000 0.0436 0.0000 0.0250 0.0000 0.0082
>>

EECE314, Matlab Assignment

Page 19 of 19

Potrebbero piacerti anche