Assignment 1 Solution
Assignment 1 Solution
Mohamad Eid
Fall 2020
Relevant information:
𝑍 = 6𝑅 " + 𝑋! "
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.
𝑍 = 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𝜋𝑓𝑅𝐶
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.
Input/output Description :
Cutoff/center frequency
Selection
Gain (logarithmic and linear)
R and C values
Phase shift
Frequency
Bandwidth (BPF)
Test cases:
The user enters the values R=10, C=2, F=100, and the output would be:
The user enters the values R=20, C=10, F=100, and the output would be:
The user enters the values R1=10, R2=10, C1=10, C2=5, and the output would
be:
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
(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
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
(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
(exit condition)
If choice is equal to ‘x':
Print "Program terminating ...", Newline
Assign false to repeat
(invalid selection)
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;
do {
cout << "Enter Resistance for Low pass filter in kOhm" << endl;
cin >> resistance;
do{
cout << endl << "Enter input frequency in Hz to compute gain of
the low pass filter" << endl;
cin >> frequency_in;
}while (frequency_in <= 0);
// linear gain
linearGain = pow(10.0, gain / 20.0);
cout << "Linear gain: " << linearGain << 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);
// linear gain
linearGain = pow(10.0, gain / 20.0);
cout << "Linear gain: " << linearGain << 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;
// exiy condition
// invalid selection
default:
cout << "Invalid Choice, please enter a character (l, b, h, or
x)." << endl << endl;
}
}
return 0;
}
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’.