# capacitor-coupled half-wave rectifier

clear all
close all

deg = linspace(-180, 180, 360 * 2 + 1);
wt = deg / 180 * pi;
n = length(deg);

Mout = input('Vout/Vm = ');

min = sin(wt);
mout = Mout * ones(1, n);
jin0 = cos(wt);

alpha = asin(Mout - 1);

d1 = ((- pi + alpha < wt) & (wt <= - pi / 2)) | (pi + alpha < wt);
X1 = (- pi /2 < wt) & (wt <= alpha);
d2 = (alpha < wt) & (wt <= pi / 2);
X2 = ((pi / 2 < wt) & (wt <= pi + alpha)) | (wt <= - pi + alpha);

jd1 = - jin0 .* d1;
jd2 = jin0 .* d2;
jin = - jd1 + jd2;
mc = min .* d1 + (-1) .* X1 + (min - mout) .* d2 + (1 - mout) .* X2;

Jout = mean(abs(jd2));
printf('Iout = %6.4f * w * C * Vm\n', Jout);

figure(1)

subplot(2, 1, 1)
plot(deg, jin)
set(gca, 'XTick', -180:30:180)
axis([-180, 180, -1.2, 1.2])
xlabel('wt [deg]')
ylabel('i_{IN}/(wCV_m)')

subplot(2, 1, 2)
plot(deg, mc)
set(gca, 'XTick', -180:30:180)
axis([-180, 180, -1.2, 1.2])
xlabel('wt [deg]')
ylabel('v_C/V_m')

hwdata = [deg', wt', min', mout', jd1', jd2', jin', mc'];
save hwdata hwdata
