Advertisement
hiddenGem

Visualizing Phase Shift

Jun 29th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.98 KB | None | 0 0
  1. %% Visualizing Phase Shift
  2. % This code creates a Phase shift sin(theta) with animation and movie
  3. %% a) Animation of phase shift
  4.  
  5. axis( [-pi, pi, -1, 1] );                             % axis constraints
  6. theta = linspace( -pi, pi, 100);                      % value of theta
  7. delta_theta = 0 : 0.1 : pi;                           % phase shift
  8. y = sin(theta);                              
  9. h = plot( theta, y );                                 % plot sin(theta)
  10. xticks ( [-pi -2*pi/3 -pi/3 0 pi/3 2*pi/3 pi] )       % x-axis tick marks
  11. xticklabels({'-\pi', '-2/3\pi', '-1/3\pi' '0\pi', ...
  12.     '1/3\pi' , '2/3\pi', '\pi'})                      % x-axis tick labels
  13. title('phase shift by 0')                      
  14. a = 1;                              
  15.  
  16. for b = delta_theta(1:end)
  17.     a = a + 1;
  18.     y = sin(theta - b);
  19.     set(h, 'XData', ...
  20.         theta, 'YData', y)
  21.     drawnow
  22.     str = sprintf('Phase Shift by %s',...
  23.           num2str(b));
  24.     title(str)
  25.     pause(0.05)
  26. end  % for loop
  27.  
  28. %% b) Movie of phase shift
  29.  
  30. axis( [-pi, pi, -1, 1] );                            % axis constraints
  31. theta = linspace( -pi, pi, 100);                      % value of theta
  32. delta_theta = 0 : 0.1 : pi;                          % phase shift
  33. y = sin(theta);                                        % g1(x)
  34. h = plot( theta, y );                                 % plot sin(theta)
  35. xticks ( [-pi -2*pi/3 -pi/3 0 pi/3 2*pi/3 pi] )        % x-axis tick marks
  36. xticklabels({'-\pi', '-2/3\pi', '-1/3\pi' '0\pi', ...
  37.     '1/3\pi' , '2/3\pi', '\pi'})                        % x-axis tick labels
  38. title('phase shift by 0')                        
  39. a = 1;                                
  40.  
  41. Phase_Shift(1) = getframe;
  42. for b = delta_theta(1:end)
  43.     a = a + 1;
  44.     y = sin(theta - b);
  45.     set(h, 'XData', ...
  46.         theta, 'YData', y)
  47.     str = sprintf('Phase shift by %s', ...
  48.           num2str(b));
  49.     title(str)
  50.     pause(0.1)
  51.     Phase_Shift(a) = getframe;
  52. end %for loop
  53. pause;
  54. movie(Phase_Shift)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement