## Description

ECE 3723 – Electric Circuits II

Project 2: Functions and Filters

Introduction

One of the benefits of MATLAB®is the vast amount of built-in functions. For example the residue function from

Project 3 can be used to solve partial fraction expansion of any fraction. We are not limited to using only built-in

functions, new functions can be readily created and customized.

In this project, the basics of creating functions will be revised, and then a filter function will be created to

demonstrate the usefulness of having custom functions.

One extra note: all MATLAB plots should be exported from the MATLAB figure window using Edit-> Copy

Figure. Do not take screen captures of the plot windows.

1 Functions – Basics

Functions are a type of .m file where the first line contains the function command. The format of the first line or

header line is

function [output1,output2,…] = filename(input1,input2,…)

and filename needs to match the actual name of the .m file (filename.m). Here is an example of how we would

create a function for a simple parabola with the function f(x) = 2x

2 − 3x + 1:

function y = pbola(x)

y = 2*x.ˆ2-3*x+1;

Note that we suppress the matrix operations of the squared quantity. That allows us to pass matrices into the function without issues. We still need to be cautious – sometimes we want the functions to deal with matrices instead of

vectors of numbers, and in those cases we cannot suppress the matrix operations. Create the function file above.

Then pass the number -1 (pbola(-1)) into the function. What is the output? Next create an x-vector from

-1 to 2 and pass into the function. Plot the response (10 points).

We can also pass functions into functions. Pass the output of pbola(x) into itself. Plot pbola(x) and pbola(pbola(x))

on the same graph using hold (10 points).

The next task is to create a function for a line, y = hx + q. The input parameters should be the slope (h), the

y-intercept point (q), and a vector x lims that contains the “turn on” and “turn off” values of x for the line. For

example, if an x-vector is given that ranges from 0 to 5 and we want to plot a line that starts at x = 1 and turns off at

x = 4, with a slope of 3 and a y-intercept point of 1, then the desired output of the function can be seen in the figure

on the next page.

1

Create a function that can generate lines with the parameters mentioned. Then plot the following lines when x

is from 0 to 4 (use different formatting for each line to make the easily distinguishable from each other).

Show the plot in the report (10 points).

y1(x) = x − 2

y2(x) = −x + 2

Now create a function that can generate a parabola of the format y(x) = a2x

2 + a1x + a0, where a0, a1, and a2 are

input parameters to the function as well as the vector x and the start and stop x values (x1 and x2). Use these two

functions to create:

y3(x) =

2x − x

2

for 0 ≤ x ≤ x1

y2(x) for x1 ≤ x ≤ x2

y1(x) for x2 ≤ x ≤ x3

6x − x

2 − 8 for x3 ≤ x ≤ 4

,

where x1, x2, and x3 are the intercept points between the adjacent functions.

Plot the resulting vector (y3(x)). Scale the plot to be from −1 ≤ x ≤ 5 and −1 ≤ y ≤ 2y3,max (20 points).

2

2 Filter Functions

In this section a function for a bandpass filter should be created. The transfer function of the bandpass filter is shown

below.

H(s) =

1

RC s

s

2 +

1

RC s +

1

LC

Create a function that outputs the magnitude and phase of the filter transfer function along with a frequency

vector. The input parameters should be the center frequency (in Hz), bandwidth (in Hz), capacitor value, and a

plot toggle (f0, BW, C, and pt). The function should calculate the remaining component values. Furthermore, the

function should automatically generate a frequency vector that goes two decades below the lower cutoff frequency

(fc1) and two decades above the higher cutoff frequency (fc2). If the plot toggle value is 1, then the function should

automatically plot the magnitude and phase in a two- row, one-column subplot configuration. If the plot toggle value

is zero, then no plot should be generated.

Verify your function by having it plot a bandpass filter with a center frequency of 10 kHz and a bandwidth of 5

kHz. Assume C = 1 µF.

Show the magnitude plot (in dB) and phase plot (in degrees) of the filter (20 points).

3 Deliverables

This project should be written up in a neatly organized report that includes the results from all the sections, answers

to all the questions, and all plots that are generated. The format of the report needs to be formal, which includes a

front page, introduction, main body, and conclusions. All the code generated for this project must be included in an

appendix (in small font, less than 12pt).

4 Grading

• The report itself is worth 20 points, which will be given based on the structure of the report, grammar, clarity,

and formatting.

• Answering the questions and showing the graphs, listed in the handout, in the main body of the report is worth

70 points.

• Turning in the code in an appendix is worth 10 points.

3