# capacitor-coupled full-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(2 * Mout - 1);

d23 = ((- pi + alpha < wt) & (wt <= - pi / 2)) | (pi + alpha < wt);
X23 = (- pi /2 < wt) & (wt <= alpha);
d14 = (alpha < wt) & (wt <= pi / 2);
X14 = ((pi / 2 < wt) & (wt <= pi + alpha)) | (wt <= - pi + alpha);
jd23 = - jin0 .* d23;
jd14 = jin0 .* d14;
jc = - jd23 + jd14;
mc = (min + mout) .* d23 + (mout - 1) .* X23 + \
     (min - mout) .* d14 + (1 - mout) .* X14;

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

figure(1)

subplot(2, 1, 1)
plot(deg, jc)
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')

fwdata = [deg', wt', min', mout', jd14', jd23', jc', mc'];
save fwdata fwdata
