Home > wafo > spec > spec2spec.m

# spec2spec

## PURPOSE

Transforms between different types of spectra

## SYNOPSIS

[Snew]=spec2spec(S,newtype,phi,v)

## DESCRIPTION

``` SPEC2SPEC Transforms between different types of spectra

CALL:  Snew = spec2spec(S,newtype,phi,v)

Snew = spectrum with new type
S = spectrum
newtype = new type given as a string, the allowed types:
'dir','freq','k2d','k1d','encdir','enc',
(default S.type, i.e. no change)

In special cases, specify also:
phi = rotation angle of the cordinate system (x,y) counter-clockwise
(rotation of spectrum clockwise), e.g. direction of ship when
encountered spectrum is to be computed; the wavenumber
spectrum along a line going through origin and with
angle phi (default 0).
v = speed of ship, when transforming to an encountered spectrum
(default 0)

SPEC2SPEC performs a change of variables from the type given in input
spectrum S, to the given 'newtype', when possible.
Remark1: the field .note is not changed, but may need an update by hand.
Remark2: the encountered spectrum is derived by means of numerical
variable change, hence dense spectrum is needed to get good accuracy of
the transformation. For negative velocities the enc. spectrum has a
singularity hence spec2mom may give uncorrect values for the moments.

Example:
S    = demospec('dir')
Snew = spec2spec(S,'enc',pi/6,10)

## CROSS-REFERENCE INFORMATION

This function calls:
 dir2enc Transform a dir. spectrum to an encountered. (Used in spec2spec) rotspec Rotate spectrum anti-clockwise around the origin. simpson Numerical integration with the Simpson method spa2time Transform of spectrum from wave no. to frequency (used in spec2spec) spec2spec Transforms between different types of spectra time2spa Transform of spectrum from frequency to wave no. (used in spec2spec) datestr String representation of date. error Display message and abort function. lower Convert string to lowercase. now Current date and time as date number. rmfield Remove fields from a structure array. strcmp Compare strings. strcmpi Compare strings ignoring case.
This function is called by:
 Chapter2 % CHAPTER2 Modelling random loads and stochastic waves dir2enc Transform a dir. spectrum to an encountered. (Used in spec2spec) dspec2char Evaluates directional spectral characteristics dspec2dcov Return covariance function given a directional spectrum scalespec Scale spectral density so that the moments equals m0,m2. seasim Spectral simulation of a Gaussian sea, 2D (x,t) or 3D (x,y,t) spec2AcAt Evaluates survival function R(h1,h2)=P(Ac>h1,At>h2). spec2Acdf Evaluates cdf of crests P(Ac<=h) or troughs P(At<=h). spec2bw Evaluates some spectral bandwidth and irregularity factors spec2char Evaluates spectral characteristics and their covariance spec2cov Computes covariance function and its derivatives spec2mmtpdf Calculates joint density of Maximum, minimum and period. spec2mom Calculates spectral moments from spectrum spec2skew Estimates the moments of 2'nd order non-linear waves spec2spec Transforms between different types of spectra spec2tccpdf Evaluates densities of wave period Tcc, wave lenght Lcc. spec2thpdf Joint density of amplitude and period/wave-length characteristics spec2tpdf Evaluates densities for crest-,trough-period, length. spec2tpdf2 Evaluates densities for various wave periods or wave lengths wafofig7 Joint distribution (pdf) of crest wavelength, Lc, and crest amplitude, Ac wafofig8 Joint distribution (pdf) of crest wavelength, Lc, and crest amplitude, Ac for extremal waves wnormspec Normalize a spectral density such that m0=m2=1 wspecplot Plot a spectral density

## SOURCE CODE

```001 function [Snew]=spec2specspec2spec(S,newtype,phi,v)
002 %SPEC2SPEC Transforms between different types of spectra
003 %
004 % CALL:  Snew = spec2spec(S,newtype,phi,v)
005 %
006 %     Snew = spectrum with new type
007 %        S = spectrum
008 %  newtype = new type given as a string, the allowed types:
009 %            'dir','freq','k2d','k1d','encdir','enc',
010 %            (default S.type, i.e. no change)
011 %
012 %  In special cases, specify also:
013 %      phi = rotation angle of the cordinate system (x,y) counter-clockwise
014 %            (rotation of spectrum clockwise), e.g. direction of ship when
015 %            encountered spectrum is to be computed; the wavenumber
016 %            spectrum along a line going through origin and with
017 %            angle phi (default 0).
018 %        v = speed of ship, when transforming to an encountered spectrum
019 %            (default 0)
020 %
021 % SPEC2SPEC performs a change of variables from the type given in input
022 % spectrum S, to the given 'newtype', when possible.
023 % Remark1: the field .note is not changed, but may need an update by hand.
024 % Remark2: the encountered spectrum is derived by means of numerical
025 % variable change, hence dense spectrum is needed to get good accuracy of
026 % the transformation. For negative velocities the enc. spectrum has a
027 % singularity hence spec2mom may give uncorrect values for the moments.
028 %
029 % Example:
030 %    S    = demospec('dir')
031 %    Snew = spec2spec(S,'enc',pi/6,10)
032 %
034
035 % Tested on: Matlab 5.3
036 % History:
037 % revised by pab 21.09.2004:
038 %  -Moved code to rotspec + small cosmetics fixes
039 % revised by IR 27.06.2004: implemented freq -> enc.
040 % revised by IR 03.04.2001: Major changes.
041 % revised by jr 31.03.2001
042 % - case 'freq' -> 'enc': Changed text in error message
043 % revised by es 05.06.00: encdir -> freq results in enc
044 % revised by es 25.05.00: return if nargin<2 (default for newtype)
045 % revised by es 24.05.00: removed Message: New type identical to old type
046 % revised by es 09.02.2000: more corrections of change to 'k1d'
047 % revised by es 28.01.2000: correction and improvement of change to 'k1d'
048 % by es 13.08.99
049
050 error(nargchk(1,4,nargin))
051 if nargin<0|isempty(S)
052   error('Needs an input spectrum')
053 end
054
055 Snew = S;
056 if (nargin<2|isempty(newtype))
057   return
058 end
059 if (nargin<3 | isempty(phi))
060   phi=0.;
061 end
062 if (nargin<4|isempty(v))
063   v=0;
064 end
065
066 % New call: pab 21Sep2004
067 Snew = rotspec(Snew,phi);
068 % Old call: pab 21Sep2004
069 %if ~isfield(Snew,'phi') | isempty(Snew.phi),
070 %  Snew.phi=0;
071 %end
072 %
073 %Snew.phi=mod(Snew.phi+phi+pi,2*pi)-pi;  %such that -pi<phi<pi
074
075
076 newtype=lower(newtype);
077 if strcmpi(S.type,newtype)
078 %   disp(' Message: New type identical to old type')
079   return
080 end
081
082 if ~any(strcmpi(newtype,{'dir','freq','k2d','k1d','encdir','enc'}))
083   % Check if newtype is a proper type
084   error('Not known new type, check spelling')
085 end
086
087 indim=sum(size(S.S)>1); %dimension of old spectrum
088 if indim==1 %then new spectrum can not have dimension larger
089   if strcmp(newtype(end-2:end),'dir')|strcmp(newtype(end-2:end),'k2d')
090     error('Impossible to transform from one dimension to two')
091   end
092 end
093
094
095
096 switch lower(Snew.type)
097   case 'dir'
098    switch newtype
099     case 'freq' % from 'dir'
100      Snew.S=simpson(Snew.theta(:),Snew.S);
101      %integrate out angle but if the spectrum is
102      %true directional spectrum then
103      %one should just take SS.S(1,:)
104         Snew=rmfield(Snew,'theta');
105         Snew.type='freq';
106       case 'k2d' % from 'dir'
107         Snew=time2spa(Snew);
108       case 'k1d' % from 'dir'
109         Snew=spec2spec(Snew,'k2d');
110         Snew=spec2spec(Snew,'k1d');
111       case 'encdir' % from 'dir'
112         Snew=dir2enc(Snew,2,v);
113       case 'enc' % from 'dir'
114         Snew=dir2enc(Snew,1,v);
115     end
116    case 'freq'
117     switch newtype
118       case 'k1d' % from 'freq'
119         Snew=time2spa(Snew);
120       case 'enc' % from freq
121           Snew=dir2enc(Snew,1,v);
122       otherwise
123         error('Specified transformation not possible')
124     end
125   case 'k2d'
126    switch newtype
127     case 'dir' % from 'k2d'
128      Snew=spa2time(Snew);
129     case 'freq' % from 'k2d'
130      Snew=spec2spec(Snew,'dir'); %transform via dir (k1d better???)
131      Snew=spec2spec(Snew,'freq');
132
133     case 'k1d'
134      % from 'k2d'
135      % For spectrum in Cartesian representation:
136      %    The grid is rotated, the size of it is preserved
137      %    (maybe it must be increased such that no nonzero points are
138      %    affected, but this is not inplemented yet: i.e. corners are cut off)
139      % The spectrum is assumed to be zero outside original grid.
140
141      % PAB 2004: old calls kept just in case:
142      %[k,k2] = meshgrid(S.k,S.k2);
143      %[th,r] = cart2pol(k,k2);
144      %phi=Snew.phi;
145      %[k,k2] = pol2cart(th+phi,r);
146      %Sn     = interp2(S.k,S.k2,S.S,k,k2);
147      %Sn(isnan(Sn))=0.;
148      %Snew.S = Sn;
149
150      physicallyRotateGrid = 1;
151      Snew = rotspec(Snew,0,physicallyRotateGrid);
152
153      Snew.S = simpson(Snew.k2(:),Snew.S).'; %integrate out second variable
154      Snew   = rmfield(Snew,'k2');
155      Snew.type = 'k1d';
156      if (Snew.k(1)<0)
157        Snew.S = Snew.S(Snew.k>=0)+flipud(Snew.S(Snew.k<=0));
158        Snew.S = Snew.S';
159        Snew.k = Snew.k(Snew.k>=0);
160      end
161      Snew.type='k1d';
162     case 'encdir' % from 'k2d'
163      Snew=spec2spec(Snew,'dir');
164      Snew=spec2spec(Snew,'encdir',v);
165     case 'enc' % from 'k2d'
166      Snew=spec2spec(Snew,'dir');
167      Snew=spec2spec(Snew,'enc',v);
168     otherwise
169      error('Specified transformation not possible')
170    end
171  case 'k1d'
172   switch newtype
173    case 'freq' % from 'k1d'
174     Snew=spa2time(Snew);
175    case 'enc' % from 'k1d'
176     Snew=spa2time(Snew);
177     Snew=spec2spec(Snew,'enc',v);
178    otherwise
179     error('Specified transformation not possible')
180   end
181
182  case 'encdir'
183   switch newtype
184    case 'enc' % from 'encdir'
185     Snew.S=simpson(Snew.theta(:),Snew.S); % integrate out direction
186     Snew.type='enc';
187     Snew=rmfield(Snew,'theta');
188    otherwise
189     error('Specified transformation not possible or not yet available')
190   end
191  case 'enc'
192   switch newtype
193    case 'freq' % from 'enc'
194     Snew.type='freq';
195    otherwise
196     error('Specified transformation not possible or not yet available')
197   end
198 end
199 Snew.date=datestr(now);
200```

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