Blogsheet week 10
In this week’s lab, you will collect more data on low pass
and high pass filters and “process” them using MATLAB.
PART A: MATLAB
practice.
1.
Open MATLAB. Open the editor and copy paste the
following code. Name your code as FirstCode.m
Save the resulting plot as a JPEG image and
put it here.
clear all;
close all;
x = [1 2 3 4 5];
y = 2.^x;
plot(x, y, 'LineWidth', 6)
xlabel('Numbers', 'FontSize', 12)
2.
What does clear
all do?
Remove items from MATLAB workspace and reset MuPAD engine.
Remove items from MATLAB workspace and reset MuPAD engine.
3.
What does close
all do?
Deletes all figures whose handles are not hidden.
Deletes all figures whose handles are not hidden.
4.
In the command line, type x and press enter. This is a matrix. How many rows and columns are
there in the matrix?
Figure 1. Type x then enter in Matlab |
As figure 1 shows, there are 1 row and 5 columns.
5.
Why is there a semicolon at the end of the line
of x and y?
The semicolon can be used to construct arrays, suppress output from a MATLAB command, or to separate commands entered on the same line.
The semicolon can be used to construct arrays, suppress output from a MATLAB command, or to separate commands entered on the same line.
6. Remove the dot on the y = 2.^x; line and execute
the code again. What does the error message mean?
The error message in figure 2 means the dot after 2 can not be omitted. Because arithmetic operations can be performed on each entry of an array x, but you must insert a “period” before certain operators, specifically “*”, “/”, and “^” for multiplication, division, and exponentiation respectively. These operations have a different definition as matrix operations and without the “period”, they would be performed as such. To indicate that you want to perform an element-by-element multiplication, division, or exponentiation, you must precede the operation symbol by a period.
Figure 2. y=2^x in Matlab |
The error message in figure 2 means the dot after 2 can not be omitted. Because arithmetic operations can be performed on each entry of an array x, but you must insert a “period” before certain operators, specifically “*”, “/”, and “^” for multiplication, division, and exponentiation respectively. These operations have a different definition as matrix operations and without the “period”, they would be performed as such. To indicate that you want to perform an element-by-element multiplication, division, or exponentiation, you must precede the operation symbol by a period.
7.
How does the LineWidth affect the plot? Explain.
Figure 3. Skinny line width |
Figure 4. Fat line width |
As figure 3 and 4 show, if we put different values off line width, then the width of the line in the plot will changed. We have to define proper line width of the plot, or it will make the plot become hard to read.
8.
Type help
plot on the command line and study the options for plot command. Provide how you would change the line for plot command to obtain the following
figure (Hint: Like ‘LineWidth’, there
is another property called ‘MarkerSize’)
Figure 5. The plot of question 8 |
clear all;
close all;
x = [1 2 3 4 5];
y = x.^2;
plot(x, y,':ks', 'LineWidth', 5,'MarkerSize', 15);
xlabel('Numbers', 'FontSize', 12);
ylabel('Results', 'FontSize', 12);
"Marker size" need to be added if we want to change the size of dots.
9.
What happens if you change the line for x to x
= [1; 2; 3; 4; 5]; ? Explain.
Nothing happened. The semicolons after 1, 2, 3, 4, and 5 separate numbers in the matrix, and they are optional.
Nothing happened. The semicolons after 1, 2, 3, 4, and 5 separate numbers in the matrix, and they are optional.
10. Provide the code for the following figure. You need to figure
out the function for y. Notice there are grids on the plot.
11. Degree
vs. radian in MATLAB:
a.
Calculate sinus of 30 degrees using a calculator
or internet.
sin = 1/2.
sin = 1/2.
b.
Type sin(30) in the command line of the MATLAB. Why
is this number different? (Hint: MATLAB treats angles as radians).
The answer of sin30 in Matlab is -0.9880 as figure 6 shows, and it is different than what we got in the last question, because Matlab treats angles as radians, so it gave as the answer of sin 30 in radians.
Figure 6. sin 30 in Matlab |
c.
How can you modify sin(30) so we get the correct
number?
As figure 7 shows, we need to add "degtorad" before 30 to get the correct answer of sin 30 in degrees.
Figure 7. correct answer of sin30 in Matlab |
12. Plot
y = 10 sin (100 t) using Matlab with
two different resolutions on the same
plot: 10 points per period and 1000 points per period. The plot needs to
show only two periods. Commands you
might need to use are linspace, plot, hold on, legend, xlabel, and ylabel.
Provide your code and resulting figure. The output figure should look like the
following:
close all;
x = linspace(0,.14,10);
x1 = linspace(0,.14,1000)
y1 = 10*sin(100*x)
y2 = 10*sin(100*x1)
plot(x,y1,'-ro',x1,y2,'-b')
legend('Coarse','Fine')
xlabel('Numbers', 'FontSize', 12)
ylabel('Results', 'FontSize', 12)
Figure 8.Plot y = 10 sin (100 t) using with two different resolutions |
Figure 8 show and the code above show how we plot y = 10 sin (100 t) using Matlab with two different resolutions.
13. Explain
what is changed in the following plot comparing to the previous one.
The difference is that the y function of "Fine" line has been clipped to lower than 5.
14. The
command find was used to create this
code. Study the use of find (help find)
and try to replicate the plot above. Provide your code.
Figure 9 is the code that how we replicate the plot, we need to use command find to clip the plot, and figure 10 is the result plot.
Figure 9. Code for replicate the plot of number 13 |
Figure 10. Replicate the plot of number 13 |
Figure 9 is the code that how we replicate the plot, we need to use command find to clip the plot, and figure 10 is the result plot.
15. Create a code that would clip the negative part of the
sinusoidal signal for the fine plot to -5.
PART B: Filters and
MATLAB
1.
Build a low pass filter using a resistor and
capacitor in which the cut off frequency is 1 kHz. Observe the output signal
using the oscilloscope. Collect several data points particularly around the cut
off frequency. Provide your data in a table.
Figure 11 is the data we collected of low pass filter's Vout with different frequency, and the Vin equals to 5.9V(rms).
Figure 11. Vout table of low pass filter |
2.
Plot your data using MATLAB. Make sure to use
proper labels for the plot and make your plot line and fonts readable. Provide
your code and the plot.
Figure 12. Code of plot low pass filter |
Figure 13. Plot of low pass filter |
Figure 12 is the code that we used to plot low pass filter, and the figure 13 shows the plot.
3. Calculate the cut off frequency using MATLAB. find command will be used. Provide your code.
As figure 14 shows, we need to add find command to calculated and find the cut off frequency.
The completed code will be:
clear all;
close all;
Figure 14. Find command which need to be added. |
Figure 15: Low Pass Cutoff Frequency |
4.
Put a horizontal dashed
line on the previous plot that passes through the cutoff frequency.
5.
Repeat 1-3 by modifying the circuit to a high
pass filter.
(1) Build a high pass filter using a resistor and capacitor in which the cut off frequency is 1 kHz. Observe the output signal using the oscilloscope. Collect several data points particularly around the cut off frequency. Provide your data in a table.
Figure 16 is the data we collected of high pass filter's Vout with different frequency, and the Vin equals to 5.9V(rms).
(2) Plot your data using MATLAB. Make sure to use proper labels for the plot and make your plot line and fonts readable. Provide your code and the plot.
Figure 17 is the code that we used to plot high pass filter, and the figure 18 shows the plot.
(1) Build a high pass filter using a resistor and capacitor in which the cut off frequency is 1 kHz. Observe the output signal using the oscilloscope. Collect several data points particularly around the cut off frequency. Provide your data in a table.
Figure 16. Vout table of high pass filter |
(2) Plot your data using MATLAB. Make sure to use proper labels for the plot and make your plot line and fonts readable. Provide your code and the plot.
Figure 17. Code of plot high pass filter |
Figure 18. Plot of high pass filter |
Figure 17 is the code that we used to plot high pass filter, and the figure 18 shows the plot.
(3) Calculate the cut off frequency using MATLAB. find command will be used. Provide your code.
Also, find command need to be added here, and the completed code is:
6.
Repeat #4 for #5.Also, find command need to be added here, and the completed code is:
Figure 19: Code to calculate cutoff frequency |
Figure 20: High Pass cutoff frequency |
I like how in #7 you guys showed two different LineWidth displays and what the error message actually was in #6, then explaining it.
ReplyDeleteThank you! :)
ReplyDeleteThe blog is easy to read and the answers are very clear to understand. Pictures look good also!
ReplyDeleteThe blog is easy to read and the answers are very clear to understand. Pictures look good also!
ReplyDeleteI like how you go the extra mile with the other pictures showing error messages and line widths.
ReplyDeleteAll looked good. BUT, you plagiarized by copying and pasting MATLAB explanations in A2,3, and 5. That is a big NO!
ReplyDeleteScreenshotting your matlab code makes the blog look much cleaner. I might steal that idea from you guys! Also including the error message that is generated when the carrot is removed was very informative!
ReplyDelete