Friday, March 25, 2016

Week 10

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)
ylabel('Results', 'FontSize', 12)





Image 1: MATLAB result




2.       What does clear all do?
Clear all clears out all variables that were previously defined

3.       What does close all do?
Close all deletes all figures in handles that aren’t 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?
There are 5 columns and 1 row. 

x = [ 1 2 3 4 5]



5.       Why is there a semicolon at the end of the line of x and y?
There’s a semicolon at the end of the line of x and y because it ends each line, that way they're separate from each other.



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

 The message means that the line can't perform the code because the code is an element by element, and with that you need to have a period between, in this case, the 2 and the power of x. If x was only equaled to one number, then the code may have executed. The code may also be executed if the matrix was squared. 

7.       How does the LineWidth affect the plot? Explain.
LineWidth affects the plot by making the plotted line either thinner or thicker



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


This is our code: 

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


 We added 'ro-' to get the red solid line and circle markers, and then added 'MarkerSize' to size the circles.We made the  MarkerSize bigger than the LineWidth in order to have the open circles; otherwise, the circles looked filled.





9.       What happens if you change the line for x to x = [1; 2; 3; 4; 5]; ? Explain.
If you change the line in the code, you'll get nothing. The semicolons defines the numbers in the brackets to x.

 However, if you were to change the line alone (without the rest of the code), you would get a new row. So in this case, the output would look like


x =

     1
     2
     3
     4
     5


instead of


x =

     1     2     3     4     5








10.   Degree vs. radian in MATLAB:
a.       Calculate sine of 30 degrees using a calculator or internet.

Degree Mode: sin(30) = 1/2 = 0.5

Radian Mode: sin(30) = pi/6


b.      Type sin(30) in the command line of the MATLAB. Why is this number different? (Hint: MATLAB treats angles as radians).

In MATLAB, we got -0.9880. This is different because MATLAB is treating 30 as radians instead of degrees.


c.       How can you modify sin(30) so we get the correct number?

You can modify sin(30) by changing 30 to pi/6. We when did this, we got 0.5.








11.   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:



t1 = linspace(0, (4*pi)/100, 10);
t2 = linspace(0, (4*pi)/100, 1000);
y1 = 10*sin(100*t1);
y2 = 10*sin(100*t2);
plot(t1, y1, 'ro-', t2, y2)
xlabel('Time (s)')
ylabel('y function')
legend('coarse', 'fine')





Image 2: MATLAB result




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





What changes in this plot is the fine line. It flattens out at y(x) = 5. 






13.   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.

 clear all;
close all;
t1 = linspace(0, (4*pi)/100, 10);
t2 = linspace(0, (4*pi)/100, 1000);
y1 = 10*sin(100*t1);
y2 = 10*sin(100*t2);
x = find (y2>5);
y2(x) = 5;
plot(t1, y1, 'ro-', t2, y2)
xlabel('Time (s)')
ylabel('y function')
legend('coarse', 'fine')



Image 3: MATLAB result








PART B: Filters and MATLAB
14.       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 1: Frequency data points and the output voltage of a low pass filter
Data Points
Output (low pass)
100 Hz
6.09 V
200 Hz
6.02 V
300 Hz
5.91 V
500 Hz
5.60 V
700 Hz
5.21 V
750 Hz
5.12 V
800 Hz
5.03 V
850 Hz
4.92 V
900 Hz
4.81 V
950 Hz
4.71 V
1 kHz
4.61 V
1.1 kHz
4.4 V
1.2 kHz
4.23 V
1.3 kHz
4.05 V
1.4 kHz
3.88 V
1.5 kHz
3.71 V
1.6 kHz
3.43 V
1.7 kHz
3.28 V
1.8 kHz
3.13 V
1.9 kHz
2.99 V
2.0 kHz
2.87 V
2.1 kHz
2.74 V
2.2 kHz
2.63 V
2.3 kHz
2.4 V

According to our table, the higher the frequency, the lower the voltage output.





15.       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.

%Low Pass Filter
clear all;
close all;

x = [100, 200, 300, 500, 700, 750, 800, 850, 900, 950, 1000, 1100, 1200,1300,1400,1500,1600,1700,1800,1900,2000,2100,2200,2300];
y= [6.09, 6.02, 5.91, 5.60, 5.21, 5.12, 5.03, 4.92, 4.81, 4.71, 4.61, 4.4, 4.23, 4.05, 3.88, 3.71, 3.43, 3.28, 3.13, 2.99, 2.87, 2.74, 2.63, 2.4];

z = max(y)*0.707;
%display(z);

a = find(y<z);
%display(a);

b= min(a);
%display(b);
%display(y(b));

q = ((y(b)+y(b+1))/2);
%display(q);

F_cut =((x(b)+x(b+1))/2);
%display(F_cut);

plot(x,y);
hold on;

plot([0 2300], [q q], 'b:', 'LineWidth', 1);
hold on;

xlabel('Frequency(Hz)');
ylabel('Output Voltage');

legend('Response Curve', 'Cutoff Frequency', 'location', 'Northeast');



Image 4: MATLAB result of Low Pass Filter 







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

*Please refer to the code above in #15, because we went ahead on accident.

According to our plot and code, the cutoff frequency is 1250 Hz



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

Table 2: Frequency data points and output voltage of high pass filter
Data Points
Output (high pass)
100 Hz
0.526 V
200 Hz
1.03 V
300 Hz
1.52 V
500 Hz
2.39 V
700 Hz
3.1 V
750 Hz
3.25 V
800 Hz
3.4 V
850 Hz
3.56 V
900 Hz
3.65 V
950 Hz
3.77 V
1 kHz
3.87 V
1.1 kHz
4.14 V
1.2 kHz
4.32 V
1.3 kHz
4.48 V
1.4 kHz
4.62 V
1.5 kHz
4.74 V
1.6 kHz
4.85 V
1.7 kHz
4.95 V
1.8 kHz
5.03 V
1.9 kHz
5.11 V
2.0 kHz
5.18 V
2.1 kHz
5.23 V
2.2 kHz
5.29 V
2.3 kHz
5.33 V

According to our table, the higher the frequency, the higher the voltage output.


18.       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.

%High Pass Filter
clear all;
close all;

x = [100, 200, 300, 500, 700, 750, 800, 850, 900, 950, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300];  
y = [0.526, 1.03, 1.52, 2.39, 3.1, 3.25, 3.4, 3.56, 3.65, 3.77, 3.87, 4.14, 4.32, 4.48, 4.62, 4.74, 4.85, 4.95, 5.03, 5.11, 5.18, 5.23, 5.29, 5.33];

z = max(y)*0.707;
%display(z);

a = find(y<z);
%display(a);

b= max(a);
%display(b);

q = ((y(b)+y(b+1))/2);
%display(q);

F_cut =((x(b)+x(b+1))/2);
%display(F_cut);

plot(x,y);
hold on;

plot([0 2300], [q q], 'b:', 'LineWidth', 1);
hold on;



Image 5: MATLAB result of High Pass Filter


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

*Again, our code is found above, in #18.
The cut off frequency is at 925 Hz



9 comments:

  1. Great jobs guys!! Your blog looks great, especially how you wrote your code to find the cut off frequency.

    ReplyDelete
  2. Looks great! But wouldn't it make more sense for the dotted cut-off frequency to be a vertical line instead of a horizontal? I feel like you could read the actual cut-off frequency better that way.

    -Ben S.

    ReplyDelete
    Replies
    1. Maybe having both a vertical line and a horizontal line would've been better if you wanted to read the actual cut-off frequency. We just decided on horizontal, but vertical definitely would've been a better choice.

      Delete
  3. Everything is nice n easy to understand. Why do you think the cutoff was different for the high and low pass?

    ReplyDelete
    Replies
    1. It was probably different because of the output readings for the low pass. We didn't really like what we were getting for that one, but we noticed other groups were getting funky readings as well. We were hoping to get better readings for our plot, but we basically got the same thing after the second time.

      Delete
  4. I enjoy how you space out the code, it makes it easy to decipher.

    ReplyDelete
  5. I like that you used a specific interval of points for your data. It made it much easier to understand. My group sometimes chooses random data points, but this convinced me to be more organized.

    ReplyDelete