NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

root/DPWavesProc/trunk/DPWavesProc/diwasp_1_1GD/dirspec.m

Revision 215 (checked in by gdusek, 16 years ago)

--

Line 
1 function [SM,EP]=dirspec(ID,SM,EP,varargin)
2
3 %DIWASP V1.1 function with GD edit
4 %dirspec: main spectral estimation routine
5 %
6 %Note that there have been some chages made to this function since V1.1
7 %
8 %-The outdated matlab function csd was replaced with the new version cpsd on
9 %line 90
10 %
11 %-The ability to use along beam radial velocities has been added, and so the transfer
12 %parameters have been edited (line 109) so that the x,y position of the data input
13 %is also included
14 %
15 %-In addition, the first data input cell is radial velocity data, the initial 1-d
16 %frequency spectrum will be generated using the spectral average of all of
17 %the data input.  Note that if the first data input cell is something other
18 %than 'radial' (pressure or sea surface height may be preferable),
19 %the initial 1-d frequency spectrum will be generated using ONLY that first
20 %data input.  (see line 123)
21 %
22 %
23 %[SMout,EPout]=dirspec(ID,SM,EP,{options})
24 %
25 %Outputs:
26 %SMout      A spectral matrix structure containing the results
27 %Epout          The estimation parameters structure with the values actually used for the computation including any default settings.
28 %
29 %Inputs:
30 %ID                     An instrument data structure containing the measured data
31 %SM             A spectral matrix structure; data in field SM.S is ignored.
32 %EP                 The estimation parameters structure. For default values enter EP as []
33 %[options]  options entered as cell array with parameter/value pairs: e.g.{'MESSAGE',1,'PLOTTYPE',2};
34 %                Available options with default values:
35 %                    'MESSAGE',1,    Level of screen display: 0,1,2 (increasing output)
36 %                    'PLOTTYPE',1,   Plot type: 0 none, 1 3d surface, 2 polar type plot, 3 3d surface(compass angles), 4 polar plot(compass angles)
37 %                    'FILEOUT',''        Filename for output file: empty string means no file output
38 %               
39 %Input structures ID and SM are required. Either [EP] or [options] can be included but must be in order if both are included.
40 %Type:%"help data_structures" for information on the DIWASP data structures
41
42 %All of the implemented calculation algorithms are as described by:
43 %Hashimoto,N. 1997 "Analysis of the directional wave spectrum from field data"
44 %In: Advances in Coastal Engineering Vol.3. Ed:Liu,P.L-F. Pub:World Scientific,Singapore
45 %
46 %Copyright (C) 2002 Coastal Oceanography Group, CWR, UWA, Perth
47
48 Options=struct('MESSAGE',1,'PLOTTYPE',1,'FILEOUT','');
49
50 if nargin<3
51    error('All inputs other than OPTIONS required');
52 elseif nargin>=4
53    nopts=length(varargin{1});
54 end
55
56 ID=check_data(ID,1);if isempty(ID) return;end;
57 SM=check_data(SM,2);if isempty(SM) return;end;
58 EP=check_data(EP,3);if isempty(EP) return;end;
59
60 if ~isempty(nopts)
61 if(rem(nopts,2)~=0)
62         warning('Options must be in Name/Value pairs - setting to defaults');
63         else
64         for i=1:(nopts/2)
65         arg=varargin{1}{(2*i)};
66         field=varargin{1}{(2*i-1)};
67         Options=setfield(Options,field,arg);end;end   
68 end
69
70 ptype=Options.PLOTTYPE;displ=Options.MESSAGE;
71
72 disp(' ');disp('calculating.....');disp(' ');disp('cross power spectra');
73
74 data=detrend(ID.data);
75 szd=size(ID.data,2);
76 dt=1/(ID.fs);
77
78 %get resolution of FFT - if not specified, calculate a sensible value depending on sampling frequency
79 if isempty(EP.nfft)
80     nfft=2^(8+round(log2(ID.fs)));
81     EP.nfft=nfft;
82 else
83     nfft=EP.nfft;
84 end
85
86 %NOTE: changed on 9/5/08 window length for cpsd set to be a window equal to
87 %or slightly larger than nfft so padding is limited
88 dataLength=size(ID.data,1);
89 numWindows=ceil(dataLength/nfft);
90 window=ceil(dataLength/numWindows);
91
92 %Do a version check for the upcoming cpsd function. If the version is 2006b
93 %or later (date Aug,3,2006 or datenum 732892 cpsd will be handled
94 %differently.  added on 9/17/08
95 [v d]=version;
96 ver_date=datenum(d);
97
98
99 % NOTE: changed cross power spectral function from csd to cpsd 6/15/07
100 for m=1:szd
101 for n=1:szd
102     % Need to have 2 different cpsd functions depending on Matlab version.
103     % added of 9/17/08
104     if ver_date < 732892
105         [xpstmp,Ftmp]=cpsd(data(:,m),data(:,n),window,[],nfft,ID.fs);
106     else
107         [xpstmp,Ftmp]=cpsd(data(:,n),data(:,m),window,[],nfft,ID.fs);
108     end;
109    xps(m,n,:)=xpstmp(2:(nfft/2)+1);
110 end
111 end
112
113 F=Ftmp(2:(nfft/2)+1);nf=nfft/2;
114
115 %calculate wavenumbers
116 disp('wavenumbers')
117 wns=wavenumber(2*pi*F,ID.depth*ones(size(F)));
118 pidirs=[-pi:(2*pi/EP.dres):pi-(2*pi/EP.dres)]; % this is dirs
119
120 %  NOTE:  edited the transfer parameters to include x and y position
121 %  x is (layout(1,m)),y is (layout(2,m))   (6/15/07)
122
123 %calculate transfer parameters
124 disp('transfer parameters');
125 disp(' ');
126 for m=1:szd
127         trm(m,:,:)=feval(ID.datatypes{m},2*pi*F,pidirs,wns,ID.layout(3,m),ID.depth,ID.layout(1,m),ID.layout(2,m)); % 2piF is ffreqs
128  for n=1:szd
129     kx(m,n,:,:)=wns*((ID.layout(1,n)-ID.layout(1,m))*cos(pidirs)+(ID.layout(2,n)-ID.layout(2,m))*sin(pidirs));
130 end
131 end
132
133 for m=1:szd
134    tfn(:,:)=trm(m,:,:);
135    Sxps(1:nf)=2*dt*xps(m,m,:);
136    Ss(m,:)=Sxps./(max(tfn').*conj(max(tfn')));
137 end
138
139 %What are the number of sensors (data series) used to compute the power
140 %spectrum?  If we aren't using radial velocities it is just 1.
141 dataUsed=1;
142
143
144 %NOTE: edited on 8/07
145 %have Ss = the spectral average if the radial velocities are used without a
146 %pressure or vertical range datatype first
147 compare=strcmp('radial',ID.datatypes(1));
148 if compare == 1
149     Ss=mean(Ss);
150     %since this is using radial velocities the number of data series used
151     %the total number of sensors or szd
152     dataUsed=szd;
153 end
154
155
156
157 %Compute the degrees of freedom depending on cpsd options and the data used
158 %See Emery and Thomson, 2004, added on 9/17/08
159 windowConstant=2.5164; %for Hamming Window (Matlab default for cpsd)
160 degF=dataUsed*2*numWindows*windowConstant;
161 SM.degF=degF;
162
163
164 ffs=sum(F<=max(SM.freqs));
165
166     % call appropriate estimation function
167 disp(['directional spectra using' blanks(1) EP.method ' method']);disp(' ')
168 Specout=feval(EP.method,xps(:,:,1:ffs),trm(:,1:ffs,:),kx(:,:,1:ffs,:),Ss(:,1:ffs),pidirs,EP.iter,displ);
169
170 Specout(find(isnan(Specout)))=0.0;
171 Hs=Hsig(Specout,F(2)-F(1),pidirs(2)-pidirs(1));
172
173 % map spectrum onto user defined spectrum matrix - need extra line of frequencies to avoid NaNs
174 [df,ddir]=meshgrid(SM.freqs,SM.dirs);
175 pidirs(EP.dres+1)=pi;
176 Specout=Specout';
177 Specout(EP.dres+1,:)=Specout(1,:);
178 [Ff,Dd]=meshgrid(F(1:ffs),(180/pi)*pidirs);
179 S=interp2(Ff,Dd,Specout,df,ddir,'nearest');
180 S=S*pi/180;
181 S(find(isnan(S)))=0.0;
182
183 %check Hsig of mapped spectrum and check sufficiently close to original
184 Hs2=Hsig(S,SM.freqs(2)-SM.freqs(1),SM.dirs(2)-SM.dirs(1));
185 if (Hs2-Hs)/Hs >0.01
186     warning('User defined grid may be too coarse; try increasing resolution of ''SM.freqs'' or ''SM.dirs''');
187 end
188
189 %smooth spectrum
190 if(strcmp(EP.smooth,'ON'))
191     disp(' ');disp('smoothing spectrum...');disp(' ');
192     S=smoothspec(S,[1 0.5 0.25;1 0.5 0.25]);
193 end
194 SM.S=S';
195
196 infospec(SM);
197
198 %write out spectrum matrix in DIWASP format
199 filename=Options.FILEOUT;
200 if(size(filename,2)>0)
201    disp('writing out spectrum matrix to file');
202    writespec(SM,filename);
203 end
204
205 %plot spectrum
206 if(ptype>0)
207     disp('finished...plotting spectrum');
208     plotspec(SM,ptype);
209     T=['Directional spectrum estimated using ' blanks(1) EP.method ' method'];title(T);
210 end
Note: See TracBrowser for help on using the browser.