0% found this document useful (0 votes)
4 views

Assignment 1 Solution

The document describes a software program that analyzes different types of passive filters. It explains the characteristics of low pass, high pass, and band pass filters that the program calculates based on user input resistor and capacitor values. It provides examples of test cases and outlines the algorithm design for the program.

Uploaded by

Line Li
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment 1 Solution

The document describes a software program that analyzes different types of passive filters. It explains the characteristics of low pass, high pass, and band pass filters that the program calculates based on user input resistor and capacitor values. It provides examples of test cases and outlines the algorithm design for the program.

Uploaded by

Line Li
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ENGR-UH 1000

Computer Programming for Engineers

Assignment 1 – Electrical Engineering Case Study


Passive Filter Analysis
Software Report

Mohamad Eid

Fall 2020

Assignment 1 Page 1 Fall 2020


Step 1: Problem Identification and Statement
The objective is to develop a software that provides characteristic analysis for different types of
passive filters, namely low pass filters, high pass filters, and band pass filters. The software asks
the user to input the values for the respective electronic components like resistor and capacitor
and displays the characteristics of the corresponding filter. The program enables the user to
perform multiple filter analysis by repeating the process as desired.

Step 2: Gathering of Information and Input/Output Description

Relevant information:

Low Pass RC Filter


A low pass filter allows signals ranging from 0 𝐻𝑧 to a cutoff frequency Fc, while blocking all other signals
of higher frequencies. A first order low pass RC filter can be designed as a linear combination of a resistor
and a capacitor connected in series. Input signal (𝑉𝑖𝑛) is applied across resistor and a capacitor and output
signal (𝑉𝑜𝑢𝑡) is measured across capacitor only. Schematic and frequency response of a low pass filter is
shown below.

Cutoff frequency of an LPF can be calculated as:


1
𝐹! =
2𝜋𝑅𝐶
Gain of low pass filter.

Assignment 1 Page 2 Fall 2020


𝑉𝑜𝑢𝑡
𝐺 = 20𝑙𝑜𝑔
𝑉𝑖𝑛
Where 𝑉𝑜𝑢𝑡/𝑉𝑖𝑛 can be calculated from the capacitive reactance (𝑋𝑐) and impedance (𝑍) of a circuit as
follows:
1
𝑋! =
2𝜋𝑓𝐶

𝑍 = 6𝑅 " + 𝑋! "

𝑉𝑜𝑢𝑡 across a capacitor through potential divider rule


𝑋&
𝑉!"# = 𝑉$%
𝑍
Linear gain can be calculated as
𝐿𝑖𝑛𝑒𝑎𝑟 𝑔𝑎𝑖𝑛 = 10((/*+)
Phase shift of a low pass filter

𝑃ℎ𝑎𝑠𝑒 𝑠ℎ𝑖𝑓𝑡 φ = −tan-. (2𝜋𝑓𝑅𝐶) ∗ (180/𝜋)

High Pass Filter

A high pass filter allows signals ranging from a cutoff frequency Fc and higher to infinity while blocking all
other signals of lower frequencies. A first order high pass RC filter can be designed as a linear combination
of a capacitor and a resistor connected in series. Input signal (𝑉!" ) is applied across capacitor and resistor and
output signal (𝑉#$% ) is measured across resistor only. Schematic and frequency response of a high pass filter
is shown below.

Cutoff frequency of a high pass filter can be calculated as:


1
𝐹! =
2𝜋𝑅𝐶
Gain of high pass filter.
𝑉𝑜𝑢𝑡
𝐺 = 20𝑙𝑜𝑔
𝑉𝑖𝑛
Assignment 1 Page 3 Fall 2020
Where 𝑉𝑜𝑢𝑡/𝑉𝑖𝑛 can be calculated from the capacitive reactance (𝑋& ) and impedance (𝑍) of a circuit as
follows:
1
𝑋! =
2𝜋𝑓𝐶

𝑍 = 6𝑅 " + 𝑋! "
Output signal 𝑉𝑜𝑢𝑡 across the resistor through potential divider rule
𝑅
𝑉!"# = 𝑉$%
𝑍
Linear gain can be calculated as
𝐿𝑖𝑛𝑒𝑎𝑟 𝑔𝑎𝑖𝑛 = 10((/*+)
Phase shift of a high pass filter
1
𝑃ℎ𝑎𝑠𝑒 𝑠ℎ𝑖𝑓𝑡 φ = tan-. ( ) ∗ (180/𝜋)
2𝜋𝑓𝑅𝐶

Band Pass Filter

A band pass filter allows signals in a particular band or range of frequencies while rejecting all other
frequencies. A second order band pass RC filter can be designed by cascading a high pass filter followed by
a low pass filter. This arrangement results in a band pass filter with band frequencies controlled by high
pass and low pass filters. A high pass filter gives a lower cutoff frequency 𝒇𝒍 followed by a higher cutoff
frequency 𝒇𝒉 by a low pass filter. The bandwidth of a bandpass filter is the difference between a higher and
a lower cut off frequency (𝒇𝒉 − 𝒇𝒍 ). Schematic and frequency response of a band pass filter is shown
below.

Center frequency (resonant frequency) of a band pass filter can be calculated as:

𝑓/ = B𝑓0 × 𝑓1
Where 𝑓) and 𝑓* are the higher and lower cutoff frequencies and can be calculated using same formulae for
a low pass and a high pass filter.

Bandwidth of a band pass filter

Assignment 1 Page 4 Fall 2020


𝐵𝑎𝑛𝑑𝑤𝑖𝑑𝑡ℎ = 𝑓0 − 𝑓1
Phase shift should be ≈ 0 at center frequency
1
phase shift φ ≈ (tan-. ( ) −tan-. (2𝜋𝑓𝑅𝐶)) ∗ (180/𝜋)
2𝜋𝑓𝑅𝐶

Input/output Description :

Cutoff/center frequency
Selection
Gain (logarithmic and linear)
R and C values
Phase shift
Frequency
Bandwidth (BPF)

The following explains how the program executes.

Enter the choice of your filter to compute characteristics


Enter 'l' for low pass
'b' for band pass and
'h' for high pass
Or enter 'x' to exit
l (for low pass filter)
Enter Resistance for Low pass filter in kOhm
10
Enter capacitance in nF
2
Cut off frequency of a passive low pass filter with a resistance 10 kOhm
and at capacitance 2 nF is 7957.73 Hz

Enter input frequency in Hz to compute gain of the low pass filter


100
Gain of the low pass filter at frequency 100 Hz is: -0.000685759 db
Linear gain: 0.999921
Low pass filter phase shift at input frequency 100 Hz is: -0.719962
degrees

(back to the main menu)


Enter the choice of your filter to compute characteristics
Enter 'l' for low pass
'b' for band pass and
'h' for high pass
Or enter 'x' to exit

(When the user enters ‘x’, the program exits)

Step 3: Design of the algorithm and hand-solved problems

Test cases:

Test Case 1: Invalid selection input


Enter the choice of your filter to compute characteristics
Enter 'l' for low pass

Assignment 1 Page 5 Fall 2020


'b' for band pass and
'h' for high pass
Or enter 'x' to exit
W (invalid choice)

(The program must display an error message):

Invalid choice, please enter a valid character.

Test Case 2: Low pass filter validation

The user enters the values R=10, C=2, F=100, and the output would be:

Cutoff frequency: 7957.73 Hz


Gain: -0.000685759 db
Linear gain: 0.999921
Phase shift: -0.719962 degrees

Test Case 3: High pass filter validation

The user enters the values R=20, C=10, F=100, and the output would be:

Cutoff frequency: 795.773 Hz


Gain: -18.0838 db
Linear gain: 0.124683
Phase shift: 82.8373 degrees

Test Case 4: Band pass filter validation

The user enters the values R1=10, R2=10, C1=10, C2=5, and the output would
be:

Center frequency: 2250.79 Hz


Bandwidth: 1591.55
Phase shift: 6.36109e-15 degrees

Test Case 5: Exit the program


Enter ‘x’ to exit the program:
Print the message “Program terminating ...” and exit the program.

Assignment 1 Page 6 Fall 2020


Algorithm design:

Define PI as 3.1416

Main() Function
Declare choice as Character
Declare resistance, capacitance, frequency_in, fl, fh, fr, bp_c2, bp_c1, bp_r2, bp_r1, gain, fc,
linearGain, phaseShift, bandwidth as double

Assign true to repeat

Repeat while repeat variable is true


Print "Enter the choice of your filter to compute characteristics", Newline
Print "Enter 'l' for low pass”, Newline,
Print “'b' for band pass and”, newline,
Print “'h' for high pass”, Newline,
Print “Or enter 'x' to exit"
Read value into choice

(check the user input)


(Low pass filter characterization.)
If choice is equal to l':
Repeat
Print "Enter Resistance for Low pass filter in kOhm", Newline
Read value into resistance
Print "Enter capacitance in nF", Newline
Read value into capacitance
While resistance or capacitance are less than or equal 0
Assign 1.0 / (2 * PI * resistance * 10^3 * capacitance * 10^-9) to fc
Print "Cut off frequency of a passive low pass filter with a resistance ",
resistance, " kOhm and at capacitance ", capacitance, " nF is ", fc, " Hz",
Newline
Repeat
Print "Enter input frequency in Hz to compute gain of the low pass filter"
Read value into frequency_in
While frequency_in less than or equal 0
Assign 1.0 / (2 * PI * frequency_in * capacitance * 10^-9) to reactance_Xc
Assign SquareRoot((resistance * 10^3)^2) + reactance_Xc^2) to impedance_Z
Assign 20 * log10(reactance_Xc / impedance_Z) to gain
Print "Gain of the low pass filter at frequency ", frequency_in, " Hz is: ", gain, "
db", Newline

(linear gain)
Assign 10.0^(gain / 20.0) to linearGain
Print "Linear gain: ", linearGain, Newline

(Phase shift at cutoff frequency and conversion to degree (should be -45 at fc))
Assign -atan(2 * PI * frequency_in * resistance * 10^3 * capacitance * 10^-9) *
180 / PI to phaseShift
Print "Low pass filter phase shift at input frequency ", frequency_in, " Hz is: ",
phaseShift, " degrees", Newline

(high pass filter characterization.)


If choice is equal to ‘h':
Repeat
Print "Enter Resistance for Low pass filter in kOhm", Newline
Read value into resistance
Print "Enter capacitance in nF", Newline
Read value into capacitance
While resistance or capacitance are less than or equal 0

Assignment 1 Page 7 Fall 2020


Assign 1.0 / (2 * PI * resistance * 10^3 * capacitance * 10^-9) to fc
Print "Cut off frequency of the passive high pass filter for a resistance ",
resistance, " kOhm and a capacitance ", capacitance, " nF is ", fc, " Hz",
Newline

Repeat
Print "Enter input frequency in Hz to compute gain of the low pass filter"
Read value into frequency_in
While frequency_in less than or equal 0

Assign 1.0 / (2 * PI * frequency_in * capacitance * 10^-9) to reactance_Xc


Assign SquareRoot((resistance * pow(10, 3)(^2) + (reactance_Xc^2)) to
impedance_Z
Assign 20 * log10(resistance * 10^3) / impedance_Z) to gain
Print "Gain of the high pass filter at cutoff frequency fc ", fc, " Hz is: ", gain, "
db", Newline

(linear gain)
Assign (10.0^(gain / 20.0) to linearGain
Print "Linear gain: ", linearGain, Newline

(Phase shift at cutoff frequency and conversion to degree (should be +45 at fc)
Assign atan(1.0 / (2 * PI * frequency_in * resistance * 10^3 * capacitance *
10^-9)) * 180 / PI to phaseShift
Print "High pass filter phase shift at input frequency ", frequency_in, " Hz is: ",
phaseShift, " degrees", Newline

(band pass filter characterization)


If choice is equal to ‘b':
Repeat
Print "Enter resistance (R1) for the high pass filter in kOhm", Newline
Read value into bp_r1
Print "Enter resistance (R2) for the low pass filter in kOhm", Newline
Read value into bp_r2
Print "Enter capacitance (C1) in nF for the high pass filter", Newline
Read value into bp_c1
Print "Enter capacitance (C2) in nF for the low pass filter", Newline
Read value into bp_c2
While bp_r1 or bp_r2 or bp_c1 or bp_c2 less than or equal 0
Assign 1.0 / (2 * PI * bp_r1 * 10^3 * bp_c1 * 10^-9) to fl
Assign 1.0 / (2 * PI * bp_r2 * 10^3 * bp_c2 * 10^-9) to fh
(Center frequency)
Assign SquareRoot(fl * fh) to fr
Print "Center frequency for band pass filter is: ", fr, " Hz", Newline
Assign fh – fl to bandwidth
Print "Bandwidth: ", bandwidth, Newline

(Phase shift at center frequency and conversion to degree (should be ~0 at fr))


(Add phase shifts from High pass and Low pass.)
Assign (atan(1.0 / (2 * PI * fr * bp_r1 * pow(10, 3) * bp_c1 * pow(10, -9))) -
atan(2 * PI * fr * bp_r2 * pow(10, 3) * bp_c2 * pow(10, -9))) * 180 / PI to
phaseShift
Print "Band pass filter phase shift at center frequency fr ", fr, " Hz is: ",
phaseShift, " degrees", Newline

(exit condition)
If choice is equal to ‘x':
Print "Program terminating ...", Newline
Assign false to repeat

(invalid selection)

Assignment 1 Page 8 Fall 2020


Otherwise:
Print "Invalid Choice, please enter a character (l, b, h, or x).", Newline;

End program.

Step 4: Implementation
//***********************************************************//
//** Author:Muhammad Hassan Jamil **//
//** Date Created: July 20, 2020 **//
//** Assignment 1: Filter Analysis **//
//** UH-1000 Computer Programming for Engineers, NYUAD **//
//** Problem: **//
//** Computation of the characteristics of Passive **//
//** Filters (Low Pass, High Pass and Band Pass) given the **//
//** Resistance, Capacitance and Frequency **//
//***********************************************************//

#include<iostream>
#include<cmath>
using namespace std;

#define PI 3.1416

int main() {
//Input variables
char choice;
double resistance;
double capacitance;
double frequency_in;
//For Band Pass
double fl, fh, fr;
double bp_c2, bp_c1;
double bp_r2, bp_r1;
//Output variables
double gain;
double fc; //cut off frequency
double linearGain;
double phaseShift;
double bandwidth;

//temporary variables
double reactance_Xc;
double impedance_Z;
bool repeat = true;

// A sentinal loop for the main menu.


while (repeat) {
// present the menu and prompt the user for a character input.
cout << "\nEnter the choice of your filter to compute characteristics"
<< endl;
cout << "\tEnter 'l' for low pass\n\t'b' for band pass and\n\t'h' for
high pass\n\tOr enter 'x' to exit\n";
cin >> choice;

// check the user input


switch (choice)
{
// low pass filter characterization.
case 'l':

do {
cout << "Enter Resistance for Low pass filter in kOhm" << endl;
cin >> resistance;

Assignment 1 Page 9 Fall 2020


cout << "Enter capacitance in nF" << endl;
cin >> capacitance;
} while (resistance <= 0 ||capacitance <= 0);

fc = 1.0 / (2 * PI * resistance * pow(10, 3) * capacitance *


pow(10, -9));
cout << "Cut off frequency of a passive low pass filter with a
resistance " << resistance
<< " kOhm and at capacitance " << capacitance << " nF is " <<
fc << " Hz" << endl;

do{
cout << endl << "Enter input frequency in Hz to compute gain of
the low pass filter" << endl;
cin >> frequency_in;
}while (frequency_in <= 0);

reactance_Xc = 1.0 / (2 * PI * frequency_in * capacitance *


pow(10, -9));
impedance_Z = sqrt(pow(resistance * pow(10, 3), 2) +
pow(reactance_Xc, 2));
gain = 20 * log10(reactance_Xc / impedance_Z);
cout << "Gain of the low pass filter at frequency " <<
frequency_in << " Hz is: " << gain << " db" << endl;

// linear gain
linearGain = pow(10.0, gain / 20.0);
cout << "Linear gain: " << linearGain << endl;

//Phase shift at cutoff frequency and conversion to degree


(should be -45 at fc)
phaseShift = -atan(2 * PI * frequency_in * resistance * pow(10,
3) * capacitance * pow(10, -9)) * 180 / PI;
cout << "Low pass filter phase shift at input frequency " <<
frequency_in << " Hz is: " << phaseShift << " degrees" << endl;
break;

// high pass filter characterization.


case 'h':
do {
cout << "Enter Resistance for High pass filter in kOhm" <<
endl;
cin >> resistance;
cout << "Enter capacitance in nF" << endl;
cin >> capacitance;
}while (resistance <= 0 || capacitance <= 0);

fc = 1.0 / (2 * PI * resistance * pow(10, 3) * capacitance *


pow(10, -9));
cout << "Cut off frequency of the passive high pass filter for
a resistance " << resistance
<< " kOhm and a capacitance " << capacitance << " nF is " << fc
<< " Hz" << endl;

do {
cout << endl << "Enter input frequency in Hz to compute gain of
the high pass filter" << endl;
cin >> frequency_in;
}while (frequency_in <= 0);

reactance_Xc = 1.0 / (2 * PI * frequency_in * capacitance *


pow(10, -9));
impedance_Z = sqrt(pow(resistance * pow(10, 3), 2) +
pow(reactance_Xc, 2));

Assignment 1 Page 10 Fall 2020


cout << "Reactance Xc= " << reactance_Xc << " Impedance Z= " <<
impedance_Z << endl;
gain = 20 * log10(resistance * pow(10, 3) / impedance_Z);
cout << "Gain of the high pass filter at cutoff frequency fc "
<< fc << " Hz is: " << gain << " db" << endl;

// linear gain
linearGain = pow(10.0, gain / 20.0);
cout << "Linear gain: " << linearGain << endl;

//Phase shift at cutoff frequency and conversion to degree


(should be +45 at fc)
phaseShift = atan(1.0 / (2 * PI * frequency_in * resistance *
pow(10, 3) * capacitance * pow(10, -9))) * 180 / PI;
cout << "High pass filter phase shift at input frequency " <<
frequency_in << " Hz is: " << phaseShift << " degrees" << endl;
break;

// band pass filter characterization.


case 'b':
do {
cout << "Enter resistance (R1) for the high pass filter in
kOhm" << endl;
cin >> bp_r1;
cout << "Enter resistance (R2) for the low pass filter in kOhm"
<< endl;
cin >> bp_r2;
cout << "Enter capacitance (C1) in nF for the high pass filter"
<< endl;
cin >> bp_c1;
cout << "Enter capacitance (C2) in nF for the low pass filter"
<< endl;
cin >> bp_c2;
} while (bp_r1 <= 0 || bp_r2 <= 0 || bp_c1 <= 0 || bp_c2 <= 0);

fl = 1.0 / (2 * PI * bp_r1 * pow(10, 3) * bp_c1 * pow(10, -9));


cout << "Higher cut off frequency Fl for a resistance " <<
bp_r1
<< " kOhm and at a capacitance " << bp_c1 << " nF is " << fl <<
" Hz" << endl;

fh = 1.0 / (2 * PI * bp_r2 * pow(10, 3) * bp_c2 * pow(10, -9));


cout << "Lower Cut off frequency Fh for a resistance " << bp_r2
<< " kOhm and a capacitance " << bp_c2 << " nF is " << fh << "
Hz" << endl;

//Center freq
fr = sqrt(fl * fh);
cout << "Center frequency for band pass filter is: " << fr << "
Hz" << endl;

bandwidth = fh - fl;
cout << "Bandwidth: " << bandwidth << endl;

//Phase shift at center frequency and conversion to degree


(should be ~0 at fr)
//Add phase shifts from High pass and Low pass.
phaseShift = (atan(1.0 / (2 * PI * fr * bp_r1 * pow(10, 3) *
bp_c1 * pow(10, -9))) - atan(2 * PI * fr * bp_r2 * pow(10, 3) * bp_c2 * pow(10,
-9))) * 180 / PI;
cout << "Band pass filter phase shift at center frequency fr "
<< fr << " Hz is: " << phaseShift << " degrees" << endl;
break;

// exiy condition

Assignment 1 Page 11 Fall 2020


case 'x':
cout << "Program terminating ..." << endl;
repeat = false;
break;

// invalid selection
default:
cout << "Invalid Choice, please enter a character (l, b, h, or
x)." << endl << endl;
}
}

return 0;
}

Step 5: Software Testing and Verification

Test Case 1: Invalid selection input

Test Case 2: Low pass filter validation

Assignment 1 Page 12 Fall 2020


Test Case 3: High pass filter validation

Test Case 4: Band pass filter validation

Test Case 5: Exit the program

User Guide
This program will help you determine the characteristics of a passive filter (low pass,
band pass, or high pass). You may select which type of passive filters to analyze. Once
selected, you enter the values for the resistor and capacitor and the program determines
the characteristics for the respective filter.
To terminate the software, simply type ‘x’.

Assignment 1 Page 13 Fall 2020

You might also like