1 | %'filter_tps': find the thin plate spline coefficients for interpolation-smoothing |
---|
2 | %------------------------------------------------------------------------ |
---|
3 | % [SubRange,NbCentres,Coord_tps,U_tps,V_tps,W_tps,U_smooth,V_smooth,W_smooth,FF] =filter_tps(Coord,U,V,W,SubDomain,Rho,Threshold) |
---|
4 | % |
---|
5 | % OUTPUT: |
---|
6 | % SubRange(NbCoord,NbSubdomain,2): range (min, max) of the coordiantes x and y respectively, for each subdomain |
---|
7 | % NbCentres(NbSubdomain): number of source points for each subdomain |
---|
8 | % FF: false flags |
---|
9 | % U_smooth, V_smooth: filtered velocity components at the positions of the initial data |
---|
10 | % Coord_tps(NbCentres,NbCoord,NbSubdomain): positions of the tps centres |
---|
11 | % U_tps,V_tps: weight of the tps for each subdomain |
---|
12 | % to get the interpolated field values, use the function calc_field.m |
---|
13 | % |
---|
14 | % INPUT: |
---|
15 | % coord=[X Y]: matrix whose first column is the x coordinates of the initial data, the second column the y coordiantes |
---|
16 | % U,V: set of velocity components of the initial data |
---|
17 | % Rho: smoothing parameter |
---|
18 | % Threshold: max diff accepted between smoothed and initial data |
---|
19 | % Subdomain: estimated number of data points in each subdomain |
---|
20 | |
---|
21 | function [SubRange,NbCentres,Coord_tps,U_tps,V_tps,W_tps,U_smooth,V_smooth,W_smooth,FF] =filter_tps(Coord,U,V,W,SubDomain,Rho,Threshold) |
---|
22 | |
---|
23 | %% adjust subdomain decomposition |
---|
24 | warning off |
---|
25 | NbVec=size(Coord,1); |
---|
26 | NbCoord=size(Coord,2); |
---|
27 | MinCoord=min(Coord,[],1);%lower coordinate bounds |
---|
28 | MaxCoord=max(Coord,[],1);%upper coordinate bounds |
---|
29 | Range=MaxCoord-MinCoord; |
---|
30 | AspectRatio=Range(2)/Range(1); |
---|
31 | NbSubDomain=NbVec/SubDomain; |
---|
32 | NbSubDomainX=max(floor(sqrt(NbSubDomain/AspectRatio)),1); |
---|
33 | NbSubDomainY=max(floor(sqrt(NbSubDomain*AspectRatio)),1); |
---|
34 | NbSubDomain=NbSubDomainX*NbSubDomainY; |
---|
35 | Siz(1)=Range(1)/NbSubDomainX;%width of subdomains |
---|
36 | Siz(2)=Range(2)/NbSubDomainY;%height of subdomains |
---|
37 | CentreX=linspace(MinCoord(1)+Siz(1)/2,MaxCoord(1)-Siz(1)/2,NbSubDomainX); |
---|
38 | CentreY=linspace(MinCoord(2)+Siz(2)/2,MaxCoord(2)-Siz(2)/2,NbSubDomainY); |
---|
39 | [CentreX,CentreY]=meshgrid(CentreX,CentreY); |
---|
40 | CentreY=reshape(CentreY,1,[]);% Y positions of subdomain centres |
---|
41 | CentreX=reshape(CentreX,1,[]);% X positions of subdomain centres |
---|
42 | |
---|
43 | %% smoothing parameter |
---|
44 | rho=Siz(1)*Siz(2)*Rho/1000;%optimum rho increase as the area of the subdomain (division by 1000 to reach good values with the default GUI input) |
---|
45 | |
---|
46 | %% default output |
---|
47 | SubRange=zeros(NbCoord,2,NbSubDomain);%initialise the positions of subdomains |
---|
48 | NbCentres=zeros(1,NbSubDomain);%number of interpolated values per subdomain, =0 by default |
---|
49 | %Coord_tps=zeros(NbVec,NbCoord,NbSubDomain);% default positions of the tps source= initial positions of the good vectors sorted by subdomain |
---|
50 | %U_tps=zeros(NbVec,NbSubDomain);%default spline |
---|
51 | %V_tps=zeros(NbVec,NbSubDomain);%default spline |
---|
52 | W_tps=[];%default (2 component case) |
---|
53 | U_smooth=zeros(NbVec,1); % smoothed velocity U at the initial positions |
---|
54 | V_smooth=zeros(NbVec,1);% smoothed velocity V at the initial positions |
---|
55 | W_smooth=[];%default (2 component case) |
---|
56 | FF=zeros(NbVec,1); |
---|
57 | nb_select=zeros(NbVec,1); |
---|
58 | check_empty=zeros(1,NbSubDomain); |
---|
59 | |
---|
60 | %% calculate tps coeff in each subdomain |
---|
61 | for isub=1:NbSubDomain |
---|
62 | SubRange(1,:,isub)=[CentreX(isub)-0.55*Siz(1) CentreX(isub)+0.55*Siz(1)]; |
---|
63 | SubRange(2,:,isub)=[CentreY(isub)-0.55*Siz(2) CentreY(isub)+0.55*Siz(2)]; |
---|
64 | ind_sel_previous=[]; |
---|
65 | ind_sel=0; |
---|
66 | %increase iteratively the subdomain if it contains less than SubDomainNbVec/4 source vectors |
---|
67 | while numel(ind_sel)>numel(ind_sel_previous) |
---|
68 | ind_sel_previous=ind_sel; |
---|
69 | ind_sel=find(Coord(:,1)>=SubRange(1,1,isub) & Coord(:,1)<=SubRange(1,2,isub) & Coord(:,2)>=SubRange(2,1,isub) & Coord(:,2)<=SubRange(2,2,isub)); |
---|
70 | % if no vector in the subdomain, skip the subdomain |
---|
71 | if isempty(ind_sel) |
---|
72 | check_empty(isub)=1; |
---|
73 | U_tps(1,isub)=0;%define U_tps and V_tps by default |
---|
74 | V_tps(1,isub)=0; |
---|
75 | break |
---|
76 | % if too few selected vectors, increase the subrange for next iteration |
---|
77 | elseif numel(ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous); |
---|
78 | SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4; |
---|
79 | SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4; |
---|
80 | else |
---|
81 | [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel,:),U(ind_sel),rho); |
---|
82 | [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel,:),V(ind_sel),rho); |
---|
83 | UDiff=U_smooth_sub-U(ind_sel); |
---|
84 | VDiff=V_smooth_sub-V(ind_sel); |
---|
85 | NormDiff=UDiff.*UDiff+VDiff.*VDiff; |
---|
86 | ind_ind_sel=1:numel(ind_sel);%default |
---|
87 | if exist('Threshold','var') |
---|
88 | FF(ind_sel)=20*(NormDiff>Threshold);%put FF value to 20 to identify the criterium of elimmination |
---|
89 | ind_ind_sel=find(FF(ind_sel)==0); % select the indices of ind_sel corresponding to the remaining vectors |
---|
90 | end |
---|
91 | % if no value exceeds threshold, the result is recorded |
---|
92 | if isequal(numel(ind_ind_sel),numel(ind_sel)) |
---|
93 | U_smooth(ind_sel)=U_smooth(ind_sel)+U_smooth_sub; |
---|
94 | V_smooth(ind_sel)=V_smooth(ind_sel)+V_smooth_sub; |
---|
95 | NbCentres(isub)=numel(ind_sel); |
---|
96 | Coord_tps(1:NbCentres(isub),:,isub)=Coord(ind_sel,:); |
---|
97 | U_tps(1:NbCentres(isub)+3,isub)=U_tps_sub; |
---|
98 | V_tps(1:NbCentres(isub)+3,isub)=V_tps_sub; |
---|
99 | nb_select(ind_sel)=nb_select(ind_sel)+1; |
---|
100 | display('good') |
---|
101 | break |
---|
102 | % if too few selected vectors, increase the subrange for next iteration |
---|
103 | elseif numel(ind_ind_sel)<SubDomain/4 && ~isequal( ind_sel,ind_sel_previous); |
---|
104 | SubRange(:,1,isub)=SubRange(:,1,isub)-Siz'/4; |
---|
105 | SubRange(:,2,isub)=SubRange(:,2,isub)+Siz'/4; |
---|
106 | % else interpolation-smoothing is done again with the selected vectors |
---|
107 | else |
---|
108 | [U_smooth_sub,U_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),U(ind_sel(ind_ind_sel)),rho); |
---|
109 | [V_smooth_sub,V_tps_sub]=tps_coeff(Coord(ind_sel(ind_ind_sel),:),V(ind_sel(ind_ind_sel)),rho); |
---|
110 | U_smooth(ind_sel(ind_ind_sel))=U_smooth(ind_sel(ind_ind_sel))+U_smooth_sub; |
---|
111 | V_smooth(ind_sel(ind_ind_sel))=V_smooth(ind_sel(ind_ind_sel))+V_smooth_sub; |
---|
112 | NbCentres(isub)=numel(ind_ind_sel); |
---|
113 | Coord_tps(1:NbCentres(isub),:,isub)=Coord(ind_sel(ind_ind_sel),:); |
---|
114 | U_tps(1:NbCentres(isub)+3,isub)=U_tps_sub; |
---|
115 | V_tps(1:NbCentres(isub)+3,isub)=V_tps_sub; |
---|
116 | nb_select(ind_sel(ind_ind_sel))=nb_select(ind_sel(ind_ind_sel))+1; |
---|
117 | display('good2') |
---|
118 | break |
---|
119 | end |
---|
120 | end |
---|
121 | end |
---|
122 | end |
---|
123 | |
---|
124 | %% remove empty subdomains |
---|
125 | ind_empty=find(check_empty); |
---|
126 | if ~isempty(ind_empty) |
---|
127 | SubRange(:,:,ind_empty)=[]; |
---|
128 | Coord_tps(:,:,ind_empty)=[]; |
---|
129 | U_tps(:,ind_empty)=[]; |
---|
130 | V_tps(:,ind_empty)=[]; |
---|
131 | end |
---|
132 | |
---|
133 | %% final adjustments |
---|
134 | nb_select(nb_select==0)=1; |
---|
135 | U_smooth=U_smooth./nb_select;% take the average at the intersection of several subdomains |
---|
136 | V_smooth=V_smooth./nb_select; |
---|
137 | fill=zeros(NbCoord+1,NbCoord,size(SubRange,3)); %matrix of zeros to complement the matrix Data.Civ1_Coord_tps (conveninent for file storage) |
---|
138 | Coord_tps=cat(1,Coord_tps,fill); |
---|
139 | |
---|