#### Matlab Kullanılarak Yapılan Sinyal Ve Sistemler Projesi

###### Son Teslim Tarihi: 12 Ağustos 2018 17:00

###### Bütçe: ~150 TL

Elektronik, Matlab, Elektrik, Sinyal ve Sistemler, Elektrik Elektronik Mühendisliği,Numerical Evaluation of Fourier Series

1) Preparation (12 points)

In this project, we will use a feature of MATLAB that allows you to evaluate integrals by numerical approximation. MATLAB has several numerical integration functions, such as quad() or quad8() that are very powerful methods for evaluating integrals of function specified by formulas. The "formula" for the integrand must be written as a MATLAB function. Then the numerical integration M-file will adapively figure out the best way to approximate the integral with a sum. We can use these functions to evaluate Fourier Series integrals and then we will plot the resulting Fourier Series coefficients to display the spectrum.

1.1) Numerical Integration with quad

In order to use quad('f',a,b) we must pass three arguments: the function definition (of the integrand), and also the lower and upper limits of integration. The limits of integration are scalar constants, so the tricky part is passing the function definition. There are two ways to do this: (1) write a separate MATLAB function to define the integrand and use the name in quad, or (2) use the MATLAB function inline() to define the entire function via one string.

In MATLAB it seems reasonable that we might be able to define the first argument in quad by writing 'cos(pi*t)' as a string. However this won't work with quad unless we use the inline() function. The alternative is to write a seperate M-file that sets up the integrand as a function that can be evaluated. For example, we might create an auxiliary M-file called fcos.m by writing the following simple function:

function out = fcos(t)

%FCOS function definition os a cosine used in an integral

% the input t can be a vector

%

out = cos(pi*t);

Once we have fcos() function stored in the file fcos.m, we can perform the integration by using the name of the function in the call to quad():

quad('fcos',0,5)

Run this example and verify that it gives the correct answer. Notice that MATLAB gives an answer that is correct to the inherent numerical precision of double-precision floating point which is usually about 15 digits of acuracy.

(a) Modify the example above to do the integral of 𝑒|𝑡| from t=-2 to t=2. This requires that you write a new auxiliary function M-file to generate "e to the minus absolute value of t"

(b) Verify by hand that the correct value of the definite integral in part (a) is 1.7293.

1.2) Plotting with fplot

A useful plotting function when you have a function definition programmed into an M-file is fplot(). The syntax for fplot is nearly the same as quad, except that the range must be given as a two- element vector, not as two seperate arguments.

Use fplot() to plot the function 𝑒|𝑡| over the range −2 ≤ 𝑡 ≤ 2.

1.3) Function Evaluation with feval

Both fplot and quad are based on MATLAB's evaluation function called feval, which can obtain values of a function defined by an M-file. For example, a set of values of the cosine function defined above can be obtained via:

feval('fcos',0:0.25:2)

1.4) Inline Functions

We can eliminate the auxiliary M-file if we exploit MATLAB's capability to define the function as a string. This only works when the function is simple and can be expressed as a "one-liner." Consult help on inline.

For cosine example above, here is the method:

quad(inline('cos(pi*t)'),0,5)

Likewise, you could call fplot() with an inline function definition.

(a) Make a plot of the signal 𝑓(𝑡) = 2 − |𝑡| over the range −1 ≤ 𝑡 ≤ 1 by using fplot and

inline.

(b) Evaluate the definite integral of 𝑓(𝑡) = 2 − |𝑡| over the range by using one call to quad and

inline. Verify by hand that MATLAB gave the correct answer.

2) Warmup (28 points)

In the warm-up you will use quad or fplot to do more complicated integrals, such as those needed in Fourier Series analysis to extract the Fourier coefficients, 𝑎𝑘 .

(a) Consider the problem of plotting the triangle wave of Fig.1 in order to define the waveform so that you can plot it over any interval, it is necessary to figure out two things: how to plot one period of the signal, and then how to plot other periods. In this part, write an auxiliary function that will define the triangle wave over one period from t=0 to t=0.04. In other words, convert the following mathematical statement into vectorized MATLAB code:

There are two ways to approach this (use either one to write your triangle wave function):

(i) Use the find()function to identify the subset of inputs associated with each case. For example, jkl=find(tt>=0 & tt<=1); yy=exp(tt(jkl)); will define a signal that is 𝑒𝑡 over the interval 0 ≤ 𝑡 ≤ 1

(ii) Define one period of the triangle wave using unit-step signals to isolate different regions of the signal. For example, you can define 𝑓(𝑡) = 𝑒𝑡 [𝑢(𝑡) − 𝑢(𝑡 − 1)] by writing:

function out = myfunc(tt)

out = exp(tt).*((tt>=0)-(tt>=1));

The logical expression ((tt>=0)-(tt>=1)) evaluates to 1 if the condition is true and it evaluates to 0 if the condition is false. Thus, by multiplying by ((tt>=0)-(tt>=1)) has the effect of "switching on" the exponential function over the desired set of values of tt.

You can use this technique to switch on and off various pieces of the function definition as required in the triangle wave question. Notice that the two cases of the triangle wave defined in the triangle question can be written as:

(b) One simple way to make the function defined periodic is to use the mod() function which is defined in mathematics as a remainder function that gives a a positive answer, e.g., mod(1.15,0.5) is 0.15 because when 1.15 is divided by 0.5 the integer quotient is 2

and the remainder is 0.15. For negative numbers, the example mod(-0.7,0.5)=0.3 is true because -0.7=0.5(-2)+0.3. Consider the following vector example:

tt= -1:0.01:1; plot(tt,cos(pi*mod(tt,0.5)))

Use this idea to define the periodic triangle wave, but put the mod()operation inside the M-file that defines the triangle wave.

(c) Use your triangle wave function and fplot to make a plot of the triangle wave over the time interval 0.95 ≤ 𝑡 ≤ 1.05 .

(d) Show that you can calculate the DC value of the triangle wave by integrating over any one period of the signal. For example, you can do the integral from 𝑡 = −0.02 to 𝑡 = 0.02 or from 𝑡 = 0 to 𝑡 = 0.04 . Do the integral over both ranges and compare. Remember to divide by the period. Compare your answer to the correct answer which you can obtain by hand.

Submission: You are required to submit a report that includes clear matlab figures, the piece of codes you write in matlab and your comments and calculations if necessary.

1) Preparation (12 points)

In this project, we will use a feature of MATLAB that allows you to evaluate integrals by numerical approximation. MATLAB has several numerical integration functions, such as quad() or quad8() that are very powerful methods for evaluating integrals of function specified by formulas. The "formula" for the integrand must be written as a MATLAB function. Then the numerical integration M-file will adapively figure out the best way to approximate the integral with a sum. We can use these functions to evaluate Fourier Series integrals and then we will plot the resulting Fourier Series coefficients to display the spectrum.

1.1) Numerical Integration with quad

In order to use quad('f',a,b) we must pass three arguments: the function definition (of the integrand), and also the lower and upper limits of integration. The limits of integration are scalar constants, so the tricky part is passing the function definition. There are two ways to do this: (1) write a separate MATLAB function to define the integrand and use the name in quad, or (2) use the MATLAB function inline() to define the entire function via one string.

In MATLAB it seems reasonable that we might be able to define the first argument in quad by writing 'cos(pi*t)' as a string. However this won't work with quad unless we use the inline() function. The alternative is to write a seperate M-file that sets up the integrand as a function that can be evaluated. For example, we might create an auxiliary M-file called fcos.m by writing the following simple function:

function out = fcos(t)

%FCOS function definition os a cosine used in an integral

% the input t can be a vector

%

out = cos(pi*t);

Once we have fcos() function stored in the file fcos.m, we can perform the integration by using the name of the function in the call to quad():

quad('fcos',0,5)

Run this example and verify that it gives the correct answer. Notice that MATLAB gives an answer that is correct to the inherent numerical precision of double-precision floating point which is usually about 15 digits of acuracy.

(a) Modify the example above to do the integral of 𝑒|𝑡| from t=-2 to t=2. This requires that you write a new auxiliary function M-file to generate "e to the minus absolute value of t"

(b) Verify by hand that the correct value of the definite integral in part (a) is 1.7293.

1.2) Plotting with fplot

A useful plotting function when you have a function definition programmed into an M-file is fplot(). The syntax for fplot is nearly the same as quad, except that the range must be given as a two- element vector, not as two seperate arguments.

Use fplot() to plot the function 𝑒|𝑡| over the range −2 ≤ 𝑡 ≤ 2.

1.3) Function Evaluation with feval

Both fplot and quad are based on MATLAB's evaluation function called feval, which can obtain values of a function defined by an M-file. For example, a set of values of the cosine function defined above can be obtained via:

feval('fcos',0:0.25:2)

1.4) Inline Functions

We can eliminate the auxiliary M-file if we exploit MATLAB's capability to define the function as a string. This only works when the function is simple and can be expressed as a "one-liner." Consult help on inline.

For cosine example above, here is the method:

quad(inline('cos(pi*t)'),0,5)

Likewise, you could call fplot() with an inline function definition.

(a) Make a plot of the signal 𝑓(𝑡) = 2 − |𝑡| over the range −1 ≤ 𝑡 ≤ 1 by using fplot and

inline.

(b) Evaluate the definite integral of 𝑓(𝑡) = 2 − |𝑡| over the range by using one call to quad and

inline. Verify by hand that MATLAB gave the correct answer.

2) Warmup (28 points)

In the warm-up you will use quad or fplot to do more complicated integrals, such as those needed in Fourier Series analysis to extract the Fourier coefficients, 𝑎𝑘 .

(a) Consider the problem of plotting the triangle wave of Fig.1 in order to define the waveform so that you can plot it over any interval, it is necessary to figure out two things: how to plot one period of the signal, and then how to plot other periods. In this part, write an auxiliary function that will define the triangle wave over one period from t=0 to t=0.04. In other words, convert the following mathematical statement into vectorized MATLAB code:

There are two ways to approach this (use either one to write your triangle wave function):

(i) Use the find()function to identify the subset of inputs associated with each case. For example, jkl=find(tt>=0 & tt<=1); yy=exp(tt(jkl)); will define a signal that is 𝑒𝑡 over the interval 0 ≤ 𝑡 ≤ 1

(ii) Define one period of the triangle wave using unit-step signals to isolate different regions of the signal. For example, you can define 𝑓(𝑡) = 𝑒𝑡 [𝑢(𝑡) − 𝑢(𝑡 − 1)] by writing:

function out = myfunc(tt)

out = exp(tt).*((tt>=0)-(tt>=1));

The logical expression ((tt>=0)-(tt>=1)) evaluates to 1 if the condition is true and it evaluates to 0 if the condition is false. Thus, by multiplying by ((tt>=0)-(tt>=1)) has the effect of "switching on" the exponential function over the desired set of values of tt.

You can use this technique to switch on and off various pieces of the function definition as required in the triangle wave question. Notice that the two cases of the triangle wave defined in the triangle question can be written as:

(b) One simple way to make the function defined periodic is to use the mod() function which is defined in mathematics as a remainder function that gives a a positive answer, e.g., mod(1.15,0.5) is 0.15 because when 1.15 is divided by 0.5 the integer quotient is 2

and the remainder is 0.15. For negative numbers, the example mod(-0.7,0.5)=0.3 is true because -0.7=0.5(-2)+0.3. Consider the following vector example:

tt= -1:0.01:1; plot(tt,cos(pi*mod(tt,0.5)))

Use this idea to define the periodic triangle wave, but put the mod()operation inside the M-file that defines the triangle wave.

(c) Use your triangle wave function and fplot to make a plot of the triangle wave over the time interval 0.95 ≤ 𝑡 ≤ 1.05 .

(d) Show that you can calculate the DC value of the triangle wave by integrating over any one period of the signal. For example, you can do the integral from 𝑡 = −0.02 to 𝑡 = 0.02 or from 𝑡 = 0 to 𝑡 = 0.04 . Do the integral over both ranges and compare. Remember to divide by the period. Compare your answer to the correct answer which you can obtain by hand.

Submission: You are required to submit a report that includes clear matlab figures, the piece of codes you write in matlab and your comments and calculations if necessary.