Corina Roofthooft
CSTN2000-L052
Calculating a low pass filter output
Purpose
The purpose of this lab is to write a program in QBASIC that will calculate the output voltage of a low pass filter (resistor/capacitor circuit) in phasor form. We will then use the program to calculate the output for a wide range of frequencies.
The results will be put in a table as well as plotted on a graph. This will clearly demonstrate the behaviour of a low pass filter.
We will also verify the output results of our program by manual calculations and measurements to compare the accuracy of all three methods.
Equipment
-Microsoft Windows 98 Second Edition 4.10.2222 A
-Microsoft Word 2000 version 9.0.2720
-Microsoft Excel 2000 version 9.0.2720
-MS-DOS QBASIC version 1.1
-1 x 1KΩ resistor
-1 x 0.1μF capacitor
-Signal Generator
-Oscilloscope
Procedure
Procedure overview
Before writing the program we covered the procedure and theory for calculating the output of a low pass filter circuit. Then we wrote our program in QBASIC.
After completing and troubleshooting the program we compared its output values at different frequencies with the manually calculated output at those frequencies. Following that we wired up a low pass filter RC circuit and measured the output at the same frequencies, once again comparing the results.
To finish the lab we generated data for a wide range of frequencies and plotted the results on a graph using Microsoft Excel.
1
Low pass filter output explanation
R = resistance of R1
Xc = reactance of C1
Formulas:
In the formula we need to divide Xc and R + Xc in their phasor form.
Xc is a phasor of magnitude Xc at angle -90°.
R+Xc forms an imaginary number and can be converted to a phasor where
magnitude = and angle = .
Applying this to the formula for Vout we find the result
Magnitude = at and angle = -90° - .
QBASIC program explanation
We used MS-DOS QBASIC, version 1.1 . It is an external command available in all versions of MS-DOS, Windows 95 and Windows 98. It is also widely available on-line.
It is found under /WINDOWS/COMMAND and can be run from command line or through start->run. Since it is a DOS application it will run in a DOS window on a Windows operating system.
To calculate the output of a low pass filter circuit we need the user to enter values for voltage in, resistance, capacitance and frequency. The syntax for the INPUT command is INPUT "question displayed on screen"; variable.
After letting the user enter the values for variables vin, r, c and freq; we declare a variable pi for .
Since QBASIC uses radians for angles and we want the resulting angle in degrees, we put in a variable called degperrad to convert radians to degrees.
1 radian = 360/2degrees.
The LOG function of QBASIC uses natural logs instead of the base 10 logarithms. To convert from natural logs to base 10 logs we divide the result by 2.30258. Variable natlogcon does this.
With the values we have now we can calculate Xc using the formula Xc=1/2*pi*freq*c.
QBASIC displays powers of 10 as numberEpower. This is also the way to enter powers of 10 into QBASIC.
After running through all the calculations and displaying the results, the program allows you to repeat the process for a different frequency using the same Vin, R and C values.
Measurement setup
R1 = 1000Ω
C1 = 0.1μF
Vin = 10Vp-p
We hooked up our signal generator as voltage source in the schematic. It was set up to 10Vp-p at frequencies 1591.55Hz, 159.155Hz and 15915.5Hz.
Our oscilloscope was connected to show the input wave on channel 1 and the output on channel 2.
Note: To find the phase using the oscilloscope, use .
Determining wether the program works
To determine the accuracy of our code we compared generated, calculated and measured results for Vout at different frequencies. The first frequency we tested was the cut-off frequency. The cut-off frequency (fc) of a low pass filter can be found by applying its primary characteristic, Xc=R at fc, to the formula for Xc.
A table containing the results for this comparison can be found in the results part of this report. The full manual calculations can be found in appendix 2.
Results
There are 3 sections to the result part of this report. The first shows a table with the comparison between generated, calculated and measured output results for the low pass filter circuit. The second part contains tables with data generated by the QBASIC program and Excel for frequencies ranging from 1Hz to 1MHz. The third part contains 3 graphs plotting the results of the data generated by Excel.
Comparison table
Magnitude (V) / Angle (degrees) / Attenuation decibels (db)fc 1591.55Hz
Program output / 7.07007 / -45.00805 / -3.011533
Calculated output / 7.07 / -45 / -3.01
Measured output / 7 / -40.1
1/10th fc 159.155Hz
Program output / 9.950344 / -5.712126 / -0.04323811
Calculated output / 9.95 / -6 / -0.044
Measured output / 10 / -5.62
10x fc 15915.5Hz
Program output / 0.9950686 / -84.28922 / -20.04298
Calculated output / 0.995 / -84 / -20
Measured output / 1.1 / -81
Generated data using QBASIC program
Voltage in (Vin) / 10Resistance (R) / 1000
Capacitance (C) / 0.1E-6
Cut-off frequency (fc) / 1591.55
Frequency f (Hz) / Magnitude Vout (V) / Phase Angle (degrees) / Attenuation (db)
1 / 9.999998 / -0.03592704 / -1.656702E-06
2 / 9.999992 / -.071927 / -6.626811E-06
5 / 9.99995 / -.1799265 / -4.307436E-05
10 / 9.999803 / -.3599223 / -1.714704E-04
20 / 9.99921 / -.7198892 / -6.859018E-04
50 / 9.995069 / -1.799335 / -4.28446E-03
100 / 9.980319 / -3.595201 / -1.711152E-02
200 / 9.921967 / -7.162383 / -6.804483E-02
500 / 9.540283 / -17.44052 / -.4087747
1000 / 8.467332 / -32.14184 / -1.445068
1591.55 / 7.071069 / -44.99995 / -3.010298
2000 / 6.226773 / -51.48806 / -4.11474
5000 / 3.033147 / -72.34319 / -10.36213
10000 / 1.571769 / -80.95692 / -16.07223
20000 / .7932676 / -85.45013 / -22.01161
50000 / .318149 / -88.17683 / -29.94739
100000 / .1591349 / -89.08819 / -35.96469
200000 / 7.957502E-02 / -89.54406 / -41.98447
500000 / 3.183085E-02 / -89.81762 / -49.94304
1000000 / 1.591549E-02 / -89.90881 / -55.96361
Generated data using Office Excel 2000
Voltage in (Vin) / 10
Resistance (R) / 1000
Capacitance (C) / 1.00E-07
Cut-off frequency (fc) / 1591.55
Frequency f (Hz) / Magnitude Vout (V) / Phase Angle (degrees) / Attenuation (db)
1 / 9.9999980 / -0.0360 / -1.7145E-06
2 / 9.9999921 / -0.0720 / -6.8581E-06
5 / 9.9999507 / -0.1800 / -4.2863E-05
10 / 9.9998026 / -0.3600 / -1.7145E-04
20 / 9.9992105 / -0.7200 / -6.8576E-04
50 / 9.9950688 / -1.7994 / -4.2842E-03
100 / 9.9803190 / -3.5953 / -1.7112E-02
200 / 9.9219662 / -7.1625 / -6.8045E-02
500 / 9.5402822 / -17.4406 / -4.0878E-01
1.00E+03 / 8.4673302 / -32.1419 / -1.4451E+00
1.59E+03 / 7.0710665 / -45.0000 / -3.0103E+00
2.00E+03 / 6.2267699 / -51.4881 / -4.1147E+00
5.00E+03 / 3.0331447 / -72.3432 / -1.0362E+01
1.00E+04 / 1.5717673 / -80.9569 / -1.6072E+01
2.00E+04 / 0.7932670 / -85.4501 / -2.2012E+01
5.00E+04 / 0.3181488 / -88.1768 / -2.9947E+01
1.00E+05 / 0.1591348 / -89.0882 / -3.5965E+01
2.00E+05 / 0.0795750 / -89.5441 / -4.1984E+01
5.00E+05 / 0.0318308 / -89.8176 / -4.9943E+01
1.00E+06 / 0.0159155 / -89.9088 / -5.5964E+01
Cut-off frequency (fc) / 1/(2*PI()*$B$3*$B$2)
Reactance Xc (Ohms) / 1/(2*PI()*C8*$B$3)
Magnitude Vout (V) / $B$1*(D8/SQRT($B$2^2+D8^2))
Phase Angle (degrees) / -90-(DEGREES(ATAN(-D32/$B$2)))
Attenuation (db) / 20*LOG10(E32/$B$1)
1
1
Conclusion
After comparing the results of our manual calculations to the output of the program it is clear that the program works, and can give us more significant figures than manual calculation. There is a very slight difference in accuracy between the QBASIC program we wrote and using Excel to do the same thing. The most likely cause for this is the extra variables we had to put into QBASIC for pi and for converting natural logs to base 10 logs. Excel can work with base 10 logarithms and has a built in value for pi.
When comparing the results of the program with the measured results we find a larger difference mainly because it is hard to measure very accurately using our oscilloscope. We should also take into consideration that the resistor and capacitor used for measurements are real, not perfect. The capacitor I used was measured to be about 82.2 nF instead of 100nF.
We used logarithmic scales for plotting values that cover a wide range, such as 1Hz to 1Mhz. This is so that values are evenly spread over the graph as opposed to having an area where all dots are almost on top of eachother leading to an area where dots are far apart.
From the Magnitude vs Frequency graph we can tell that Vout is equal to Vin at low frequencies, that the magnitude is 7.07V at the cut-off frequency, and that magnitude starts going towards 0 at high frequencies.
From the Attenuation vs Frequency graph we can tell that after cut-off frequency is reached attenuation drops about 6 dB per octave and attenuation is near 0 at low frequencies. Attenuation drops at 20 dB per decade after cut-off frequency.
Comparing the Magnitude vs. Frequency and Attenuation vs. Frequency graphs shows that magnitude drops when frequency rises, and tells us that lower frequencies pass through with very little attenuation, whereas higher frequences are nearly blocked and very attenuated. This is the behaviour of a low-pass filter.
1
Appendix 1 - QBASIC program
CLS
INPUT "Enter input voltage "; vin
INPUT "Enter resistance (in ohms) "; r
INPUT "Enter capacitance (in farads) "; c
again: INPUT "Enter frequency (in Hz) "; freq
pi = 3.14159
degperrad = 360 / (2 * pi)
natlogcon = 1 / 2.302585
xc = 1 / (2 * pi * freq * c)
mag = vin * (xc / (SQR(xc * xc + r * r)))
angle = -90 - (degperrad * (ATN(-xc / r)))
decibels = natlogcon * (20 * LOG(mag / vin))
PRINT "mag="; mag; " angle="; angle; " freq="; freq; " db="; decibels
GOTO again
END
1
Appendix 2 - Calculations
Manual Calculations for magnitude, angle and attenuation decibels.
at cut-off frequency
cut-off frequency for R = 1KΩ and C = 0.1μF
R=X → R=1000Ω → → = 1591.55 Hz
at 1/10th of cut-off frequency
1/10th cut-off frequency = 159.155 Hz
at 10x cut-off frequency
10x cut-off frequency = 15915.5 Hz
1