close all
clc
format short
1)
clearvars
%%%%%%%%%%%%%% Data Loading and Some Array Operations %%%%%%%%%%%%%%
myData = load('1b.mat');
AArr = myData.Trace_3_GetMeasurement_Pk_Pk_1__V;
BArr = myData.Trace_3_GetMeasurement_Pk_Pk_2__V;
omega = myData.Trace_2_SweepChannel1_Frequency_Hz*2*pi;
absHw = BArr./AArr;
angleHw = myData.Trace_3_GetMeasurement_Phase_2_1_;
%%%%%%%%%%%%%%%%%%%%%%% Obtaining Asked Values %%%%%%%%%%%%%%%%%%%%%%%
[wCResponse cutoffIdx] = findClosest(absHw, max(absHw)/sqrt(2)); %find closest point
wC = omega(cutoffIdx)
wC = 3.1416e+03
immersiveFinder(omega, absHw, wC/5)
ans = 0.1900
immersiveFinder(omega, absHw, wC)
ans = 0.6965
immersiveFinder(omega, absHw, wC*5)
ans = 0.9752
immersiveFinder(omega, angleHw, wC/5)
ans = 76
immersiveFinder(omega, angleHw, wC)
ans = 45
immersiveFinder(omega, angleHw, wC*5)
ans = 11.2000
%%%%%%%%%%%%%%%%%%%%%%%%%% Plotting Graphs %%%%%%%%%%%%%%%%%%%%%%%%%%
figure()
subplot(2,1,1);
plot(omega, absHw)
hold on
plot(wC, wCResponse,'r.','MarkerSize',20)
putPre1b("magnitude")
legend('Experimental Response Curve', 'Experimental \omega_c', 'Theoratical Response Curve', 'Theoratical \omega_c')
hold off
subplot(2,1,2);
plot(omega, angleHw)
hold on
plot(wC, angleHw(cutoffIdx),'r.','MarkerSize',20)
putPre1b("angle")
legend('Experimental Response Curve', 'Experimental \omega_c', 'Theoratical Response Curve', 'Theoratical \omega_c')
hold off
saveas(gcf,'1b.emf') % Save the graph to the local drive
2-a)
clearvars
%%%%%%%%%%%%%% Data Loading and Some Array Operations %%%%%%%%%%%%%%
myData = load('2a.mat');
AArr = myData.Trace_3_GetMeasurement_Pk_Pk_1__V;
BArr = myData.Trace_3_GetMeasurement_Pk_Pk_2__V;
omega = myData.Trace_2_SweepChannel1_Frequency_Hz*2*pi;
absHw = BArr./AArr;
angleHw = myData.Trace_3_GetMeasurement_Phase_2_1_;
%%%%%%%%%%%%%%%%%%%%%%% Obtaining Asked Values %%%%%%%%%%%%%%%%%%%%%%%
[wCResponse cutoffIdx] = findClosest(absHw, max(absHw)/sqrt(2)); %find closest point
wC = omega(cutoffIdx)
wC = 2.7332e+04
immersiveFinder(omega, absHw, wC/5)
ans = 0.9746
immersiveFinder(omega, absHw, wC)
ans = 0.7015
immersiveFinder(omega, absHw, wC*5)
ans = 0.2020
immersiveFinder(omega, angleHw, wC/5)
ans = -12.2000
immersiveFinder(omega, angleHw, wC)
ans = -45
immersiveFinder(omega, angleHw, wC*5)
ans = -79
%%%%%%%%%%%%%%%%%%%%%%%%%% Plotting Graphs %%%%%%%%%%%%%%%%%%%%%%%%%%
figure()
subplot(2,1,1);
plot(omega, absHw)
hold on
plot(wC, wCResponse,'r.','MarkerSize',20)
putPre2a("magnitude")
legend('Experimental Response Curve', 'Experimental \omega_c', 'Theoratical Response Curve', 'Theoratical \omega_c')
hold off
subplot(2,1,2);
plot(omega, angleHw)
hold on
plot(wC, angleHw(cutoffIdx),'r.','MarkerSize',20)
putPre2a("angle")
legend('Experimental Response Curve', 'Experimental \omega_c', 'Theoratical Response Curve', 'Theoratical \omega_c')
hold off
saveas(gcf,'2a.emf') % Save the graph to the local drive
2-b-i)
clearvars
%%%%%%%%%%%%%% Data Loading and Some Array Operations %%%%%%%%%%%%%%
myData = load('2bi.mat');
AArr = myData.Trace_3_GetMeasurement_Pk_Pk_1__V;
BArr = myData.Trace_3_GetMeasurement_Pk_Pk_2__V;
omega = myData.Trace_2_SweepChannel1_Frequency_Hz*2*pi;
absHw = BArr./AArr;
angleHw = myData.Trace_3_GetMeasurement_Phase_2_1_;
%%%%%%%%%%%%%%%%%%%%%%% Obtaining Asked Values %%%%%%%%%%%%%%%%%%%%%%%
[wC1 w0 wC2 wC1Idx w0Index wC2Idx wC1y w0Response wC2y qF]...
= handleBandpass(omega, absHw)
wC1 = 2.6389e+03
w0 = 8.4446e+03
wC2 = 3.1139e+04
wC1Idx = 5
w0Index = 16
wC2Idx = 59
wC1y = 0.6244
w0Response = 0.8788
wC2y = 0.6219
qF = 0.2963
wc1Phase = immersiveFinder(omega, angleHw, wC1)
wc1Phase = 44
w0Phase = immersiveFinder(omega, angleHw, w0)
w0Phase = 0.9000
wc2Phase = immersiveFinder(omega, angleHw, wC2)
wc2Phase = -44
%%%%%%%%%%%%%%%%%%%%%%%%%% Plotting Graphs %%%%%%%%%%%%%%%%%%%%%%%%%%
figure()
%subplot(2,1,1); % when it is activated, legend blocks the graph. Even if I
% tried to overcome this with my handcrafted function wideSP(), it caused
% more problems: the "hold on" seems ineffective with this approach. So I
% disabled subplotting.
plot(omega, absHw)
hold on
plot(w0, w0Response,'r.','MarkerSize',20)
plot(wC1, wC1y,'rx','MarkerSize',10)
plot(wC2, wC2y,'r*','MarkerSize',10)
putPre2bi("magnitude")
legend('Experimental Response Curve', 'Experimental \omega_0', 'Experimental \omega_{c1}', 'Experimental \omega_{c2}',...
'Theoratical Response Curve', 'Theoratical \omega_0', 'Theoratical \omega_{c1}', 'Theoratical \omega_{c2}')
hold off
saveas(gcf,'2b-i-magnitude.emf') % Save the graph to the local drive % Save the graph to the local drive
%subplot(2,1,2);
plot(omega, angleHw)
hold on
plot(w0, angleHw(w0Index),'r.','MarkerSize',20)
plot(wC1, angleHw(wC1Idx),'rx','MarkerSize',10)
plot(wC2, angleHw(wC2Idx),'r*','MarkerSize',10)
putPre2bi("angle")
legend('Experimental Response Curve', 'Experimental \omega_0', 'Experimental \omega_{c1}', 'Experimental \omega_{c2}',...
'Theoratical Response Curve', 'Theoratical \omega_0', 'Theoratical \omega_{c1}', 'Theoratical \omega_{c2}')
hold off
saveas(gcf,'2b-i-phase.emf') % Save the graph to the local drive % Save the graph to the local drive
2-b-ii)
clearvars
figure()
myData = load('2bii.mat');
input = myData.Trace_1;
output = myData.Trace_2;
plot(output)
title('v_{out}(t) versus Sampling Instance Plot')
xlabel('Sampling Instance')
ylabel('v_{out}(t) (V)')
grid on
grid minor
saveas(gcf,'2b-ii.emf') % Save the graph to the local drive % Save the graph to the local drive
3a)
clearvars
%%%%%%%%%%%%%% Data Loading and Some Array Operations %%%%%%%%%%%%%%
myData = load('3a.mat');
AArr = myData.Trace_3_GetMeasurement_Pk_Pk_1__V;
BArr = myData.Trace_3_GetMeasurement_Pk_Pk_2__V;
omega = myData.Trace_2_SweepChannel1_Frequency_Hz*2*pi;
absHw = BArr./AArr;
angleHw = myData.Trace_3_GetMeasurement_Phase_2_1_;
%%%%%%%%%%%%%%%%%%%%%%% Obtaining Asked Values %%%%%%%%%%%%%%%%%%%%%%%
[wC1 w0 wC2 wC1Idx w0Index wC2Idx wC1y w0Response wC2y qF]...
= handleBandpass(omega, absHw)
wC1 = 8.6708e+03
w0 = 9.2991e+03
wC2 = 1.0556e+04
wC1Idx = 23
w0Index = 25
wC2Idx = 29
wC1y = 0.3950
w0Response = 0.5920
wC2y = 0.4179
qF = 4.9333
wc1Phase = immersiveFinder(omega, angleHw, wC1)
wc1Phase = 48
w0Phase = immersiveFinder(omega, angleHw, w0)
w0Phase = 2.8000
wc2Phase = immersiveFinder(omega, angleHw, wC2)
wc2Phase = -52
%%%%%%%%%%%%%%%%%%%%%%%%%% Plotting Graphs %%%%%%%%%%%%%%%%%%%%%%%%%%
figure()
plot(omega, absHw)
hold on
plot(w0, w0Response,'r.','MarkerSize',20)
plot(wC1, wC1y,'rx','MarkerSize',10)
plot(wC2, wC2y,'r*','MarkerSize',10)
putPre3a("magnitude")
legend('Experimental Response Curve', 'Experimental \omega_0', 'Experimental \omega_{c1}', 'Experimental \omega_{c2}',...
'Theoratical Response Curve', 'Theoratical \omega_0', 'Theoratical \omega_{c1}', 'Theoratical \omega_{c2}')
hold off
saveas(gcf,'3a-magnitude.emf') % Save the graph to the local drive % Save the graph to the local drive
plot(omega, angleHw)
hold on
plot(w0, angleHw(w0Index),'r.','MarkerSize',20)
plot(wC1, angleHw(wC1Idx),'rx','MarkerSize',10)
plot(wC2, angleHw(wC2Idx),'r*','MarkerSize',10)
putPre3a("angle")
legend('Experimental Response Curve', 'Experimental \omega_0', 'Experimental \omega_{c1}', 'Experimental \omega_{c2}',...
'Theoratical Response Curve', 'Theoratical \omega_0', 'Theoratical \omega_{c1}', 'Theoratical \omega_{c2}')
hold off
saveas(gcf,'3a-phase.emf') % Save the graph to the local drive % Save the graph to the local drive
3b)
clearvars
figure()
myData = load('3b.mat');
input = myData.Trace_1;
output = myData.Trace_2;
plot(output)
% hold on
% plot(input)
title('v_{out}(t) versus Sampling Instance Plot')
xlabel('Sampling Instance')
ylabel('v_{out}(t) (V)')
grid on
grid minor
saveas(gcf,'3b.emf') % Save the graph to the local drive % Save the graph to the local drive
4)
clearvars
figure()
myData = load('4.mat');
v1 = myData.Trace_1;
v2 = myData.Trace_2;
plot(v1)
hold on
plot(v2)
title('v_{out1}(t) and v_{out2}(t) versus Sampling Instance Plot')
xlabel('Sampling Instance')
ylabel('v_{out1}(t) and v_{out2}(t) in Volts')
legend('v_{out1}(t)','v_{out2}(t)')
grid on
grid minor
saveas(gcf,'4.emf') % Save the graph to the local drive % Save the graph to the local drive
function [wC1 w0 wC2 wC1Idx w0Idx wC2Idx wC1y w0y wC2y qF] = handleBandpass(xArray, yArray)
% handle values about resonant frequency
[w0y w0Idx] = max(yArray);
w0 = xArray(w0Idx);
%slice arrays from peak point
xLeft = xArray(1:w0Idx-1);
xRight = xArray(w0Idx+1:end);
yLeft = yArray(1:w0Idx-1);
yRight = yArray(w0Idx+1:end);
% handle values about first cutoff frequency
[wC1y wC1Idx] = findClosest(yLeft, w0y/sqrt(2)); %find closest point
wC1 = xLeft(wC1Idx);
% handle values about second cutoff frequency
[wC2y wC2Idx] = findClosest(yRight, w0y/sqrt(2)); %find closest point
wC2 = xRight(wC2Idx);
% due to silicing, right indices are defective, fix it here:
wC2Idx = wC2Idx + 1 + length(xLeft);
qF = w0/(wC2 - wC1);
end
function [valToReturn index] = findClosest(array, value)
[garbage index] = min(abs(array - value));
valToReturn = array(index);
end
function valToReturn = immersiveFinder(xArray, yArray, xValue)
xValue = double(xValue);
[garbageX xIndex] = findClosest(xArray, xValue);
valToReturn = yArray(xIndex);
end
function plotPre1(mode, omg, omgc, H)
if mode == "magnitude"
subplot(2,1,1);
plot(omg,abs(H(omg)),'k--');
plot(omgc, 1/sqrt(2),'g.','MarkerSize',20);
xlabel('\omega (rad/s)'); ylabel('|H(j\omega)|');
title("Magnitude Response")
grid minor;
end
if mode == "angle"
subplot(2,1,2);
plot(omg, rad2deg(angle(H(omg))),'k--');
plot(omgc, rad2deg(angle(H(omgc))),'g.','MarkerSize',20);
xlabel('\omega (rad/s)');
ylabel('\angle H(j\omega) (\circ)');
title("Phase Response")
grid minor;
end
end
function plotPre2(mode, omg, H)
[wC1 w0 wC2 wC1Idx w0Idx wC2Idx wC1y w0y wC2y qF] = handleBandpass(omg, abs(H(omg)));
if mode == "magnitude"
%subplot(2,1,1);
plot(omg,abs(H(omg)),'k--');
plot(w0, w0y,'g.','MarkerSize',20)
plot(wC1, wC1y,'gx','MarkerSize',10)
plot(wC2, wC2y,'g*','MarkerSize',10)
xlabel('\omega (rad/s)'); ylabel('|H(j\omega)|');
title("Magnitude Response")
grid minor;
end
if mode == "angle"
angleHw = rad2deg(angle(H(omg)));
%subplot(2,1,2);
plot(omg, angleHw,'k--');
plot(w0, angleHw(w0Idx),'g.','MarkerSize',20)
plot(wC1, angleHw(wC1Idx),'gx','MarkerSize',10)
plot(wC2, angleHw(wC2Idx),'g*','MarkerSize',10)
xlabel('\omega (rad/s)');
ylabel('\angle H(j\omega) (\circ)');
title("Phase Response")
grid minor;
end
end
function putPre1b(mode)
R = 33e3;
C = 1e-8;
omgc = 1/(R*C);
omg = linspace(0,5*omgc,10000);
H = @(omg)R./(R + 1./(j*omg*C));
plotPre1(mode, omg, omgc, H);
end
function putPre2a(mode)
R1Value = 2.7e3;
L1Value = 0.1;
omgc = R1Value/L1Value;
omg = linspace(0,5*omgc,10000);
H = @(omg) R1Value./(R1Value + j*omg*L1Value);
plotPre1(mode, omg, omgc, H);
end
function putPre2bi(mode)
omg = 0:1:16e4;
R1 = 2.7e3;
R2 = 33e3;
C1 = 1e-8;
L1 = 0.1;
s = j*omg;
Z1 = 1./( R1^-1 + ( R2 + 1./(s*C1) ).^-1 );
H = @(omg) ( Z1./(s*L1 + Z1) ).*( R2./( R2 + 1./(s*C1) ) );
plotPre2(mode, omg, H);
end
function putPre3a(mode)
omg0 = 10e3;
LValue = 0.1;
RValue = 10e3;
CValue = 1e-7;
omg = linspace(0,5*omg0,10000);
H = @(omg) (j*omg*LValue)./(-omg.^2*LValue*CValue*RValue + j*omg*LValue + RValue);
plotPre2(mode, omg, H);
end