Low-Pass Filter Analysis and C Programming Assignment for EE2 Course

Verified

Added on  2022/10/12

|3
|530
|57
Homework Assignment
AI Summary
This assignment presents a C program designed to analyze a second-order low-pass filter. The program prompts the user to input the values of an inductor (in mH) and a capacitor (in µF). It then calculates and displays a table of filter parameters, including inductive reactance, capacitive reactance, attenuation (|Vout/Vin|), and attenuation in dB, across a range of frequencies. The program uses the formula for calculating the filter's frequency response. The calculated data is presented both on the console and saved to a user-specified text file. The code includes necessary header files, defines PI for calculations, and uses a loop to iterate through the frequency values. The program effectively demonstrates the relationship between frequency, reactance, and attenuation in a low-pass filter circuit, providing a practical solution for an electrical engineering assignment.
Document Page
C program code:
#include <stdio.h>
#include <conio.h>
#include <math.h>
#define PI acos(-1.0) // defining value of pi
int main()
{
// Initializing with paramters of low pass filters
float L,C; int i;
float XL[18],XC[18],atten[18],dBatten[18],omega[18];
printf("Enter the value of inductor in mH: ");
scanf("%f",&L);
printf("Enter the value of capacitor in uF: ");
scanf("%f",&C);
float freq[18] =
{100,200,300,400,500,600,700,800,900,1000,2000,3000,4000,5000,6000,7000,8000,9000}; /
/ Initializing frequency array
// Calculation loop
L = L*1e-3;
C= C*1e-6;
for(i=0;i <= 17;i++)
{
omega[i] = 2*PI*freq[i]; // calculating natural frequency
XL[i] = omega[i]*L; XC[i] = 1/(omega[i]*C); // calculating capacitive and inductive
reactance
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
atten[i] = fabs(1/(1-pow(omega[i],2)*L*C)); // calculating attenuation value
dBatten[i] = 20*log10(atten[i]); // calculating attenuation in dB
}
// Printing values to table in command window
printf("INDUCTANCE = %.2f mH\t\t\t\t\tCAPACITANCE = %.2f microF\
n",L*1e3,C*1e6);
printf("Frequency InDUCTIVE CAPACITIVE ATTENUATION ATTENUATION\
n(Hz) REACTANCE REACTANCE |Vout/Vin| (dB)\n");
for(i = 0; i <= 17; i++)
{
printf("%.0f\t %.3f %.3f %.3f %.3f\n",freq[i],XL[i],XC[i],atten[i],dBatten[i]);
}
// Writing to file name specified by user
FILE *fp; /* file pointer*/
char fName[20];
printf("\nEnter file name to create (max 20 characters name) :");
scanf("%s",fName);
fp=fopen(fName,"w");
fwrite(freq,sizeof(float),1,fp);
fwrite(XL,sizeof(float),1,fp);
fwrite(XC,sizeof(float),1,fp);
fwrite(atten,sizeof(float),1,fp);
fwrite(dBatten,sizeof(float),1,fp);
fclose(fp);
printf("All values of table stored to file %s.txt",fName);
return 0;
Document Page
}
Output:
chevron_up_icon
1 out of 3
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]