Home > wafo > spec > rotspec.m

# rotspec

## PURPOSE

Rotate spectrum anti-clockwise around the origin.

## SYNOPSIS

Snew=rotspec(S,phi,rotateGrid)

## DESCRIPTION

``` ROTSPEC Rotate spectrum anti-clockwise around the origin.

CALL:  Snew=rotspec(S,phi,rotateGrid)

Snew = new rotated spectrum
S = spectrum
phi = rotation angle (default 0)
rotateGrid = 1 if rotate grid of Snew physically (thus Snew.phi=0).
0 if rotate so that only Snew.phi is changed
(the grid is not physically rotated)  (default)

ROTSPEC Rotates a spectrum anti-clockwise around the origin.
The spectrum can be of any of the two-dimensional types.
For spectrum in polar representation:
newtheta = theta+phi, but circulant such that -pi<newtheta<pi
For spectrum in Cartesian representation:
If the grid is rotated physically, the size of it is preserved
(maybe it must be increased such that no nonzero points are
affected, but this is not implemented yet: i.e. corners are cut off)
The spectrum is assumed to be zero outside original grid.
NB! The routine does not change the type of spectrum, use spec2spec
for this.

Example
S=demospec('dir');
wspecplot(S), hold on
wspecplot(rotspec(S,pi/2),'r'), hold off

## CROSS-REFERENCE INFORMATION

This function calls:
 cart2pol Transform Cartesian to polar coordinates. error Display message and abort function. interp2 2-D interpolation (table lookup). isfield True if field is in structure array. lower Convert string to lowercase. meshgrid X and Y arrays for 3-D plots. pol2cart Transform polar to Cartesian coordinates.
This function is called by:
 Chapter3 % CHAPTER3 Demonstrates distributions of wave characteristics spec2spec Transforms between different types of spectra

## SOURCE CODE

```001 function Snew=rotspec(S,phi,rotateGrid)
002 %ROTSPEC Rotate spectrum anti-clockwise around the origin.
003 %
004 % CALL:  Snew=rotspec(S,phi,rotateGrid)
005 %
006 %       Snew = new rotated spectrum
007 %          S = spectrum
008 %        phi = rotation angle (default 0)
009 % rotateGrid = 1 if rotate grid of Snew physically (thus Snew.phi=0).
010 %              0 if rotate so that only Snew.phi is changed
011 %                (the grid is not physically rotated)  (default)
012 %
013 % ROTSPEC Rotates a spectrum anti-clockwise around the origin.
014 % The spectrum can be of any of the two-dimensional types.
015 % For spectrum in polar representation:
016 %    newtheta = theta+phi, but circulant such that -pi<newtheta<pi
017 % For spectrum in Cartesian representation:
018 %    If the grid is rotated physically, the size of it is preserved
019 %    (maybe it must be increased such that no nonzero points are
020 %    affected, but this is not implemented yet: i.e. corners are cut off)
021 % The spectrum is assumed to be zero outside original grid.
022 % NB! The routine does not change the type of spectrum, use spec2spec
023 %     for this.
024 %
025 % Example
026 %  S=demospec('dir');
027 %  wspecplot(S), hold on
028 %  wspecplot(rotspec(S,pi/2),'r'), hold off
029 %
031
032 % History:
033 % Revised pab mar 2005
034 % Revised pab Jan 2005
035 % fixed an illegal else statement
036 % Revised pab Sept 2004:
037 %  -Removed old unused code
038 %  -Added: Example and rotateGrid to input
039 % revised IR Aug 2004
040 % revised IR, PAB
041 %  - BUG: Made sure  -pi<newtheta<pi
042 % by es 17.07.1999
043
044
045 % TODO % Make physical grid rotation of cartesian coordinates more robust.
046
047 error(nargchk(1,3,nargin))
048 if (nargin<2 | isempty(phi))
049   phi =0;
050 end
051 if (nargin<3 | isempty(rotateGrid))
052   rotateGrid = 0;
053 end
054
055 Snew=S;
056
057 if (~isfield(S,'phi') | isempty(S.phi)),
058   Snew.phi=0;
059 end
060 Snew.phi=mod(Snew.phi+phi+pi,2*pi)-pi;
061
062 switch lower(S.type(end-2:end))
063   case 'dir',
064    % any of the directinal types
065    % Make sure theta is from -pi to pi
066    phi0       = Snew.theta(1)+pi;
067    Snew.theta = Snew.theta-phi0;
068
069    % make sure -pi<phi<pi
070    Snew.phi   = mod(Snew.phi-phi0+pi,2*pi)-pi;
071    if (rotateGrid & (Snew.phi~=0))
072      % Do a physical rotation of spectrum
073      theta = Snew.theta;
074      ntOld = length(theta)
075      nt = ntOld-(theta(1)==theta(end));
076      Snew.theta(1:nt) = mod(Snew.theta(1:nt)+Snew.phi+pi,2*pi)-pi;
077      Snew.phi = 0;
078      [Snew.theta,ind] = sort(Snew.theta(:));
079      Snew.S = Snew.S(ind,:);
080
081      if (Snew.theta(1)==-pi)
082         if (nt<ntOld)
083           Snew.S(ntOld,:) = Snew.S(1,:);
084         else
085            Snew.S(nt+1,:) = Snew.S(1,:);
086         end
087      elseif (nt<ntOld)
088         Snew.S(ntOld,:)   = []
089         Snew.theta(ntOld) = [];
090      end
091    end
092
093  case 'k2d',
094   % any of the 2D wave number types
095    %Snew.phi   = mod(Snew.phi+phi+pi,2*pi)-pi;
096
097
098   if (rotateGrid & (Snew.phi~=0))
099     % Do a physical rotation of spectrum
100
101     [k,k2] = meshgrid(S.k,S.k2);
102     [th,r] = cart2pol(k,k2);
103     [k,k2] = pol2cart(th+Snew.phi,r);
104     Sn = interp2(S.k,S.k2,S.S,k,k2);
105     Sn(isnan(Sn))=0.;
106     Snew.S   = Sn;
107     Snew.phi = 0;
108   end
109 otherwise
110   %disp('Can only rotate two dimensional spectra')
111 end
112
113 return
114
115```

Mathematical Statistics
Centre for Mathematical Sciences
Lund University with Lund Institute of Technology

Comments or corrections to the WAFO group

Generated on Thu 06-Oct-2005 02:21:16 for WAFO by m2html © 2003