Monday, March 20, 2017

Week 10: MatLab

Part A: MATLAB Practice
Number 1:
MatLab Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
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)

Number 2: What does clear all do?
clear all Clears all objects in the work space.

Number 3: What does close all do?
close all Closes all figures currently open.

Number 4: Type x in the command line and press enter. how many rows and columns are there?
There are 5 columns and 1 row in the matrix x.

Number 5: Why is there a semicolon at the end of the line of x and y?
Because its the end of a statement.

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

ERROR MESSAGE: "error using ^ inputs must be a scalar and a square matrix to compute element wise power, use power (.^)  instead."

This means that without the '.' it thinks we are doing a matrix math power but since we are trying to do a element by element power with want the '.' to do that.

Number 7: How does the LineWidth affect the plot?
Line width changes the width of the plot line of the graph making it thicker or thinner

Number 8: Change code to copy figure from blogsheet week 10 PART A number 8 and provide code.


MatLab Code:
1
2
3
4
5
6
7
clear all;
close all;
x = [1 2 3 4 5];
y = 2.^x;
plot(x, y, '-or', 'LineWidth',  2, 'MarkerSize', 10)
xlabel('Numbers', 'FontSize', 12)
ylabel('Results', 'FontSize', 12)
Number 9: What happens when you change x = [1 2 3 4 5]; to x = [1;2;3;4;5];
Nothing happens different when we add in [1;2;3;4;5] instead of [1 2 3 4 5] at least visibly

Number 10: Change code to copy figure from blogsheet week 10 PART A number 10 and provide code.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
clear all;
close all;

x = [1 2 3 4 5];
y = x.^2

plot(x, y, ':sk', 'LineWidth',  4, 'MarkerSize', 15)
grid on
set(gcc, 'GridLineStyle', '--'')

xlabel('Numbers', 'FontSize', 12)
ylabel('Results', 'FontSize', 12)

Number 11: Degree vs. radian in MatLab
a.) Calculate sin(30) using the Internet
sin(30) = .5

b.) Calculate sin(30) using MatLab
sin(30) = -.9880

c.) How do you modify sin(30) so we get the correct number?
Sin of 30 degrees in MatLab is sind(30) = .5

Number 12: Plot y = 10*sin(100*t) in MatLab with 2 different resolutions on the same plot. Provide Code.

MatLab Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
clear all;
close all;

tc = linspace(0,.125, 10);
tf = linspace(0,.125, 1000);

yc = 10*sin(100*tc);
yf = 10*sin(100*tf);

plot(tc, yc, '-or', 'LineWidth', 1, 'MarkerSize', 6)
hold on
plot(tf, yf, '-k', 'LineWidth', 1, 'MarkerSize', 2)

xlim([0 .14])

xlabel('Time (s)', 'FontSize', 12)
ylabel('y function', 'FontSize', 12)
legend('Coarse','Fine')

Number 13: Explain whats change in the following plot from the previous one. (fig. 5)
The plot(fig. 5) has all values above 5 removed for the fine plot.


Number 14: Replicate the plot from the blogsheet week 10 number 13 using MatLab and find.
Figure 5 Number 14 code output
MatLab Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
clear all;
close all;

tc = linspace(0,.125, 10);
tf = linspace(0,.125, 1000);

yc = 10*sin(100*tc);
yf = 10*sin(100*tf);
yff = find(yf<5);

plot(tc, yc, '-or', 'LineWidth', 1, 'MarkerSize', 6)
hold on
plot(tf(yff), yf(yff), '-k', 'LineWidth', 1, 'MarkerSize', 2)

xlim([0 .14])

xlabel('Time (s)', 'FontSize', 12)
ylabel('y function', 'FontSize', 12)
legend('Coarse','Fine')

Part B: Fliters and MATLAB
Low Pass Filter

MatLab Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
clear all;
close all;


x = [50 100 200 300 400 500 600 700 800 850 860 870 880 890 895 900 1000 1200 1400 1600 1800 2000];
y = [5.95 5.9 5.75 5.55 5.31 5.06 4.81 4.57 4.34 4.28 4.24 4.22 4.2 4.18 4.16 4.12 3.92 3.57 3.26 3.00 2.77 2.58 ];
y = y/5.94;

plot(x, y, '-or', 'LineWidth', 1, 'MarkerSize', 6)
hold on
refline(0,0.707);

xlabel('Frequency', 'FontSize', 12)
ylabel('Output/Input', 'FontSize', 12)

High Pass Filter
MatLab Code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
clear all;
close all;


x = [50 100 300 600 800 850 860 870 880 890 895 900 910 920 930 940 950 1000 1200 1400 1600 1800 2000 2200 2400 2600 2800 3000 3500 4000 4500 5000];
y = [.4 .747 2 3.34 3.94 4.05 4.07 4.08 4.1 4.12 4.14 4.14 4.17 4.19 4.22 4.24 4.25 4.36 4.67 4.91 5.09 5.23 5.34 5.42 5.5 5.56 5.6 5.64 5.73 5.8 5.85 5.93];
y = y/5.94;

plot(x, y, '-or', 'LineWidth', 1, 'MarkerSize', 6)
hold on
refline(0,0.707);

xlabel('Frequency', 'FontSize', 12)
ylabel('Output/Input', 'FontSize', 12)

Sunday, March 19, 2017

Week 9: High and Low Pass Filters

Problem 1: Measure the resistance of the speaker. Compare this value with the value you would find online.

Between 8.6 and 8.7 Ohms, the listed resistance online is 8 Ohms.

Problem 2: Build the following circuit using a function generator setting the amplitude to 5V (0V offset). What happens when you change the frequency?

The pitch of the speaker changes.

Fig. 1: Speaker directly connected to function generator.


Problem 3: Add one resistor to the circuit in series with the speaker (first 47 Ω, then 820 Ω). Measure the voltage across the speaker. Briefly explain your observations. 

Resistor value Oscilloscope output  Observation
47 Ω 448 mV peak to peak Period of the wave decreases as the freq. increases
820 Ω 54 mV peak to peak Same as the other resistor


Problem 4: Build the following circuit. Add a resistor in series to the speaker to have an equivalent resistance of 100 Ω. Note that this circuit is a high pass filter. Set the amplitude of the input signal to 8 V. Change the frequency from low to high to observe the speaker sound. You should not hear anything at the beginning and start hearing the sound after a certain frequency. Use 22 nF for the capacitor. 

a.) Explain the operation. 


Fig. 2: High pass filter explanation.

b.) Fill out the following table by adding enough (10-15 data points) frequency measurements. Vout is measured with the DMM, thus it will be rms value. 

Frequency Vout (mV)(rms) Vout (rms) / Vin (rms)
10 Hz 15.4 0.002570952
50 Hz 15.6 0.002604341
100 Hz 15.4 0.002570952
200 Hz 15.3 0.002554257
300 Hz 15.6 0.002604341
500 Hz 15.6 0.002604341
600 Hz 15.8 0.00263773
700 Hz 15.9 0.002654424
800 Hz 16.6 0.002771285
900 Hz 17.5 0.002921536
1 KHz 16.8 0.002804674
1.2 kHz 17.2 0.002871452
1.4 kHz 17.7 0.002954925
1.6 kHz 18.4 0.003071786
1.8 kHz 18.9 0.003155259
2 kHz 20.8 0.003472454
2.5 kHz 22.1 0.003689482
3 kHz 24.3 0.004056761
3.5 kHz 25.6 0.00427379
4 kHz 28.3 0.004724541
5 kHz 34.2 0.005709516
6 kHz 41.2 0.00687813
8 kHz 56.1 0.009365609
10 kHz 71.5 0.011936561
12 kHz 87.9 0.014674457
14 kHz 102 0.017028381
16 kHz 116 0.019365609
18 kHz 130 0.021702838
20 kHz 145 0.024207012
40 kHz 294 0.049081803
80 kHz 551 0.091986644
100 kHz 630 0.105175292
200 kHz 1120 0.186978297
400 kHz 1900 0.317195326
600 kHz 1920 0.320534224
800 kHz 2320 0.387312187
1000 kHz 2660 0.444073456

c.) Draw Vout/Vin with respect to frequency using Excel. 

d.) What is the cut off frequency by looking at the plot in b?

Between 80 and 40 kHz.

e.) Draw Vout/Vin with respect to frequency using MATLAB.


f.) Calculate the cut off frequency theoretically and compare with one that was found in c. 

V(Max)*1/sqrt(2)=.44*.707=0.311mV which is about 400 Hz, which does not coincide with what was found in part c at all.

g.) Explain how the circuit works as a high pass filter.

The circuit works as a high pass filter because low frequencies are blocked by the capacitor in series due to its impedance. As the frequency goes lower, the harder it is for the voltage to pass through the capacitor.

Problem 5: Design the circuit in 4 to act as a low pass filter and show its operation. Where would you put the speaker? Repeat 4a-g using the new designed circuit.

a.) Explain the operation.



b.) Fill out the following table by adding enough (10-15 data points) frequency measurements. Vout is measured with the DMM, thus it will be rms value. 

Frequency Vout (mV)(rms) Vout (rms) / Vin (rms)
5 Hz 330 0.05509182
10 Hz 330 0.05509182
50 Hz 331 0.055258765
100 Hz 332 0.05542571
200 Hz 334 0.055759599
300 Hz 337 0.056260434
500 Hz 348 0.058096828
1 KHz 373 0.062270451
5 kHz 338 0.056427379
10 kHz 343 0.057262104
50 kHz 582 0.097161937
100 kHz 835 0.139398998
200 kHz 1200 0.20033389
300 kHz 1810 0.302170284
400 kHz 1530 0.25542571
500 kHZ 1100 0.183639399
1000 kHz 429 0.071619366
2000 kHz 182 0.030383973
3000 kHz 105 0.017529215
4000 kHz 62 0.010350584

c.) Draw Vout/Vin with respect to frequency using Excel.

d.) What is the cut off frequency by looking at the plot in b? 

300 kHz.

e.) Draw Vout/Vin with respect to frequency using MATLAB.


f.) Calculate the cut off frequency theoretically and compare with one that was found in c. 

V(Max)*1/sqrt(2)=0.3*.707=0.21mV = 300 Hz which is was similar to what was found earlier.

g.) Explain how the circuit works as a low pass filter.

The circuit works as a low pass filter by having the capacitor in parallel with the output, and having said capacitor then grounded. This means that when given a low frequency, the capacitor will offer a higher impedance to the flow of electricity, allowing the voltage to flow unhindered. However, as the frequency gets higher, the capacitor allows for the voltage to flow across it easier, and since electricity follows the path of least resistance, it will flow through the capacitor, and not to the output, thus "filtering" out the high frequencies.

Problem 6: Construct the following circuit and test the speaker with headsets. Connect the amplifier output directly to the headphone jack (without the potentiometer). Load is the headphone jack in the schematic. “Speculate” the operation of the circuit with a video.