-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample2_13.m
More file actions
67 lines (48 loc) · 1.5 KB
/
Example2_13.m
File metadata and controls
67 lines (48 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
% File: Example2_13.m for Example 2-13
% Plot a Fourier series for square wave.
% This file demonstrates the Fourier Series representation of a
% 50% duty cycle square wave. That is To=2T.
clear;
% Time is evaluated (i.e. the waveform is plotted) over the time
% interval [0,10] in increments of t_inc.
% If you wish to observe Gibbs' phenomenon you should decrease the value
% of t_inc to 0.01, and include a large number of harmonics.
t_inc = 0.1;
% Enter the Number of Harmomics to include in the F.S.
fprintf('Enter N, the number of Harmonics to use in the Fourier Series.\n');
N = input('where N is an odd integer: ');
if (rem(N,2) == 0)
N = N+1;
end;
fprintf('\n\nCalculating....please wait\n');
A = 1;
fo = 0.5;
wo = 2 * pi * fo;
t = 0:t_inc:10;
j = sqrt(-1);
% Evaluate tne complex Fourier coefficients using Eq. (2-119) for n>0
for (n = 1:2:N)
c(n) = A/(n*pi*j);
end;
fprintf('The complex Fourier coefficients c(n), for n>=0, are:\n')
% The Fourier series coefficient for n=0 is d(1)
d(1) = A/2;
for k = 2:1:N+1
d(k) = c(k-1);
end
disp(d)
i = 1;
w = zeros(length(t),1);
while (i <= length(t))
w(i) = A/2;
for (n = 1:2:N)
w(i) = w(i) + c(n)*exp(n*wo*t(i)*j) - c(n)*exp(n*wo*t(i)*(-j));
end;
i = i+1;
end;
fprintf('See the plot for the waveform generated by the Fourier series.\n')
plot(t,w);
title(['Fourier Series of Square Wave using ',int2str(N),' Harmonics']);
xlabel('t time in seconds');
ylabel('w(t)');