Home > wafo > spec > ttspec.m

ttspec

PURPOSE ^

Toggle Transform between angular frequency and frequency spectrum

SYNOPSIS ^

Snew = ttspec(S,varargin)

DESCRIPTION ^

 TTSPEC Toggle Transform between angular frequency and frequency spectrum
 
  CALL:  Snew = ttspec(S,ftype,thtype)
 
      Snew = spectrum with new frequency type
         S = spectrum 
     ftype = 'f' if Snew should be given with frequency in Hz
             'w' if Snew should be given with angular frequency in rad/s
             (default is the opposite of what is given in S)
    thtype = 'radians' if Snew.theta should be given in radians (default)
             'degrees' if Snew.theta should be given in degrees 
             
 
  If ftype is the same frequency type as for S then Snew = S
  If S is a wavenumber spectrum then Snew = S.
  
  Note: The order of ftype and thtype is arbitrary and only the
        letters 'f','w','r' or 'd' is needed for unique identification
 
  Examples: Change from angular frequency to frequency in Hz and from
           angle in radians to angle in degrees.  
          S   = demospec('dir');
          Sf  = ttspec(S);
          Sf1 = ttspec(Sf,'f','d'); % = ttspec(Sf,'f','degrees'); 
          wspecplot(S,3), figure(2)
          wspecplot(Sf,3), figure(3)
          wspecplot(Sf1,3)
 
  See also  datastructures

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

001 function Snew = ttspec(S,varargin)
002 %TTSPEC Toggle Transform between angular frequency and frequency spectrum
003 %
004 % CALL:  Snew = ttspec(S,ftype,thtype)
005 %
006 %     Snew = spectrum with new frequency type
007 %        S = spectrum 
008 %    ftype = 'f' if Snew should be given with frequency in Hz
009 %            'w' if Snew should be given with angular frequency in rad/s
010 %            (default is the opposite of what is given in S)
011 %   thtype = 'radians' if Snew.theta should be given in radians (default)
012 %            'degrees' if Snew.theta should be given in degrees 
013 %            
014 %
015 % If ftype is the same frequency type as for S then Snew = S
016 % If S is a wavenumber spectrum then Snew = S.
017 % 
018 % Note: The order of ftype and thtype is arbitrary and only the
019 %       letters 'f','w','r' or 'd' is needed for unique identification
020 %
021 % Examples: Change from angular frequency to frequency in Hz and from
022 %          angle in radians to angle in degrees.  
023 %         S   = demospec('dir');
024 %         Sf  = ttspec(S);
025 %         Sf1 = ttspec(Sf,'f','d'); % = ttspec(Sf,'f','degrees'); 
026 %         wspecplot(S,3), figure(2)
027 %         wspecplot(Sf,3), figure(3)
028 %         wspecplot(Sf1,3)
029 %
030 % See also  datastructures
031 
032 
033 
034 %Tested on: Matlab 5.3, 5.2
035 % History: 
036 % revised pab 22.06.2001
037 % - fixed a bug: S.phi field was not checked when 'd', or 'r' option was used.
038 % revised pab 13.06.2000
039 %  - added more checks on input
040 %  - added thtype
041 %  - removed recursive call to itself => more efficient code.
042 % revised pab 08.02.2000
043 %  - S can now be an array of structs
044 % revised pab 24.01.2000
045 %  -added ftype
046 % by pab 13.11.99
047 
048 
049 
050 % Error checking
051 %~~~~~~~~~~~~~~~
052 if ~isstruct(S)
053   error('Input must be a spectral density struct)')
054 end
055 if ~(strcmpi(S(1).type(end-2:end),'req') |strcmpi(S(1).type(end-2:end),'dir'))
056   disp('This is not a frequency spectrum. Nothing is changed.')
057   Snew = S; % return old if not a freq
058   return
059 end
060 
061 ind = [isfield(S,'f'), isfield(S,'w')];
062 if all(ind==0),
063   error('This is not a correct spectral density struct: w and f field does not exist')
064 elseif all(ind==1),
065   error('This is not a correct spectral density struct: w and f field can not both exist')
066 end
067 
068 % Setting default values
069 %~~~~~~~~~~~~~~~~~~~~~~~~~
070 ftypeold = 'wf';
071 ftype    = ftypeold(ind); 
072 ftypeold = ftypeold(~ind);
073 thtype   = 'radians'; 
074 
075 P = varargin; Np = length(P);
076 for ix=1:Np,
077   switch lower(P{ix}(1)),
078     case {'f','w'}, ftype = lower(P{ix});
079     case {'d','r'}, thtype = lower(P{ix});
080   end
081 end
082 
083 
084 % Main computations
085 %~~~~~~~~~~~~~~~~~~~
086 isphi = isfield(S , 'theta');
087 Ns       = length(S(:));
088 if strcmp(ftypeold,ftype)
089   Snew = S; % nothing is changed
090   
091   if isfield(S , 'theta')
092     for iy=1:Ns,
093       if strcmpi(S(iy).type(end-2:end),'dir'),  % Directional spectrum 
094     if (abs(max(S(iy).theta)-min(S(iy).theta))>3*pi)  % theta given in degrees
095       if strcmpi(thtype(1),'r'),  % radians wanted
096         Snew(iy).theta = S(iy).theta*pi/180;
097         Snew(iy).S     = S(iy).S*180/pi;
098         if isphi,  Snew(iy).phi     = S(iy).phi*pi/180;end
099       end 
100     else                             % theta given in radians
101       if strcmpi(thtype(1),'d'),  % degrees wanted 
102         Snew(iy).theta = S(iy).theta*180/pi; 
103         Snew(iy).S     = S(iy).S*pi/180;
104         if isphi,  Snew(iy).phi     = S(iy).phi*180/pi;end
105       end 
106     end
107       end
108     end
109   end
110 else % change to new type
111   fnames   = fieldnames(S(1));
112   ind      = strmatch(ftypeold,fnames);
113   fnames   = {fnames{[1:ind-1, ind+1:end] }};
114   
115   Snew  = createspec(S(1).type,ftype);
116   for ix = 1:length(fnames),             % make sure all non-standard elements of S
117                                          % are also transfeered to Snew
118     Snew = setfield(Snew,fnames{ix},[]);
119   end
120   Snew(Ns) = Snew(1); % make sure it is an array of structs
121   
122   for iy=1:Ns,
123     for ix = 1:length(fnames),
124       Snew(iy) = setfield(Snew(iy),fnames{ix},getfield(S(iy),fnames{ix}));
125     end
126     if strcmp(ftypeold,'f')    
127       Snew(iy).w = S(iy).f*2*pi;
128       Snew(iy).S = S(iy).S/(2*pi);
129     else
130       Snew(iy).f = S(iy).w/(2*pi);
131       Snew(iy).S = S(iy).S*(2*pi);
132     end
133     
134     if strcmpi(S(iy).type(end-2:end),'dir'),  % Directional spectrum 
135       if (abs(max(S(iy).theta)-min(S(iy).theta))>3*pi)  % theta given in degrees
136     if strcmpi(thtype(1),'r'), % radians wanted
137       Snew(iy).theta = S(iy).theta*pi/180; 
138       Snew(iy).S     = Snew(iy).S*180/pi;
139        if isphi,  Snew(iy).phi     = S(iy).phi*pi/180;end
140     end 
141       else                            % theta given in radians
142     if strcmpi(thtype(1),'d'), % degrees wanted 
143       Snew(iy).theta = S(iy).theta*180/pi;
144       Snew(iy).S     = Snew(iy).S*pi/180;
145        if isphi,  Snew(iy).phi     = S(iy).phi*180/pi;end
146     end 
147       end
148     end
149   end % iy
150 end
151 
152

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