Created
June 9, 2011 00:10
-
-
Save tobin/1015769 to your computer and use it in GitHub Desktop.
Phasor demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
% Phasor demo | |
% | |
% Tobin Fricke <[email protected]> 2011-06-08 | |
% modulation depth | |
m = pi/4; | |
% vector of (normalized) frequencies to include | |
freqs = [0 1 -1 2 -2 3 -3 4 -4]; | |
% initial amplitudes of the phasors | |
amp = besselj(freqs, m); | |
set(0, 'defaultlinelinewidth', 5); | |
% Set up the axes | |
cla reset; | |
axis equal; | |
xlim([-1 1.5]); | |
ylim([-1 1]); | |
% Draw the unit circle | |
rectangle('Position', [-1 -1 2 2], 'Curvature', [1 1]) | |
% make some lines | |
L = []; | |
L(1) = line([0], [0]); | |
L(2) = line([0], [0], 'color', 'red'); | |
save_to_disk = 0; | |
% how many frames to render? | |
if save_to_disk, | |
n_frames = 200; | |
else | |
n_frames = inf; | |
end | |
% how much the 1-Omega should rotate in one frame of the animation? | |
if isinf(n_frames) | |
dphi = 0.1; | |
else | |
dphi = (2*pi)/(n_frames + 1); | |
end | |
frameno = 0; | |
while (frameno < n_frames), | |
frameno = frameno + 1; | |
% Add up the phasors cumulatively | |
seg = cumsum([0 amp]); | |
% Draw both the collection of phasors and the resultant | |
set(L(1), 'xdata', real(seg), 'ydata', imag(seg)); | |
set(L(2), 'xdata', real(seg([1,end])), 'ydata', imag(seg([1,end]))); | |
% Update the display | |
drawnow; | |
if (save_to_disk), | |
print('-dpng', '-r72', sprintf('pm%03d.png', frameno)); | |
else | |
pause(0.05); | |
end | |
% Advance the phasors for the next frame | |
amp = amp .* exp(1i * dphi * freqs); | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment