% Block commutation with varying phase offset.

% Real rotor phase.
p_rotor = 0:.1:360;

% Phase offset, determined by sensor position.  Ideally 90.
for p_delta = 0:1:359
	% Sensed phase.
	p_sense = p_rotor + p_delta;

	% Driven stator phase.
	% Rounded to a multiple of 60 for block commutation.
	p_stator = 60 * round(p_sense / 60);

	% Torque, determined by difference in stator and rotor angles.
	torque = sin((p_stator - p_rotor) * pi / 180);

	one = ones(1, length(p_rotor));
	min_torque = min(torque);
	max_torque = max(torque);

	plot(p_rotor, one * mean(torque), p_rotor, one * min_torque, p_rotor, one * max_torque, p_rotor, torque, 'k');
	axis([0 360 -1 1]);
	xlabel('Rotor position (degrees)');
	ylabel('Torque');
	title(sprintf('Stator-rotor phase difference = %g\nMean torque = %g\nTorque ripple = %g', p_delta, mean(torque), max_torque - min_torque));
	print(sprintf('%03d.png', p_delta), '-S800,600');
end
