Define f(t)
Ts = 0.2; %seconds for sample
T = 2; %seconds
t = 0 : Ts : T-Ts
t = 1×10
0 0.2000 0.4000 0.6000 0.8000 1.0000 1.2000 1.4000
((t >= (0) ) & (t< T/2 ))
ans = 1×10 logical array
1 1 1 1 1 0 0 0 0 0
%to find logical values (true(1) or false (0))
%(t < T/2) %to find f(t) coressponding for t
%t(0)=2;
%t(.2)=2;
%t(.4)=2;
%t(.6)=2;
%t(.8)=2;
%t(1), 1 is not smaller than 1 , we want to find t<T/2 , T/2 = 1;
f((t >= (0) ) & (t< (T /2))) = 2
f = 1×10
2 2 2 2 2 -2 -2 -2 -2 -2
f((t >= (T/2) ) & (t< T )) = -2
f = 1×10
2 2 2 2 2 -2 -2 -2 -2 -2
plot (t,f)
1
calcualte real_valued coefficients
N=10;
a = zeros(1,N+1);
b = zeros(1,N+1);
for n = 0 : N
a(n+1)=(2*Ts/T)*sum(f.*cos(2*pi*n*t/T)); %shift by 1 as Array indices must be positive inte
b(n+1)=(2*Ts/T)*sum(f.*sin(2*pi*n*t/T));
end
plot (0:N,a)
hold on
plot (0:N,b)
hold off
legend ('a','b')
2
%plot disrete
N=10;
a = zeros(1,N+1);
b = zeros(1,N+1);
for n = 0 : N
a(n+1)=(2*Ts/T)*sum(f.*cos(2*pi*n*t/T)); %shift by 1 as Array indices must be positive inte
b(n+1)=(2*Ts/T)*sum(f.*sin(2*pi*n*t/T));
end
stem (0:N,a)
hold on
stem (0:N,b)
hold off
legend ('a','b')
3
fourier series sine/cosine form
fs = (a(1)/2)*ones (size(t)) ;
for n =1: N
fs = fs + (a(n+1)*cos (2*pi*n*t/T) +b (n+1)*sin (2*pi*n*t/T))
end
fs = 1×10
0.8000 2.0944 2.5889 2.0944 0.8000 -0.8000 -2.0944 -2.5889
fs = 1×10
0.8000 2.0944 2.5889 2.0944 0.8000 -0.8000 -2.0944 -2.5889
fs = 1×10
1.6000 2.4000 1.6000 2.4000 1.6000 -1.6000 -2.4000 -1.6000
fs = 1×10
1.6000 2.4000 1.6000 2.4000 1.6000 -1.6000 -2.4000 -1.6000
fs = 1×10
2.4000 1.6000 2.4000 1.6000 2.4000 -2.4000 -1.6000 -2.4000
fs = 1×10
2.4000 1.6000 2.4000 1.6000 2.4000 -2.4000 -1.6000 -2.4000
fs = 1×10
3.2000 1.9056 1.4111 1.9056 3.2000 -3.2000 -1.9056 -1.4111
fs = 1×10
3.2000 1.9056 1.4111 1.9056 3.2000 -3.2000 -1.9056 -1.4111
fs = 1×10
4.0000 4.0000 4.0000 4.0000 4.0000 -4.0000 -4.0000 -4.0000
fs = 1×10
4.0000 4.0000 4.0000 4.0000 4.0000 -4.0000 -4.0000 -4.0000
4
plot(t,fs)