You are on page 1of 5

Sin

clear;
t=[-3:0.1:3];
f=zeros(size(t));
for k=1:length(t)
if sin(t(k))<0; f(k) =0; else f(k) = sin(t(k)); end
if sin(t(k))<-pi; f(k) = sin(t(k)) + 2*pi; end
if sin(t(k))>pi; f(k) = 0; end
end
fs=(1/pi) * ones(size(t));
fs=fs+1/2*sin(t);
clf
for n=1:50
fs=fs - (2/pi) * (cos(2*n*t) / (4*n^2-1));
%fs=fs - 0;
%subplot(3,2,n);
plot(t,fs,t,f,'--')
%if n ==1
%legend ('mean plus 1 term','f(t)'); legend boxoff;
%else
%legend(['mean plus',num2str(n),'terms'],'f(t)')
%legend boxoff
% an example
stem(fs);
% now obtain the discrete Fourier transform
fsdft = fft(fs);
stem(abs(fsdft));
end
%stem(t,fs)
%if n >=5; xlabel ('t'); end;
%end

1-t

clear;
t=[-.5:0.1:1.5];
f=zeros(size(t));
for k=1:length(t)
if t(k)<1/2; f(k) =t(k); else f(k) = 1-t(k); end
if t(k)<-1/2; f(k) = t(k); end
if t(k)>3/2; f(k) = 0; end
end
%fs=0;
fs=0;
clf
for n=1:135
%fs=fs - (2/pi) * (cos(2*n*t) / (4*n^2-1));
fs=fs - (4/pi^2) * ((-1)^n/(2*n-1)^2) * sin((2*n-1)*pi*t);
%subplot(3,2,n);
plot(t,fs,t,f,'--')
%if n ==1
%legend ('mean plus 1 term','f(t)'); legend boxoff;
%else
%legend(['mean plus',num2str(n),'terms'],'f(t)')
%legend boxoff
% an example
stem(fs);
% now obtain the discrete Fourier transform
fsdft = fft(fs);
stem(abs(fsdft));
end
%if n >=5; xlabel ('t'); end;
%end

2t

clear;
t=[-2:0.1:2];
f=zeros(size(t));
for k=1:length(t)
if t(k)<0; f(k) =0; else f(k) = 2*t(k); end
if t(k)<-pi; f(k) = 2*t(k); end
if t(k)>pi; f(k) = 0; end
end
%fs=0;
fs=(pi/2);
clf
for n=1:135
fs=fs - (4/pi) * ((1/(((2*n)-1)^2))*cos(((2*n)-1)*t));
fs=fs - (2) * ((((-1)^n)/n)*sin(n*t));
%subplot(3,2,n);
plot(t,fs,t,f,'--')
%if n ==1
%legend ('mean plus 1 term','f(t)'); legend boxoff;
%else
%legend(['mean plus',num2str(n),'terms'],'f(t)')
%legend boxoff
stem(fs);
% now obtain the discrete Fourier transform
fsdft = fft(fs);
stem(abs(fsdft));
end
%if n >=5; xlabel ('t'); end;
%end

T^2

clear;
t=[-2:0.1:2];
f=zeros(size(t));
for k=1:length(t)
if t(k)<0; f(k) =0; else f(k) = t(k)^2; end
if t(k)<-pi; f(k) = t(k)^2; end
if t(k)>pi; f(k) = 0; end
end
%fs=0;
fs=((pi^2)/6);
clf
for n=1:135
fs=fs + (2) * (((-1)^n)/n^2)*cos(n*t);
fs=fs + (1/pi) * ((((2-(n^2)*(pi^2))*((-1)^n)-
2))/(n^3))*sin(n*t);
%subplot(3,2,n);
plot(t,fs,t,f,'--')
%if n ==1
%legend ('mean plus 1 term','f(t)'); legend boxoff;
%else
%legend(['mean plus',num2str(n),'terms'],'f(t)')
%legend boxoff
stem(fs);
% now obtain the discrete Fourier transform
fsdft = fft(fs);
stem(abs(fsdft));
end
%if n >=5; xlabel ('t'); end;
%end

Salva Tutorial

clear
T=0.1;
F=1/T
a=0;
b=T;
N=5;
%f0=@(t) 2*t;
%fa=@(t) 2*t.*cos(2*pi*n*F*t);
%fb=@(t) 2*t.*sin(2*pi*n*F*t);
%A0=integral(f0,a,b)
f0=@(t) 5+cos(2*pi*F*t)+2*cos(2*pi*2*F*t);
A0=(1/T)*integral(f0,a,b)
H=[A0];
for n=1:1:N;
%fa=@(t) 2*t.*cos(2*pi*n*F*t);
%fb=@(t) 2*t.*sin(2*pi*n*F*t);
fa=@(t)
(5+cos(2*pi*F*t)+2*cos(2*pi*2*F*t)).*(cos(2*pi*n*F*t));
fb=@(t)
(5+cos(2*pi*F*t)+2*cos(2*pi*2*F*t)).*(sin(2*pi*n*F*t));
A=(2/T)*integral(fa,a,b)
B=(2/T)*integral(fb,a,b)
C=sqrt(A^2+B^2);
H=[H C];
end
Freq=[0:F:N*F];
stem(Freq,H)
flip(stem, left)

%A1=integral(fa,a,b)
%B1=integral(fb,a,b)

You might also like