-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfilterDirWavespec.m
91 lines (91 loc) · 3.07 KB
/
filterDirWavespec.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
%% filterDirWavespec.m
function Ew = filterDirWavespec(E,nc,n)
%%
% Function used to apply a 2-D moving averaging filter on a matrix
% representing directional spectrum. The objective is to allow that the
% two direction ends converge from 0 to 360 degs and vice versa. It is similar
% to repeated boundary conditions in numerical modeling.
%
%% Inputs
% E = matrix to be convoluted
% nc = number of convolutions (default = 1)
% n = size of convolution window (default = 3)
%
%% Output
% Ew = filtered matrix E of the same dimensions
%
% E =
% |------------------------------|
% | |-----------------------|
% | | -3 -2 1 1 1 1 -20 -30
% 20 30 3 2 1 1 1 1 20 30
% 3 2 1 1 1 1 20 30
% 3 2 1 1 1 1 20 30
% 3 2 1 1 1 1 20 30 3 2
% 4 3 1 1 1 1 25 35 | |
% |-------------------------| |
% |-----------------------|
%
% with n=5, becomes
%
% Ew =
% -20 -30 -3 -2 1 1 1 1 -20 -30 -3 -2
% -20 -30 -3 -2 1 1 1 1 -20 -30 -3 -2
% -20 -30 -3 -2 1 1 1 1 -20 -30 -3 -2
% 20 30 3 2 1 1 1 1 20 30 3 2
% 20 30 3 2 1 1 1 1 20 30 3 2
% 20 30 3 2 1 1 1 1 20 30 3 2
% 20 30 3 2 1 1 1 1 20 30 3 2
% 25 35 4 3 1 1 1 1 25 35 4 3
% 25 35 4 3 1 1 1 1 25 35 4 3
% 25 35 4 3 1 1 1 1 25 35 4 3
%
%% Authors
% Douglas Cahl and George Voulgaris
% School of the Earth, Ocean and Environment
% University of South Carolina, Columbia, SC, USA
%
%% Copyright 2019 Douglas Cahl, George Voulgaris
%
% This file is part of WavePart.
%
% WavePart is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <https://www.gnu.org/licenses/>.
%
%% Main Function
if nargin<2
n=3; nc=1;
end
if nargin<3
nc=1;
end
if mod(n,2)== 0 % Even number
n=n+1; % Make it odd
end
W=ones(n,n)/(n^2); % n x n normalized window to be used
% [nf,nd]=size(E); % size of the 2-d matrix array representing directional spectra
% nf = no of freq. bins, nd = no of direction bins
mn = floor(n/2); % size of extra columns / rows the window will create
%
% repeat the 1st and last frequency columns at the start and end
%
E1 = [repmat(E(1,:),mn,1); E; repmat(E(end,:),mn,1) ];
% place the last mn directions to the top (in reversed order) and the first to the end in reversed order
E2 = [E1(:,end-mn+1:end), E1, E1(:,1:mn)];
for i=1:nc
E3 = conv2(E2,W,'same');
E2 = E3;
end
% remove the extra columns / rows that had been placed so Ew has the same size as E
Ew = E3(mn+1:end-mn, mn+1:end-mn);
end