-1

I am trying to determine the approximation of the following function as a ctfs:

x(t) = exp(-7|t|)*cos(10*pi*t) for -0.5 <= t < 0.5

I was told to use trigonometric form of continuous time Fourier series (CTFS) to calculate a[k] and b[k] of x(t), where k is harmonic number. Kmax = 4.

I have the following code, and while it doesn't give me any errors. I don't know how to check if it is right, or what I am doing in general. This is my first time using matlab. Can someone please help me?

fun = @(t) exp((-7).*abs(t)).*cos(10.*pi.*t);

a = @(k, T, t) fun(t).*cos((2.*pi.*k.*t)./(T));
b = @(k, T, t) fun(t).*sin((2.*pi.*k.*t)./(T));

T = 1; %T =0.5 - (-0.5) where T is fundamental period of x(t)

t = -0.5: T/1000: 0.5;

Kmax = 4; % part a

for k=1:Kmax
  ax = (2./T).*integral(@(t)a(k,T,t),0,T)
end

for k=1:Kmax
  a0 = (1./T).*integral(@(t)fun(t),0,T)
end

for k=1:Kmax
  bx = (2./T).*integral(@(t)b(k,T,t),0,T)
end

% Calcultating x_aprox

x_approx = a0;

for k=1:Kmax
  x_approx = x_approx + ax*cos(2*pi*k*t/T) + bx*sin(2*pi*k*t/T);
end
HansHirse
  • 18,010
  • 10
  • 38
  • 67
  • 1
    "I don't know how to check if it is right, or what I am doing in general." So, how then should any of us know that? Don't you have some kind of example data with the correct, expected output to compare to? – HansHirse Apr 03 '19 at 04:45

1 Answers1

0

The clues you want to look for are in your MATLAB workspace.

By the definition of the Fourier Series, the program is substituting and integrating (summing the values over an interval) the periodic functions needed to compute the Fourier coefficients.

When you do

a = @(k, T, t) fun(t).*cos((2.*pi.*k.*t)./(T));

b = @(k, T, t) fun(t).*sin((2.*pi.*k.*t)./(T));

You are generalizing something over which you will substitute another function into fun(t), and in fact, whenever you do see an @, it allows MATLAB to resolve what's actually going to be substituted in, to be delayed, until a later time, at which that information is available.

Here, a and b will have parameters, k, T, and t. When it comes time to substitute parameters into these functions, MATLAB will know what to do based on what you have first defined here.

Really, most of the work is already done for you within the first three lines of code.

Here, the line

fun = @(t) exp((-7).*abs(t)).*cos(10.*pi.*t);

Specifies that the function handle to fun will have parameter t, where t is the time row-vector parameter, over which all the periodic functions will be defined.