Tuesday, March 22, 2016

Lab Team 4 Week 10

Blog Sheet 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
clear all;
close all;
x = [1 2 3 4 5];
y = 2.^x;
plot(x, y, 'LineWidth', 6)
xlabel('Numbers', 'FontSize', 12)
ylabel('Results', 'FontSize', 12)

<Save the resulting plot as a JPEG image and put it here.>



(FirstCode.m code and output figure.)

  2.  What does clear all do?

Clear All clears all previously assigned variables/Scripts and functions.

  3.  What does close all do?

Close All clears all 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?

When “x” is input into the command window it returns a 10 column single row matrix as seen below:

>> x

x =

         0    0.0140    0.0279    0.0419    0.0559    0.0698    0.0838    0.0977    0.1117    0.1257



  5.  Why is there a semicolon at the end of the line of x and y?

The Semi colon at the end of x and y prevent those lines of text from being displayed in the command window.

  6.  Remove the dot on the y = 2.^x; line and execute the code again. What does the error message mean?

When the periods is removed  from line four the following error is returned

Error using  ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.

The reason we are getting this error is that we are trying to use an operator that only works with scalar values in a calculation involving matrices.

  7.  How does the LineWidth affect the plot? Explain.

As the Name implies the LineWidth value sets the width of the line connecting the points on your plot.  A low LineWidth value outputs a thin line while a higher value will  result in a thicker line.

  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’)


Below is a version of our original code, along with a screenshot of its output, that our group modified to mimic the figure above.

clear all;
close all;
x = [1; 2; 3; 4; 5];
y = x.^2;
plot(x, y, '-ro','LineWidth', 5, 'MarkerSize', 18, 'MarkerEdgeColor', 'r')
xlabel('Numbers', 'FontSize', 12)
ylabel('Results', 'FontSize', 12)




By adding the text ‘-ro’ (r for red and o for circle) sets the line color to red and adds circular markers at each specified point.  The size of the marker is determined by the “‘MarkerSize’, x,” script additionally the edge color of the marker be set using the “‘MarkerEdgeColor’, x,” although in this case it matches the line color.

  9.  What happens if you change the line for x to x = [1; 2; 3; 4; 5]; ? Explain.

The addition of the semi-colons between the values of x makes no visible change to the output of our script.  The reason for this is that the final semi-colon outside of our closing bracket suppresses the x values from being displayed in the command window.  If that final semicolon were removed the output of x would change from output 1 below to something closer to output 2 (please ignore any vertical alignment issues).

1.)   x = 
         Columns 1 through 5
                        1 2 3 4 5   

2.)  x = 
              1
              2
              3
              4
              5


  10.  Provide the code for the following figure. You need to figure out the function for y. Notice there are grids on the plot.



Below is the code and output we came up with to mimic the figure shown above.

clear all;
close all;
x = [1; 2; 3; 4; 5];
y = x.^2;
plot(x, y, ':sk','LineWidth', 6, 'MarkerSize', 19)
grid
xlabel('Numbers', 'FontSize', 12)
ylabel('Results', 'FontSize', 12)




To achieve the output above we changed the ‘-or’ script to ‘:sk’ (: for a dotted line, s for square markers and k for the color black).

  11.  Degree vs. Radian in MATLAB:
a.       Calculate sinus of 30 degrees using a calculator or internet.

sin(30°) equals 1/2 or 0.5 on my TI-Nspire Calculator
b.      Type sin(30) in the command line of the MATLAB. Why is this number different? (Hint: MATLAB treats angles as radians).

In MatLab sin(30) equals -0.9880.  This is because sin(x) function in MatLab looks at x as a radian value.
c.       How can you modify sin(30) so we get the correct number?

To find the value of sin(30 degrees) in MatLab use the sind(x) function.  When sind(30) is input into MatLab  you get the following result.

>> sind(30)

ans =

    0.5000

  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:

Below is the code and output we came up with to mimic the figure shown above.

clear all;
close all;
t = linspace(0,pi/25,1000);
x = linspace(0,pi/25,10);
y = 10*sin(100.*t);
z = 10*sin(100.*x);
plot(t, y, '-k','LineWidth', 1)
hold on
plot(x, z, '-or','LineWidth', 1, 'MarkerSize', 5)
hold off
xlabel('Time', 'FontSize', 12)
ylabel('Line Function', 'FontSize', 12)




  13.  Explain what is changed in the following plot comparing to the previous one.

The change in this figure is that the maximum y value of the “Fine” plot has its maximum value capped at 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.

Below is the code and output we came up with to mimic the figure shown above, using the find command to cap the Fine plot’s y value at 5.

clear all;
close all;
t = linspace(0,pi/25,10);
y = 10*sin(100.*t);
plot(t, y, '-or','LineWidth', 1, 'MarkerSize', 10)
hold on
x = linspace(0,pi/25,1000);
y = 10.*sin(100.*x);
k = find(y>5);
y(k)= 5;
plot(x,y,'-k')
legend('Coarse','Fine',0);
xlabel('Time (s)', 'FontSize', 12)
ylabel('y function', 'FontSize', 12)




 15.  Create a code that would clip the negative part of the sinusoidal signal for the fine plot to -5.

Below is the Code, and output I wrote to clip the limit the minimum y value of the Fine plot to -5.

clear all;
close all;
t = linspace(0,pi/25,10);
y = 10*sin(100.*t);
plot(t, y, '-or','LineWidth', 1, 'MarkerSize', 10)
hold on
x = linspace(0,pi/25,1000);
y = 10.*sin(100.*x);
k = find(y>5);
j = find(y<-5);
y(k)= 5;
y(j)= -5;
plot(x,y,'-k')
legend('Coarse','Fine',0);
xlabel('Time (s)', 'FontSize', 12)

ylabel('y function', 'FontSize', 12)




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.

Table B-1

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.

Below is the Code, and output we wrote Displaying the output of Table B-1.

clear all;
close all;
vin = [5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94];
vout = [5.89 4.39 2.88 2.03 1.57 1.28 1.07 .929 .813 .656 .267];
x = [30 200 400 600 800 1000 1200 1400 1600 2000 5000];
y = vout ./ vin;
plot(x, y, '-ro','LineWidth', 2, 'MarkerSize', 8, 'MarkerEdgeColor', 'k')
xlabel('Frequency', 'FontSize', 12)

ylabel('Vout / Vin RMS', 'FontSize', 12)


Low Pass Filter Output 

3.  Calculate the cut off frequency using MATLAB. find command will be used. Provide your code.

Below is the Code, and output we wrote to calculate our filter's cutoff value.

clear all;
close all;
vin = [5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94];
vout = [5.94 5.86 5.61 5.26 4.88 4.48 4.12 3.79 3.49 2.99 2.12 1.64];
x = [30 200 400 600 800 1000 1200 1400 1600 2000 3000 4000];
y = vout ./ vin;
plot(x, y, '-ro','LineWidth', 2, 'MarkerSize', 8, 'MarkerEdgeColor', 'k')
xlabel('Frequency', 'FontSize', 12)
ylabel('Vout / Vin RMS', 'FontSize', 12)
k = find (vout>5.94*.65, 1, 'last')
x(k)

Output

>> w10B1
k =  7
ans = 1200

>> 

We were not entirely surprised that the cutoff was around 1200 Hz as opposed to the expected 1.0 kHz.  The reason for this was although our capacitor was labelled as 22 nF our DMM showed it to actually be close to 18 NF and when calculated

1/(2*pi*7230 Ohms*18E-9 NF) =1222 Hz

 4.  Omitted, Not required for Team Blog

 5.  Repeat 1-3 by modifying the circuit to a high pass filter.

   We modified our circuit to construct a high pass filter similar to the one we worked with in week 9 by placing our capacitor in series between our Frequency Generator and Resistor.  

    1B.  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.

Table B-2 Below shows the data we collected as request in step 1B


Table B-2

    2B.  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.


Below is the Code, and output we wrote to calculate our filter's cutoff value.

clear all;
close all;
vin = [5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94];
vout = [5.89 4.39 2.88 2.03 1.57 1.28 1.07 .929 .813 .656 .267];
x = [30 200 400 600 800 1000 1200 1400 1600 2000 5000];
y = vout ./ vin;
plot(x, y, '-ro','LineWidth', 2, 'MarkerSize', 8, 'MarkerEdgeColor', 'k')
xlabel('Frequency', 'FontSize', 12)
ylabel('Vout / Vin RMS', 'FontSize', 12)

High Pass Filter Output




3B.  Calculate the cut off frequency using MATLAB. find command will be used. Provide your code.

clear all;
close all;
vin = [5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94 5.94];
vout = [.261 1.02 1.95 2.78 3.41 3.91 4.29 4.59 4.82 5.14 5.53];
x = [30 200 400 600 800 1000 1200 1400 1600 2000 3000 4000];
y = vout ./ vin;
plot(x, y, '-ro','LineWidth', 2, 'MarkerSize', 8, 'MarkerEdgeColor', 'k')
xlabel('Frequency', 'FontSize', 12)
ylabel('Vout / Vin RMS', 'FontSize', 12)
k = find (vout>5.94*.7, 1, 'first')
x(k)

Output

>> w10B1
k =  7
ans = 1200

>> 

Once again MatLab calculated our cutoff frequency to 1200 Hz.  Again this is not surprising as the formula for calculating the cutoff frequency is the same in both High and Low Pass Capacitive Filter, so;  

1/(2*pi*7230 Ohms*18E-9 NF) 

still equals 1222 Hz in our High Pass setup.



Please feel free to comment below and we will be back again next week

Lab Team 4 Out


8 comments:

  1. All of your graphs look great and very similar to ours. Great work this week!

    ReplyDelete
  2. Great job as always! i like how you build Table B-1, it looks clear and easy to understand all the information. -Yao

    ReplyDelete
  3. Your charts and graphs are very easy to read, I like how the colors scheme changes depending on what you are answering as well.

    ReplyDelete
  4. Thanks, we thought the color scheme was a good addition to our blog too.

    ReplyDelete
  5. I like that you included the pictures from the blog sheet as a reference of the matlab code we were told to write. It is helpful to not have to open the blog sheet to check if your plots are correct.

    ReplyDelete
  6. Good job. All of your graph looks clearly and your code is easy to follow

    ReplyDelete